| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | using System.Diagnostics; |
| | 4 | | using System.Text; |
| | 5 | |
|
| | 6 | | namespace Ice.UtilInternal; |
| | 7 | |
|
| | 8 | | public sealed class StringUtil |
| | 9 | | { |
| | 10 | | // |
| | 11 | | // Return the index of the first character in str to |
| | 12 | | // appear in match, starting from 0. Returns -1 if none is |
| | 13 | | // found. |
| | 14 | | // |
| 1 | 15 | | public static int findFirstOf(string str, string match) => findFirstOf(str, match, 0); |
| | 16 | |
|
| | 17 | | // |
| | 18 | | // Return the index of the first character in str to |
| | 19 | | // appear in match, starting from start. Returns -1 if none is |
| | 20 | | // found. |
| | 21 | | // |
| | 22 | | public static int findFirstOf(string str, string match, int start) |
| | 23 | | { |
| 1 | 24 | | int len = str.Length; |
| 1 | 25 | | for (int i = start; i < len; i++) |
| | 26 | | { |
| 1 | 27 | | char ch = str[i]; |
| 1 | 28 | | if (match.Contains(ch, StringComparison.Ordinal)) |
| | 29 | | { |
| 1 | 30 | | return i; |
| | 31 | | } |
| | 32 | | } |
| | 33 | |
|
| 1 | 34 | | return -1; |
| | 35 | | } |
| | 36 | |
|
| | 37 | | // |
| | 38 | | // Return the index of the first character in str which does |
| | 39 | | // not appear in match, starting from 0. Returns -1 if none is |
| | 40 | | // found. |
| | 41 | | // |
| 0 | 42 | | public static int findFirstNotOf(string str, string match) => findFirstNotOf(str, match, 0); |
| | 43 | |
|
| | 44 | | // |
| | 45 | | // Return the index of the first character in str which does |
| | 46 | | // not appear in match, starting from start. Returns -1 if none is |
| | 47 | | // found. |
| | 48 | | // |
| | 49 | | public static int findFirstNotOf(string str, string match, int start) |
| | 50 | | { |
| 1 | 51 | | int len = str.Length; |
| 1 | 52 | | for (int i = start; i < len; i++) |
| | 53 | | { |
| 1 | 54 | | char ch = str[i]; |
| 1 | 55 | | if (!match.Contains(ch, StringComparison.Ordinal)) |
| | 56 | | { |
| 1 | 57 | | return i; |
| | 58 | | } |
| | 59 | | } |
| | 60 | |
|
| 1 | 61 | | return -1; |
| | 62 | | } |
| | 63 | |
|
| | 64 | | private static void |
| | 65 | | encodeChar(char c, StringBuilder sb, string special, Ice.ToStringMode toStringMode) |
| | 66 | | { |
| | 67 | | switch (c) |
| | 68 | | { |
| | 69 | | case '\\': |
| | 70 | | { |
| 1 | 71 | | sb.Append("\\\\"); |
| 1 | 72 | | break; |
| | 73 | | } |
| | 74 | | case '\'': |
| | 75 | | { |
| 1 | 76 | | sb.Append("\\'"); |
| 1 | 77 | | break; |
| | 78 | | } |
| | 79 | | case '"': |
| | 80 | | { |
| 0 | 81 | | sb.Append("\\\""); |
| 0 | 82 | | break; |
| | 83 | | } |
| | 84 | | case '\a': |
| | 85 | | { |
| 0 | 86 | | if (toStringMode == Ice.ToStringMode.Compat) |
| | 87 | | { |
| | 88 | | // Octal escape for compatibility with 3.6 and earlier |
| 0 | 89 | | sb.Append("\\007"); |
| | 90 | | } |
| | 91 | | else |
| | 92 | | { |
| 0 | 93 | | sb.Append("\\a"); |
| | 94 | | } |
| 0 | 95 | | break; |
| | 96 | | } |
| | 97 | | case '\b': |
| | 98 | | { |
| 0 | 99 | | sb.Append("\\b"); |
| 0 | 100 | | break; |
| | 101 | | } |
| | 102 | | case '\f': |
| | 103 | | { |
| 0 | 104 | | sb.Append("\\f"); |
| 0 | 105 | | break; |
| | 106 | | } |
| | 107 | | case '\n': |
| | 108 | | { |
| 0 | 109 | | sb.Append("\\n"); |
| 0 | 110 | | break; |
| | 111 | | } |
| | 112 | | case '\r': |
| | 113 | | { |
| 0 | 114 | | sb.Append("\\r"); |
| 0 | 115 | | break; |
| | 116 | | } |
| | 117 | | case '\t': |
| | 118 | | { |
| 0 | 119 | | sb.Append("\\t"); |
| 0 | 120 | | break; |
| | 121 | | } |
| | 122 | | case '\v': |
| | 123 | | { |
| 0 | 124 | | if (toStringMode == Ice.ToStringMode.Compat) |
| | 125 | | { |
| | 126 | | // Octal escape for compatibility with 3.6 and earlier |
| 0 | 127 | | sb.Append("\\013"); |
| | 128 | | } |
| | 129 | | else |
| | 130 | | { |
| 0 | 131 | | sb.Append("\\v"); |
| | 132 | | } |
| 0 | 133 | | break; |
| | 134 | | } |
| | 135 | | default: |
| | 136 | | { |
| 1 | 137 | | if (special != null && special.Contains(c, StringComparison.Ordinal)) |
| | 138 | | { |
| 1 | 139 | | sb.Append('\\'); |
| 1 | 140 | | sb.Append(c); |
| | 141 | | } |
| | 142 | | else |
| | 143 | | { |
| 1 | 144 | | int i = (int)c; |
| 1 | 145 | | if (i < 32 || i > 126) |
| | 146 | | { |
| 1 | 147 | | if (toStringMode == Ice.ToStringMode.Compat) |
| | 148 | | { |
| | 149 | | // |
| | 150 | | // When ToStringMode=Compat, c is a UTF-8 byte |
| | 151 | | // |
| | 152 | | Debug.Assert(i < 256); |
| | 153 | |
|
| 1 | 154 | | sb.Append('\\'); |
| 1 | 155 | | string octal = System.Convert.ToString(i, 8); |
| | 156 | | // |
| | 157 | | // Add leading zeroes so that we avoid problems during |
| | 158 | | // decoding. For example, consider the encoded string |
| | 159 | | // \0013 (i.e., a character with value 1 followed by |
| | 160 | | // the character '3'). If the leading zeroes were omitted, |
| | 161 | | // the result would be incorrectly interpreted by the |
| | 162 | | // decoder as a single character with value 11. |
| | 163 | | // |
| 1 | 164 | | for (int j = octal.Length; j < 3; j++) |
| | 165 | | { |
| 1 | 166 | | sb.Append('0'); |
| | 167 | | } |
| 1 | 168 | | sb.Append(octal); |
| | 169 | | } |
| 1 | 170 | | else if (i < 32 || i == 127 || toStringMode == Ice.ToStringMode.ASCII) |
| | 171 | | { |
| | 172 | | // append \\unnnn |
| 1 | 173 | | sb.Append("\\u"); |
| 1 | 174 | | string hex = System.Convert.ToString(i, 16); |
| 1 | 175 | | for (int j = hex.Length; j < 4; j++) |
| | 176 | | { |
| 1 | 177 | | sb.Append('0'); |
| | 178 | | } |
| 1 | 179 | | sb.Append(hex); |
| | 180 | | } |
| | 181 | | else |
| | 182 | | { |
| | 183 | | // keep as is |
| 1 | 184 | | sb.Append(c); |
| | 185 | | } |
| | 186 | | } |
| | 187 | | else |
| | 188 | | { |
| | 189 | | // printable ASCII character |
| 1 | 190 | | sb.Append(c); |
| | 191 | | } |
| | 192 | | } |
| | 193 | | break; |
| | 194 | | } |
| | 195 | | } |
| 1 | 196 | | } |
| | 197 | |
|
| | 198 | | // |
| | 199 | | // Add escape sequences (such as "\n", or "\007") to the input string |
| | 200 | | // |
| | 201 | | public static string escapeString(string s, string special, Ice.ToStringMode toStringMode) |
| | 202 | | { |
| 1 | 203 | | if (special != null) |
| | 204 | | { |
| 1 | 205 | | for (int i = 0; i < special.Length; ++i) |
| | 206 | | { |
| 1 | 207 | | if (special[i] < 32 || special[i] > 126) |
| | 208 | | { |
| 0 | 209 | | throw new ArgumentException("special characters must be in ASCII range 32-126", nameof(special)); |
| | 210 | | } |
| | 211 | | } |
| | 212 | | } |
| | 213 | |
|
| 1 | 214 | | if (toStringMode == Ice.ToStringMode.Compat) |
| | 215 | | { |
| | 216 | | // Encode UTF-8 bytes |
| | 217 | |
|
| 1 | 218 | | var utf8 = new UTF8Encoding(); |
| 1 | 219 | | byte[] bytes = utf8.GetBytes(s); |
| | 220 | |
|
| 1 | 221 | | var result = new StringBuilder(bytes.Length); |
| 1 | 222 | | for (int i = 0; i < bytes.Length; i++) |
| | 223 | | { |
| 1 | 224 | | encodeChar((char)bytes[i], result, special, toStringMode); |
| | 225 | | } |
| | 226 | |
|
| 1 | 227 | | return result.ToString(); |
| | 228 | | } |
| | 229 | | else |
| | 230 | | { |
| 1 | 231 | | var result = new StringBuilder(s.Length); |
| | 232 | |
|
| 1 | 233 | | for (int i = 0; i < s.Length; i++) |
| | 234 | | { |
| 1 | 235 | | char c = s[i]; |
| 1 | 236 | | if (toStringMode == Ice.ToStringMode.Unicode || !char.IsSurrogate(c)) |
| | 237 | | { |
| 1 | 238 | | encodeChar(c, result, special, toStringMode); |
| | 239 | | } |
| | 240 | | else |
| | 241 | | { |
| | 242 | | Debug.Assert(toStringMode == Ice.ToStringMode.ASCII && char.IsSurrogate(c)); |
| 1 | 243 | | if (i + 1 == s.Length) |
| | 244 | | { |
| 0 | 245 | | throw new System.ArgumentException("High surrogate without low surrogate"); |
| | 246 | | } |
| | 247 | | else |
| | 248 | | { |
| 1 | 249 | | i++; |
| 1 | 250 | | int codePoint = char.ConvertToUtf32(c, s[i]); |
| | 251 | | // append \Unnnnnnnn |
| 1 | 252 | | result.Append("\\U"); |
| 1 | 253 | | string hex = System.Convert.ToString(codePoint, 16); |
| 1 | 254 | | for (int j = hex.Length; j < 8; j++) |
| | 255 | | { |
| 1 | 256 | | result.Append('0'); |
| | 257 | | } |
| 1 | 258 | | result.Append(hex); |
| | 259 | | } |
| | 260 | | } |
| | 261 | | } |
| | 262 | |
|
| 1 | 263 | | return result.ToString(); |
| | 264 | | } |
| | 265 | | } |
| | 266 | |
|
| | 267 | | private static char |
| | 268 | | checkChar(string s, int pos) |
| | 269 | | { |
| 1 | 270 | | char c = s[pos]; |
| 1 | 271 | | if (c < 32 || c == 127) |
| | 272 | | { |
| | 273 | | string msg; |
| 1 | 274 | | if (pos > 0) |
| | 275 | | { |
| 1 | 276 | | msg = $"character after `{s[..pos]}'"; |
| | 277 | | } |
| | 278 | | else |
| | 279 | | { |
| 0 | 280 | | msg = "first character"; |
| | 281 | | } |
| 1 | 282 | | msg += " is not a printable ASCII character (ordinal " + (int)c + ")"; |
| 1 | 283 | | throw new System.ArgumentException(msg); |
| | 284 | | } |
| 1 | 285 | | return c; |
| | 286 | | } |
| | 287 | |
|
| | 288 | | // |
| | 289 | | // Decode the character or escape sequence starting at start and appends it to result; |
| | 290 | | // returns the index of the first character following the decoded character |
| | 291 | | // or escape sequence. |
| | 292 | | // |
| | 293 | | private static int |
| | 294 | | decodeChar(string s, int start, int end, string special, StringBuilder result, UTF8Encoding utf8Encoding) |
| | 295 | | { |
| | 296 | | Debug.Assert(start >= 0); |
| | 297 | | Debug.Assert(start < end); |
| | 298 | | Debug.Assert(end <= s.Length); |
| | 299 | |
|
| 1 | 300 | | if (s[start] != '\\') |
| | 301 | | { |
| 1 | 302 | | result.Append(checkChar(s, start++)); |
| | 303 | | } |
| 1 | 304 | | else if (start + 1 == end) |
| | 305 | | { |
| 1 | 306 | | ++start; |
| 1 | 307 | | result.Append('\\'); // trailing backslash |
| | 308 | | } |
| | 309 | | else |
| | 310 | | { |
| 1 | 311 | | char c = s[++start]; |
| | 312 | |
|
| | 313 | | switch (c) |
| | 314 | | { |
| | 315 | | case '\\': |
| | 316 | | case '\'': |
| | 317 | | case '"': |
| | 318 | | case '?': |
| | 319 | | { |
| 1 | 320 | | ++start; |
| 1 | 321 | | result.Append(c); |
| 1 | 322 | | break; |
| | 323 | | } |
| | 324 | | case 'a': |
| | 325 | | { |
| 0 | 326 | | ++start; |
| 0 | 327 | | result.Append('\a'); |
| 0 | 328 | | break; |
| | 329 | | } |
| | 330 | | case 'b': |
| | 331 | | { |
| 1 | 332 | | ++start; |
| 1 | 333 | | result.Append('\b'); |
| 1 | 334 | | break; |
| | 335 | | } |
| | 336 | | case 'f': |
| | 337 | | { |
| 1 | 338 | | ++start; |
| 1 | 339 | | result.Append('\f'); |
| 1 | 340 | | break; |
| | 341 | | } |
| | 342 | | case 'n': |
| | 343 | | { |
| 1 | 344 | | ++start; |
| 1 | 345 | | result.Append('\n'); |
| 1 | 346 | | break; |
| | 347 | | } |
| | 348 | | case 'r': |
| | 349 | | { |
| 1 | 350 | | ++start; |
| 1 | 351 | | result.Append('\r'); |
| 1 | 352 | | break; |
| | 353 | | } |
| | 354 | | case 't': |
| | 355 | | { |
| 1 | 356 | | ++start; |
| 1 | 357 | | result.Append('\t'); |
| 1 | 358 | | break; |
| | 359 | | } |
| | 360 | | case 'v': |
| | 361 | | { |
| 0 | 362 | | ++start; |
| 0 | 363 | | result.Append('\v'); |
| 0 | 364 | | break; |
| | 365 | | } |
| | 366 | | case 'u': |
| | 367 | | case 'U': |
| | 368 | | { |
| 1 | 369 | | int codePoint = 0; |
| 1 | 370 | | bool inBMP = c == 'u'; |
| 1 | 371 | | int size = inBMP ? 4 : 8; |
| 1 | 372 | | ++start; |
| 1 | 373 | | while (size > 0 && start < end) |
| | 374 | | { |
| 1 | 375 | | c = s[start++]; |
| | 376 | | int charVal; |
| 1 | 377 | | if (c >= '0' && c <= '9') |
| | 378 | | { |
| 1 | 379 | | charVal = c - '0'; |
| | 380 | | } |
| 1 | 381 | | else if (c >= 'a' && c <= 'f') |
| | 382 | | { |
| 1 | 383 | | charVal = 10 + (c - 'a'); |
| | 384 | | } |
| 0 | 385 | | else if (c >= 'A' && c <= 'F') |
| | 386 | | { |
| 0 | 387 | | charVal = 10 + (c - 'A'); |
| | 388 | | } |
| | 389 | | else |
| | 390 | | { |
| | 391 | | break; // while |
| | 392 | | } |
| 1 | 393 | | codePoint = (codePoint * 16) + charVal; |
| 1 | 394 | | --size; |
| | 395 | | } |
| 1 | 396 | | if (size > 0) |
| | 397 | | { |
| 0 | 398 | | throw new System.ArgumentException("Invalid universal character name: too few hex digits"); |
| | 399 | | } |
| 1 | 400 | | if (codePoint >= 0xD800 && codePoint <= 0xDFFF) |
| | 401 | | { |
| 1 | 402 | | throw new System.ArgumentException("A universal character name cannot designate a surrogate"); |
| | 403 | | } |
| 1 | 404 | | if (inBMP || codePoint <= 0xFFFF) |
| | 405 | | { |
| 1 | 406 | | result.Append((char)codePoint); |
| | 407 | | } |
| | 408 | | else |
| | 409 | | { |
| 1 | 410 | | result.Append(char.ConvertFromUtf32(codePoint)); |
| | 411 | | } |
| 1 | 412 | | break; |
| | 413 | | } |
| | 414 | |
|
| | 415 | | case '0': |
| | 416 | | case '1': |
| | 417 | | case '2': |
| | 418 | | case '3': |
| | 419 | | case '4': |
| | 420 | | case '5': |
| | 421 | | case '6': |
| | 422 | | case '7': |
| | 423 | | case 'x': |
| | 424 | | { |
| | 425 | | // UTF-8 byte sequence encoded with octal escapes |
| | 426 | |
|
| 1 | 427 | | byte[] arr = new byte[end - start]; |
| 1 | 428 | | int i = 0; |
| 1 | 429 | | bool more = true; |
| 1 | 430 | | while (more) |
| | 431 | | { |
| 1 | 432 | | int val = 0; |
| 1 | 433 | | if (c == 'x') |
| | 434 | | { |
| 1 | 435 | | int size = 2; |
| 1 | 436 | | ++start; |
| 1 | 437 | | while (size > 0 && start < end) |
| | 438 | | { |
| 1 | 439 | | c = s[start++]; |
| | 440 | | int charVal; |
| 1 | 441 | | if (c >= '0' && c <= '9') |
| | 442 | | { |
| 1 | 443 | | charVal = c - '0'; |
| | 444 | | } |
| 1 | 445 | | else if (c >= 'a' && c <= 'f') |
| | 446 | | { |
| 0 | 447 | | charVal = 10 + (c - 'a'); |
| | 448 | | } |
| 1 | 449 | | else if (c >= 'A' && c <= 'F') |
| | 450 | | { |
| 0 | 451 | | charVal = 10 + (c - 'A'); |
| | 452 | | } |
| | 453 | | else |
| | 454 | | { |
| 1 | 455 | | --start; // move back |
| 1 | 456 | | break; // while |
| | 457 | | } |
| 1 | 458 | | val = (val * 16) + charVal; |
| 1 | 459 | | --size; |
| | 460 | | } |
| 1 | 461 | | if (size == 2) |
| | 462 | | { |
| 0 | 463 | | throw new System.ArgumentException("Invalid \\x escape sequence: no hex digit"); |
| | 464 | | } |
| | 465 | | } |
| | 466 | | else |
| | 467 | | { |
| 1 | 468 | | for (int j = 0; j < 3 && start < end; ++j) |
| | 469 | | { |
| 1 | 470 | | int charVal = s[start++] - '0'; |
| 1 | 471 | | if (charVal < 0 || charVal > 7) |
| | 472 | | { |
| 1 | 473 | | --start; // move back |
| | 474 | | Debug.Assert(j != 0); // must be at least one digit |
| 1 | 475 | | break; // for |
| | 476 | | } |
| 1 | 477 | | val = (val * 8) + charVal; |
| | 478 | | } |
| 1 | 479 | | if (val > 255) |
| | 480 | | { |
| 1 | 481 | | string msg = |
| 1 | 482 | | "octal value \\" + |
| 1 | 483 | | System.Convert.ToString(val, 8) + |
| 1 | 484 | | " (" + val + ") is out of range"; |
| 1 | 485 | | throw new System.ArgumentException(msg); |
| | 486 | | } |
| | 487 | | } |
| | 488 | |
|
| 1 | 489 | | arr[i++] = (byte)val; |
| | 490 | |
|
| 1 | 491 | | more = false; |
| | 492 | |
|
| 1 | 493 | | if ((start + 1 < end) && s[start] == '\\') |
| | 494 | | { |
| 1 | 495 | | c = s[start + 1]; |
| 1 | 496 | | if (c == 'x' || (c >= '0' && c <= '9')) |
| | 497 | | { |
| 1 | 498 | | start++; |
| 1 | 499 | | more = true; |
| | 500 | | } |
| | 501 | | } |
| | 502 | | } |
| | 503 | |
|
| 1 | 504 | | result.Append(utf8Encoding.GetString(arr, 0, i)); // May raise ArgumentException. |
| 1 | 505 | | break; |
| | 506 | | } |
| | 507 | | default: |
| | 508 | | { |
| 1 | 509 | | if (string.IsNullOrEmpty(special) || !special.Contains(c, StringComparison.Ordinal)) |
| | 510 | | { |
| 0 | 511 | | result.Append('\\'); // not in special, so we keep the backslash |
| | 512 | | } |
| 1 | 513 | | result.Append(checkChar(s, start++)); |
| | 514 | | break; |
| | 515 | | } |
| | 516 | | } |
| | 517 | | } |
| 1 | 518 | | return start; |
| | 519 | | } |
| | 520 | |
|
| | 521 | | // |
| | 522 | | // Remove escape sequences added by escapeString. Throws System.ArgumentException |
| | 523 | | // for an invalid input string. |
| | 524 | | // |
| | 525 | | public static string unescapeString(string s, int start, int end, string special) |
| | 526 | | { |
| | 527 | | Debug.Assert(start >= 0 && start <= end && end <= s.Length); |
| | 528 | |
|
| 1 | 529 | | if (special != null) |
| | 530 | | { |
| 1 | 531 | | for (int i = 0; i < special.Length; ++i) |
| | 532 | | { |
| 1 | 533 | | if (special[i] < 32 || special[i] > 126) |
| | 534 | | { |
| 0 | 535 | | throw new ArgumentException("special characters must be in ASCII range 32-126", nameof(special)); |
| | 536 | | } |
| | 537 | | } |
| | 538 | | } |
| | 539 | |
|
| | 540 | | // Optimization for strings without escapes |
| 1 | 541 | | if (start == end || s.IndexOf('\\', start, end - start) == -1) |
| | 542 | | { |
| 1 | 543 | | int p = start; |
| 1 | 544 | | while (p < end) |
| | 545 | | { |
| 1 | 546 | | checkChar(s, p++); |
| | 547 | | } |
| 1 | 548 | | return s[start..end]; |
| | 549 | | } |
| | 550 | | else |
| | 551 | | { |
| 1 | 552 | | var sb = new StringBuilder(end - start); |
| 1 | 553 | | var utf8Encoding = new UTF8Encoding(false, true); |
| 1 | 554 | | while (start < end) |
| | 555 | | { |
| 1 | 556 | | start = decodeChar(s, start, end, special, sb, utf8Encoding); |
| | 557 | | } |
| 1 | 558 | | return sb.ToString(); |
| | 559 | | } |
| | 560 | | } |
| | 561 | |
|
| | 562 | | // |
| | 563 | | // Split string helper; returns null for unmatched quotes |
| | 564 | | // |
| | 565 | | public static string[] splitString(string str, string delim) |
| | 566 | | { |
| 1 | 567 | | var l = new List<string>(); |
| 1 | 568 | | char[] arr = new char[str.Length]; |
| 1 | 569 | | int pos = 0; |
| | 570 | |
|
| 1 | 571 | | int n = 0; |
| 1 | 572 | | char quoteChar = '\0'; |
| 1 | 573 | | while (pos < str.Length) |
| | 574 | | { |
| 1 | 575 | | if (quoteChar == '\0' && (str[pos] == '"' || str[pos] == '\'')) |
| | 576 | | { |
| 1 | 577 | | quoteChar = str[pos++]; |
| 1 | 578 | | continue; // Skip the quote. |
| | 579 | | } |
| 1 | 580 | | else if (quoteChar == '\0' && str[pos] == '\\' && pos + 1 < str.Length && |
| 1 | 581 | | (str[pos + 1] == '\'' || str[pos + 1] == '"')) |
| | 582 | | { |
| 1 | 583 | | ++pos; // Skip the backslash |
| | 584 | | } |
| 1 | 585 | | else if (quoteChar != '\0' && str[pos] == '\\' && pos + 1 < str.Length && str[pos + 1] == quoteChar) |
| | 586 | | { |
| 1 | 587 | | ++pos; // Skip the backslash |
| | 588 | | } |
| 1 | 589 | | else if (quoteChar != '\0' && str[pos] == quoteChar) |
| | 590 | | { |
| 1 | 591 | | ++pos; |
| 1 | 592 | | quoteChar = '\0'; |
| 1 | 593 | | continue; // Skip the quote. |
| | 594 | | } |
| 1 | 595 | | else if (delim.Contains(str[pos], StringComparison.Ordinal)) |
| | 596 | | { |
| 1 | 597 | | if (quoteChar == '\0') |
| | 598 | | { |
| 1 | 599 | | ++pos; |
| 1 | 600 | | if (n > 0) |
| | 601 | | { |
| 1 | 602 | | l.Add(new string(arr, 0, n)); |
| 1 | 603 | | n = 0; |
| | 604 | | } |
| 1 | 605 | | continue; |
| | 606 | | } |
| | 607 | | } |
| | 608 | |
|
| 1 | 609 | | if (pos < str.Length) |
| | 610 | | { |
| 1 | 611 | | arr[n++] = str[pos++]; |
| | 612 | | } |
| | 613 | | } |
| | 614 | |
|
| 1 | 615 | | if (n > 0) |
| | 616 | | { |
| 1 | 617 | | l.Add(new string(arr, 0, n)); |
| | 618 | | } |
| 1 | 619 | | if (quoteChar != '\0') |
| | 620 | | { |
| 1 | 621 | | return null; // Unmatched quote. |
| | 622 | | } |
| 1 | 623 | | return l.ToArray(); |
| | 624 | | } |
| | 625 | |
|
| 0 | 626 | | public static int checkQuote(string s) => checkQuote(s, 0); |
| | 627 | |
|
| | 628 | | // |
| | 629 | | // If a single or double quotation mark is found at the start position, |
| | 630 | | // then the position of the matching closing quote is returned. If no |
| | 631 | | // quotation mark is found at the start position, then 0 is returned. |
| | 632 | | // If no matching closing quote is found, then -1 is returned. |
| | 633 | | // |
| | 634 | | public static int checkQuote(string s, int start) |
| | 635 | | { |
| 1 | 636 | | char quoteChar = s[start]; |
| 1 | 637 | | if (quoteChar == '"' || quoteChar == '\'') |
| | 638 | | { |
| 1 | 639 | | start++; |
| 1 | 640 | | int len = s.Length; |
| | 641 | | int pos; |
| 1 | 642 | | while (start < len && (pos = s.IndexOf(quoteChar, start)) != -1) |
| | 643 | | { |
| 1 | 644 | | if (s[pos - 1] != '\\') |
| | 645 | | { |
| 1 | 646 | | return pos; |
| | 647 | | } |
| 0 | 648 | | start = pos + 1; |
| | 649 | | } |
| 1 | 650 | | return -1; // Unmatched quote |
| | 651 | | } |
| 1 | 652 | | return 0; // Not quoted |
| | 653 | | } |
| | 654 | |
|
| | 655 | | public static bool match(string s, string pat, bool emptyMatch) |
| | 656 | | { |
| | 657 | | Debug.Assert(s.Length > 0); |
| | 658 | | Debug.Assert(pat.Length > 0); |
| | 659 | |
|
| | 660 | | // |
| | 661 | | // If pattern does not contain a wildcard just compare strings. |
| | 662 | | // |
| 1 | 663 | | int beginIndex = pat.IndexOf('*', StringComparison.Ordinal); |
| 1 | 664 | | if (beginIndex < 0) |
| | 665 | | { |
| 1 | 666 | | return s.Equals(pat, StringComparison.Ordinal); |
| | 667 | | } |
| | 668 | |
|
| | 669 | | // |
| | 670 | | // Make sure start of the strings match |
| | 671 | | // |
| 1 | 672 | | if (beginIndex > s.Length || |
| 1 | 673 | | !s[..beginIndex].Equals(pat[..beginIndex], StringComparison.Ordinal)) |
| | 674 | | { |
| 1 | 675 | | return false; |
| | 676 | | } |
| | 677 | |
|
| | 678 | | // |
| | 679 | | // Make sure there is something present in the middle to match the |
| | 680 | | // wildcard. If emptyMatch is true, allow a match of "". |
| | 681 | | // |
| 1 | 682 | | int endLength = pat.Length - beginIndex - 1; |
| 1 | 683 | | if (endLength > s.Length) |
| | 684 | | { |
| 0 | 685 | | return false; |
| | 686 | | } |
| 1 | 687 | | int endIndex = s.Length - endLength; |
| 1 | 688 | | if (endIndex < beginIndex || (!emptyMatch && endIndex == beginIndex)) |
| | 689 | | { |
| 0 | 690 | | return false; |
| | 691 | | } |
| | 692 | |
|
| | 693 | | // |
| | 694 | | // Make sure end of the strings match |
| | 695 | | // |
| 1 | 696 | | if (!s[endIndex..].Equals( |
| 1 | 697 | | pat.Substring(beginIndex + 1, pat.Length - beginIndex - 1), |
| 1 | 698 | | StringComparison.Ordinal)) |
| | 699 | | { |
| 0 | 700 | | return false; |
| | 701 | | } |
| | 702 | |
|
| 1 | 703 | | return true; |
| | 704 | | } |
| | 705 | |
|
| | 706 | | private class OrdinalStringComparerImpl : IComparer<string> |
| | 707 | | { |
| 0 | 708 | | public int Compare(string l, string r) => string.CompareOrdinal(l, r); |
| | 709 | | } |
| | 710 | |
|
| 0 | 711 | | public static IComparer<string> OrdinalStringComparer = new OrdinalStringComparerImpl(); |
| | 712 | | } |