Changed ScriptHelper to StringExtensions

- Helper methods are now a part of string class
This commit is contained in:
Tony Bark 2025-05-07 12:53:53 -04:00
parent eb5539f45f
commit e3c62e1708
6 changed files with 68 additions and 41 deletions

View file

@ -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();

View file

@ -1,4 +1,5 @@
global using Glyph;
global using Glyph.Extensions;
global using QRCoder;
global using CliFx;
global using CliFx.Attributes;

View file

@ -6,7 +6,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<AssemblyName>glyph</AssemblyName>
<Nullable>enable</Nullable>
<Version>0.2.100</Version>
<Version>0.2.103</Version>
</PropertyGroup>
<ItemGroup>

View file

@ -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))
{

View file

@ -1,35 +0,0 @@
namespace Glyph;
using System.Text;
public static class ScriptHelper
{
/// <summary>
/// Encodes a string to Base64.
/// </summary>
/// <param name="toEncode">The string to encode.</param>
/// <returns>The Base64 encoded string.</returns>
// 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;
}
/// <summary>
/// Converts an array of strings to a single string with each element separated by a newline.
/// </summary>
/// <param name="lines">The array of strings to convert.</param>
/// <returns>The resulting string.</returns>
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);
}
}

61
StringExtensions.cs Normal file
View file

@ -0,0 +1,61 @@
namespace Glyph.Extensions;
using System.Text;
/// <summary>
/// Provides extension methods for common string transformations and validations.
/// </summary>
public static class StringExtensions
{
/// <summary>
/// Encodes the current string instance to Base64 using Unicode encoding.
/// </summary>
/// <param name="input">The input string to encode.</param>
/// <returns>A Base64-encoded version of the input string.</returns>
/// <remarks>
/// This uses Unicode (UTF-16) encoding, which is standard in .NET strings.
/// </remarks>
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);
}
/// <summary>
/// Joins an array of strings into a single string separated by the system's newline.
/// </summary>
/// <param name="lines">Array of strings to join.</param>
/// <returns>A single string composed of all input lines, separated by newlines.</returns>
public static string JoinLines(this string[] lines)
{
if (lines is null)
throw new ArgumentNullException(nameof(lines));
return string.Join(Environment.NewLine, lines);
}
/// <summary>
/// Checks if the string exceeds the specified character limit and returns a warning if it does.
/// </summary>
/// <param name="text">The text to check.</param>
/// <param name="limit">The maximum allowed character length.</param>
/// <returns>
/// A tuple containing a boolean flag and an optional warning message.
/// The flag is <c>true</c> if the limit is exceeded, and <c>false</c> otherwise.
/// </returns>
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);
}
}