| | | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | | 3 | | // See the LICENSE file in the project root for more information. |
| | | 4 | | |
| | | 5 | | using System; |
| | | 6 | | using System.IO; |
| | | 7 | | using System.Runtime.CompilerServices; |
| | | 8 | | using System.Runtime.ConstrainedExecution; |
| | | 9 | | using System.Security; |
| | | 10 | | using System.Security.Cryptography; |
| | | 11 | | using System.Text; |
| | | 12 | | using System.Text.RegularExpressions; |
| | | 13 | | using GDX.Collections.Generic; |
| | | 14 | | |
| | | 15 | | namespace GDX |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// <see cref="string" /> Based Extension Methods |
| | | 19 | | /// </summary> |
| | | 20 | | [VisualScriptingCompatible(2)] |
| | | 21 | | public static class StringExtensions |
| | | 22 | | { |
| | | 23 | | /// <summary> |
| | | 24 | | /// The ASCII decimal value shift required to change the case of a letter. |
| | | 25 | | /// </summary> |
| | | 26 | | public const int AsciiCaseShift = 32; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// The ASCII decimal value for a. |
| | | 30 | | /// </summary> |
| | | 31 | | public const int AsciiLowerCaseStart = 97; |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// The ASCII decimal value for lowercase z. |
| | | 35 | | /// </summary> |
| | | 36 | | public const int AsciiLowerCaseEnd = 122; |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// The ASCII decimal value for the number sign -. |
| | | 40 | | /// </summary> |
| | | 41 | | public const int AsciiNumberSign = 45; |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// The ASCII decimal value for the decimal (.). |
| | | 45 | | /// </summary> |
| | | 46 | | public const int AsciiNumberDecimal = 46; |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// The ASCII decimal value for the , separator. |
| | | 50 | | /// </summary> |
| | | 51 | | public const int AsciiNumberSeparator = 47; |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// The ASCII decimal value for 0. |
| | | 55 | | /// </summary> |
| | | 56 | | public const int AsciiNumberStart = 48; |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// The ASCII decimal value for 9. |
| | | 60 | | /// </summary> |
| | | 61 | | public const int AsciiNumberEnd = 57; |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// The ASCII decimal value for uppercase A. |
| | | 65 | | /// </summary> |
| | | 66 | | public const int AsciiUpperCaseStart = 65; |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// The ASCII decimal value for uppercase Z. |
| | | 70 | | /// </summary> |
| | | 71 | | public const int AsciiUpperCaseEnd = 90; |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// The default encryption key used when none is provided to the encryption related extensions. |
| | | 75 | | /// </summary> |
| | | 76 | | /// <remarks> |
| | | 77 | | /// You can change this at runtime during some sort of initialization pass to being something unique to your |
| | | 78 | | /// but it is not absolutely necessary. This must be a multiple of 8 bytes. |
| | | 79 | | /// </remarks> |
| | 2 | 80 | | public static byte[] EncryptionDefaultKey = Encoding.UTF8.GetBytes("Awesome!"); |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// The IV (Initialization Vector) provided to the <see cref="DESCryptoServiceProvider" />. |
| | | 84 | | /// </summary> |
| | | 85 | | /// <remarks> |
| | | 86 | | /// You can change this at runtime during some sort of initialization pass to being something unique to your |
| | | 87 | | /// but it is not absolutely necessary. This must be a multiple of 8 bytes. |
| | | 88 | | /// </remarks> |
| | 2 | 89 | | public static byte[] EncryptionInitializationVector = Encoding.UTF8.GetBytes("dotBunny"); |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Concatenate an array of strings into one unified string. |
| | | 93 | | /// </summary> |
| | | 94 | | /// <param name="pieces">An array of strings</param> |
| | | 95 | | /// <param name="delimiter">An optional string which to use between <paramref name="pieces" /> when combining.</ |
| | | 96 | | /// <param name="trailingDelimiter">Should a trailing <paramref name="delimiter" /> be appended?</param> |
| | | 97 | | /// <returns>A concatenated <see cref="string" />.</returns> |
| | | 98 | | public static string Concatenate(this string[] pieces, string delimiter = null, bool trailingDelimiter = false) |
| | 2 | 99 | | { |
| | 2 | 100 | | StringBuilder builder = new StringBuilder(); |
| | | 101 | | |
| | 2 | 102 | | int count = pieces.Length; |
| | 2 | 103 | | bool hasDelimiter = delimiter != null; |
| | 2 | 104 | | int tail = count - 1; |
| | | 105 | | |
| | 2 | 106 | | if (trailingDelimiter) |
| | 1 | 107 | | { |
| | 8 | 108 | | for (int i = 0; i < count; i++) |
| | 3 | 109 | | { |
| | 3 | 110 | | builder.Append(pieces[i]); |
| | 3 | 111 | | if (hasDelimiter) |
| | 3 | 112 | | { |
| | 3 | 113 | | builder.Append(delimiter); |
| | 3 | 114 | | } |
| | 3 | 115 | | } |
| | 1 | 116 | | } |
| | | 117 | | else |
| | 1 | 118 | | { |
| | 8 | 119 | | for (int i = 0; i < count; i++) |
| | 3 | 120 | | { |
| | 3 | 121 | | builder.Append(pieces[i]); |
| | 3 | 122 | | if (hasDelimiter && i != tail) |
| | 2 | 123 | | { |
| | 2 | 124 | | builder.Append(delimiter); |
| | 2 | 125 | | } |
| | 3 | 126 | | } |
| | 1 | 127 | | } |
| | | 128 | | |
| | 2 | 129 | | return builder.ToString(); |
| | 2 | 130 | | } |
| | | 131 | | |
| | | 132 | | /// <summary> |
| | | 133 | | /// Decrypt an encrypted <see cref="string" /> created by <see cref="Encrypt" />. |
| | | 134 | | /// </summary> |
| | | 135 | | /// <remarks>This will have quite a few allocations.</remarks> |
| | | 136 | | /// <param name="encryptedString">The encrypted <see cref="string" />.</param> |
| | | 137 | | /// <param name="encryptionKey">The key used to encrypt the <see cref="string" />.</param> |
| | | 138 | | /// <returns>The decrypted <see cref="string" />.</returns> |
| | | 139 | | public static string Decrypt(this string encryptedString, byte[] encryptionKey = null) |
| | 2 | 140 | | { |
| | 2 | 141 | | DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider |
| | | 142 | | { |
| | | 143 | | Mode = CipherMode.ECB, |
| | | 144 | | Padding = PaddingMode.PKCS7, |
| | | 145 | | Key = encryptionKey != null && encryptionKey.Length > 0 ? encryptionKey : EncryptionDefaultKey, |
| | | 146 | | IV = EncryptionInitializationVector |
| | | 147 | | }; |
| | 2 | 148 | | using MemoryStream stream = new MemoryStream(Convert.FromBase64String(encryptedString)); |
| | 2 | 149 | | using CryptoStream cs = new CryptoStream(stream, desProvider.CreateDecryptor(), CryptoStreamMode.Read); |
| | 2 | 150 | | using StreamReader sr = new StreamReader(cs, Encoding.UTF8); |
| | 2 | 151 | | return sr.ReadToEnd(); |
| | 2 | 152 | | } |
| | | 153 | | |
| | | 154 | | /// <summary> |
| | | 155 | | /// Encrypt a <see cref="string" /> utilizing a <see cref="DESCryptoServiceProvider" />. |
| | | 156 | | /// </summary> |
| | | 157 | | /// <remarks>This will have quite a few allocations.</remarks> |
| | | 158 | | /// <param name="decryptedString">The original <see cref="string" />.</param> |
| | | 159 | | /// <param name="encryptionKey"> |
| | | 160 | | /// The key to be used when encrypting the <see cref="string" />. This must be a |
| | | 161 | | /// multiple of 8 bytes. |
| | | 162 | | /// </param> |
| | | 163 | | /// <returns>The encrypted <see cref="string" />.</returns> |
| | | 164 | | public static string Encrypt(this string decryptedString, byte[] encryptionKey = null) |
| | 3 | 165 | | { |
| | 3 | 166 | | DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider |
| | | 167 | | { |
| | | 168 | | Mode = CipherMode.ECB, |
| | | 169 | | Padding = PaddingMode.PKCS7, |
| | | 170 | | Key = encryptionKey != null && encryptionKey.Length > 0 ? encryptionKey : EncryptionDefaultKey, |
| | | 171 | | IV = EncryptionInitializationVector |
| | | 172 | | }; |
| | 3 | 173 | | using MemoryStream stream = new MemoryStream(); |
| | 3 | 174 | | using CryptoStream cs = new CryptoStream(stream, desProvider.CreateEncryptor(), CryptoStreamMode.Write); |
| | 3 | 175 | | byte[] data = Encoding.Default.GetBytes(decryptedString); |
| | 3 | 176 | | cs.Write(data, 0, data.Length); |
| | 3 | 177 | | cs.FlushFinalBlock(); |
| | 3 | 178 | | return Convert.ToBase64String(stream.ToArray()); |
| | 3 | 179 | | } |
| | | 180 | | |
| | | 181 | | /// <summary> |
| | | 182 | | /// Get the <see cref="string" /> after the first identified <paramref name="splitString" /> in |
| | | 183 | | /// <paramref name="targetString" />. |
| | | 184 | | /// </summary> |
| | | 185 | | /// <param name="targetString">The target <see cref="string" /> to look in.</param> |
| | | 186 | | /// <param name="splitString">The divider which the <paramref name="targetString" /> should be split on.</param> |
| | | 187 | | /// <param name="comparison">Specifies the culture, case, and sort rules to be used.</param> |
| | | 188 | | /// <returns> |
| | | 189 | | /// The content following the <paramref name="splitString" />, or <c>null</c> if none is found. |
| | | 190 | | /// </returns> |
| | | 191 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 192 | | public static string GetAfterFirst(this string targetString, string splitString, |
| | | 193 | | StringComparison comparison = StringComparison.Ordinal) |
| | 1 | 194 | | { |
| | 1 | 195 | | int splitIndex = targetString.IndexOf(splitString, 0, comparison); |
| | 1 | 196 | | return splitIndex < 0 ? null : targetString.Substring(splitIndex + splitString.Length); |
| | 1 | 197 | | } |
| | | 198 | | |
| | | 199 | | /// <summary> |
| | | 200 | | /// Get the <see cref="string" /> after the last identified <paramref name="splitString" /> in |
| | | 201 | | /// <paramref name="targetString" />. |
| | | 202 | | /// </summary> |
| | | 203 | | /// <param name="targetString">The target <see cref="string" /> to look in.</param> |
| | | 204 | | /// <param name="splitString">The divider which the <paramref name="targetString" /> should be split on.</param> |
| | | 205 | | /// <param name="comparison">Specifies the culture, case, and sort rules to be used.</param> |
| | | 206 | | /// <returns> |
| | | 207 | | /// The content following the <paramref name="splitString" />, or <c>null</c> if none is found. |
| | | 208 | | /// </returns> |
| | | 209 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 210 | | public static string GetAfterLast(this string targetString, string splitString, |
| | | 211 | | StringComparison comparison = StringComparison.Ordinal) |
| | 1 | 212 | | { |
| | 1 | 213 | | int splitIndex = targetString.LastIndexOf(splitString, targetString.Length - 1, comparison); |
| | 1 | 214 | | return splitIndex < 0 ? null : targetString.Substring(splitIndex + splitString.Length); |
| | 1 | 215 | | } |
| | | 216 | | |
| | | 217 | | /// <summary> |
| | | 218 | | /// Get the <see cref="string" /> before the first identified <paramref name="splitString" /> in |
| | | 219 | | /// <paramref name="targetString" />. |
| | | 220 | | /// </summary> |
| | | 221 | | /// <param name="targetString">The target <see cref="string" /> to look in.</param> |
| | | 222 | | /// <param name="splitString">The divider which the <paramref name="targetString" /> should be split on.</param> |
| | | 223 | | /// <param name="comparison">Specifies the culture, case, and sort rules to be used.</param> |
| | | 224 | | /// <returns>The content before the <paramref name="splitString" />, or <c>null</c> if none is found.</returns> |
| | | 225 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 226 | | public static string GetBeforeFirst(this string targetString, string splitString, |
| | | 227 | | StringComparison comparison = StringComparison.Ordinal) |
| | 1 | 228 | | { |
| | 1 | 229 | | int splitIndex = targetString.IndexOf(splitString, 0, comparison); |
| | 1 | 230 | | return splitIndex < 0 ? null : targetString.Substring(0, splitIndex); |
| | 1 | 231 | | } |
| | | 232 | | |
| | | 233 | | /// <summary> |
| | | 234 | | /// Get the <see cref="string" /> before the last identified <paramref name="splitString" /> in |
| | | 235 | | /// <paramref name="targetString" />. |
| | | 236 | | /// </summary> |
| | | 237 | | /// <param name="targetString">The target <see cref="string" /> to look in.</param> |
| | | 238 | | /// <param name="splitString">The divider which the <paramref name="targetString" /> should be split on.</param> |
| | | 239 | | /// <param name="comparison">Specifies the culture, case, and sort rules to be used.</param> |
| | | 240 | | /// <returns>The content before the <paramref name="splitString" />, or <c>null</c> if none is found.</returns> |
| | | 241 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 242 | | public static string GetBeforeLast(this string targetString, string splitString, |
| | | 243 | | StringComparison comparison = StringComparison.Ordinal) |
| | 1 | 244 | | { |
| | 1 | 245 | | int splitIndex = targetString.LastIndexOf(splitString, targetString.Length - 1, comparison); |
| | 1 | 246 | | return splitIndex < 0 ? null : targetString.Substring(0, splitIndex); |
| | 1 | 247 | | } |
| | | 248 | | |
| | | 249 | | /// <summary> |
| | | 250 | | /// <para> |
| | | 251 | | /// Get the stable hash code value of <paramref name="targetString" />. |
| | | 252 | | /// </para> |
| | | 253 | | /// </summary> |
| | | 254 | | /// <remarks> |
| | | 255 | | /// This loosely based on the Fowler–Noll–Vo (FNV) hash function. It's value will be identical |
| | | 256 | | /// to the value produced natively by processing a <see cref="string" /> with |
| | | 257 | | /// <see cref="string.GetHashCode()" />, but with no allocations and no virtual calls. |
| | | 258 | | /// </remarks> |
| | | 259 | | /// <param name="targetString">The target <see cref="string" />.</param> |
| | | 260 | | /// <returns>A <see cref="int" /> value.</returns> |
| | | 261 | | [SecuritySafeCritical] |
| | | 262 | | [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] |
| | | 263 | | public static unsafe int GetStableHashCode(this string targetString) |
| | 793 | 264 | | { |
| | 793 | 265 | | fixed (char* src = targetString) |
| | 793 | 266 | | { |
| | 793 | 267 | | int hash1 = 5381; |
| | 793 | 268 | | int hash2 = hash1; |
| | | 269 | | int c; |
| | 793 | 270 | | char* s = src; |
| | | 271 | | |
| | | 272 | | // Get character |
| | 3966 | 273 | | while ((c = s[0]) != 0) |
| | 3484 | 274 | | { |
| | | 275 | | // Add to Hash #1 |
| | 3484 | 276 | | hash1 = ((hash1 << 5) + hash1) ^ c; |
| | | 277 | | |
| | | 278 | | // Get our second character |
| | 3484 | 279 | | c = s[1]; |
| | | 280 | | |
| | 3484 | 281 | | if (c == 0) |
| | 311 | 282 | | { |
| | 311 | 283 | | break; |
| | | 284 | | } |
| | | 285 | | |
| | 3173 | 286 | | hash2 = ((hash2 << 5) + hash2) ^ c; |
| | 3173 | 287 | | s += 2; |
| | 3173 | 288 | | } |
| | | 289 | | |
| | 793 | 290 | | return hash1 + hash2 * 1566083941; |
| | | 291 | | } |
| | 793 | 292 | | } |
| | | 293 | | |
| | | 294 | | /// <summary> |
| | | 295 | | /// <para> |
| | | 296 | | /// Get the stable hash code value of <paramref name="targetString" /> (converted to an uppercase |
| | | 297 | | /// <see cref="string" />). |
| | | 298 | | /// </para> |
| | | 299 | | /// </summary> |
| | | 300 | | /// <remarks> |
| | | 301 | | /// This loosely based on the Fowler–Noll–Vo (FNV) hash function. It's value will be identical |
| | | 302 | | /// to the value produced natively by processing a <see cref="string" /> with |
| | | 303 | | /// <see cref="string.ToLower()" />.<see cref="string.GetHashCode()" />, but with no |
| | | 304 | | /// allocations. |
| | | 305 | | /// </remarks> |
| | | 306 | | /// <param name="targetString">The target <see cref="string" />.</param> |
| | | 307 | | /// <returns>A <see cref="int" /> value.</returns> |
| | | 308 | | [SecuritySafeCritical] |
| | | 309 | | [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] |
| | | 310 | | public static unsafe int GetStableLowerCaseHashCode(this string targetString) |
| | 11 | 311 | | { |
| | 11 | 312 | | fixed (char* src = targetString) |
| | 11 | 313 | | { |
| | 11 | 314 | | int hash1 = 5381; |
| | 11 | 315 | | int hash2 = hash1; |
| | | 316 | | int c; |
| | 11 | 317 | | char* s = src; |
| | | 318 | | |
| | | 319 | | // Get character |
| | 64 | 320 | | while ((c = s[0]) != 0) |
| | 61 | 321 | | { |
| | | 322 | | // Check character value and shift it if necessary (32) |
| | 61 | 323 | | if (c >= AsciiUpperCaseStart && c <= AsciiUpperCaseEnd) |
| | 20 | 324 | | { |
| | 20 | 325 | | c ^= AsciiCaseShift; |
| | 20 | 326 | | } |
| | | 327 | | |
| | | 328 | | // Add to Hash #1 |
| | 61 | 329 | | hash1 = ((hash1 << 5) + hash1) ^ c; |
| | | 330 | | |
| | | 331 | | // Get our second character |
| | 61 | 332 | | c = s[1]; |
| | | 333 | | |
| | 61 | 334 | | if (c == 0) |
| | 8 | 335 | | { |
| | 8 | 336 | | break; |
| | | 337 | | } |
| | | 338 | | |
| | | 339 | | // Check character value and shift it if necessary (32) |
| | 53 | 340 | | if (c >= AsciiUpperCaseStart && c <= AsciiUpperCaseEnd) |
| | 13 | 341 | | { |
| | 13 | 342 | | c ^= AsciiCaseShift; |
| | 13 | 343 | | } |
| | | 344 | | |
| | 53 | 345 | | hash2 = ((hash2 << 5) + hash2) ^ c; |
| | 53 | 346 | | s += 2; |
| | 53 | 347 | | } |
| | | 348 | | |
| | 11 | 349 | | return hash1 + hash2 * 1566083941; |
| | | 350 | | } |
| | 11 | 351 | | } |
| | | 352 | | |
| | | 353 | | /// <summary> |
| | | 354 | | /// <para> |
| | | 355 | | /// Get the stable hash code value of <paramref name="targetString" /> (converted to an uppercase |
| | | 356 | | /// <see cref="string" />). |
| | | 357 | | /// </para> |
| | | 358 | | /// </summary> |
| | | 359 | | /// <remarks> |
| | | 360 | | /// This loosely based on the Fowler–Noll–Vo (FNV) hash function. It's value will be identical |
| | | 361 | | /// to the value produced natively by processing a <see cref="string" /> with |
| | | 362 | | /// <see cref="string.ToUpper()" />.<see cref="string.GetHashCode()" />, but with no |
| | | 363 | | /// allocations. |
| | | 364 | | /// </remarks> |
| | | 365 | | /// <param name="targetString">The target <see cref="string" />.</param> |
| | | 366 | | /// <returns>A <see cref="int" /> value.</returns> |
| | | 367 | | [SecuritySafeCritical] |
| | | 368 | | [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] |
| | | 369 | | public static unsafe int GetStableUpperCaseHashCode(this string targetString) |
| | 34 | 370 | | { |
| | 34 | 371 | | fixed (char* src = targetString) |
| | 34 | 372 | | { |
| | 34 | 373 | | int hash1 = 5381; |
| | 34 | 374 | | int hash2 = hash1; |
| | | 375 | | int c; |
| | 34 | 376 | | char* s = src; |
| | | 377 | | |
| | | 378 | | // Get character |
| | 191 | 379 | | while ((c = s[0]) != 0) |
| | 162 | 380 | | { |
| | | 381 | | // Check character value and shift it if necessary (32) |
| | 162 | 382 | | if (c >= AsciiLowerCaseStart && c <= AsciiLowerCaseEnd) |
| | 83 | 383 | | { |
| | 83 | 384 | | c ^= AsciiCaseShift; |
| | 83 | 385 | | } |
| | | 386 | | |
| | | 387 | | // Add to Hash #1 |
| | 162 | 388 | | hash1 = ((hash1 << 5) + hash1) ^ c; |
| | | 389 | | |
| | | 390 | | // Get our second character |
| | 162 | 391 | | c = s[1]; |
| | | 392 | | |
| | 162 | 393 | | if (c == 0) |
| | 5 | 394 | | { |
| | 5 | 395 | | break; |
| | | 396 | | } |
| | | 397 | | |
| | | 398 | | // Check character value and shift it if necessary (32) |
| | 157 | 399 | | if (c >= AsciiLowerCaseStart && c <= AsciiLowerCaseEnd) |
| | 132 | 400 | | { |
| | 132 | 401 | | c ^= AsciiCaseShift; |
| | 132 | 402 | | } |
| | | 403 | | |
| | 157 | 404 | | hash2 = ((hash2 << 5) + hash2) ^ c; |
| | 157 | 405 | | s += 2; |
| | 157 | 406 | | } |
| | | 407 | | |
| | 34 | 408 | | return hash1 + hash2 * 1566083941; |
| | | 409 | | } |
| | 34 | 410 | | } |
| | | 411 | | |
| | | 412 | | /// <summary> |
| | | 413 | | /// Determine if there are any lowercase letters in the provided <paramref name="targetString" />. |
| | | 414 | | /// </summary> |
| | | 415 | | /// <param name="targetString">The target <see cref="string" />.</param> |
| | | 416 | | /// <returns>true/false if lowercase letters were found.</returns> |
| | | 417 | | [SecuritySafeCritical] |
| | | 418 | | [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] |
| | | 419 | | public static unsafe bool HasLowerCase(this string targetString) |
| | 2 | 420 | | { |
| | 2 | 421 | | fixed (char* src = targetString) |
| | 2 | 422 | | { |
| | | 423 | | int c; |
| | 2 | 424 | | char* s = src; |
| | | 425 | | |
| | | 426 | | // Get character |
| | 24 | 427 | | while ((c = s[0]) != 0) |
| | 23 | 428 | | { |
| | 23 | 429 | | if (c >= AsciiLowerCaseStart && c <= AsciiLowerCaseEnd) |
| | 1 | 430 | | { |
| | 1 | 431 | | return true; |
| | | 432 | | } |
| | | 433 | | |
| | 22 | 434 | | s += 1; |
| | 22 | 435 | | } |
| | 1 | 436 | | } |
| | | 437 | | |
| | 1 | 438 | | return false; |
| | 2 | 439 | | } |
| | | 440 | | |
| | | 441 | | /// <summary> |
| | | 442 | | /// Determine if there are any uppercase letters in the provided <paramref name="targetString" />. |
| | | 443 | | /// </summary> |
| | | 444 | | /// <param name="targetString">The target <see cref="string" />.</param> |
| | | 445 | | /// <returns>true/false if uppercase letters were found.</returns> |
| | | 446 | | [SecuritySafeCritical] |
| | | 447 | | [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] |
| | | 448 | | public static unsafe bool HasUpperCase(this string targetString) |
| | 2 | 449 | | { |
| | 2 | 450 | | fixed (char* src = targetString) |
| | 2 | 451 | | { |
| | | 452 | | int c; |
| | 2 | 453 | | char* s = src; |
| | | 454 | | |
| | | 455 | | // Get character |
| | 23 | 456 | | while ((c = s[0]) != 0) |
| | 22 | 457 | | { |
| | 22 | 458 | | if (c >= AsciiUpperCaseStart && c <= AsciiUpperCaseEnd) |
| | 1 | 459 | | { |
| | 1 | 460 | | return true; |
| | | 461 | | } |
| | | 462 | | |
| | 21 | 463 | | s += 1; |
| | 21 | 464 | | } |
| | 1 | 465 | | } |
| | | 466 | | |
| | 1 | 467 | | return false; |
| | 2 | 468 | | } |
| | | 469 | | |
| | | 470 | | /// <summary> |
| | | 471 | | /// Determine if the <paramref name="targetString" /> represents a boolean value arrangement. |
| | | 472 | | /// </summary> |
| | | 473 | | /// <param name="targetString">The target <see cref="string" />.</param> |
| | | 474 | | /// <returns>true/false if the <paramref name="targetString" /> can be evaluated as a boolean value.</returns> |
| | | 475 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 476 | | public static bool IsBooleanValue(this string targetString) |
| | 2 | 477 | | { |
| | | 478 | | // Get an optimized hash value |
| | 2 | 479 | | int hash = targetString.GetStableLowerCaseHashCode(); |
| | | 480 | | |
| | | 481 | | // Check |
| | 2 | 482 | | switch (hash) |
| | | 483 | | { |
| | | 484 | | case -971704226: // true |
| | | 485 | | case -1685090051: // false |
| | | 486 | | case 372029325: // 1 |
| | | 487 | | case 372029326: // 0 |
| | | 488 | | case -1273338385: // yes |
| | | 489 | | case 1496915069: // no |
| | | 490 | | case -1231968287: // on |
| | | 491 | | case -870054309: // off |
| | 1 | 492 | | return true; |
| | | 493 | | } |
| | | 494 | | |
| | 1 | 495 | | return false; |
| | 2 | 496 | | } |
| | | 497 | | |
| | | 498 | | /// <summary> |
| | | 499 | | /// Determine if the <paramref name="targetString" /> represents a positive boolean value arrangement. |
| | | 500 | | /// </summary> |
| | | 501 | | /// <example> |
| | | 502 | | /// Useful method when trying to parse data for branching. |
| | | 503 | | /// <code> |
| | | 504 | | /// if(data["set"].IsBooleanPositiveValue()) |
| | | 505 | | /// { |
| | | 506 | | /// ShouldBlueBox(); |
| | | 507 | | /// } |
| | | 508 | | /// </code> |
| | | 509 | | /// </example> |
| | | 510 | | /// <param name="targetString">The target <see cref="string" />.</param> |
| | | 511 | | /// <returns>true/false if the <paramref name="targetString" /> can be evaluated as a positive boolean value.</r |
| | | 512 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 513 | | public static bool IsBooleanPositiveValue(this string targetString) |
| | 2 | 514 | | { |
| | 2 | 515 | | int hash = targetString.GetStableLowerCaseHashCode(); |
| | 2 | 516 | | switch (hash) |
| | | 517 | | { |
| | | 518 | | case -971704226: // true |
| | | 519 | | case 372029325: // 1 |
| | | 520 | | case -1273338385: // yes |
| | | 521 | | case -1231968287: // on |
| | 1 | 522 | | return true; |
| | | 523 | | default: |
| | 1 | 524 | | return false; |
| | | 525 | | } |
| | 2 | 526 | | } |
| | | 527 | | |
| | | 528 | | /// <summary> |
| | | 529 | | /// Determine if the <paramref name="targetString" /> is an <see cref="int" /> value. |
| | | 530 | | /// </summary> |
| | | 531 | | /// <remarks> |
| | | 532 | | /// This method is meant for when you do not actually need the value returned, merely an evaluation if |
| | | 533 | | /// the provided <paramref name="targetString" /> is an <see cref="int" />. This does not qualify |
| | | 534 | | /// <see cref="float" /> values positively. |
| | | 535 | | /// </remarks> |
| | | 536 | | /// <param name="targetString">The target <see cref="string" />.</param> |
| | | 537 | | /// <returns>true/false if it contains an <see cref="int" />.</returns> |
| | | 538 | | [SecuritySafeCritical] |
| | | 539 | | [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] |
| | | 540 | | public static unsafe bool IsIntegerValue(this string targetString) |
| | 5 | 541 | | { |
| | 5 | 542 | | fixed (char* src = targetString) |
| | 5 | 543 | | { |
| | 5 | 544 | | char* s = src; |
| | 5 | 545 | | int c = s[0]; |
| | | 546 | | |
| | | 547 | | // Nothing |
| | 5 | 548 | | if (c == 0) |
| | 1 | 549 | | { |
| | 1 | 550 | | return false; |
| | | 551 | | } |
| | | 552 | | |
| | | 553 | | // Check first character |
| | 4 | 554 | | if (c != AsciiNumberSign && (c < AsciiNumberStart || c > AsciiNumberEnd)) |
| | 1 | 555 | | { |
| | 1 | 556 | | return false; |
| | | 557 | | } |
| | | 558 | | |
| | | 559 | | // Get character |
| | 12 | 560 | | while ((c = s[1]) != 0) |
| | 10 | 561 | | { |
| | 10 | 562 | | if (c < AsciiNumberStart || c > AsciiNumberEnd) |
| | 1 | 563 | | { |
| | 1 | 564 | | return false; |
| | | 565 | | } |
| | | 566 | | |
| | 9 | 567 | | s += 1; |
| | 9 | 568 | | } |
| | 2 | 569 | | } |
| | | 570 | | |
| | 2 | 571 | | return true; |
| | 5 | 572 | | } |
| | | 573 | | |
| | | 574 | | /// <summary> |
| | | 575 | | /// Is the <paramref name="targetString" /> a numeric value. |
| | | 576 | | /// </summary> |
| | | 577 | | /// <remarks> |
| | | 578 | | /// <para> |
| | | 579 | | /// The following requirements must be met to be considered a valid number in this method: |
| | | 580 | | /// </para> |
| | | 581 | | /// <list type="bullet"> |
| | | 582 | | /// <item> |
| | | 583 | | /// <description> |
| | | 584 | | /// The first character may be an indicator of its sign, an explicit acceptance of <c>-</c> is m |
| | | 585 | | /// prefixed with <c>+</c>, the number will be found invalid. |
| | | 586 | | /// </description> |
| | | 587 | | /// </item> |
| | | 588 | | /// <item> |
| | | 589 | | /// <description>A single decimal point <c>.</c> may be present in the <paramref name="targetString" |
| | | 590 | | /// </item> |
| | | 591 | | /// <item> |
| | | 592 | | /// <description>No alphabet characters are present in the <paramref name="targetString" />.</descri |
| | | 593 | | /// </item> |
| | | 594 | | /// </list> |
| | | 595 | | /// </remarks> |
| | | 596 | | /// <param name="targetString">The target <see cref="string" />.</param> |
| | | 597 | | /// <returns>true/false if the <paramref name="targetString" /> qualifies as a numeric value.</returns> |
| | | 598 | | [SecuritySafeCritical] |
| | | 599 | | [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] |
| | | 600 | | public static unsafe bool IsNumeric(this string targetString) |
| | 3177 | 601 | | { |
| | 3177 | 602 | | fixed (char* src = targetString) |
| | 3177 | 603 | | { |
| | 3177 | 604 | | char* s = src; |
| | 3177 | 605 | | int c = s[0]; |
| | 3177 | 606 | | bool hasDecimal = false; |
| | | 607 | | |
| | | 608 | | // Nothing |
| | 3177 | 609 | | if (c == 0) |
| | 1 | 610 | | { |
| | 1 | 611 | | return false; |
| | | 612 | | } |
| | | 613 | | |
| | | 614 | | // Check first character |
| | 3176 | 615 | | if (c != AsciiNumberSign && (c < AsciiNumberStart || c > AsciiNumberEnd)) |
| | 2345 | 616 | | { |
| | 2345 | 617 | | return false; |
| | | 618 | | } |
| | | 619 | | |
| | | 620 | | // Get character |
| | 1672 | 621 | | while ((c = s[1]) != 0) |
| | 842 | 622 | | { |
| | 842 | 623 | | if (c < AsciiNumberStart || c > AsciiNumberEnd) |
| | 3 | 624 | | { |
| | 3 | 625 | | if (c == AsciiNumberDecimal && !hasDecimal) |
| | 2 | 626 | | { |
| | 2 | 627 | | hasDecimal = true; |
| | 2 | 628 | | s += 1; |
| | 2 | 629 | | continue; |
| | | 630 | | } |
| | | 631 | | |
| | 1 | 632 | | return false; |
| | | 633 | | } |
| | | 634 | | |
| | 839 | 635 | | s += 1; |
| | 839 | 636 | | } |
| | 830 | 637 | | } |
| | | 638 | | |
| | 830 | 639 | | return true; |
| | 3177 | 640 | | } |
| | | 641 | | |
| | | 642 | | /// <summary> |
| | | 643 | | /// Counts the number of times the needle (<paramref name="targetCharacter" />) appears in the haystack ( |
| | | 644 | | /// <paramref name="targetString" />). |
| | | 645 | | /// </summary> |
| | | 646 | | /// <remarks>Specifically created to avoid using LINQ and avoid an allocation.</remarks> |
| | | 647 | | /// <param name="targetString">The haystack.</param> |
| | | 648 | | /// <param name="targetCharacter">The needle.</param> |
| | | 649 | | /// <returns>The number of times <paramref name="targetCharacter" /> is found in <paramref name="targetString" / |
| | | 650 | | [SecuritySafeCritical] |
| | | 651 | | [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] |
| | | 652 | | public static unsafe int CountOccurence(this string targetString, char targetCharacter) |
| | 2 | 653 | | { |
| | 2 | 654 | | int counter = 0; |
| | 2 | 655 | | fixed (char* src = targetString) |
| | 2 | 656 | | { |
| | | 657 | | int c; |
| | 2 | 658 | | char* s = src; |
| | 54 | 659 | | while ((c = s[0]) != 0) |
| | 52 | 660 | | { |
| | 52 | 661 | | if (c == targetCharacter) |
| | 2 | 662 | | { |
| | 2 | 663 | | counter++; |
| | 2 | 664 | | } |
| | | 665 | | |
| | 52 | 666 | | s += 1; |
| | 52 | 667 | | } |
| | 2 | 668 | | } |
| | | 669 | | |
| | 2 | 670 | | return counter; |
| | 2 | 671 | | } |
| | | 672 | | |
| | | 673 | | /// <summary> |
| | | 674 | | /// Does the <paramref name="haystack" /> partially contain the <paramref name="needle" />? |
| | | 675 | | /// </summary> |
| | | 676 | | /// <param name="haystack">An array of <see cref="string" />s.</param> |
| | | 677 | | /// <param name="needle">The <see cref="string" /> that is being looked for.</param> |
| | | 678 | | /// <returns>true/false if found.</returns> |
| | | 679 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 680 | | public static bool PartialMatch(this string[] haystack, string needle) |
| | 3 | 681 | | { |
| | 3 | 682 | | if (haystack == null) |
| | 1 | 683 | | { |
| | 1 | 684 | | return false; |
| | | 685 | | } |
| | | 686 | | |
| | 2 | 687 | | int count = haystack.Length; |
| | 14 | 688 | | for (int i = 0; i < count; i++) |
| | 6 | 689 | | { |
| | 6 | 690 | | if (haystack[i].Contains(needle)) |
| | 1 | 691 | | { |
| | 1 | 692 | | return true; |
| | | 693 | | } |
| | 5 | 694 | | } |
| | | 695 | | |
| | 1 | 696 | | return false; |
| | 3 | 697 | | } |
| | | 698 | | |
| | | 699 | | /// <summary> |
| | | 700 | | /// Does the <paramref name="haystack" /> partially contain the <paramref name="needle" />? |
| | | 701 | | /// </summary> |
| | | 702 | | /// <param name="haystack">A <see cref="SimpleList{T}" /> of <see cref="string" />s.</param> |
| | | 703 | | /// <param name="needle">The <see cref="string" /> that is being looked for.</param> |
| | | 704 | | /// <returns>true/false if found.</returns> |
| | | 705 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 706 | | public static bool PartialMatch(this SimpleList<string> haystack, string needle) |
| | 3 | 707 | | { |
| | 3 | 708 | | if (haystack.Count == 0) |
| | 1 | 709 | | { |
| | 1 | 710 | | return false; |
| | | 711 | | } |
| | | 712 | | |
| | 2 | 713 | | int count = haystack.Count; |
| | 14 | 714 | | for (int i = 0; i < count; i++) |
| | 6 | 715 | | { |
| | 6 | 716 | | if (haystack.Array[i].Contains(needle)) |
| | 1 | 717 | | { |
| | 1 | 718 | | return true; |
| | | 719 | | } |
| | 5 | 720 | | } |
| | | 721 | | |
| | 1 | 722 | | return false; |
| | 3 | 723 | | } |
| | | 724 | | |
| | | 725 | | /// <summary> |
| | | 726 | | /// Create a new string, splitting an existing string up based on camel case formatting. |
| | | 727 | | /// </summary> |
| | | 728 | | /// <param name="targetString">The target <see cref="string" />.</param> |
| | | 729 | | /// <param name="divider">The <see cref="string" /> to put in between the split <see cref="string" />.</param> |
| | | 730 | | /// <returns>A new <see cref="string" />.</returns> |
| | | 731 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 732 | | public static string SplitCamelCase(this string targetString, string divider = " ") |
| | 2 | 733 | | { |
| | 2 | 734 | | return Regex.Replace(targetString, "([A-Z])", $"{divider}$1", RegexOptions.None).Trim(); |
| | 2 | 735 | | } |
| | | 736 | | |
| | | 737 | | /// <summary> |
| | | 738 | | /// Remove non ASCII characters from a <see cref="string" />. |
| | | 739 | | /// </summary> |
| | | 740 | | /// <param name="targetString">The <see cref="string" /> to be cleaned.</param> |
| | | 741 | | /// <returns>A <see cref="string" /> without ASCII characters.</returns> |
| | | 742 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 743 | | public static string StripNonAscii(this string targetString) |
| | 36188 | 744 | | { |
| | 36188 | 745 | | return Regex.Replace(targetString, @"[^\u0000-\u007F]+", string.Empty); |
| | 36188 | 746 | | } |
| | | 747 | | } |
| | | 748 | | } |