< Summary

Class:Azure.Core.Http.Multipart.StringTokenizer
Assembly:Azure.Storage.Blobs.Batch
File(s):C:\Git\azure-sdk-for-net\sdk\storage\Azure.Storage.Blobs.Batch\src\Shared\StringTokenizer.cs
Covered lines:0
Uncovered lines:42
Coverable lines:42
Total lines:128
Line coverage:0% (0 of 42)
Covered branches:0
Total branches:14
Branch coverage:0% (0 of 14)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-0%0%
.ctor(...)-0%0%
GetEnumerator()-0%100%
System.Collections.Generic.IEnumerable<Azure.Core.Http.Multipart.StringSegment>.GetEnumerator()-0%100%
System.Collections.IEnumerable.GetEnumerator()-0%100%
.ctor(...)-0%100%
.ctor(...)-0%100%
get_Current()-0%100%
System.Collections.IEnumerator.get_Current()-0%100%
Dispose()-0%100%
MoveNext()-0%0%
Reset()-0%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\storage\Azure.Storage.Blobs.Batch\src\Shared\StringTokenizer.cs

#LineLine coverage
 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
 6using System;
 7using System.Collections;
 8using System.Collections.Generic;
 9
 10#pragma warning disable IDE0034 // Default expression can be simplified
 11
 12namespace 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        {
 029            if (value == null)
 30            {
 031                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value);
 32            }
 33
 034            if (separators == null)
 35            {
 036                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.separators);
 37            }
 38
 039            _value = value;
 040            _separators = separators;
 041        }
 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        {
 050            if (!value.HasValue)
 51            {
 052                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value);
 53            }
 54
 055            if (separators == null)
 56            {
 057                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.separators);
 58            }
 59
 060            _value = value;
 061            _separators = separators;
 062        }
 63
 064        public Enumerator GetEnumerator() => new Enumerator(in _value, _separators);
 65
 066        IEnumerator<StringSegment> IEnumerable<StringSegment>.GetEnumerator() => GetEnumerator();
 67
 068        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            {
 078                _value = value;
 079                _separators = separators;
 080                Current = default;
 081                _index = 0;
 082            }
 83
 84            public Enumerator(ref StringTokenizer tokenizer)
 85            {
 086                _value = tokenizer._value;
 087                _separators = tokenizer._separators;
 088                Current = default(StringSegment);
 089                _index = 0;
 090            }
 91
 092            public StringSegment Current { get; private set; }
 93
 094            object IEnumerator.Current => Current;
 95
 96            public void Dispose()
 97            {
 098            }
 99
 100            public bool MoveNext()
 101            {
 0102                if (!_value.HasValue || _index > _value.Length)
 103                {
 0104                    Current = default(StringSegment);
 0105                    return false;
 106                }
 107
 0108                var next = _value.IndexOfAny(_separators, _index);
 0109                if (next == -1)
 110                {
 111                    // No separator found. Consume the remainder of the string.
 0112                    next = _value.Length;
 113                }
 114
 0115                Current = _value.Subsegment(_index, next - _index);
 0116                _index = next + 1;
 117
 0118                return true;
 119            }
 120
 121            public void Reset()
 122            {
 0123                Current = default(StringSegment);
 0124                _index = 0;
 0125            }
 126        }
 127    }
 128}