< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 17
Coverable lines: 17
Total lines: 64
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 4
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%110%
GetEnumerator()100%110%
MoveNext()0%440%
System.Collections.IEnumerator.Reset()100%110%
System.IDisposable.Dispose()100%110%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Private.CoreLib/src/System/Text/SpanRuneEnumerator.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System;
 5using System.Collections;
 6using System.Collections.Generic;
 7
 8namespace System.Text
 9{
 10    // An enumerator for retrieving System.Text.Rune instances from a ROS<char>.
 11    // Methods are pattern-matched by compiler to allow using foreach pattern.
 12    public ref struct SpanRuneEnumerator : IEnumerator<Rune>
 13    {
 14        private ReadOnlySpan<char> _remaining;
 15        private Rune _current;
 16
 17        internal SpanRuneEnumerator(ReadOnlySpan<char> buffer)
 18        {
 019            _remaining = buffer;
 020            _current = default;
 021        }
 22
 023        public Rune Current => _current;
 24
 025        public SpanRuneEnumerator GetEnumerator() => this;
 26
 27        public bool MoveNext()
 28        {
 029            if (_remaining.IsEmpty)
 30            {
 31                // reached the end of the buffer
 032                _current = default;
 033                return false;
 34            }
 35
 036            int scalarValue = Rune.ReadFirstRuneFromUtf16Buffer(_remaining);
 037            if (scalarValue < 0)
 38            {
 39                // replace invalid sequences with U+FFFD
 040                scalarValue = Rune.ReplacementChar.Value;
 41            }
 42
 43            // In UTF-16 specifically, invalid sequences always have length 1, which is the same
 44            // length as the replacement character U+FFFD. This means that we can always bump the
 45            // next index by the current scalar's UTF-16 sequence length. This optimization is not
 46            // generally applicable; for example, enumerating scalars from UTF-8 cannot utilize
 47            // this same trick.
 48
 049            _current = Rune.UnsafeCreate((uint)scalarValue);
 050            _remaining = _remaining.Slice(_current.Utf16SequenceLength);
 051            return true;
 52        }
 53
 54        /// <inheritdoc />
 055        object IEnumerator.Current => Current;
 56
 57        /// <inheritdoc />
 058        void IEnumerator.Reset() => throw new NotSupportedException();
 59
 60        /// <inheritdoc />
 061        void IDisposable.Dispose() { }
 62    }
 63}
 64