mscorlib(4.0.0.0) API with additions
TextElementEnumerator.cs
1 using System.Collections;
4 
5 namespace System.Globalization
6 {
9  [ComVisible(true)]
10  [__DynamicallyInvokable]
12  {
13  private string str;
14 
15  private int index;
16 
17  private int startIndex;
18 
19  [NonSerialized]
20  private int strLen;
21 
22  [NonSerialized]
23  private int currTextElementLen;
24 
25  [OptionalField(VersionAdded = 2)]
26  private UnicodeCategory uc;
27 
28  [OptionalField(VersionAdded = 2)]
29  private int charLen;
30 
31  private int endIndex;
32 
33  private int nextTextElementLen;
34 
38  [__DynamicallyInvokable]
39  public object Current
40  {
41  [__DynamicallyInvokable]
42  get
43  {
44  return GetTextElement();
45  }
46  }
47 
51  [__DynamicallyInvokable]
52  public int ElementIndex
53  {
54  [__DynamicallyInvokable]
55  get
56  {
57  if (index == startIndex)
58  {
59  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumNotStarted"));
60  }
61  return index - currTextElementLen;
62  }
63  }
64 
65  internal TextElementEnumerator(string str, int startIndex, int strLen)
66  {
67  this.str = str;
68  this.startIndex = startIndex;
69  this.strLen = strLen;
70  Reset();
71  }
72 
73  [OnDeserializing]
74  private void OnDeserializing(StreamingContext ctx)
75  {
76  charLen = -1;
77  }
78 
79  [OnDeserialized]
80  private void OnDeserialized(StreamingContext ctx)
81  {
82  strLen = endIndex + 1;
83  currTextElementLen = nextTextElementLen;
84  if (charLen == -1)
85  {
86  uc = CharUnicodeInfo.InternalGetUnicodeCategory(str, index, out charLen);
87  }
88  }
89 
90  [OnSerializing]
91  private void OnSerializing(StreamingContext ctx)
92  {
93  endIndex = strLen - 1;
94  nextTextElementLen = currTextElementLen;
95  }
96 
100  [__DynamicallyInvokable]
101  public bool MoveNext()
102  {
103  if (index >= strLen)
104  {
105  index = strLen + 1;
106  return false;
107  }
108  currTextElementLen = StringInfo.GetCurrentTextElementLen(str, index, strLen, ref uc, ref charLen);
109  index += currTextElementLen;
110  return true;
111  }
112 
116  [__DynamicallyInvokable]
117  public string GetTextElement()
118  {
119  if (index == startIndex)
120  {
121  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumNotStarted"));
122  }
123  if (index > strLen)
124  {
125  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumEnded"));
126  }
127  return str.Substring(index - currTextElementLen, currTextElementLen);
128  }
129 
131  [__DynamicallyInvokable]
132  public void Reset()
133  {
134  index = startIndex;
135  if (index < strLen)
136  {
137  uc = CharUnicodeInfo.InternalGetUnicodeCategory(str, index, out charLen);
138  }
139  }
140  }
141 }
Definition: __Canon.cs:3
int ElementIndex
Gets the index of the text element that the enumerator is currently positioned over.
void Reset()
Sets the enumerator to its initial position, which is before the first text element in the string.
Describes the source and destination of a given serialized stream, and provides an additional caller-...
Provides functionality to split a string into text elements and to iterate through those text element...
Definition: StringInfo.cs:10
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
Enumerates the text elements of a string.
object Current
Gets the current text element in the string.
bool MoveNext()
Advances the enumerator to the next text element of the string.
string GetTextElement()
Gets the current text element in the string.
Retrieves information about a Unicode character. This class cannot be inherited.
UnicodeCategory
Defines the Unicode category of a character.
Specifies that the class can be serialized.
The exception that is thrown when a method call is invalid for the object's current state.
Supports a simple iteration over a non-generic collection.
Definition: IEnumerator.cs:9