From e3c62e17083d3021ee47486254fa88c7091f0014 Mon Sep 17 00:00:00 2001 From: Tony Bark Date: Wed, 7 May 2025 12:53:53 -0400 Subject: [PATCH] Changed ScriptHelper to StringExtensions - Helper methods are now a part of string class --- BuildCommand.cs | 8 +++--- GlobalUsing.cs | 1 + Glyph.csproj | 2 +- RunCommand.cs | 2 +- ScriptHelper.cs | 35 -------------------------- StringExtensions.cs | 61 +++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 68 insertions(+), 41 deletions(-) delete mode 100644 ScriptHelper.cs create mode 100644 StringExtensions.cs diff --git a/BuildCommand.cs b/BuildCommand.cs index f962c6b..85c7315 100644 --- a/BuildCommand.cs +++ b/BuildCommand.cs @@ -27,17 +27,17 @@ public class BuildCommand : ICommand } ; var lines = await File.ReadAllLinesAsync(path); - var source = ScriptHelper.LinesToString(lines); + var source = lines.JoinLines(); if (Verbose) { await console.Output.WriteLineAsync($"Compiling: {path}"); } - var counter = ScriptHelper.CharacterCounter(1259, source); + var counter = source.ExceedsLengthLimit(1259); - if (counter.checker) - await console.Output.WriteAsync(counter.output); + if (counter.IsOverLimit) + await console.Output.WriteAsync(counter.WarningMessage); var qrGenerator = new QRCodeGenerator(); diff --git a/GlobalUsing.cs b/GlobalUsing.cs index 45c2ce0..5cd5a46 100644 --- a/GlobalUsing.cs +++ b/GlobalUsing.cs @@ -1,4 +1,5 @@ global using Glyph; +global using Glyph.Extensions; global using QRCoder; global using CliFx; global using CliFx.Attributes; diff --git a/Glyph.csproj b/Glyph.csproj index fcbcbff..7390897 100644 --- a/Glyph.csproj +++ b/Glyph.csproj @@ -6,7 +6,7 @@ enable glyph enable - 0.2.100 + 0.2.103 diff --git a/RunCommand.cs b/RunCommand.cs index f75567d..45e359b 100644 --- a/RunCommand.cs +++ b/RunCommand.cs @@ -33,7 +33,7 @@ public class RunCommand : ICommand } lines = await System.IO.File.ReadAllLinesAsync(FilePath); - var source = ScriptHelper.LinesToString(lines); + var source = lines.JoinLines(); if (!string.IsNullOrEmpty(InputPath)) { diff --git a/ScriptHelper.cs b/ScriptHelper.cs deleted file mode 100644 index 8c049bc..0000000 --- a/ScriptHelper.cs +++ /dev/null @@ -1,35 +0,0 @@ -namespace Glyph; - -using System.Text; - -public static class ScriptHelper -{ - /// - /// Encodes a string to Base64. - /// - /// The string to encode. - /// The Base64 encoded string. - // https://arcanecode.com/2007/03/21/encoding-strings-to-base64-in-c/ - static public string EncodeTo64(string toEncode) - { - byte[] toEncodeAsBytes = System.Text.UnicodeEncoding.Default.GetBytes(toEncode); - string returnValue = System.Convert.ToBase64String(toEncodeAsBytes); - return returnValue; - - } - - /// - /// Converts an array of strings to a single string with each element separated by a newline. - /// - /// The array of strings to convert. - /// The resulting string. - static public string LinesToString(string[] lines) => string.Join(Environment.NewLine, lines); - - static public (bool checker, string output) CharacterCounter(int limit, string text) - { - if (text.Length >= limit) - return (true, $"Warning source is over {limit:n} characters"); - - return (false, string.Empty); - } -} diff --git a/StringExtensions.cs b/StringExtensions.cs new file mode 100644 index 0000000..7a947dd --- /dev/null +++ b/StringExtensions.cs @@ -0,0 +1,61 @@ +namespace Glyph.Extensions; + +using System.Text; + +/// +/// Provides extension methods for common string transformations and validations. +/// +public static class StringExtensions +{ + /// + /// Encodes the current string instance to Base64 using Unicode encoding. + /// + /// The input string to encode. + /// A Base64-encoded version of the input string. + /// + /// This uses Unicode (UTF-16) encoding, which is standard in .NET strings. + /// + public static string ToBase64(this string input) + { + if (input is null) + throw new ArgumentNullException(nameof(input)); + + byte[] bytes = Encoding.Unicode.GetBytes(input); + return Convert.ToBase64String(bytes); + } + + /// + /// Joins an array of strings into a single string separated by the system's newline. + /// + /// Array of strings to join. + /// A single string composed of all input lines, separated by newlines. + public static string JoinLines(this string[] lines) + { + if (lines is null) + throw new ArgumentNullException(nameof(lines)); + + return string.Join(Environment.NewLine, lines); + } + + /// + /// Checks if the string exceeds the specified character limit and returns a warning if it does. + /// + /// The text to check. + /// The maximum allowed character length. + /// + /// A tuple containing a boolean flag and an optional warning message. + /// The flag is true if the limit is exceeded, and false otherwise. + /// + public static (bool IsOverLimit, string WarningMessage) ExceedsLengthLimit(this string text, int limit) + { + if (text is null) + throw new ArgumentNullException(nameof(text)); + + if (limit < 0) + throw new ArgumentOutOfRangeException(nameof(limit), "Limit must be non-negative."); + + return text.Length >= limit + ? (true, $"Warning: text is over {limit:n0} characters") + : (false, string.Empty); + } +}