- Split into three projects: Library, Console, and Tests - Library compiles as QRScript.Interpreter to avoid conflicts
56 lines
2 KiB
C#
56 lines
2 KiB
C#
namespace QRScript.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 IEnumerable<string> lines)
|
|
{
|
|
if (lines is null)
|
|
throw new ArgumentNullException(nameof(lines));
|
|
|
|
return string.Join(Environment.NewLine, lines);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Determines whether the string exceeds the specified character length limit.
|
|
/// </summary>
|
|
/// <param name="text">The input string to evaluate.</param>
|
|
/// <param name="limit">The maximum number of allowed characters.</param>
|
|
/// <returns><c>true</c> if the string's length is greater than or equal to <paramref name="limit"/>; otherwise, <c>false</c>.</returns>
|
|
public static bool 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;
|
|
}
|
|
}
|