| | | 1 | | using AmbientServices.Utilities; |
| | | 2 | | using System; |
| | | 3 | | using System.Globalization; |
| | | 4 | | using System.Linq; |
| | | 5 | | |
| | | 6 | | namespace AmbientServices.Extensions; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// A static partial class that extends <see cref="string"/>. |
| | | 10 | | /// </summary> |
| | | 11 | | /// <remarks> |
| | | 12 | | /// <pitch>Natural string comparison ("a99b" before "a100b") for sorting user-visible names that emb |
| | | 13 | | /// <pledge> |
| | | 14 | | /// Natural comparison orders embedded numeric sequences by numeric value rather than character order: comma-grouped dig |
| | | 15 | | /// Every comparison in the class is invariant-culture or ordinal — no member's result is affected by the thread's curre |
| | | 16 | | /// </pledge> |
| | | 17 | | /// <plan> |
| | | 18 | | /// Natural comparison is regex normalization followed by a single ordinary compare: both strings are rewritten by <see |
| | | 19 | | /// </plan> |
| | | 20 | | /// </remarks> |
| | | 21 | | public static partial class StringExtensions |
| | | 22 | | { |
| | 2 | 23 | | private static readonly System.Text.RegularExpressions.Regex DigitSequenceRegex = new(@"\d+", System.Text.RegularExp |
| | 2 | 24 | | private static readonly System.Text.RegularExpressions.Regex CommaSeparatedDigits = new(@"(?<csn>\d+(?:,\d+)+)", Sys |
| | | 25 | | /// <summary> |
| | | 26 | | /// Compares two strings naturally, so that numeric sequences embedded in the strings are sorted numerically instead |
| | | 27 | | /// For example, a regular string sort would sort "a100b" before "a99b", but a natural string sort would not. |
| | | 28 | | /// Numeric sequences sort numerically as if they were zero-padded. |
| | | 29 | | /// Floating-point numbers and negatives are supported, but sequences of numbers separated by single dashes are trea |
| | | 30 | | /// </summary> |
| | | 31 | | /// <param name="a">The first string to compare.</param> |
| | | 32 | | /// <param name="b">The second string to compare.</param> |
| | | 33 | | /// <returns>>0 if the first string should be sorted after the second one, <0 if the second string should be s |
| | | 34 | | public static int CompareNaturalInvariant(this string a, string b) |
| | | 35 | | { |
| | 2 | 36 | | return RegexCompareNaturalInvariant(a, b, false); |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Compares two strings naturally, so that numeric sequences embedded in the strings are sorted numerically instead |
| | | 41 | | /// For example, a regular string sort would sort "a100b" before "a99b", but a natural string sort would not. |
| | | 42 | | /// Numeric sequences sort numerically as if they were zero-padded. |
| | | 43 | | /// Floating-point numbers and negatives are supported, but sequences of numbers separated by single dashes are trea |
| | | 44 | | /// </summary> |
| | | 45 | | /// <param name="a">The first string to compare.</param> |
| | | 46 | | /// <param name="b">The second string to compare.</param> |
| | | 47 | | /// <param name="ignoreCase">Whether to ignore casing when comparing.</param> |
| | | 48 | | /// <returns>>0 if the first string should be sorted after the second one, <0 if the second string should be s |
| | | 49 | | public static int CompareNaturalInvariant(this string a, string b, bool ignoreCase) |
| | | 50 | | { |
| | 2 | 51 | | return RegexCompareNaturalInvariant(a, b, ignoreCase); |
| | | 52 | | } |
| | | 53 | | /// <summary> |
| | | 54 | | /// Compares two strings using a 'natural' compare algorithm which compares embedded numbers numerically rather than |
| | | 55 | | /// </summary> |
| | | 56 | | /// <param name="a">The first string to compare.</param> |
| | | 57 | | /// <param name="b">The second string to compare.</param> |
| | | 58 | | /// <param name="ignoreCase">Whether to ignore casing when comparing.</param> |
| | | 59 | | /// <returns>0 if the strings are the same, >0 if the first string is 'greater' than the second, or <0 if the |
| | | 60 | | public static int RegexCompareNaturalInvariant(this string a, string b, bool ignoreCase) |
| | | 61 | | { |
| | 2 | 62 | | if (string.IsNullOrEmpty(a)) |
| | | 63 | | { |
| | 2 | 64 | | if (string.IsNullOrEmpty(b)) return 0; |
| | 2 | 65 | | return -1; |
| | | 66 | | } |
| | 2 | 67 | | else if (string.IsNullOrEmpty(b)) |
| | | 68 | | { |
| | 2 | 69 | | return 1; |
| | | 70 | | } |
| | | 71 | | // first replace any sequences of comma-separated digits with just the digits |
| | 2 | 72 | | string normA = CommaSeparatedDigits.Replace(a, m => m.Value.Replace(",", "", StringComparison.Ordinal)); |
| | 2 | 73 | | string normB = CommaSeparatedDigits.Replace(b, m => m.Value.Replace(",", "", StringComparison.Ordinal)); |
| | | 74 | | // next count the longest digit sequence in either string |
| | 2 | 75 | | int maxDigitsA = DigitSequenceRegex.Matches(normA + ".0").Cast<System.Text.RegularExpressions.Match>().Select(di |
| | 2 | 76 | | int maxDigitsB = DigitSequenceRegex.Matches(normB + ".0").Cast<System.Text.RegularExpressions.Match>().Select(di |
| | 2 | 77 | | int maxDigits = Math.Max(maxDigitsA, maxDigitsB); |
| | | 78 | | // next expand each digit sequence to that length and transform the string into one that will sorts the way we w |
| | 2 | 79 | | normA = StringUtilities.NormalizeStringWithNumberSequences(normA, maxDigits); |
| | 2 | 80 | | normB = StringUtilities.NormalizeStringWithNumberSequences(normB, maxDigits); |
| | | 81 | | // now compare the strings |
| | 2 | 82 | | return CultureInfo.InvariantCulture.CompareInfo.Compare(normA, normB, ignoreCase ? CompareOptions.OrdinalIgnoreC |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | #if !NETCOREAPP3_1 && !NET5_0_OR_GREATER |
| | | 86 | | #pragma warning disable CA1801 // these functions specifically make the code behave the old pre net5.0 way. |
| | | 87 | | /// <summary> |
| | | 88 | | /// Replaces matching parts of the string with another string. |
| | | 89 | | /// </summary> |
| | | 90 | | /// <param name="source">The source string.</param> |
| | | 91 | | /// <param name="find">The string to find.</param> |
| | | 92 | | /// <param name="target">The string to put in place of <paramref name="find"/>.</param> |
| | | 93 | | /// <param name="compare">A <see cref="StringComparison"/> indicating how to perform the search.</param> |
| | | 94 | | /// <returns><paramref name="source"/> string with instances of <paramref name="find"/> replaced with <paramref name |
| | | 95 | | public static string Replace(this string source, string find, string target, StringComparison compare) |
| | | 96 | | { |
| | | 97 | | if (source == null) throw new ArgumentNullException(nameof(source)); |
| | | 98 | | return source.Replace(find, target, StringComparison.Ordinal); |
| | | 99 | | } |
| | | 100 | | /// <summary> |
| | | 101 | | /// Checks to see if a string contains the specified character. |
| | | 102 | | /// </summary> |
| | | 103 | | /// <param name="source">The source string.</param> |
| | | 104 | | /// <param name="find">The character to look for.</param> |
| | | 105 | | /// <param name="compare">A <see cref="StringComparison"/> indicating how to perform the search.</param> |
| | | 106 | | /// <returns>Whether or not <paramref name="source"/> contains <paramref name="find"/>.</returns> |
| | | 107 | | public static bool Contains(this string source, char find, StringComparison compare) |
| | | 108 | | { |
| | | 109 | | if (source == null) throw new ArgumentNullException(nameof(source)); |
| | | 110 | | return source.Contains(find, StringComparison.Ordinal); |
| | | 111 | | } |
| | | 112 | | /// <summary> |
| | | 113 | | /// Checks to see if a string contains a specified string. |
| | | 114 | | /// </summary> |
| | | 115 | | /// <param name="source">The source string.</param> |
| | | 116 | | /// <param name="find">The string to look for.</param> |
| | | 117 | | /// <param name="compare">A <see cref="StringComparison"/> indicating how to perform the search.</param> |
| | | 118 | | /// <returns>Whether or not <paramref name="source"/> contains <paramref name="find"/>.</returns> |
| | | 119 | | public static bool Contains(this string source, string find, StringComparison compare) |
| | | 120 | | { |
| | | 121 | | if (source == null) throw new ArgumentNullException(nameof(source)); |
| | | 122 | | return source.Contains(find, StringComparison.Ordinal); |
| | | 123 | | } |
| | | 124 | | /// <summary> |
| | | 125 | | /// Gets a 32-bit hash code for the specified string. |
| | | 126 | | /// </summary> |
| | | 127 | | /// <param name="source">The string.</param> |
| | | 128 | | /// <param name="compare">A <see cref="StringComparison"/> indicating whether to ignore case and such when making th |
| | | 129 | | /// <returns>A 32-bit hash code for <paramref name="source"/>.</returns> |
| | | 130 | | public static int GetHashCode(this string source, StringComparison compare) |
| | | 131 | | { |
| | | 132 | | if (source == null) throw new ArgumentNullException(nameof(source)); |
| | | 133 | | return source.GetHashCode(StringComparison.Ordinal); |
| | | 134 | | } |
| | | 135 | | #pragma warning restore CA1801 |
| | | 136 | | #endif |
| | | 137 | | #if !NET8_0_OR_GREATER |
| | | 138 | | /// <summary> |
| | | 139 | | /// Checks to see if the string starts with the specified character, using standard ordinal comparison. |
| | | 140 | | /// </summary> |
| | | 141 | | /// <param name="str">The string to test.</param> |
| | | 142 | | /// <param name="value">The character to look for.</param> |
| | | 143 | | /// <returns></returns> |
| | | 144 | | public static bool StartsWith(this string str, char value) |
| | | 145 | | { |
| | | 146 | | if (str == null) throw new ArgumentNullException(nameof(str)); |
| | | 147 | | return str.StartsWith(value.ToString(), StringComparison.Ordinal); |
| | | 148 | | } |
| | | 149 | | /// <summary> |
| | | 150 | | /// Checks to see if the string ends with the specified character, using standard ordinal comparison. |
| | | 151 | | /// </summary> |
| | | 152 | | /// <param name="str">The string to test.</param> |
| | | 153 | | /// <param name="value">The character to look for.</param> |
| | | 154 | | /// <returns></returns> |
| | | 155 | | public static bool EndsWith(this string str, char value) |
| | | 156 | | { |
| | | 157 | | if (str == null) throw new ArgumentNullException(nameof(str)); |
| | | 158 | | return str.EndsWith(value.ToString(), StringComparison.Ordinal); |
| | | 159 | | } |
| | | 160 | | #endif |
| | | 161 | | /// <summary> |
| | | 162 | | /// Gets the index of the first occurrence of a character in a string using ordinal comparison. |
| | | 163 | | /// </summary> |
| | | 164 | | /// <param name="str">The string to search.</param> |
| | | 165 | | /// <param name="c">The character to search for.</param> |
| | | 166 | | /// <returns>The index of the first occurrence of the character in the string, or -1 if the character is not found.< |
| | | 167 | | public static int IndexOfOrdinal(this string str, char c) |
| | | 168 | | { |
| | 2 | 169 | | if (str == null) throw new ArgumentNullException(nameof(str)); |
| | | 170 | | #if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER |
| | 2 | 171 | | return str.IndexOf(c, StringComparison.Ordinal); |
| | | 172 | | #else |
| | | 173 | | return str.IndexOf(c); |
| | | 174 | | #endif |
| | | 175 | | } |
| | | 176 | | /// <summary> |
| | | 177 | | /// Checks if a string contains a character using ordinal comparison. |
| | | 178 | | /// </summary> |
| | | 179 | | /// <param name="str">The string to search.</param> |
| | | 180 | | /// <param name="c">The character to search for.</param> |
| | | 181 | | /// <returns>true if the character is found in the string, false otherwise.</returns> |
| | | 182 | | public static bool ContainsOrdinal(this string str, char c) |
| | | 183 | | { |
| | 2 | 184 | | if (str == null) throw new ArgumentNullException(nameof(str)); |
| | | 185 | | #if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER |
| | 2 | 186 | | return str.Contains(c, StringComparison.Ordinal); |
| | | 187 | | #else |
| | | 188 | | return str.Contains(c); |
| | | 189 | | #endif |
| | | 190 | | } |
| | | 191 | | /// <summary> |
| | | 192 | | /// Replaces all occurrences of a substring within a string using ordinal comparison. |
| | | 193 | | /// </summary> |
| | | 194 | | /// <param name="str">The string to search.</param> |
| | | 195 | | /// <param name="find">The string to find.</param> |
| | | 196 | | /// <param name="replacement">The string to put in in place of <paramref name="find"/>.</param> |
| | | 197 | | /// <returns>The resulting string with all occurrences of <paramref name="find"/> replaced by <paramref name="replac |
| | | 198 | | public static string ReplaceOrdinal(this string str, string find, string replacement) |
| | | 199 | | { |
| | 2 | 200 | | if (str == null) throw new ArgumentNullException(nameof(str)); |
| | | 201 | | #if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER |
| | 2 | 202 | | return str.Replace(find, replacement, StringComparison.Ordinal); |
| | | 203 | | #else |
| | | 204 | | return str.Replace(find, replacement); |
| | | 205 | | #endif |
| | | 206 | | } |
| | | 207 | | } |