< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 30
Coverable lines: 30
Total lines: 93
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 10
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(...)0%220%
MoveNext()0%220%
GetTextElement()0%440%
Reset()100%110%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Private.CoreLib/src/System/Globalization/TextElementEnumerator.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.Diagnostics;
 6using System.Text.Unicode;
 7
 8namespace System.Globalization
 9{
 10    public class TextElementEnumerator : IEnumerator
 11    {
 12        private readonly string _str;
 13        private readonly int _strStartIndex; // where in _str the enumeration should begin
 14
 15        private int _currentTextElementOffset;
 16        private int _currentTextElementLength;
 17        private string? _currentTextElementSubstr;
 18
 019        internal TextElementEnumerator(string str, int startIndex)
 20        {
 021            Debug.Assert(str != null, "TextElementEnumerator(): str != null");
 022            Debug.Assert(startIndex >= 0 && startIndex <= str.Length, "TextElementEnumerator(): startIndex >= 0 && start
 23
 024            _str = str;
 025            _strStartIndex = startIndex;
 26
 027            Reset();
 028        }
 29
 30        public bool MoveNext()
 31        {
 032            _currentTextElementSubstr = null; // clear any cached substr
 33
 034            int newOffset = _currentTextElementOffset + _currentTextElementLength;
 035            _currentTextElementOffset = newOffset; // advance
 036            _currentTextElementLength = 0; // prevent future calls to MoveNext() or get_Current from succeeding if we've
 37
 038            if (newOffset >= _str.Length)
 39            {
 040                return false; // reached the end of the data
 41            }
 42
 043            _currentTextElementLength = TextSegmentationUtility.GetLengthOfFirstUtf16ExtendedGraphemeCluster(_str.AsSpan
 044            return true;
 45        }
 46
 047        public object Current => GetTextElement();
 48
 49        public string GetTextElement()
 50        {
 51            // Returned the cached substr if we've already generated it.
 52            // Otherwise perform the substr operation now.
 53
 054            string? currentSubstr = _currentTextElementSubstr;
 055            if (currentSubstr is null)
 56            {
 057                if (_currentTextElementOffset >= _str.Length)
 58                {
 059                    throw new InvalidOperationException(SR.InvalidOperation_EnumOpCantHappen);
 60                }
 61
 062                currentSubstr = _str.Substring(_currentTextElementOffset, _currentTextElementLength);
 063                _currentTextElementSubstr = currentSubstr;
 64            }
 65
 066            return currentSubstr;
 67        }
 68
 69        public int ElementIndex
 70        {
 71            get
 72            {
 073                if (_currentTextElementOffset >= _str.Length)
 74                {
 075                    throw new InvalidOperationException(SR.InvalidOperation_EnumOpCantHappen);
 76                }
 77
 078                return _currentTextElementOffset - _strStartIndex;
 79            }
 80        }
 81
 82        public void Reset()
 83        {
 84            // These first two fields are set to intentionally out-of-range values.
 85            // They'll be fixed up once the enumerator starts.
 86
 087            _currentTextElementOffset = _str.Length;
 088            _currentTextElementLength = _strStartIndex - _str.Length;
 089            _currentTextElementSubstr = null;
 090        }
 91    }
 92}
 93