mirror of
https://github.com/Stone-Red-Code/YesNt-Interpreter.git
synced 2026-09-08 07:56:06 +02:00
Add more escape codes
This commit is contained in:
@@ -20,7 +20,14 @@ public static class StringExtensions
|
|||||||
{">", "~grt" },
|
{">", "~grt" },
|
||||||
{",", "~com" },
|
{",", "~com" },
|
||||||
{"!", "~exm" },
|
{"!", "~exm" },
|
||||||
{"|", "~pip" }
|
{"|", "~pip" },
|
||||||
|
{"\n","~nli" },
|
||||||
|
{"\r","~ret" },
|
||||||
|
{"\t","~tab" },
|
||||||
|
{"\b","~bac" },
|
||||||
|
{"\f","~for" },
|
||||||
|
{"\a","~ale" },
|
||||||
|
{"", "~emp" },
|
||||||
};
|
};
|
||||||
|
|
||||||
[SuppressMessage("Minor Code Smell", "S3963:\"static\" fields should be initialized inline", Justification = "Doesn't work because it throws a TypeInitializationException")]
|
[SuppressMessage("Minor Code Smell", "S3963:\"static\" fields should be initialized inline", Justification = "Doesn't work because it throws a TypeInitializationException")]
|
||||||
@@ -42,26 +49,7 @@ public static class StringExtensions
|
|||||||
|
|
||||||
public static string FromSafeString(this string input)
|
public static string FromSafeString(this string input)
|
||||||
{
|
{
|
||||||
return ReplaceOnce(input.Replace("\v", ""), reverseReplacementRules);
|
return ReplaceOnce(input.Replace("\v", string.Empty), reverseReplacementRules);
|
||||||
}
|
|
||||||
|
|
||||||
public static string ReplaceOnce(string input, Dictionary<string, string> replacementRules)
|
|
||||||
{
|
|
||||||
IEnumerable<KeyValuePair<string, string>> matches = replacementRules.Where(rule => input.Contains(rule.Key));
|
|
||||||
if (!matches.Any())
|
|
||||||
{
|
|
||||||
return input;
|
|
||||||
}
|
|
||||||
|
|
||||||
KeyValuePair<string, string> match = matches.First();
|
|
||||||
int startIndex = input.IndexOf(match.Key);
|
|
||||||
int endIndex = startIndex + match.Key.Length;
|
|
||||||
|
|
||||||
string before = ReplaceOnce(input[..startIndex], replacementRules);
|
|
||||||
string replaced = match.Value;
|
|
||||||
string after = ReplaceOnce(input[endIndex..], replacementRules);
|
|
||||||
|
|
||||||
return before + replaced + after;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bool ToStandardizedNumber(this string input, out double result)
|
public static bool ToStandardizedNumber(this string input, out double result)
|
||||||
@@ -92,4 +80,24 @@ public static class StringExtensions
|
|||||||
|
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static string ReplaceOnce(string input, Dictionary<string, string> replacementRules)
|
||||||
|
{
|
||||||
|
// ~emp/string.Empty is a special case, it is used to represent empty strings and won't work with the normal rules because an empty string always matches and causes an infinite loop 3 letter abbreviation
|
||||||
|
IEnumerable<KeyValuePair<string, string>> matches = replacementRules.Where(rule => rule.Key != string.Empty && input.Contains(rule.Key));
|
||||||
|
if (!matches.Any())
|
||||||
|
{
|
||||||
|
return input;
|
||||||
|
}
|
||||||
|
|
||||||
|
KeyValuePair<string, string> match = matches.First();
|
||||||
|
int startIndex = input.IndexOf(match.Key);
|
||||||
|
int endIndex = startIndex + match.Key.Length;
|
||||||
|
|
||||||
|
string before = ReplaceOnce(input[..startIndex], replacementRules);
|
||||||
|
string replaced = match.Value;
|
||||||
|
string after = ReplaceOnce(input[endIndex..], replacementRules);
|
||||||
|
|
||||||
|
return before + replaced + after;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user