| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | // Copied from https://github.com/aspnet/Extensions/tree/master/src/Primitives/src |
| | 5 | |
|
| | 6 | | using System; |
| | 7 | | using System.Collections; |
| | 8 | | using System.Collections.Generic; |
| | 9 | | using System.Diagnostics; |
| | 10 | | using System.Runtime.CompilerServices; |
| | 11 | |
|
| | 12 | | #pragma warning disable IDE0032 // Use auto property |
| | 13 | | #pragma warning disable IDE0066 // Use switch expression |
| | 14 | |
|
| | 15 | | namespace Azure.Core.Http.Multipart |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Represents zero/null, one, or many strings in an efficient way. |
| | 19 | | /// </summary> |
| | 20 | | internal readonly struct StringValues : IList<string>, IReadOnlyList<string>, IEquatable<StringValues>, IEquatable<s |
| | 21 | | { |
| 0 | 22 | | public static readonly StringValues Empty = new StringValues(Array.Empty<string>()); |
| | 23 | |
|
| | 24 | | private readonly object _values; |
| | 25 | |
|
| | 26 | | public StringValues(string value) |
| | 27 | | { |
| 2540 | 28 | | _values = value; |
| 2540 | 29 | | } |
| | 30 | |
|
| | 31 | | public StringValues(string[] values) |
| | 32 | | { |
| 0 | 33 | | _values = values; |
| 0 | 34 | | } |
| | 35 | |
|
| | 36 | | public static implicit operator StringValues(string value) |
| | 37 | | { |
| 0 | 38 | | return new StringValues(value); |
| | 39 | | } |
| | 40 | |
|
| | 41 | | public static implicit operator StringValues(string[] values) |
| | 42 | | { |
| 0 | 43 | | return new StringValues(values); |
| | 44 | | } |
| | 45 | |
|
| | 46 | | public static implicit operator string(StringValues values) |
| | 47 | | { |
| 0 | 48 | | return values.GetStringValue(); |
| | 49 | | } |
| | 50 | |
|
| | 51 | | public static implicit operator string[](StringValues value) |
| | 52 | | { |
| 0 | 53 | | return value.GetArrayValue(); |
| | 54 | | } |
| | 55 | |
|
| | 56 | | public int Count |
| | 57 | | { |
| | 58 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 59 | | get |
| | 60 | | { |
| | 61 | | // Take local copy of _values so type checks remain valid even if the StringValues is overwritten in mem |
| 1268 | 62 | | var value = _values; |
| 1268 | 63 | | if (value is string) |
| | 64 | | { |
| 1268 | 65 | | return 1; |
| | 66 | | } |
| 0 | 67 | | if (value is null) |
| | 68 | | { |
| 0 | 69 | | return 0; |
| | 70 | | } |
| | 71 | | else |
| | 72 | | { |
| | 73 | | // Not string, not null, can only be string[] |
| 0 | 74 | | return Unsafe.As<string[]>(value).Length; |
| | 75 | | } |
| | 76 | | } |
| | 77 | | } |
| | 78 | |
|
| | 79 | | bool ICollection<string>.IsReadOnly |
| | 80 | | { |
| 0 | 81 | | get { return true; } |
| | 82 | | } |
| | 83 | |
|
| | 84 | | string IList<string>.this[int index] |
| | 85 | | { |
| 0 | 86 | | get { return this[index]; } |
| 0 | 87 | | set { throw new NotSupportedException(); } |
| | 88 | | } |
| | 89 | |
|
| | 90 | | public string this[int index] |
| | 91 | | { |
| | 92 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 93 | | get |
| | 94 | | { |
| | 95 | | // Take local copy of _values so type checks remain valid even if the StringValues is overwritten in mem |
| 1268 | 96 | | var value = _values; |
| 1268 | 97 | | if (index == 0 && value is string str) |
| | 98 | | { |
| 1268 | 99 | | return str; |
| | 100 | | } |
| 0 | 101 | | else if (value != null) |
| | 102 | | { |
| | 103 | | // Not string, not null, can only be string[] |
| 0 | 104 | | return Unsafe.As<string[]>(value)[index]; // may throw |
| | 105 | | } |
| | 106 | | else |
| | 107 | | { |
| 0 | 108 | | return OutOfBounds(); // throws |
| | 109 | | } |
| | 110 | | } |
| | 111 | | } |
| | 112 | |
|
| | 113 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | 114 | | private static string OutOfBounds() |
| | 115 | | { |
| 0 | 116 | | return Array.Empty<string>()[0]; // throws |
| | 117 | | } |
| | 118 | |
|
| | 119 | | public override string ToString() |
| | 120 | | { |
| 0 | 121 | | return GetStringValue() ?? string.Empty; |
| | 122 | | } |
| | 123 | |
|
| | 124 | | private string GetStringValue() |
| | 125 | | { |
| | 126 | | // Take local copy of _values so type checks remain valid even if the StringValues is overwritten in memory |
| 0 | 127 | | var value = _values; |
| 0 | 128 | | if (value is string s) |
| | 129 | | { |
| 0 | 130 | | return s; |
| | 131 | | } |
| | 132 | | else |
| | 133 | | { |
| 0 | 134 | | return GetStringValueFromArray(value); |
| | 135 | | } |
| | 136 | |
|
| | 137 | | static string GetStringValueFromArray(object value) |
| | 138 | | { |
| 0 | 139 | | if (value is null) |
| | 140 | | { |
| 0 | 141 | | return null; |
| | 142 | | } |
| | 143 | |
|
| | 144 | | Debug.Assert(value is string[]); |
| | 145 | | // value is not null or string, array, can only be string[] |
| 0 | 146 | | var values = Unsafe.As<string[]>(value); |
| 0 | 147 | | switch (values.Length) |
| | 148 | | { |
| | 149 | | case 0: |
| 0 | 150 | | return null; |
| | 151 | | case 1: |
| 0 | 152 | | return values[0]; |
| | 153 | | default: |
| 0 | 154 | | return GetJoinedStringValueFromArray(values); |
| | 155 | | } |
| | 156 | | } |
| | 157 | |
|
| | 158 | | static string GetJoinedStringValueFromArray(string[] values) |
| | 159 | | { |
| | 160 | | // Calculate final length |
| 0 | 161 | | var length = 0; |
| 0 | 162 | | for (var i = 0; i < values.Length; i++) |
| | 163 | | { |
| 0 | 164 | | var value = values[i]; |
| | 165 | | // Skip null and empty values |
| 0 | 166 | | if (value != null && value.Length > 0) |
| | 167 | | { |
| 0 | 168 | | if (length > 0) |
| | 169 | | { |
| | 170 | | // Add seperator |
| 0 | 171 | | length++; |
| | 172 | | } |
| | 173 | |
|
| 0 | 174 | | length += value.Length; |
| | 175 | | } |
| | 176 | | } |
| | 177 | | #if NETCOREAPP |
| | 178 | | // Create the new string |
| | 179 | | return string.Create(length, values, (span, strings) => { |
| | 180 | | var offset = 0; |
| | 181 | | // Skip null and empty values |
| | 182 | | for (var i = 0; i < strings.Length; i++) |
| | 183 | | { |
| | 184 | | var value = strings[i]; |
| | 185 | | if (value != null && value.Length > 0) |
| | 186 | | { |
| | 187 | | if (offset > 0) |
| | 188 | | { |
| | 189 | | // Add seperator |
| | 190 | | span[offset] = ','; |
| | 191 | | offset++; |
| | 192 | | } |
| | 193 | |
|
| | 194 | | value.AsSpan().CopyTo(span.Slice(offset)); |
| | 195 | | offset += value.Length; |
| | 196 | | } |
| | 197 | | } |
| | 198 | | }); |
| | 199 | | #else |
| | 200 | | #pragma warning disable CS0618 |
| 0 | 201 | | var sb = new InplaceStringBuilder(length); |
| | 202 | | #pragma warning restore CS0618 |
| 0 | 203 | | var hasAdded = false; |
| | 204 | | // Skip null and empty values |
| 0 | 205 | | for (var i = 0; i < values.Length; i++) |
| | 206 | | { |
| 0 | 207 | | var value = values[i]; |
| 0 | 208 | | if (value != null && value.Length > 0) |
| | 209 | | { |
| 0 | 210 | | if (hasAdded) |
| | 211 | | { |
| | 212 | | // Add seperator |
| 0 | 213 | | sb.Append(','); |
| | 214 | | } |
| | 215 | |
|
| 0 | 216 | | sb.Append(value); |
| 0 | 217 | | hasAdded = true; |
| | 218 | | } |
| | 219 | | } |
| | 220 | |
|
| 0 | 221 | | return sb.ToString(); |
| | 222 | | #endif |
| | 223 | | } |
| | 224 | | } |
| | 225 | |
|
| | 226 | | public string[] ToArray() |
| | 227 | | { |
| 0 | 228 | | return GetArrayValue() ?? Array.Empty<string>(); |
| | 229 | | } |
| | 230 | |
|
| | 231 | | private string[] GetArrayValue() |
| | 232 | | { |
| | 233 | | // Take local copy of _values so type checks remain valid even if the StringValues is overwritten in memory |
| 0 | 234 | | var value = _values; |
| 0 | 235 | | if (value is string[] values) |
| | 236 | | { |
| 0 | 237 | | return values; |
| | 238 | | } |
| 0 | 239 | | else if (value != null) |
| | 240 | | { |
| | 241 | | // value not array, can only be string |
| 0 | 242 | | return new[] { Unsafe.As<string>(value) }; |
| | 243 | | } |
| | 244 | | else |
| | 245 | | { |
| 0 | 246 | | return null; |
| | 247 | | } |
| | 248 | | } |
| | 249 | |
|
| | 250 | | int IList<string>.IndexOf(string item) |
| | 251 | | { |
| 0 | 252 | | return IndexOf(item); |
| | 253 | | } |
| | 254 | |
|
| | 255 | | private int IndexOf(string item) |
| | 256 | | { |
| | 257 | | // Take local copy of _values so type checks remain valid even if the StringValues is overwritten in memory |
| 0 | 258 | | var value = _values; |
| 0 | 259 | | if (value is string[] values) |
| | 260 | | { |
| 0 | 261 | | for (int i = 0; i < values.Length; i++) |
| | 262 | | { |
| 0 | 263 | | if (string.Equals(values[i], item, StringComparison.Ordinal)) |
| | 264 | | { |
| 0 | 265 | | return i; |
| | 266 | | } |
| | 267 | | } |
| 0 | 268 | | return -1; |
| | 269 | | } |
| | 270 | |
|
| 0 | 271 | | if (value != null) |
| | 272 | | { |
| | 273 | | // value not array, can only be string |
| 0 | 274 | | return string.Equals(Unsafe.As<string>(value), item, StringComparison.Ordinal) ? 0 : -1; |
| | 275 | | } |
| | 276 | |
|
| 0 | 277 | | return -1; |
| | 278 | | } |
| | 279 | |
|
| | 280 | | bool ICollection<string>.Contains(string item) |
| | 281 | | { |
| 0 | 282 | | return IndexOf(item) >= 0; |
| | 283 | | } |
| | 284 | |
|
| | 285 | | void ICollection<string>.CopyTo(string[] array, int arrayIndex) |
| | 286 | | { |
| 0 | 287 | | CopyTo(array, arrayIndex); |
| 0 | 288 | | } |
| | 289 | |
|
| | 290 | | private void CopyTo(string[] array, int arrayIndex) |
| | 291 | | { |
| | 292 | | // Take local copy of _values so type checks remain valid even if the StringValues is overwritten in memory |
| 0 | 293 | | var value = _values; |
| 0 | 294 | | if (value is string[] values) |
| | 295 | | { |
| 0 | 296 | | Array.Copy(values, 0, array, arrayIndex, values.Length); |
| 0 | 297 | | return; |
| | 298 | | } |
| | 299 | |
|
| 0 | 300 | | if (value != null) |
| | 301 | | { |
| 0 | 302 | | if (array == null) |
| | 303 | | { |
| 0 | 304 | | throw new ArgumentNullException(nameof(array)); |
| | 305 | | } |
| 0 | 306 | | if (arrayIndex < 0) |
| | 307 | | { |
| 0 | 308 | | throw new ArgumentOutOfRangeException(nameof(arrayIndex)); |
| | 309 | | } |
| 0 | 310 | | if (array.Length - arrayIndex < 1) |
| | 311 | | { |
| 0 | 312 | | throw new ArgumentException( |
| 0 | 313 | | $"'{nameof(array)}' is not long enough to copy all the items in the collection. Check '{nameof(a |
| | 314 | | } |
| | 315 | |
|
| | 316 | | // value not array, can only be string |
| 0 | 317 | | array[arrayIndex] = Unsafe.As<string>(value); |
| | 318 | | } |
| 0 | 319 | | } |
| | 320 | |
|
| | 321 | | void ICollection<string>.Add(string item) |
| | 322 | | { |
| 0 | 323 | | throw new NotSupportedException(); |
| | 324 | | } |
| | 325 | |
|
| | 326 | | void IList<string>.Insert(int index, string item) |
| | 327 | | { |
| 0 | 328 | | throw new NotSupportedException(); |
| | 329 | | } |
| | 330 | |
|
| | 331 | | bool ICollection<string>.Remove(string item) |
| | 332 | | { |
| 0 | 333 | | throw new NotSupportedException(); |
| | 334 | | } |
| | 335 | |
|
| | 336 | | void IList<string>.RemoveAt(int index) |
| | 337 | | { |
| 0 | 338 | | throw new NotSupportedException(); |
| | 339 | | } |
| | 340 | |
|
| | 341 | | void ICollection<string>.Clear() |
| | 342 | | { |
| 0 | 343 | | throw new NotSupportedException(); |
| | 344 | | } |
| | 345 | |
|
| | 346 | | public Enumerator GetEnumerator() |
| | 347 | | { |
| 0 | 348 | | return new Enumerator(_values); |
| | 349 | | } |
| | 350 | |
|
| | 351 | | IEnumerator<string> IEnumerable<string>.GetEnumerator() |
| | 352 | | { |
| 0 | 353 | | return GetEnumerator(); |
| | 354 | | } |
| | 355 | |
|
| | 356 | | IEnumerator IEnumerable.GetEnumerator() |
| | 357 | | { |
| 0 | 358 | | return GetEnumerator(); |
| | 359 | | } |
| | 360 | |
|
| | 361 | | public static bool IsNullOrEmpty(StringValues value) |
| | 362 | | { |
| 0 | 363 | | var data = value._values; |
| 0 | 364 | | if (data is null) |
| | 365 | | { |
| 0 | 366 | | return true; |
| | 367 | | } |
| 0 | 368 | | if (data is string[] values) |
| | 369 | | { |
| 0 | 370 | | switch (values.Length) |
| | 371 | | { |
| | 372 | | case 0: |
| 0 | 373 | | return true; |
| | 374 | | case 1: |
| 0 | 375 | | return string.IsNullOrEmpty(values[0]); |
| | 376 | | default: |
| 0 | 377 | | return false; |
| | 378 | | } |
| | 379 | | } |
| | 380 | | else |
| | 381 | | { |
| | 382 | | // Not array, can only be string |
| 0 | 383 | | return string.IsNullOrEmpty(Unsafe.As<string>(data)); |
| | 384 | | } |
| | 385 | | } |
| | 386 | |
|
| | 387 | | public static StringValues Concat(StringValues values1, StringValues values2) |
| | 388 | | { |
| 0 | 389 | | var count1 = values1.Count; |
| 0 | 390 | | var count2 = values2.Count; |
| | 391 | |
|
| 0 | 392 | | if (count1 == 0) |
| | 393 | | { |
| 0 | 394 | | return values2; |
| | 395 | | } |
| | 396 | |
|
| 0 | 397 | | if (count2 == 0) |
| | 398 | | { |
| 0 | 399 | | return values1; |
| | 400 | | } |
| | 401 | |
|
| 0 | 402 | | var combined = new string[count1 + count2]; |
| 0 | 403 | | values1.CopyTo(combined, 0); |
| 0 | 404 | | values2.CopyTo(combined, count1); |
| 0 | 405 | | return new StringValues(combined); |
| | 406 | | } |
| | 407 | |
|
| | 408 | | public static StringValues Concat(in StringValues values, string value) |
| | 409 | | { |
| 0 | 410 | | if (value == null) |
| | 411 | | { |
| 0 | 412 | | return values; |
| | 413 | | } |
| | 414 | |
|
| 0 | 415 | | var count = values.Count; |
| 0 | 416 | | if (count == 0) |
| | 417 | | { |
| 0 | 418 | | return new StringValues(value); |
| | 419 | | } |
| | 420 | |
|
| 0 | 421 | | var combined = new string[count + 1]; |
| 0 | 422 | | values.CopyTo(combined, 0); |
| 0 | 423 | | combined[count] = value; |
| 0 | 424 | | return new StringValues(combined); |
| | 425 | | } |
| | 426 | |
|
| | 427 | | public static StringValues Concat(string value, in StringValues values) |
| | 428 | | { |
| 0 | 429 | | if (value == null) |
| | 430 | | { |
| 0 | 431 | | return values; |
| | 432 | | } |
| | 433 | |
|
| 0 | 434 | | var count = values.Count; |
| 0 | 435 | | if (count == 0) |
| | 436 | | { |
| 0 | 437 | | return new StringValues(value); |
| | 438 | | } |
| | 439 | |
|
| 0 | 440 | | var combined = new string[count + 1]; |
| 0 | 441 | | combined[0] = value; |
| 0 | 442 | | values.CopyTo(combined, 1); |
| 0 | 443 | | return new StringValues(combined); |
| | 444 | | } |
| | 445 | |
|
| | 446 | | public static bool Equals(StringValues left, StringValues right) |
| | 447 | | { |
| 0 | 448 | | var count = left.Count; |
| | 449 | |
|
| 0 | 450 | | if (count != right.Count) |
| | 451 | | { |
| 0 | 452 | | return false; |
| | 453 | | } |
| | 454 | |
|
| 0 | 455 | | for (var i = 0; i < count; i++) |
| | 456 | | { |
| 0 | 457 | | if (left[i] != right[i]) |
| | 458 | | { |
| 0 | 459 | | return false; |
| | 460 | | } |
| | 461 | | } |
| | 462 | |
|
| 0 | 463 | | return true; |
| | 464 | | } |
| | 465 | |
|
| | 466 | | public static bool operator ==(StringValues left, StringValues right) |
| | 467 | | { |
| 0 | 468 | | return Equals(left, right); |
| | 469 | | } |
| | 470 | |
|
| | 471 | | public static bool operator !=(StringValues left, StringValues right) |
| | 472 | | { |
| 0 | 473 | | return !Equals(left, right); |
| | 474 | | } |
| | 475 | |
|
| | 476 | | public bool Equals(StringValues other) |
| | 477 | | { |
| 0 | 478 | | return Equals(this, other); |
| | 479 | | } |
| | 480 | |
|
| | 481 | | public static bool Equals(string left, StringValues right) |
| | 482 | | { |
| 0 | 483 | | return Equals(new StringValues(left), right); |
| | 484 | | } |
| | 485 | |
|
| | 486 | | public static bool Equals(StringValues left, string right) |
| | 487 | | { |
| 0 | 488 | | return Equals(left, new StringValues(right)); |
| | 489 | | } |
| | 490 | |
|
| | 491 | | public bool Equals(string other) |
| | 492 | | { |
| 0 | 493 | | return Equals(this, new StringValues(other)); |
| | 494 | | } |
| | 495 | |
|
| | 496 | | public static bool Equals(string[] left, StringValues right) |
| | 497 | | { |
| 0 | 498 | | return Equals(new StringValues(left), right); |
| | 499 | | } |
| | 500 | |
|
| | 501 | | public static bool Equals(StringValues left, string[] right) |
| | 502 | | { |
| 0 | 503 | | return Equals(left, new StringValues(right)); |
| | 504 | | } |
| | 505 | |
|
| | 506 | | public bool Equals(string[] other) |
| | 507 | | { |
| 0 | 508 | | return Equals(this, new StringValues(other)); |
| | 509 | | } |
| | 510 | |
|
| | 511 | | public static bool operator ==(StringValues left, string right) |
| | 512 | | { |
| 0 | 513 | | return Equals(left, new StringValues(right)); |
| | 514 | | } |
| | 515 | |
|
| | 516 | | public static bool operator !=(StringValues left, string right) |
| | 517 | | { |
| 0 | 518 | | return !Equals(left, new StringValues(right)); |
| | 519 | | } |
| | 520 | |
|
| | 521 | | public static bool operator ==(string left, StringValues right) |
| | 522 | | { |
| 0 | 523 | | return Equals(new StringValues(left), right); |
| | 524 | | } |
| | 525 | |
|
| | 526 | | public static bool operator !=(string left, StringValues right) |
| | 527 | | { |
| 0 | 528 | | return !Equals(new StringValues(left), right); |
| | 529 | | } |
| | 530 | |
|
| | 531 | | public static bool operator ==(StringValues left, string[] right) |
| | 532 | | { |
| 0 | 533 | | return Equals(left, new StringValues(right)); |
| | 534 | | } |
| | 535 | |
|
| | 536 | | public static bool operator !=(StringValues left, string[] right) |
| | 537 | | { |
| 0 | 538 | | return !Equals(left, new StringValues(right)); |
| | 539 | | } |
| | 540 | |
|
| | 541 | | public static bool operator ==(string[] left, StringValues right) |
| | 542 | | { |
| 0 | 543 | | return Equals(new StringValues(left), right); |
| | 544 | | } |
| | 545 | |
|
| | 546 | | public static bool operator !=(string[] left, StringValues right) |
| | 547 | | { |
| 0 | 548 | | return !Equals(new StringValues(left), right); |
| | 549 | | } |
| | 550 | |
|
| | 551 | | public static bool operator ==(StringValues left, object right) |
| | 552 | | { |
| 0 | 553 | | return left.Equals(right); |
| | 554 | | } |
| | 555 | |
|
| | 556 | | public static bool operator !=(StringValues left, object right) |
| | 557 | | { |
| 0 | 558 | | return !left.Equals(right); |
| | 559 | | } |
| | 560 | | public static bool operator ==(object left, StringValues right) |
| | 561 | | { |
| 0 | 562 | | return right.Equals(left); |
| | 563 | | } |
| | 564 | |
|
| | 565 | | public static bool operator !=(object left, StringValues right) |
| | 566 | | { |
| 0 | 567 | | return !right.Equals(left); |
| | 568 | | } |
| | 569 | |
|
| | 570 | | public override bool Equals(object obj) |
| | 571 | | { |
| 0 | 572 | | if (obj == null) |
| | 573 | | { |
| 0 | 574 | | return Equals(this, StringValues.Empty); |
| | 575 | | } |
| | 576 | |
|
| 0 | 577 | | if (obj is string) |
| | 578 | | { |
| 0 | 579 | | return Equals(this, (string)obj); |
| | 580 | | } |
| | 581 | |
|
| 0 | 582 | | if (obj is string[]) |
| | 583 | | { |
| 0 | 584 | | return Equals(this, (string[])obj); |
| | 585 | | } |
| | 586 | |
|
| 0 | 587 | | if (obj is StringValues) |
| | 588 | | { |
| 0 | 589 | | return Equals(this, (StringValues)obj); |
| | 590 | | } |
| | 591 | |
|
| 0 | 592 | | return false; |
| | 593 | | } |
| | 594 | |
|
| | 595 | | public override int GetHashCode() |
| | 596 | | { |
| 0 | 597 | | var value = _values; |
| 0 | 598 | | if (value is string[] values) |
| | 599 | | { |
| 0 | 600 | | if (Count == 1) |
| | 601 | | { |
| 0 | 602 | | return Unsafe.As<string>(this[0])?.GetHashCode() ?? Count.GetHashCode(); |
| | 603 | | } |
| 0 | 604 | | var hcc = new HashCodeCombiner(); |
| 0 | 605 | | for (var i = 0; i < values.Length; i++) |
| | 606 | | { |
| 0 | 607 | | hcc.Add(values[i]); |
| | 608 | | } |
| 0 | 609 | | return hcc.CombinedHash; |
| | 610 | | } |
| | 611 | | else |
| | 612 | | { |
| 0 | 613 | | return Unsafe.As<string>(value)?.GetHashCode() ?? Count.GetHashCode(); |
| | 614 | | } |
| | 615 | | } |
| | 616 | |
|
| | 617 | | public struct Enumerator : IEnumerator<string> |
| | 618 | | { |
| | 619 | | private readonly string[] _values; |
| | 620 | | private string _current; |
| | 621 | | private int _index; |
| | 622 | |
|
| | 623 | | internal Enumerator(object value) |
| | 624 | | { |
| 0 | 625 | | if (value is string str) |
| | 626 | | { |
| 0 | 627 | | _values = null; |
| 0 | 628 | | _current = str; |
| | 629 | | } |
| | 630 | | else |
| | 631 | | { |
| 0 | 632 | | _current = null; |
| 0 | 633 | | _values = Unsafe.As<string[]>(value); |
| | 634 | | } |
| 0 | 635 | | _index = 0; |
| 0 | 636 | | } |
| | 637 | |
|
| 0 | 638 | | public Enumerator(ref StringValues values) : this(values._values) |
| 0 | 639 | | { } |
| | 640 | |
|
| | 641 | | public bool MoveNext() |
| | 642 | | { |
| 0 | 643 | | var index = _index; |
| 0 | 644 | | if (index < 0) |
| | 645 | | { |
| 0 | 646 | | return false; |
| | 647 | | } |
| | 648 | |
|
| 0 | 649 | | var values = _values; |
| 0 | 650 | | if (values != null) |
| | 651 | | { |
| 0 | 652 | | if ((uint)index < (uint)values.Length) |
| | 653 | | { |
| 0 | 654 | | _index = index + 1; |
| 0 | 655 | | _current = values[index]; |
| 0 | 656 | | return true; |
| | 657 | | } |
| | 658 | |
|
| 0 | 659 | | _index = -1; |
| 0 | 660 | | return false; |
| | 661 | | } |
| | 662 | |
|
| 0 | 663 | | _index = -1; // sentinel value |
| 0 | 664 | | return _current != null; |
| | 665 | | } |
| | 666 | |
|
| 0 | 667 | | public string Current => _current; |
| | 668 | |
|
| 0 | 669 | | object IEnumerator.Current => _current; |
| | 670 | |
|
| | 671 | | void IEnumerator.Reset() |
| | 672 | | { |
| 0 | 673 | | throw new NotSupportedException(); |
| | 674 | | } |
| | 675 | |
|
| | 676 | | public void Dispose() |
| | 677 | | { |
| 0 | 678 | | } |
| | 679 | | } |
| | 680 | | } |
| | 681 | | } |