Skip to content

Commit

Permalink
Add TrimRestore String Modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
marcussacana committed Mar 6, 2022
1 parent 21cecd4 commit 044b4d6
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 80 deletions.
2 changes: 1 addition & 1 deletion README.MD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Help-me keep actively creating tools!
## My Ko-fi
<a href='https://ko-fi.com/Z8Z231I4Z' target='_blank'><img height='40' style='border:0px;height:40px;' src='https://cdn.ko-fi.com/cdn/kofi1.png?v=2' border='0' alt='Buy Me a Coffee at ko-fi.com' /></a>

# StringsReloads v7.5
Expand Down
83 changes: 5 additions & 78 deletions StringReloads/Engine/Match/TrimMatch.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
using StringReloads.Engine.Interface;
using StringReloads.StringModifier;

namespace StringReloads.Engine.Match
{
class TrimMatch : BasicMatch, IMatch
{
string[] Prefixes;
string[] Sufixes;

SRL Engine;
public TrimMatch(SRL Engine) : base(Engine)
{
this.Engine = Engine;
var Dic = Engine.Settings.GetValues("Trim");

if (Dic == null)
{
Prefixes = Sufixes = new string[0];
return;
}

Prefixes = Dic["prefixes"].Unescape().Split('\n');
Sufixes = Dic["sufixes"].Unescape().Split('\n');
}

bool IMatch.HasMatch(string String)
Expand All @@ -33,9 +21,9 @@ bool IMatch.HasMatch(string String)

LSTEntry? IMatch.MatchString(string String)
{
var Prefix = GetPrefix(String);
var Sufix = GetSufix(String);
var Match = MatchString(Trim(String));
var Prefix = TrimRestore.Default.GetPrefix(String);
var Sufix = TrimRestore.Default.GetSufix(String);
var Match = MatchString(TrimRestore.Default.Trim(String));
if (Match == null)
return null;

Expand All @@ -46,67 +34,6 @@ bool IMatch.HasMatch(string String)

return Result;
}

string Trim(string String)
{
String = TrimEnd(String);
return TrimStart(String);
}
string Trim(string String, string Pattern)
{
String = TrimEnd(String, Pattern);
return TrimStart(String, Pattern);
}

string GetPrefix(string String)
{
var Trimmed = TrimStart(String);
return String.Substring(0, String.Length - Trimmed.Length);
}

string GetSufix(string String)
{
var Trimmed = TrimEnd(String);
return String.Substring(Trimmed.Length);
}

string TrimStart(string String)
{
Begin: foreach (var Prefix in Prefixes)
{
var NewStr = TrimStart(String, Prefix);
bool MustRestart = String != NewStr;
String = NewStr;
if (MustRestart)
goto Begin;
}
return String;
}
string TrimStart(string String, string Pattern)
{
while (String.StartsWith(Pattern))
String = String.Substring(Pattern.Length);
return String;
}

string TrimEnd(string String)
{
Begin: foreach (var Sufix in Sufixes)
{
var NewStr = TrimEnd(String, Sufix);
bool MustRestart = String != NewStr;
String = NewStr;
if (MustRestart)
goto Begin;
}
return String;
}

string TrimEnd(string String, string Pattern)
{
while (String.EndsWith(Pattern))
String = String.Substring(0, String.Length - Pattern.Length);
return String;
}

}
}
4 changes: 3 additions & 1 deletion StringReloads/Engine/SRL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ where typeof(IPlugin).IsAssignableFrom(Typ) && !Typ.IsInterface
new MonoWordWrap(this),
new Remaper(this),
new RemaperAlt(this),
new Escape()
new Escape(),
new TrimRestore(this),
};

internal Hook.Base.Hook[] _Hooks = null;
Expand Down Expand Up @@ -147,6 +148,7 @@ public Database CurrentDatabase {

return Output;
}

internal byte* ProcessString(WCString pString) {
if (!Initialized)
Initializer.Initialize(this);
Expand Down
1 change: 1 addition & 0 deletions StringReloads/SRL.ini
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ Escape=false
MonoWordWrap=false
Remaper=false
RemaperAlt=false
TrimRestore=false

[Mods]
CoInitializeFix=false
Expand Down
3 changes: 3 additions & 0 deletions StringReloads/StringModifier/Escape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ public string Restore(string String)
case 'r':
SB.Append('\r');
break;
case 's':
SB.Append(' ');
break;
default:
throw new Exception("\\" + c + " Isn't a valid string escape.");
}
Expand Down
112 changes: 112 additions & 0 deletions StringReloads/StringModifier/TrimRestore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
using StringReloads.Engine;
using StringReloads.Engine.Interface;
using System;

namespace StringReloads.StringModifier
{
class TrimRestore : IStringModifier
{
public static TrimRestore Default = new TrimRestore(Config.Default);

public TrimRestore(Config Settings) {
var Group = Settings.GetValues("Trim");

if (Group == null)
{
Prefixes = Sufixes = new string[0];
return;
}

Prefixes = Group["prefixes"].Unescape().Split('\n');
Sufixes = Group["sufixes"].Unescape().Split('\n');
}

public TrimRestore(SRL Engine) : this(Engine.Settings) { }

public string[] Prefixes;
public string[] Sufixes;

public string Name => "TrimRestore";

public bool CanRestore => false;

public string Apply(string String, string Original)
{
var Prefix = GetPrefix(Original);
var Sufix = GetSufix(Original);

var Result = Trim(String);

return $"{Prefix}{Result}{Sufix}";
}

public string Restore(string String)
{
return String;
}

public string Trim(string String)
{
String = TrimEnd(String);
return TrimStart(String);
}

public string Trim(string String, string Pattern)
{
String = TrimEnd(String, Pattern);
return TrimStart(String, Pattern);
}

public string GetPrefix(string String)
{
var Trimmed = TrimStart(String);
return String.Substring(0, String.Length - Trimmed.Length);
}

public string GetSufix(string String)
{
var Trimmed = TrimEnd(String);
return String.Substring(Trimmed.Length);
}

string TrimStart(string String)
{
Begin: foreach (var Prefix in Prefixes)
{
var NewStr = TrimStart(String, Prefix);
bool MustRestart = String != NewStr;
String = NewStr;
if (MustRestart)
goto Begin;
}
return String;
}

string TrimStart(string String, string Pattern)
{
while (String.StartsWith(Pattern))
String = String.Substring(Pattern.Length);
return String;
}

string TrimEnd(string String)
{
Begin: foreach (var Sufix in Sufixes)
{
var NewStr = TrimEnd(String, Sufix);
bool MustRestart = String != NewStr;
String = NewStr;
if (MustRestart)
goto Begin;
}
return String;
}

string TrimEnd(string String, string Pattern)
{
while (String.EndsWith(Pattern))
String = String.Substring(0, String.Length - Pattern.Length);
return String;
}
}
}

0 comments on commit 044b4d6

Please sign in to comment.