< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 20
Coverable lines: 20
Total lines: 70
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

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Private.CoreLib/src/System/Text/StringRuneEnumerator.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.Collections;
 5using System.Collections.Generic;
 6
 7namespace System.Text
 8{
 9    // An enumerator for retrieving System.Text.Rune instances from a System.String.
 10    public struct StringRuneEnumerator : IEnumerable<Rune>, IEnumerator<Rune>
 11    {
 12        private readonly string _string;
 13        private Rune _current;
 14        private int _nextIndex;
 15
 16        internal StringRuneEnumerator(string value)
 17        {
 018            _string = value;
 019            _current = default;
 020            _nextIndex = 0;
 021        }
 22
 023        public Rune Current => _current;
 24
 025        public StringRuneEnumerator GetEnumerator() => this;
 26
 27        public bool MoveNext()
 28        {
 029            if ((uint)_nextIndex >= _string.Length)
 30            {
 31                // reached the end of the string
 032                _current = default;
 033                return false;
 34            }
 35
 036            if (!Rune.TryGetRuneAt(_string, _nextIndex, out _current))
 37            {
 38                // replace invalid sequences with U+FFFD
 039                _current = Rune.ReplacementChar;
 40            }
 41
 42            // In UTF-16 specifically, invalid sequences always have length 1, which is the same
 43            // length as the replacement character U+FFFD. This means that we can always bump the
 44            // next index by the current scalar's UTF-16 sequence length. This optimization is not
 45            // generally applicable; for example, enumerating scalars from UTF-8 cannot utilize
 46            // this same trick.
 47
 048            _nextIndex += _current.Utf16SequenceLength;
 049            return true;
 50        }
 51
 052        object? IEnumerator.Current => _current;
 53
 54        void IDisposable.Dispose()
 55        {
 56            // no-op
 057        }
 58
 059        IEnumerator IEnumerable.GetEnumerator() => this;
 60
 061        IEnumerator<Rune> IEnumerable<Rune>.GetEnumerator() => this;
 62
 63        void IEnumerator.Reset()
 64        {
 065            _current = default;
 066            _nextIndex = 0;
 067        }
 68    }
 69}
 70