< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 20
Coverable lines: 20
Total lines: 106
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/StringBuilderRuneEnumerator.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    /// <summary>
 10    /// An enumerator for retrieving <see cref="Rune"/> instances from a <see cref="StringBuilder"/>.
 11    /// </summary>
 12    public struct StringBuilderRuneEnumerator : IEnumerable<Rune>, IEnumerator<Rune>
 13    {
 14        private readonly StringBuilder _stringBuilder;
 15        private Rune _current;
 16        private int _nextIndex;
 17
 18        internal StringBuilderRuneEnumerator(StringBuilder value)
 19        {
 020            _stringBuilder = value;
 021            _current = default;
 022            _nextIndex = 0;
 023        }
 24
 25        /// <summary>
 26        /// Gets the <see cref="Rune"/> at the current position of the enumerator.
 27        /// </summary>
 028        public readonly Rune Current => _current;
 29
 30        /// <summary>
 31        /// Returns the current enumerator instance.
 32        /// </summary>
 33        /// <returns>The current enumerator instance.</returns>
 034        public readonly StringBuilderRuneEnumerator GetEnumerator() => this;
 35
 36        /// <summary>
 37        /// Advances the enumerator to the next <see cref="Rune"/> of the builder.
 38        /// </summary>
 39        /// <returns>
 40        /// <see langword="true"/> if the enumerator successfully advanced to the next item;
 41        /// <see langword="false"/> if the end of the builder has been reached.
 42        /// </returns>
 43        public bool MoveNext()
 44        {
 045            if ((uint)_nextIndex >= _stringBuilder.Length)
 46            {
 47                // reached the end of the string
 048                _current = default;
 049                return false;
 50            }
 51
 052            if (!_stringBuilder.TryGetRuneAt(_nextIndex, out _current))
 53            {
 54                // replace invalid sequences with U+FFFD
 055                _current = Rune.ReplacementChar;
 56            }
 57
 58            // In UTF-16 specifically, invalid sequences always have length 1, which is the same
 59            // length as the replacement character U+FFFD. This means that we can always bump the
 60            // next index by the current scalar's UTF-16 sequence length. This optimization is not
 61            // generally applicable; for example, enumerating scalars from UTF-8 cannot utilize
 62            // this same trick.
 63
 064            _nextIndex += _current.Utf16SequenceLength;
 065            return true;
 66        }
 67
 68        /// <summary>
 69        /// Gets the <see cref="Rune"/> at the current position of the enumerator.
 70        /// </summary>
 071        readonly object? IEnumerator.Current => _current;
 72
 73        /// <summary>
 74        /// Releases all resources used by the current <see cref="StringBuilderRuneEnumerator"/> instance.
 75        /// </summary>
 76        /// <remarks>
 77        /// This method performs no operation and produces no side effects.
 78        /// </remarks>
 79        readonly void IDisposable.Dispose()
 80        {
 81            // no-op
 082        }
 83
 84        /// <summary>
 85        /// Returns the current enumerator instance.
 86        /// </summary>
 87        /// <returns>The current enumerator instance.</returns>
 088        readonly IEnumerator IEnumerable.GetEnumerator() => this;
 89
 90        /// <summary>
 91        /// Returns the current enumerator instance.
 92        /// </summary>
 93        /// <returns>The current enumerator instance.</returns>
 094        readonly IEnumerator<Rune> IEnumerable<Rune>.GetEnumerator() => this;
 95
 96        /// <summary>
 97        /// Resets the current <see cref="StringBuilderRuneEnumerator"/> instance to the beginning of the builder.
 98        /// </summary>
 99        void IEnumerator.Reset()
 100        {
 0101            _current = default;
 0102            _nextIndex = 0;
 0103        }
 104    }
 105}
 106