Skip to content

Commit

Permalink
String Extensions complete
Browse files Browse the repository at this point in the history
  • Loading branch information
cb-martinsmith committed Aug 20, 2024
1 parent 1fa915e commit c9b2b39
Show file tree
Hide file tree
Showing 5 changed files with 1,271 additions and 967 deletions.
37 changes: 37 additions & 0 deletions src/DNX.Extensions/Strings/BuiltInTypesExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Linq;

namespace DNX.Extensions.Strings;

public static class BuiltInTypesExtensions
{
/// <summary>
/// Converts a boolean to text.
/// </summary>
/// <param name="value">if set to <c>true</c> [value].</param>
/// <param name="trueText">The true text.</param>
/// <param name="falseText">The false text.</param>
/// <returns></returns>
public static string ToText(this bool value, string trueText = "True", string falseText = "False")
{
return value ? trueText : falseText;
}

/// <summary>
/// Converts a boolean to text, using "Yes" and "No"
/// </summary>
/// <param name="value">if set to <c>true</c> [value].</param>
/// <returns></returns>
public static string ToYesNo(this bool value) => value.ToText("Yes", "No");

/// <summary>
/// Converts an int32 to hexstring.
/// </summary>
/// <param name="input">The input.</param>
/// <param name="format">The format.</param>
/// <returns></returns>
public static string ToHexString(this int input, string format = "X2")
{
return input.ToString(format);
}
}
174 changes: 93 additions & 81 deletions src/DNX.Extensions/Strings/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ namespace DNX.Extensions.Strings;
/// </summary>
public static class StringExtensions
{
private const string SI_WORDIFY_REGEX_TEXT = "(?<=[a-z])(?<x>[A-Z])|(?<=.)(?<x>[A-Z])(?=[a-z])";

private static readonly Regex WordifyRegex = new(SI_WORDIFY_REGEX_TEXT, RegexOptions.Compiled);
private const string Wordify_RegexText = @"\B[A-Z]";
private static readonly Regex Wordify_Regex = new(Wordify_RegexText);

/// <summary>
/// Determines whether the specified text is null or empty
Expand Down Expand Up @@ -57,7 +56,7 @@ public static string NullIfEmpty(this string text)
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static string NullIfWhitespace(this string text)
public static string NullIfEmptyOrWhitespace(this string text)
{
return string.IsNullOrWhiteSpace(text) ? null : text;
}
Expand Down Expand Up @@ -305,25 +304,27 @@ public static string After(this string text, string startText, StringComparison
}

/// <summary>
/// Gets the text after the specified start text.
/// Gets the text after the last instance of the specified start text.
/// </summary>
/// <param name="text">The text.</param>
/// <param name="startText">The start text.</param>
/// <param name="comparison">The comparison.</param>
/// <returns>System.String.</returns>
public static string From(this string text, string startText, StringComparison comparison = StringComparison.Ordinal)
public static string AfterLast(this string text, string startText, StringComparison comparison = StringComparison.Ordinal)
{
if (string.IsNullOrEmpty(text))
{
return null;
}

var startTextLength = startText?.Length ?? 0;

var startIndex = string.IsNullOrEmpty(startText)
? -1
: text.IndexOf(startText, comparison);
: text.LastIndexOf(startText, comparison);

var result = startIndex >= 0
? text.Substring(startIndex)
? text.Substring(startIndex + startTextLength)
: null;

return result;
Expand All @@ -336,16 +337,14 @@ public static string From(this string text, string startText, StringComparison c
/// <param name="startText">The start text.</param>
/// <param name="comparison">The comparison.</param>
/// <returns>System.String.</returns>
public static string FromLast(this string text, string startText, StringComparison comparison = StringComparison.Ordinal)
public static string From(this string text, string startText, StringComparison comparison = StringComparison.Ordinal)
{
if (string.IsNullOrEmpty(text))
if (string.IsNullOrEmpty(text) || string.IsNullOrEmpty(startText))
{
return null;
}

var startIndex = string.IsNullOrEmpty(startText)
? -1
: text.LastIndexOf(startText, comparison);
var startIndex = text.IndexOf(startText, comparison);

var result = startIndex >= 0
? text.Substring(startIndex)
Expand All @@ -355,51 +354,45 @@ public static string FromLast(this string text, string startText, StringComparis
}

/// <summary>
/// Gets the text before the specified end text.
/// Gets the text after the specified start text.
/// </summary>
/// <param name="text">The text.</param>
/// <param name="endText">The end text.</param>
/// <param name="startText">The start text.</param>
/// <param name="comparison">The comparison.</param>
/// <returns>System.String.</returns>
public static string AsFarAs(this string text, string endText, StringComparison comparison = StringComparison.Ordinal)
public static string FromLast(this string text, string startText, StringComparison comparison = StringComparison.Ordinal)
{
if (string.IsNullOrEmpty(text))
if (string.IsNullOrEmpty(text) || string.IsNullOrEmpty(startText))
{
return null;
}

var endTextLength = endText?.Length ?? 0;

var endIndex = string.IsNullOrEmpty(endText)
? -1
: text.IndexOf(endText, comparison);
var startIndex = text.LastIndexOf(startText, comparison);

var result = endIndex >= 0
? text.Substring(0, endIndex + endTextLength)
var result = startIndex >= 0
? text.Substring(startIndex)
: null;

return result;
}

/// <summary>
/// Gets the text before the last instance of the specified end text.
/// Gets the text before the specified end text.
/// </summary>
/// <param name="text">The text.</param>
/// <param name="endText">The end text.</param>
/// <param name="comparison">The comparison.</param>
/// <returns>System.String.</returns>
public static string AsFarAsLast(this string text, string endText, StringComparison comparison = StringComparison.Ordinal)
public static string AsFarAs(this string text, string endText, StringComparison comparison = StringComparison.Ordinal)
{
if (string.IsNullOrEmpty(text))
if (string.IsNullOrEmpty(text) || string.IsNullOrEmpty(endText))
{
return null;
}

var endTextLength = endText?.Length ?? 0;

var endIndex = string.IsNullOrEmpty(endText)
? -1
: text.LastIndexOf(endText, comparison);
var endIndex = text.IndexOf(endText, comparison);

var result = endIndex >= 0
? text.Substring(0, endIndex + endTextLength)
Expand All @@ -409,27 +402,25 @@ public static string AsFarAsLast(this string text, string endText, StringCompari
}

/// <summary>
/// Gets the text after the last instance of the specified start text.
/// Gets the text before the last instance of the specified end text.
/// </summary>
/// <param name="text">The text.</param>
/// <param name="startText">The start text.</param>
/// <param name="endText">The end text.</param>
/// <param name="comparison">The comparison.</param>
/// <returns>System.String.</returns>
public static string AfterLast(this string text, string startText, StringComparison comparison = StringComparison.Ordinal)
public static string AsFarAsLast(this string text, string endText, StringComparison comparison = StringComparison.Ordinal)
{
if (string.IsNullOrEmpty(text))
if (string.IsNullOrEmpty(text) || string.IsNullOrEmpty(endText))
{
return null;
}

var startTextLength = startText?.Length ?? 0;
var endTextLength = endText?.Length ?? 0;

var startIndex = string.IsNullOrEmpty(startText)
? -1
: text.LastIndexOf(startText, comparison);
var endIndex = text.LastIndexOf(endText, comparison);

var result = startIndex >= 0
? text.Substring(startIndex + startTextLength)
var result = endIndex >= 0
? text.Substring(0, endIndex + endTextLength)
: null;

return result;
Expand Down Expand Up @@ -599,18 +590,14 @@ public static string Reverse(this string text)
/// <remarks>Also available as an extension method</remarks>
public static IEnumerable<string> SplitText(this string text, string delimiters, StringSplitOptions options = StringSplitOptions.None, SplitDelimiterType delimiterType = SplitDelimiterType.Any)
{
switch (delimiterType)
return delimiterType switch
{
case SplitDelimiterType.Any:
return text.Split(delimiters.ToCharArray(), options);

case SplitDelimiterType.All:
SplitDelimiterType.Any => text.Split(delimiters.ToCharArray(), options),
SplitDelimiterType.All =>
// NOTE: A native text.Split(string, ...) is available in NET Core 2.1+
return text.SplitByText(delimiters, options);

default:
throw new ArgumentOutOfRangeException(nameof(delimiterType), delimiterType, $"Value must be one of {string.Join(",", Enum.GetNames(typeof(SplitDelimiterType)))}");
}
text.SplitByText(delimiters, options),
_ => throw new ArgumentOutOfRangeException(nameof(delimiterType), delimiterType, $"Value must be one of {string.Join(",", Enum.GetNames(typeof(SplitDelimiterType)))}")
};
}

/// <summary>
Expand Down Expand Up @@ -661,14 +648,14 @@ public static string CoalesceNullWith(this string text, params string[] items)
}

/// <summary>
/// Coalesces the list of strings to find the first not null
/// Coalesces this value with the first value that is not null or empty.
/// </summary>
/// <param name="strings">The strings.</param>
/// <returns>System.String.</returns>
/// <remarks>Also available as an extension method</remarks>
public static string CoalesceNull(params string[] strings)
/// <param name="text">The text.</param>
/// <param name="items">The items.</param>
/// <returns></returns>
public static string CoalesceNullWith(this string text, IList<string> items)
{
return strings.CoalesceNull();
return text ?? items.CoalesceNull();
}

/// <summary>
Expand Down Expand Up @@ -698,14 +685,16 @@ public static string CoalesceNullOrEmptyWith(this string text, params string[] i
}

/// <summary>
/// Coalesces the list of strings to find the first not null or empty.
/// Coalesces this value with the first value that is not null or empty.
/// </summary>
/// <param name="strings">The strings.</param>
/// <returns>System.String.</returns>
/// <remarks>Also available as an extension method</remarks>
public static string CoalesceNullOrEmpty(params string[] strings)
/// <param name="text">The text.</param>
/// <param name="items">The items.</param>
/// <returns></returns>
public static string CoalesceNullOrEmptyWith(this string text, IList<string> items)
{
return strings.CoalesceNullOrEmpty();
return string.IsNullOrEmpty(text)
? items.CoalesceNullOrEmpty()
: text;
}

/// <summary>
Expand Down Expand Up @@ -733,15 +722,18 @@ public static string CoalesceNullOrWhitespaceWith(this string text, params strin
? items.CoalesceNullOrWhitespace()
: text;
}

/// <summary>
/// Coalesces the list of strings to find the first not null or whitespace.
/// Coalesces this value with the first value that is not null or empty.
/// </summary>
/// <param name="strings">The strings.</param>
/// <returns>System.String.</returns>
/// <remarks>Also available as an extension method</remarks>
public static string CoalesceNullOrWhitespace(params string[] strings)
/// <param name="text">The text.</param>
/// <param name="items">The items.</param>
/// <returns></returns>
public static string CoalesceNullOrWhitespaceWith(this string text, IList<string> items)
{
return strings.CoalesceNullOrWhitespace();
return string.IsNullOrWhiteSpace(text)
? items.CoalesceNullOrWhitespace()
: text;
}

/// <summary>
Expand Down Expand Up @@ -831,19 +823,7 @@ public static string Wordify(this string text, IList<string> preservedWords)
if (string.IsNullOrEmpty(text))
return text;

var regexText = @"\B[A-Z]";

var regex = new Regex(regexText);

var result = regex.Replace(text, " $0");

//if (string.IsNullOrWhiteSpace(text))
// return text;
//
//var result = WordifyRegex
// //.Replace(text, " ${x}")
// .Replace(text, " $0")
// .Replace(" ", " ");
var result = Wordify_Regex.Replace(text, " $0");

if (preservedWords.HasAny())
{
Expand All @@ -869,4 +849,36 @@ public static string Truncate(this string input, int length)
? input.Substring(0, length)
: input;
}

/// <summary>
/// Converts an ASCII encoded string to Hexadecimal
/// </summary>
/// <param name="input">ASCII Encoded Input</param>
/// <returns>Input as Hexadecimal</returns>
public static string ToHexString(this string input, string format = "X2")
{
return string.Join("", input.Select(c => ((int)c).ToString(format)));
}

/// <summary>
/// Converts string into a byte[] using the default encoding (UTF-8)
/// </summary>
/// <param name="input">The string to turn into the byte[]</param>
public static byte[] GetBytes(this string input)
{
return input.GetBytes(Encoding.UTF8);
}

/// <summary>
/// Converts string into a byte[] using the specified encoding
/// </summary>
/// <param name="input">The string to turn into the byte[]</param>
/// <param name="encoding">The encoding.</param>
/// <returns></returns>
public static byte[] GetBytes(this string input, Encoding encoding)
{
encoding ??= Encoding.UTF8;

return encoding.GetBytes(input);
}
}
Loading

0 comments on commit c9b2b39

Please sign in to comment.