| | 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 | |
|
| | 10 | | #pragma warning disable IDE0034 // Default expression can be simplified |
| | 11 | |
|
| | 12 | | namespace Azure.Core.Http.Multipart |
| | 13 | | { |
| | 14 | | /// <summary> |
| | 15 | | /// Tokenizes a <see cref="string"/> into <see cref="StringSegment"/>s. |
| | 16 | | /// </summary> |
| | 17 | | internal readonly struct StringTokenizer : IEnumerable<StringSegment> |
| | 18 | | { |
| | 19 | | private readonly StringSegment _value; |
| | 20 | | private readonly char[] _separators; |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// Initializes a new instance of <see cref="StringTokenizer"/>. |
| | 24 | | /// </summary> |
| | 25 | | /// <param name="value">The <see cref="string"/> to tokenize.</param> |
| | 26 | | /// <param name="separators">The characters to tokenize by.</param> |
| | 27 | | public StringTokenizer(string value, char[] separators) |
| | 28 | | { |
| 0 | 29 | | if (value == null) |
| | 30 | | { |
| 0 | 31 | | ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value); |
| | 32 | | } |
| | 33 | |
|
| 0 | 34 | | if (separators == null) |
| | 35 | | { |
| 0 | 36 | | ThrowHelper.ThrowArgumentNullException(ExceptionArgument.separators); |
| | 37 | | } |
| | 38 | |
|
| 0 | 39 | | _value = value; |
| 0 | 40 | | _separators = separators; |
| 0 | 41 | | } |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// Initializes a new instance of <see cref="StringTokenizer"/>. |
| | 45 | | /// </summary> |
| | 46 | | /// <param name="value">The <see cref="StringSegment"/> to tokenize.</param> |
| | 47 | | /// <param name="separators">The characters to tokenize by.</param> |
| | 48 | | public StringTokenizer(StringSegment value, char[] separators) |
| | 49 | | { |
| 0 | 50 | | if (!value.HasValue) |
| | 51 | | { |
| 0 | 52 | | ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value); |
| | 53 | | } |
| | 54 | |
|
| 0 | 55 | | if (separators == null) |
| | 56 | | { |
| 0 | 57 | | ThrowHelper.ThrowArgumentNullException(ExceptionArgument.separators); |
| | 58 | | } |
| | 59 | |
|
| 0 | 60 | | _value = value; |
| 0 | 61 | | _separators = separators; |
| 0 | 62 | | } |
| | 63 | |
|
| 0 | 64 | | public Enumerator GetEnumerator() => new Enumerator(in _value, _separators); |
| | 65 | |
|
| 0 | 66 | | IEnumerator<StringSegment> IEnumerable<StringSegment>.GetEnumerator() => GetEnumerator(); |
| | 67 | |
|
| 0 | 68 | | IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
| | 69 | |
|
| | 70 | | public struct Enumerator : IEnumerator<StringSegment> |
| | 71 | | { |
| | 72 | | private readonly StringSegment _value; |
| | 73 | | private readonly char[] _separators; |
| | 74 | | private int _index; |
| | 75 | |
|
| | 76 | | internal Enumerator(in StringSegment value, char[] separators) |
| | 77 | | { |
| 0 | 78 | | _value = value; |
| 0 | 79 | | _separators = separators; |
| 0 | 80 | | Current = default; |
| 0 | 81 | | _index = 0; |
| 0 | 82 | | } |
| | 83 | |
|
| | 84 | | public Enumerator(ref StringTokenizer tokenizer) |
| | 85 | | { |
| 0 | 86 | | _value = tokenizer._value; |
| 0 | 87 | | _separators = tokenizer._separators; |
| 0 | 88 | | Current = default(StringSegment); |
| 0 | 89 | | _index = 0; |
| 0 | 90 | | } |
| | 91 | |
|
| 0 | 92 | | public StringSegment Current { get; private set; } |
| | 93 | |
|
| 0 | 94 | | object IEnumerator.Current => Current; |
| | 95 | |
|
| | 96 | | public void Dispose() |
| | 97 | | { |
| 0 | 98 | | } |
| | 99 | |
|
| | 100 | | public bool MoveNext() |
| | 101 | | { |
| 0 | 102 | | if (!_value.HasValue || _index > _value.Length) |
| | 103 | | { |
| 0 | 104 | | Current = default(StringSegment); |
| 0 | 105 | | return false; |
| | 106 | | } |
| | 107 | |
|
| 0 | 108 | | var next = _value.IndexOfAny(_separators, _index); |
| 0 | 109 | | if (next == -1) |
| | 110 | | { |
| | 111 | | // No separator found. Consume the remainder of the string. |
| 0 | 112 | | next = _value.Length; |
| | 113 | | } |
| | 114 | |
|
| 0 | 115 | | Current = _value.Subsegment(_index, next - _index); |
| 0 | 116 | | _index = next + 1; |
| | 117 | |
|
| 0 | 118 | | return true; |
| | 119 | | } |
| | 120 | |
|
| | 121 | | public void Reset() |
| | 122 | | { |
| 0 | 123 | | Current = default(StringSegment); |
| 0 | 124 | | _index = 0; |
| 0 | 125 | | } |
| | 126 | | } |
| | 127 | | } |
| | 128 | | } |