mscorlib(4.0.0.0) API with additions
StringInfo.cs
3 
4 namespace System.Globalization
5 {
8  [ComVisible(true)]
9  [__DynamicallyInvokable]
10  public class StringInfo
11  {
12  [OptionalField(VersionAdded = 2)]
13  private string m_str;
14 
15  [NonSerialized]
16  private int[] m_indexes;
17 
18  private int[] Indexes
19  {
20  get
21  {
22  if (m_indexes == null && 0 < String.Length)
23  {
24  m_indexes = ParseCombiningCharacters(String);
25  }
26  return m_indexes;
27  }
28  }
29 
33  [__DynamicallyInvokable]
34  public string String
35  {
36  [__DynamicallyInvokable]
37  get
38  {
39  return m_str;
40  }
41  [__DynamicallyInvokable]
42  set
43  {
44  if (value == null)
45  {
46  throw new ArgumentNullException("String", Environment.GetResourceString("ArgumentNull_String"));
47  }
48  m_str = value;
49  m_indexes = null;
50  }
51  }
52 
55  [__DynamicallyInvokable]
56  public int LengthInTextElements
57  {
58  [__DynamicallyInvokable]
59  get
60  {
61  if (Indexes == null)
62  {
63  return 0;
64  }
65  return Indexes.Length;
66  }
67  }
68 
70  [__DynamicallyInvokable]
71  public StringInfo()
72  : this("")
73  {
74  }
75 
80  [__DynamicallyInvokable]
81  public StringInfo(string value)
82  {
83  String = value;
84  }
85 
86  [OnDeserializing]
87  private void OnDeserializing(StreamingContext ctx)
88  {
89  m_str = string.Empty;
90  }
91 
92  [OnDeserialized]
93  private void OnDeserialized(StreamingContext ctx)
94  {
95  if (m_str.Length == 0)
96  {
97  m_indexes = null;
98  }
99  }
100 
105  [ComVisible(false)]
106  [__DynamicallyInvokable]
107  public override bool Equals(object value)
108  {
109  StringInfo stringInfo = value as StringInfo;
110  if (stringInfo != null)
111  {
112  return m_str.Equals(stringInfo.m_str);
113  }
114  return false;
115  }
116 
119  [ComVisible(false)]
120  [__DynamicallyInvokable]
121  public override int GetHashCode()
122  {
123  return m_str.GetHashCode();
124  }
125 
131  public string SubstringByTextElements(int startingTextElement)
132  {
133  if (Indexes == null)
134  {
135  if (startingTextElement < 0)
136  {
137  throw new ArgumentOutOfRangeException("startingTextElement", Environment.GetResourceString("ArgumentOutOfRange_NeedPosNum"));
138  }
139  throw new ArgumentOutOfRangeException("startingTextElement", Environment.GetResourceString("Arg_ArgumentOutOfRangeException"));
140  }
141  return SubstringByTextElements(startingTextElement, Indexes.Length - startingTextElement);
142  }
143 
153  public string SubstringByTextElements(int startingTextElement, int lengthInTextElements)
154  {
155  if (startingTextElement < 0)
156  {
157  throw new ArgumentOutOfRangeException("startingTextElement", Environment.GetResourceString("ArgumentOutOfRange_NeedPosNum"));
158  }
159  if (String.Length == 0 || startingTextElement >= Indexes.Length)
160  {
161  throw new ArgumentOutOfRangeException("startingTextElement", Environment.GetResourceString("Arg_ArgumentOutOfRangeException"));
162  }
163  if (lengthInTextElements < 0)
164  {
165  throw new ArgumentOutOfRangeException("lengthInTextElements", Environment.GetResourceString("ArgumentOutOfRange_NeedPosNum"));
166  }
167  if (startingTextElement > Indexes.Length - lengthInTextElements)
168  {
169  throw new ArgumentOutOfRangeException("lengthInTextElements", Environment.GetResourceString("Arg_ArgumentOutOfRangeException"));
170  }
171  int num = Indexes[startingTextElement];
172  if (startingTextElement + lengthInTextElements == Indexes.Length)
173  {
174  return String.Substring(num);
175  }
176  return String.Substring(num, Indexes[lengthInTextElements + startingTextElement] - num);
177  }
178 
184  [__DynamicallyInvokable]
185  public static string GetNextTextElement(string str)
186  {
187  return GetNextTextElement(str, 0);
188  }
189 
190  internal static int GetCurrentTextElementLen(string str, int index, int len, ref UnicodeCategory ucCurrent, ref int currentCharCount)
191  {
192  if (index + currentCharCount == len)
193  {
194  return currentCharCount;
195  }
196  UnicodeCategory unicodeCategory = CharUnicodeInfo.InternalGetUnicodeCategory(str, index + currentCharCount, out int charLength);
197  if (CharUnicodeInfo.IsCombiningCategory(unicodeCategory) && !CharUnicodeInfo.IsCombiningCategory(ucCurrent) && ucCurrent != UnicodeCategory.Format && ucCurrent != UnicodeCategory.Control && ucCurrent != UnicodeCategory.OtherNotAssigned && ucCurrent != UnicodeCategory.Surrogate)
198  {
199  int num = index;
200  for (index += currentCharCount + charLength; index < len; index += charLength)
201  {
202  unicodeCategory = CharUnicodeInfo.InternalGetUnicodeCategory(str, index, out charLength);
203  if (!CharUnicodeInfo.IsCombiningCategory(unicodeCategory))
204  {
205  ucCurrent = unicodeCategory;
206  currentCharCount = charLength;
207  break;
208  }
209  }
210  return index - num;
211  }
212  int result = currentCharCount;
213  ucCurrent = unicodeCategory;
214  currentCharCount = charLength;
215  return result;
216  }
217 
226  [__DynamicallyInvokable]
227  public static string GetNextTextElement(string str, int index)
228  {
229  if (str == null)
230  {
231  throw new ArgumentNullException("str");
232  }
233  int length = str.Length;
234  if (index < 0 || index >= length)
235  {
236  if (index == length)
237  {
238  return string.Empty;
239  }
240  throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index"));
241  }
242  int charLength;
243  UnicodeCategory ucCurrent = CharUnicodeInfo.InternalGetUnicodeCategory(str, index, out charLength);
244  return str.Substring(index, GetCurrentTextElementLen(str, index, length, ref ucCurrent, ref charLength));
245  }
246 
252  [__DynamicallyInvokable]
254  {
255  return GetTextElementEnumerator(str, 0);
256  }
257 
266  [__DynamicallyInvokable]
267  public static TextElementEnumerator GetTextElementEnumerator(string str, int index)
268  {
269  if (str == null)
270  {
271  throw new ArgumentNullException("str");
272  }
273  int length = str.Length;
274  if (index < 0 || index > length)
275  {
276  throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index"));
277  }
278  return new TextElementEnumerator(str, index, length);
279  }
280 
286  [__DynamicallyInvokable]
287  public static int[] ParseCombiningCharacters(string str)
288  {
289  if (str == null)
290  {
291  throw new ArgumentNullException("str");
292  }
293  int length = str.Length;
294  int[] array = new int[length];
295  if (length == 0)
296  {
297  return array;
298  }
299  int num = 0;
300  int i = 0;
301  int charLength;
302  for (UnicodeCategory ucCurrent = CharUnicodeInfo.InternalGetUnicodeCategory(str, 0, out charLength); i < length; i += GetCurrentTextElementLen(str, i, length, ref ucCurrent, ref charLength))
303  {
304  array[num++] = i;
305  }
306  if (num < length)
307  {
308  int[] array2 = new int[num];
309  Array.Copy(array, array2, num);
310  return array2;
311  }
312  return array;
313  }
314  }
315 }
override bool Equals(object value)
Indicates whether the current T:System.Globalization.StringInfo object is equal to a specified object...
Definition: StringInfo.cs:107
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Represents text as a sequence of UTF-16 code units.To browse the .NET Framework source code for this ...
Definition: String.cs:18
StringInfo(string value)
Initializes a new instance of the T:System.Globalization.StringInfo class to a specified string.
Definition: StringInfo.cs:81
Definition: __Canon.cs:3
The exception that is thrown when the value of an argument is outside the allowable range of values a...
static TextElementEnumerator GetTextElementEnumerator(string str, int index)
Returns an enumerator that iterates through the text elements of the string, starting at the specifie...
Definition: StringInfo.cs:267
string Substring(int startIndex)
Retrieves a substring from this instance. The substring starts at a specified character position and ...
Definition: String.cs:1200
static int [] ParseCombiningCharacters(string str)
Returns the indexes of each base character, high surrogate, or control character within the specified...
Definition: StringInfo.cs:287
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
int LengthInTextElements
Gets the number of text elements in the current T:System.Globalization.StringInfo object.
Definition: StringInfo.cs:57
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
Enumerates the text elements of a string.
static string GetNextTextElement(string str, int index)
Gets the text element at the specified index of the specified string.
Definition: StringInfo.cs:227
static string GetNextTextElement(string str)
Gets the first text element in a specified string.
Definition: StringInfo.cs:185
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition: Array.cs:17
string SubstringByTextElements(int startingTextElement)
Retrieves a substring of text elements from the current T:System.Globalization.StringInfo object star...
Definition: StringInfo.cs:131
string SubstringByTextElements(int startingTextElement, int lengthInTextElements)
Retrieves a substring of text elements from the current T:System.Globalization.StringInfo object star...
Definition: StringInfo.cs:153
Retrieves information about a Unicode character. This class cannot be inherited.
static void Copy(Array sourceArray, Array destinationArray, int length)
Copies a range of elements from an T:System.Array starting at the first element and pastes them into ...
Definition: Array.cs:1275
static TextElementEnumerator GetTextElementEnumerator(string str)
Returns an enumerator that iterates through the text elements of the entire string.
Definition: StringInfo.cs:253
UnicodeCategory
Defines the Unicode category of a character.
StringInfo()
Initializes a new instance of the T:System.Globalization.StringInfo class.
Definition: StringInfo.cs:71
Specifies that the class can be serialized.
override int GetHashCode()
Calculates a hash code for the value of the current T:System.Globalization.StringInfo object.
Definition: StringInfo.cs:121
int Length
Gets the number of characters in the current T:System.String object.
Definition: String.cs:61