From 9d22381085a585382634be7c041929ba18dd16a7 Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Thu, 17 Nov 2022 12:58:05 +0100 Subject: [PATCH] Rename SaveString to SafeString --- .../Runtime/RuntimeInformation.cs | 12 +-- YesNt.Interpreter/Runtime/YesNtInterpreter.cs | 4 +- .../Statements/ConsoleStatements.cs | 4 +- .../Statements/ProcessingStatements.cs | 8 +- .../Statements/SystemStatements.cs | 2 +- YesNt.Interpreter/Utilities/Evaluator.cs | 14 ++-- .../Utilities/StringExtentions.cs | 83 +++++++++---------- 7 files changed, 63 insertions(+), 64 deletions(-) diff --git a/YesNt.Interpreter/Runtime/RuntimeInformation.cs b/YesNt.Interpreter/Runtime/RuntimeInformation.cs index 8113c6b..2e70a69 100644 --- a/YesNt.Interpreter/Runtime/RuntimeInformation.cs +++ b/YesNt.Interpreter/Runtime/RuntimeInformation.cs @@ -81,16 +81,16 @@ namespace YesNt.Interpreter.Runtime { if (IsTask) { - parentRuntimeInformation!.WriteLine(output.FromSaveString(), forceWrite); + parentRuntimeInformation!.WriteLine(output.FromSafeString(), forceWrite); } else { - OnDebugOutput?.Invoke(output.FromSaveString() + Environment.NewLine); + OnDebugOutput?.Invoke(output.FromSafeString() + Environment.NewLine); } } else { - Console.WriteLine(output.FromSaveString()); + Console.WriteLine(output.FromSafeString()); } } @@ -105,16 +105,16 @@ namespace YesNt.Interpreter.Runtime { if (IsTask) { - parentRuntimeInformation!.Write(output.FromSaveString(), forceWrite); + parentRuntimeInformation!.Write(output.FromSafeString(), forceWrite); } else { - OnDebugOutput?.Invoke(output.FromSaveString()); + OnDebugOutput?.Invoke(output.FromSafeString()); } } else { - Console.Write(output.FromSaveString()); + Console.Write(output.FromSafeString()); } } diff --git a/YesNt.Interpreter/Runtime/YesNtInterpreter.cs b/YesNt.Interpreter/Runtime/YesNtInterpreter.cs index 7445996..67940ae 100644 --- a/YesNt.Interpreter/Runtime/YesNtInterpreter.cs +++ b/YesNt.Interpreter/Runtime/YesNtInterpreter.cs @@ -148,7 +148,7 @@ namespace YesNt.Interpreter.Runtime DebugEventArgs debugEventArgs = new DebugEventArgs() { LineNumber = runtimeInfo.LineNumber + 1, - OriginalLine = runtimeInfo.CurrentLine.FromSaveString(), + OriginalLine = runtimeInfo.CurrentLine.FromSafeString(), IsTask = runtimeInfo.IsTask, TaskId = runtimeInfo.TaskId }; @@ -229,7 +229,7 @@ namespace YesNt.Interpreter.Runtime } if (runtimeInfo.IsDebugMode && notSearchingLabel) { - debugEventArgs.CurrentLine = runtimeInfo.CurrentLine.FromSaveString(); + debugEventArgs.CurrentLine = runtimeInfo.CurrentLine.FromSafeString(); runtimeInfo.LineExecuted(debugEventArgs); } } diff --git a/YesNt.Interpreter/Statements/ConsoleStatements.cs b/YesNt.Interpreter/Statements/ConsoleStatements.cs index 1d07072..574df89 100644 --- a/YesNt.Interpreter/Statements/ConsoleStatements.cs +++ b/YesNt.Interpreter/Statements/ConsoleStatements.cs @@ -34,7 +34,7 @@ namespace YesNt.Interpreter.Statements RuntimeInfo.Exit("Terminated by external process", true); return; } - args = args.ReplaceFirstOccurrence("%crl ", input.ToSaveString() + " "); + args = args.ReplaceFirstOccurrence("%crl ", input.ToSafeString() + " "); } RuntimeInfo.CurrentLine = args.TrimEnd(); } @@ -46,7 +46,7 @@ namespace YesNt.Interpreter.Statements while (args.Contains("%cr ")) { string input = ConsoleExtentions.ReadKey(RuntimeInfo).ToString(); - args = args.ReplaceFirstOccurrence("%cr ", input.ToSaveString() + " "); + args = args.ReplaceFirstOccurrence("%cr ", input.ToSafeString() + " "); } RuntimeInfo.CurrentLine = args.TrimEnd(); } diff --git a/YesNt.Interpreter/Statements/ProcessingStatements.cs b/YesNt.Interpreter/Statements/ProcessingStatements.cs index 2d80026..8c77aa8 100644 --- a/YesNt.Interpreter/Statements/ProcessingStatements.cs +++ b/YesNt.Interpreter/Statements/ProcessingStatements.cs @@ -16,7 +16,7 @@ internal partial class ProcessingStatements : StatementRuntimeInformation [Statement("!calc", SearchMode.EndOfLine, SpaceAround.Start, ConsoleColor.DarkYellow, Priority = Priority.High)] public void Calculate(string args) { - MatchCollection matches = CalculationRegex().Matches(args.FromSaveString()); + MatchCollection matches = CalculationRegex().Matches(args.FromSafeString()); for (int i = 0; i < matches.Count; i++) { @@ -26,7 +26,7 @@ internal partial class ProcessingStatements : StatementRuntimeInformation RuntimeInfo.Exit("Invalid operation", true); return; } - args = args.FromSaveString().Replace(matches[i].Value, res); + args = args.FromSafeString().Replace(matches[i].Value, res); } RuntimeInfo.CurrentLine = args; @@ -35,7 +35,7 @@ internal partial class ProcessingStatements : StatementRuntimeInformation [Statement("!eval", SearchMode.EndOfLine, SpaceAround.Start, ConsoleColor.DarkYellow, Priority = Priority.VeryHigh)] public void Evaluate(string args) { - RuntimeInfo.CurrentLine = args.FromSaveString(); + RuntimeInfo.CurrentLine = args.FromSafeString(); } [Statement("!!", SearchMode.Contains, SpaceAround.None, ConsoleColor.DarkYellow, Priority = Priority.PreProcessing, KeepStatementInArgs = true)] @@ -49,7 +49,7 @@ internal partial class ProcessingStatements : StatementRuntimeInformation { char charToEscape = args[index]; args = args.Remove(index, 1); - args = args.Insert(index, charToEscape.ToString().ToSaveString()); + args = args.Insert(index, charToEscape.ToString().ToSafeString()); } } RuntimeInfo.CurrentLine = args; diff --git a/YesNt.Interpreter/Statements/SystemStatements.cs b/YesNt.Interpreter/Statements/SystemStatements.cs index ac357b7..d1a3ee4 100644 --- a/YesNt.Interpreter/Statements/SystemStatements.cs +++ b/YesNt.Interpreter/Statements/SystemStatements.cs @@ -16,7 +16,7 @@ namespace YesNt.Interpreter.Statements [Statement("exc", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.Magenta, Priority = Priority.Low, Seperator = "|")] public void ExecuteProgramWithArgs(string input) { - string[] parts = input.FromSaveString().Split('|'); + string[] parts = input.FromSafeString().Split('|'); parts[0] = parts[0].Trim(); string[] functionArgumets = parts[1].Split(','); diff --git a/YesNt.Interpreter/Utilities/Evaluator.cs b/YesNt.Interpreter/Utilities/Evaluator.cs index ecd39ff..a13fa91 100644 --- a/YesNt.Interpreter/Utilities/Evaluator.cs +++ b/YesNt.Interpreter/Utilities/Evaluator.cs @@ -8,11 +8,11 @@ internal static partial class Evaluator { public static bool? EvaluateCondition(string input) { - if (input.ToLower().FromSaveString().Trim() == "true") + if (input.ToLower().FromSafeString().Trim() == "true") { return true; } - else if (input.ToLower().FromSaveString().Trim() == "false") + else if (input.ToLower().FromSafeString().Trim() == "false") { return false; } @@ -20,16 +20,16 @@ internal static partial class Evaluator string[] parts = input.Split("=="); if (parts.Length == 2) { - string part1 = parts[0].FromSaveString().Trim(); - string part2 = parts[1].FromSaveString().Trim(); + string part1 = parts[0].FromSafeString().Trim(); + string part2 = parts[1].FromSafeString().Trim(); return part1 == part2; } parts = input.Split("!="); if (parts.Length == 2) { - string part1 = parts[0].FromSaveString().Trim(); - string part2 = parts[1].FromSaveString().Trim(); + string part1 = parts[0].FromSafeString().Trim(); + string part2 = parts[1].FromSafeString().Trim(); return part1 != part2; } @@ -86,7 +86,7 @@ internal static partial class Evaluator return null; } - input = input.FromSaveString(); + input = input.FromSafeString(); MatchCollection matches = ParenthesesRegex().Matches(input); while (matches.Count > 0) diff --git a/YesNt.Interpreter/Utilities/StringExtentions.cs b/YesNt.Interpreter/Utilities/StringExtentions.cs index c8e615e..c7a674a 100644 --- a/YesNt.Interpreter/Utilities/StringExtentions.cs +++ b/YesNt.Interpreter/Utilities/StringExtentions.cs @@ -2,52 +2,51 @@ using System.Globalization; using System.Text; -namespace YesNt.Interpreter.Utilities +namespace YesNt.Interpreter.Utilities; + +public static class StringExtentions { - public static class StringExtentions + public static string ToSafeString(this string input) { - public static string ToSaveString(this string input) + StringBuilder output = new StringBuilder(); + foreach (char c in input) { - StringBuilder output = new StringBuilder(); - foreach (char c in input) - { - _ = output.Append($"\v{c}\v"); - } - return output.ToString(); + _ = output.Append($"\v{c}\v"); + } + return output.ToString(); + } + + public static string FromSafeString(this string input) + { + return input.Replace("\v", ""); + } + + public static bool ToStandardizedNumber(this string input, out double result) + { + return double.TryParse(input.FromSafeString().Replace(',', '.'), NumberStyles.Any, CultureInfo.InvariantCulture, out result); + } + + public static string ReplaceFirstOccurrence(this string input, string oldValue, string newValue) + { + int place = input.IndexOf(oldValue); + return input.Remove(place, oldValue.Length).Insert(place, newValue); + } + + public static string ReplaceLastOccurrence(this string input, string oldValue, string newValue) + { + int place = input.LastIndexOf(oldValue); + return input.Remove(place, Math.Min(oldValue.Length, input.Length - place)).Insert(place, newValue); + } + + public static int WhiteSpaceAtEnd(this string input) + { + int count = 0; + int index = input.Length - 1; + while (index >= 0 && char.IsWhiteSpace(input[index--])) + { + count++; } - public static string FromSaveString(this string input) - { - return input.Replace("\v", ""); - } - - public static bool ToStandardizedNumber(this string input, out double result) - { - return double.TryParse(input.FromSaveString().Replace(',', '.'), NumberStyles.Any, CultureInfo.InvariantCulture, out result); - } - - public static string ReplaceFirstOccurrence(this string input, string oldValue, string newValue) - { - int place = input.IndexOf(oldValue); - return input.Remove(place, oldValue.Length).Insert(place, newValue); - } - - public static string ReplaceLastOccurrence(this string input, string oldValue, string newValue) - { - int place = input.LastIndexOf(oldValue); - return input.Remove(place, Math.Min(oldValue.Length, input.Length - place)).Insert(place, newValue); - } - - public static int WhiteSpaceAtEnd(this string input) - { - int count = 0; - int index = input.Length - 1; - while (index >= 0 && char.IsWhiteSpace(input[index--])) - { - count++; - } - - return count; - } + return count; } } \ No newline at end of file