From cf274e1db9b5f6895ed9eb91f643bef8fe8e7680 Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Wed, 7 Aug 2024 23:31:19 +0200 Subject: [PATCH] Add more escape codes --- .../Utilities/StringExtentions.cs | 50 +++++++++++-------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/YesNt.Interpreter/Utilities/StringExtentions.cs b/YesNt.Interpreter/Utilities/StringExtentions.cs index 0feb116..49d00f9 100644 --- a/YesNt.Interpreter/Utilities/StringExtentions.cs +++ b/YesNt.Interpreter/Utilities/StringExtentions.cs @@ -20,7 +20,14 @@ public static class StringExtensions {">", "~grt" }, {",", "~com" }, {"!", "~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")] @@ -42,26 +49,7 @@ public static class StringExtensions public static string FromSafeString(this string input) { - return ReplaceOnce(input.Replace("\v", ""), reverseReplacementRules); - } - - public static string ReplaceOnce(string input, Dictionary replacementRules) - { - IEnumerable> matches = replacementRules.Where(rule => input.Contains(rule.Key)); - if (!matches.Any()) - { - return input; - } - - KeyValuePair 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; + return ReplaceOnce(input.Replace("\v", string.Empty), reverseReplacementRules); } public static bool ToStandardizedNumber(this string input, out double result) @@ -92,4 +80,24 @@ public static class StringExtensions return count; } + + private static string ReplaceOnce(string input, Dictionary 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> matches = replacementRules.Where(rule => rule.Key != string.Empty && input.Contains(rule.Key)); + if (!matches.Any()) + { + return input; + } + + KeyValuePair 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; + } } \ No newline at end of file