From 789f56ba9559e368ed6f9560d146b66275bf37a8 Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Thu, 5 Mar 2026 22:55:02 +0100 Subject: [PATCH] Code cleanup --- YesNt.CodeEditor/Editor.cs | 2 +- YesNt.CodeEditor/InputHandler.cs | 2 +- YesNt.CodeEditor/SyntaxHighlighter.cs | 2 +- YesNt.Interpreter.App/Program.cs | 2 +- YesNt.Interpreter.Tests/AddStatementTests.cs | 2 +- .../CodeFlowStatementsTests.cs | 3 +- YesNt.Interpreter.Tests/CodeFlowTests.cs | 3 +- .../ConsoleStatementsTests.cs | 3 +- .../FunctionStatementsTests.cs | 3 +- .../ListStatementsTests.cs | 2 +- .../PredefinedVariableStatementsTests.cs | 2 +- .../ProcessingStatementsTests.cs | 3 +- .../StringLiteralStatementsTests.cs | 2 +- .../SystemStatementsTests.cs | 3 +- .../VariableStatementsTests.cs | 3 +- YesNt.Interpreter.Tests/YesNtAssert.cs | 2 +- .../Attributes/StatementAttribute.cs | 2 +- YesNt.Interpreter/Runtime/ExitMessages.cs | 2 +- YesNt.Interpreter/Runtime/FunctionScope.cs | 2 +- YesNt.Interpreter/Runtime/Line.cs | 4 +- .../Runtime/RuntimeInformation.cs | 2 +- YesNt.Interpreter/Runtime/StatementHandler.cs | 3 +- YesNt.Interpreter/Runtime/YesNtInterpreter.cs | 12 +-- .../Statements/CodeFlowStatements.cs | 2 +- .../Statements/ConsoleStatements.cs | 2 +- .../Statements/FunctionStatements.cs | 2 +- .../Statements/ListStatements.cs | 2 +- .../PredefinedVariableStatements.cs | 3 +- .../Statements/ProcessingStatements.cs | 2 +- .../Statements/StringLiteralStatements.cs | 2 +- .../Statements/SystemStatements.cs | 2 +- .../Statements/VariableStatements.cs | 3 +- YesNt.Interpreter/Utilities/Evaluator.cs | 2 +- .../Utilities/TemplateProcessor.cs | 85 ++++++++++++------- YesNt.Interpreter/YesNt.Interpreter.csproj | 7 ++ 35 files changed, 100 insertions(+), 80 deletions(-) diff --git a/YesNt.CodeEditor/Editor.cs b/YesNt.CodeEditor/Editor.cs index 6fead3e..25bdead 100644 --- a/YesNt.CodeEditor/Editor.cs +++ b/YesNt.CodeEditor/Editor.cs @@ -399,4 +399,4 @@ internal enum Mode Edit, Command, Debug -} +} \ No newline at end of file diff --git a/YesNt.CodeEditor/InputHandler.cs b/YesNt.CodeEditor/InputHandler.cs index 55dd583..585e96a 100644 --- a/YesNt.CodeEditor/InputHandler.cs +++ b/YesNt.CodeEditor/InputHandler.cs @@ -387,4 +387,4 @@ internal class InputHandler(TextEditor textEditor) textEditor.IsStepDebugMode = false; textEditor.EditMode = previousMode; } -} +} \ No newline at end of file diff --git a/YesNt.CodeEditor/SyntaxHighlighter.cs b/YesNt.CodeEditor/SyntaxHighlighter.cs index 6444c7d..061d834 100644 --- a/YesNt.CodeEditor/SyntaxHighlighter.cs +++ b/YesNt.CodeEditor/SyntaxHighlighter.cs @@ -173,4 +173,4 @@ internal partial class SyntaxHighlighter(ReadOnlyCollection { }); }); } -} +} \ No newline at end of file diff --git a/YesNt.Interpreter.Tests/CodeFlowStatementsTests.cs b/YesNt.Interpreter.Tests/CodeFlowStatementsTests.cs index 0c00e1f..e74d48c 100644 --- a/YesNt.Interpreter.Tests/CodeFlowStatementsTests.cs +++ b/YesNt.Interpreter.Tests/CodeFlowStatementsTests.cs @@ -77,5 +77,4 @@ public class CodeFlowStatementsTests YesNtAssert.ContainsTerminationMessage(lines, "Function \"nowhere\" not found"); } -} - +} \ No newline at end of file diff --git a/YesNt.Interpreter.Tests/CodeFlowTests.cs b/YesNt.Interpreter.Tests/CodeFlowTests.cs index 663de90..66df648 100644 --- a/YesNt.Interpreter.Tests/CodeFlowTests.cs +++ b/YesNt.Interpreter.Tests/CodeFlowTests.cs @@ -285,5 +285,4 @@ public class CodeFlowTests YesNtAssert.ContainsTerminationMessage(lines, "No matching while found"); } -} - +} \ No newline at end of file diff --git a/YesNt.Interpreter.Tests/ConsoleStatementsTests.cs b/YesNt.Interpreter.Tests/ConsoleStatementsTests.cs index 6c9fed9..61ddae7 100644 --- a/YesNt.Interpreter.Tests/ConsoleStatementsTests.cs +++ b/YesNt.Interpreter.Tests/ConsoleStatementsTests.cs @@ -106,5 +106,4 @@ public class ConsoleStatementsTests StringAssert.Contains(output.ToString(), "Terminated by external process"); } -} - +} \ No newline at end of file diff --git a/YesNt.Interpreter.Tests/FunctionStatementsTests.cs b/YesNt.Interpreter.Tests/FunctionStatementsTests.cs index 65011bf..667ae4f 100644 --- a/YesNt.Interpreter.Tests/FunctionStatementsTests.cs +++ b/YesNt.Interpreter.Tests/FunctionStatementsTests.cs @@ -223,5 +223,4 @@ public class FunctionStatementsTests YesNtAssert.IsLastLineEqual(lines, "outer_val"); } -} - +} \ No newline at end of file diff --git a/YesNt.Interpreter.Tests/ListStatementsTests.cs b/YesNt.Interpreter.Tests/ListStatementsTests.cs index cb8df50..e06f396 100644 --- a/YesNt.Interpreter.Tests/ListStatementsTests.cs +++ b/YesNt.Interpreter.Tests/ListStatementsTests.cs @@ -158,4 +158,4 @@ public class ListStatementsTests YesNtAssert.IsLastLineEqual(lines, "hello world"); } -} +} \ No newline at end of file diff --git a/YesNt.Interpreter.Tests/PredefinedVariableStatementsTests.cs b/YesNt.Interpreter.Tests/PredefinedVariableStatementsTests.cs index cf4b6c9..4deb1b1 100644 --- a/YesNt.Interpreter.Tests/PredefinedVariableStatementsTests.cs +++ b/YesNt.Interpreter.Tests/PredefinedVariableStatementsTests.cs @@ -104,4 +104,4 @@ public class PredefinedVariableStatementsTests Assert.IsTrue(int.TryParse(parts[0], out _)); Assert.IsTrue(int.TryParse(parts[1], out _)); } -} +} \ No newline at end of file diff --git a/YesNt.Interpreter.Tests/ProcessingStatementsTests.cs b/YesNt.Interpreter.Tests/ProcessingStatementsTests.cs index d4062a0..b3ec229 100644 --- a/YesNt.Interpreter.Tests/ProcessingStatementsTests.cs +++ b/YesNt.Interpreter.Tests/ProcessingStatementsTests.cs @@ -198,5 +198,4 @@ public class ProcessingStatementsTests YesNtAssert.IsLastLineEqual(lines, "2", timeout: 3000); } -} - +} \ No newline at end of file diff --git a/YesNt.Interpreter.Tests/StringLiteralStatementsTests.cs b/YesNt.Interpreter.Tests/StringLiteralStatementsTests.cs index 5f623f2..3402b69 100644 --- a/YesNt.Interpreter.Tests/StringLiteralStatementsTests.cs +++ b/YesNt.Interpreter.Tests/StringLiteralStatementsTests.cs @@ -110,4 +110,4 @@ public class StringLiteralStatementsTests YesNtAssert.ContainsTerminationMessage(lines, "Invalid string literal"); } -} +} \ No newline at end of file diff --git a/YesNt.Interpreter.Tests/SystemStatementsTests.cs b/YesNt.Interpreter.Tests/SystemStatementsTests.cs index fa7ec65..d8f4138 100644 --- a/YesNt.Interpreter.Tests/SystemStatementsTests.cs +++ b/YesNt.Interpreter.Tests/SystemStatementsTests.cs @@ -45,5 +45,4 @@ public class SystemStatementsTests YesNtAssert.ContainsTerminationMessage(lines, "Failed to start \"does_not_exist_abc_xyz\""); } -} - +} \ No newline at end of file diff --git a/YesNt.Interpreter.Tests/VariableStatementsTests.cs b/YesNt.Interpreter.Tests/VariableStatementsTests.cs index 5df7d8a..540e467 100644 --- a/YesNt.Interpreter.Tests/VariableStatementsTests.cs +++ b/YesNt.Interpreter.Tests/VariableStatementsTests.cs @@ -128,5 +128,4 @@ public class VariableStatementsTests YesNtAssert.ContainsTerminationMessage(lines, "Variable \"missing\" not found"); } -} - +} \ No newline at end of file diff --git a/YesNt.Interpreter.Tests/YesNtAssert.cs b/YesNt.Interpreter.Tests/YesNtAssert.cs index d860166..67ee9d5 100644 --- a/YesNt.Interpreter.Tests/YesNtAssert.cs +++ b/YesNt.Interpreter.Tests/YesNtAssert.cs @@ -123,4 +123,4 @@ internal static class YesNtAssert return (debugEventArgs, outputBuilder.ToString()); } -} +} \ No newline at end of file diff --git a/YesNt.Interpreter/Attributes/StatementAttribute.cs b/YesNt.Interpreter/Attributes/StatementAttribute.cs index 154936e..24f675a 100644 --- a/YesNt.Interpreter/Attributes/StatementAttribute.cs +++ b/YesNt.Interpreter/Attributes/StatementAttribute.cs @@ -107,4 +107,4 @@ public class StatementAttribute : Attribute SpaceAround = spaceAround; IgnoreSyntaxHighlighting = true; } -} +} \ No newline at end of file diff --git a/YesNt.Interpreter/Runtime/ExitMessages.cs b/YesNt.Interpreter/Runtime/ExitMessages.cs index 74b1dea..44591c8 100644 --- a/YesNt.Interpreter/Runtime/ExitMessages.cs +++ b/YesNt.Interpreter/Runtime/ExitMessages.cs @@ -80,4 +80,4 @@ internal static class ExitMessages { return $"Failed to start \"{program}\". {message}"; } -} +} \ No newline at end of file diff --git a/YesNt.Interpreter/Runtime/FunctionScope.cs b/YesNt.Interpreter/Runtime/FunctionScope.cs index e7c7abd..8203b46 100644 --- a/YesNt.Interpreter/Runtime/FunctionScope.cs +++ b/YesNt.Interpreter/Runtime/FunctionScope.cs @@ -25,4 +25,4 @@ internal class FunctionScope(int callerLine, Stack arguments) /// Gets the stack of output values pushed via push_out, consumed by the caller via %out. public Stack Results { get; } = new(); -} +} \ No newline at end of file diff --git a/YesNt.Interpreter/Runtime/Line.cs b/YesNt.Interpreter/Runtime/Line.cs index 03f7337..fcb53af 100644 --- a/YesNt.Interpreter/Runtime/Line.cs +++ b/YesNt.Interpreter/Runtime/Line.cs @@ -1,6 +1,4 @@ -using System.Collections.Generic; - -namespace YesNt.Interpreter.Runtime; +namespace YesNt.Interpreter.Runtime; /// /// Represents a single source line together with its location metadata. diff --git a/YesNt.Interpreter/Runtime/RuntimeInformation.cs b/YesNt.Interpreter/Runtime/RuntimeInformation.cs index 742674b..e099418 100644 --- a/YesNt.Interpreter/Runtime/RuntimeInformation.cs +++ b/YesNt.Interpreter/Runtime/RuntimeInformation.cs @@ -197,4 +197,4 @@ internal sealed class RuntimeInformation { Exit(ExitMessages.TerminatedByParentTask, stopAllTasks); } -} +} \ No newline at end of file diff --git a/YesNt.Interpreter/Runtime/StatementHandler.cs b/YesNt.Interpreter/Runtime/StatementHandler.cs index 710a8e1..1a2d66c 100644 --- a/YesNt.Interpreter/Runtime/StatementHandler.cs +++ b/YesNt.Interpreter/Runtime/StatementHandler.cs @@ -1,4 +1,5 @@ using System; + using YesNt.Interpreter.Attributes; namespace YesNt.Interpreter.Runtime; @@ -6,4 +7,4 @@ namespace YesNt.Interpreter.Runtime; /// /// Pre-calculated statement handler information for faster matching. /// -internal record StatementHandler(StatementAttribute Attribute, Action Handler, string FullName); +internal record StatementHandler(StatementAttribute Attribute, Action Handler, string FullName); \ No newline at end of file diff --git a/YesNt.Interpreter/Runtime/YesNtInterpreter.cs b/YesNt.Interpreter/Runtime/YesNtInterpreter.cs index c76f0ae..7d661ee 100644 --- a/YesNt.Interpreter/Runtime/YesNtInterpreter.cs +++ b/YesNt.Interpreter/Runtime/YesNtInterpreter.cs @@ -430,9 +430,9 @@ public class YesNtInterpreter { runtimeInfo.BlockBoundaries.Clear(); lineMatchingHandlers = new List>(runtimeInfo.Lines.Count); - + // Dictionary to track open blocks by their expected end statement name - var openBlocks = new Dictionary>(); + Dictionary> openBlocks = []; for (int i = 0; i < runtimeInfo.Lines.Count; i++) { @@ -449,7 +449,7 @@ public class YesNtInterpreter string blockPair = handler.Attribute.BlockPair; if (!string.IsNullOrEmpty(blockPair) && !handler.Attribute.IsBlockIntermediate) { - if (!openBlocks.TryGetValue(blockPair, out var stack)) + if (!openBlocks.TryGetValue(blockPair, out Stack stack)) { stack = new Stack(); openBlocks[blockPair] = stack; @@ -460,7 +460,7 @@ public class YesNtInterpreter // Track block ends if (handler.Attribute.IsBlockEnd) { - if (openBlocks.TryGetValue(handler.Attribute.Name, out var stack) && stack.Count > 0) + if (openBlocks.TryGetValue(handler.Attribute.Name, out Stack stack) && stack.Count > 0) { int startLine = stack.Pop(); runtimeInfo.BlockBoundaries[startLine] = i; @@ -474,7 +474,7 @@ public class YesNtInterpreter string intermediatePair = handler.Attribute.BlockPair; if (!string.IsNullOrEmpty(intermediatePair)) { - if (!openBlocks.TryGetValue(intermediatePair, out var stack)) + if (!openBlocks.TryGetValue(intermediatePair, out Stack stack)) { stack = new Stack(); openBlocks[intermediatePair] = stack; @@ -507,4 +507,4 @@ public class YesNtInterpreter _ => false }; } -} +} \ No newline at end of file diff --git a/YesNt.Interpreter/Statements/CodeFlowStatements.cs b/YesNt.Interpreter/Statements/CodeFlowStatements.cs index bbdf9e7..415f59e 100644 --- a/YesNt.Interpreter/Statements/CodeFlowStatements.cs +++ b/YesNt.Interpreter/Statements/CodeFlowStatements.cs @@ -279,4 +279,4 @@ internal class CodeFlowStatements : StatementRuntimeInformation { return RuntimeInfo.BlockBoundaries.TryGetValue(currentLine, out int cached) ? cached : -1; } -} +} \ No newline at end of file diff --git a/YesNt.Interpreter/Statements/ConsoleStatements.cs b/YesNt.Interpreter/Statements/ConsoleStatements.cs index 9751305..7df8c71 100644 --- a/YesNt.Interpreter/Statements/ConsoleStatements.cs +++ b/YesNt.Interpreter/Statements/ConsoleStatements.cs @@ -63,4 +63,4 @@ internal class ConsoleStatements : StatementRuntimeInformation { Console.Clear(); } -} +} \ No newline at end of file diff --git a/YesNt.Interpreter/Statements/FunctionStatements.cs b/YesNt.Interpreter/Statements/FunctionStatements.cs index fb83927..d6398fb 100644 --- a/YesNt.Interpreter/Statements/FunctionStatements.cs +++ b/YesNt.Interpreter/Statements/FunctionStatements.cs @@ -173,4 +173,4 @@ internal class FunctionStatements : StatementRuntimeInformation { RuntimeInfo.FunctionCallStack.Clear(); } -} +} \ No newline at end of file diff --git a/YesNt.Interpreter/Statements/ListStatements.cs b/YesNt.Interpreter/Statements/ListStatements.cs index ca4b010..04c1d41 100644 --- a/YesNt.Interpreter/Statements/ListStatements.cs +++ b/YesNt.Interpreter/Statements/ListStatements.cs @@ -271,4 +271,4 @@ internal class ListStatements : StatementRuntimeInformation return true; } -} +} \ No newline at end of file diff --git a/YesNt.Interpreter/Statements/PredefinedVariableStatements.cs b/YesNt.Interpreter/Statements/PredefinedVariableStatements.cs index c1c1c11..528880b 100644 --- a/YesNt.Interpreter/Statements/PredefinedVariableStatements.cs +++ b/YesNt.Interpreter/Statements/PredefinedVariableStatements.cs @@ -1,5 +1,4 @@ using System; -using System.Runtime.InteropServices; using YesNt.Interpreter.Attributes; using YesNt.Interpreter.Enums; @@ -47,4 +46,4 @@ internal class PredefinedVariableStatements : StatementRuntimeInformation { RuntimeInfo.CurrentLine = TemplateProcessor.ProcessDynamicPlaceholders(args, "%rand", () => random.Next(32767, int.MaxValue).ToString()).TrimEnd(); } -} +} \ No newline at end of file diff --git a/YesNt.Interpreter/Statements/ProcessingStatements.cs b/YesNt.Interpreter/Statements/ProcessingStatements.cs index d1c4983..c94aa83 100644 --- a/YesNt.Interpreter/Statements/ProcessingStatements.cs +++ b/YesNt.Interpreter/Statements/ProcessingStatements.cs @@ -103,4 +103,4 @@ internal partial class ProcessingStatements : StatementRuntimeInformation [GeneratedRegex("[0-9*+().,^%/-]+[0-9*+ ().,^%/-]+[0-9*+().,^%/-]+")] private static partial Regex CalculationRegex(); -} +} \ No newline at end of file diff --git a/YesNt.Interpreter/Statements/StringLiteralStatements.cs b/YesNt.Interpreter/Statements/StringLiteralStatements.cs index 6b56c69..8b3c161 100644 --- a/YesNt.Interpreter/Statements/StringLiteralStatements.cs +++ b/YesNt.Interpreter/Statements/StringLiteralStatements.cs @@ -79,4 +79,4 @@ internal class StringLiteralStatements : StatementRuntimeInformation _ => escapeChar }; } -} +} \ No newline at end of file diff --git a/YesNt.Interpreter/Statements/SystemStatements.cs b/YesNt.Interpreter/Statements/SystemStatements.cs index df894bc..9824bba 100644 --- a/YesNt.Interpreter/Statements/SystemStatements.cs +++ b/YesNt.Interpreter/Statements/SystemStatements.cs @@ -116,4 +116,4 @@ internal class SystemStatements : StatementRuntimeInformation RuntimeInfo.OutParametersStack = new(outputStack); RuntimeInfo.OutParametersStack.Push(process.ExitCode.ToString()); } -} +} \ No newline at end of file diff --git a/YesNt.Interpreter/Statements/VariableStatements.cs b/YesNt.Interpreter/Statements/VariableStatements.cs index a690b27..f69329c 100644 --- a/YesNt.Interpreter/Statements/VariableStatements.cs +++ b/YesNt.Interpreter/Statements/VariableStatements.cs @@ -1,4 +1,3 @@ -using System; using System.Collections.Generic; using YesNt.Interpreter.Attributes; @@ -65,4 +64,4 @@ internal partial class VariableStatements : StatementRuntimeInformation { RuntimeInfo.CurrentLine = TemplateProcessor.ProcessVariables(RuntimeInfo.CurrentLine, RuntimeInfo); } -} +} \ No newline at end of file diff --git a/YesNt.Interpreter/Utilities/Evaluator.cs b/YesNt.Interpreter/Utilities/Evaluator.cs index b8ddb3d..9252d7d 100644 --- a/YesNt.Interpreter/Utilities/Evaluator.cs +++ b/YesNt.Interpreter/Utilities/Evaluator.cs @@ -214,4 +214,4 @@ internal static partial class Evaluator [GeneratedRegex("(\\+ +\\-)+")] private static partial Regex PlusMinusRegex(); -} +} \ No newline at end of file diff --git a/YesNt.Interpreter/Utilities/TemplateProcessor.cs b/YesNt.Interpreter/Utilities/TemplateProcessor.cs index 368a171..5d497bd 100644 --- a/YesNt.Interpreter/Utilities/TemplateProcessor.cs +++ b/YesNt.Interpreter/Utilities/TemplateProcessor.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; + using YesNt.Interpreter.Runtime; namespace YesNt.Interpreter.Utilities; @@ -16,33 +17,39 @@ internal static class TemplateProcessor /// public static string ProcessVariables(string input, RuntimeInformation runtimeInfo) { - if (string.IsNullOrEmpty(input)) return input; + if (string.IsNullOrEmpty(input)) + { + return input; + } int startIdx = input.IndexOf("${", StringComparison.Ordinal); - if (startIdx == -1) return input; + if (startIdx == -1) + { + return input; + } StringBuilder sb = new StringBuilder(input.Length); int lastIdx = 0; while (startIdx != -1) { - sb.Append(input, lastIdx, startIdx - lastIdx); + _ = sb.Append(input, lastIdx, startIdx - lastIdx); int endIdx = input.IndexOf('}', startIdx + 2); if (endIdx == -1) { - sb.Append("${"); + _ = sb.Append("${"); lastIdx = startIdx + 2; } else { - string varName = input.Substring(startIdx + 2, endIdx - (startIdx + 2)); + string varName = input[(startIdx + 2)..endIdx]; if (runtimeInfo.Variables.TryGetValue(varName, out string value)) { - sb.Append(value); + _ = sb.Append(value); } else if (runtimeInfo.GlobalVariables.TryGetValue(varName, out value)) { - sb.Append(value); + _ = sb.Append(value); } else if (!runtimeInfo.IsSearching) { @@ -51,9 +58,9 @@ internal static class TemplateProcessor } else { - sb.Append("${"); - sb.Append(varName); - sb.Append('}'); + _ = sb.Append("${"); + _ = sb.Append(varName); + _ = sb.Append('}'); } lastIdx = endIdx + 1; @@ -62,7 +69,7 @@ internal static class TemplateProcessor startIdx = input.IndexOf("${", lastIdx, StringComparison.Ordinal); } - sb.Append(input, lastIdx, input.Length - lastIdx); + _ = sb.Append(input, lastIdx, input.Length - lastIdx); return sb.ToString(); } @@ -71,10 +78,16 @@ internal static class TemplateProcessor /// public static string ProcessStackParameters(string input, string placeholder, Stack stack, RuntimeInformation runtimeInfo, string emptyStackMessage) { - if (string.IsNullOrEmpty(input)) return input; + if (string.IsNullOrEmpty(input)) + { + return input; + } int startIdx = input.IndexOf(placeholder, StringComparison.Ordinal); - if (startIdx == -1) return input; + if (startIdx == -1) + { + return input; + } StringBuilder sb = new StringBuilder(input.Length); int lastIdx = 0; @@ -82,18 +95,18 @@ internal static class TemplateProcessor while (startIdx != -1) { - sb.Append(input, lastIdx, startIdx - lastIdx); + _ = sb.Append(input, lastIdx, startIdx - lastIdx); if (stack.Count == 0) { runtimeInfo.Exit(emptyStackMessage, true); return input; } - sb.Append(stack.Pop()); + _ = sb.Append(stack.Pop()); lastIdx = startIdx + placeholderLen; startIdx = input.IndexOf(placeholder, lastIdx, StringComparison.Ordinal); } - sb.Append(input, lastIdx, input.Length - lastIdx); + _ = sb.Append(input, lastIdx, input.Length - lastIdx); return sb.ToString(); } @@ -102,9 +115,7 @@ internal static class TemplateProcessor /// public static string ProcessSimplePlaceholders(string input, string placeholder, string value) { - if (string.IsNullOrEmpty(input)) return input; - - return input.Replace(placeholder, value, StringComparison.Ordinal); + return string.IsNullOrEmpty(input) ? input : input.Replace(placeholder, value, StringComparison.Ordinal); } /// @@ -112,10 +123,16 @@ internal static class TemplateProcessor /// public static string ProcessDynamicPlaceholders(string input, string placeholder, Func valueProvider) { - if (string.IsNullOrEmpty(input)) return input; + if (string.IsNullOrEmpty(input)) + { + return input; + } int startIdx = input.IndexOf(placeholder, StringComparison.Ordinal); - if (startIdx == -1) return input; + if (startIdx == -1) + { + return input; + } StringBuilder sb = new StringBuilder(input.Length); int lastIdx = 0; @@ -123,13 +140,13 @@ internal static class TemplateProcessor while (startIdx != -1) { - sb.Append(input, lastIdx, startIdx - lastIdx); - sb.Append(valueProvider()); + _ = sb.Append(input, lastIdx, startIdx - lastIdx); + _ = sb.Append(valueProvider()); lastIdx = startIdx + placeholderLen; startIdx = input.IndexOf(placeholder, lastIdx, StringComparison.Ordinal); } - sb.Append(input, lastIdx, input.Length - lastIdx); + _ = sb.Append(input, lastIdx, input.Length - lastIdx); return sb.ToString(); } @@ -138,10 +155,16 @@ internal static class TemplateProcessor /// public static string ProcessCalculations(string input, RuntimeInformation runtimeInfo, Regex calculationRegex) { - if (string.IsNullOrEmpty(input)) return input; + if (string.IsNullOrEmpty(input)) + { + return input; + } MatchCollection matches = calculationRegex.Matches(input); - if (matches.Count == 0) return input; + if (matches.Count == 0) + { + return input; + } StringBuilder sb = new StringBuilder(input.Length); int lastIdx = 0; @@ -149,19 +172,19 @@ internal static class TemplateProcessor for (int i = 0; i < matches.Count; i++) { Match match = matches[i]; - sb.Append(input, lastIdx, match.Index - lastIdx); - + _ = sb.Append(input, lastIdx, match.Index - lastIdx); + string res = Evaluator.Calculate(match.Value); if (res is null) { runtimeInfo.Exit(ExitMessages.InvalidOperation, true); return input; } - sb.Append(res); + _ = sb.Append(res); lastIdx = match.Index + match.Length; } - sb.Append(input, lastIdx, input.Length - lastIdx); + _ = sb.Append(input, lastIdx, input.Length - lastIdx); return sb.ToString(); } -} +} \ No newline at end of file diff --git a/YesNt.Interpreter/YesNt.Interpreter.csproj b/YesNt.Interpreter/YesNt.Interpreter.csproj index a396e7d..d280294 100644 --- a/YesNt.Interpreter/YesNt.Interpreter.csproj +++ b/YesNt.Interpreter/YesNt.Interpreter.csproj @@ -47,6 +47,13 @@ + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + +