2 // System.Globalization.StringInfo.cs
5 // Dick Porter (dick@ximian.com)
7 // (C) 2002 Ximian, Inc.
8 // (C) 2004 Novell, Inc.
11 using System.Collections;
13 namespace System.Globalization {
16 public class StringInfo {
21 public static string GetNextTextElement(string str)
23 if(str == null || str.Length == 0) {
24 throw new ArgumentNullException("string is null");
26 return(GetNextTextElement (str, 0));
29 public static string GetNextTextElement(string str, int index)
32 throw new ArgumentNullException("string is null");
35 if(index < 0 || index >= str.Length) {
36 throw new ArgumentOutOfRangeException ("Index is not valid");
39 /* Find the next base character, surrogate
40 * pair or combining character sequence
44 UnicodeCategory cat = char.GetUnicodeCategory (ch);
46 if (cat == UnicodeCategory.Surrogate) {
47 /* Check that it's a high surrogate
48 * followed by a low surrogate
50 if (ch >= 0xD800 && ch <= 0xDBFF) {
51 if ((index + 1) < str.Length &&
52 str[index + 1] >= 0xDC00 &&
53 str[index + 1] <= 0xDFFF) {
54 /* A valid surrogate pair */
55 return(str.Substring (index, 2));
57 /* High surrogate on its own */
58 return(new String (ch, 1));
61 /* Low surrogate on its own */
62 return(new String (ch, 1));
65 /* Look for a base character, which
66 * may or may not be followed by a
67 * series of combining characters
70 if (cat == UnicodeCategory.NonSpacingMark ||
71 cat == UnicodeCategory.SpacingCombiningMark ||
72 cat == UnicodeCategory.EnclosingMark) {
73 /* Not a base character */
74 return(new String (ch, 1));
79 while (index + count < str.Length) {
80 cat = char.GetUnicodeCategory (str[index + count]);
81 if (cat != UnicodeCategory.NonSpacingMark &&
82 cat != UnicodeCategory.SpacingCombiningMark &&
83 cat != UnicodeCategory.EnclosingMark) {
84 /* Finished the sequence */
90 return(str.Substring (index, count));
94 public static TextElementEnumerator GetTextElementEnumerator(string str)
96 if(str == null || str.Length == 0) {
97 throw new ArgumentNullException("string is null");
99 return(new TextElementEnumerator (str, 0));
102 public static TextElementEnumerator GetTextElementEnumerator(string str, int index)
105 throw new ArgumentNullException("string is null");
108 if(index < 0 || index >= str.Length) {
109 throw new ArgumentOutOfRangeException ("Index is not valid");
112 return(new TextElementEnumerator (str, index));
115 public static int[] ParseCombiningCharacters(string str)
118 throw new ArgumentNullException("string is null");
121 ArrayList indices = new ArrayList (str.Length);
122 TextElementEnumerator tee = GetTextElementEnumerator (str);
125 while(tee.MoveNext ()) {
126 indices.Add (tee.ElementIndex);
129 return((int[])indices.ToArray (typeof (int)));