2 // System.Globalization.StringInfo.cs
5 // Dick Porter (dick@ximian.com)
7 // (C) 2002 Ximian, Inc.
8 // (C) 2004 Novell, Inc.
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Collections;
36 namespace System.Globalization {
40 [System.Runtime.InteropServices.ComVisible(true)]
42 public class StringInfo {
47 public static string GetNextTextElement(string str)
49 if(str == null || str.Length == 0) {
50 throw new ArgumentNullException("string is null");
52 return(GetNextTextElement (str, 0));
55 public static string GetNextTextElement(string str, int index)
58 throw new ArgumentNullException("string is null");
61 if(index < 0 || index >= str.Length) {
62 throw new ArgumentOutOfRangeException ("Index is not valid");
65 /* Find the next base character, surrogate
66 * pair or combining character sequence
70 UnicodeCategory cat = char.GetUnicodeCategory (ch);
72 if (cat == UnicodeCategory.Surrogate) {
73 /* Check that it's a high surrogate
74 * followed by a low surrogate
76 if (ch >= 0xD800 && ch <= 0xDBFF) {
77 if ((index + 1) < str.Length &&
78 str[index + 1] >= 0xDC00 &&
79 str[index + 1] <= 0xDFFF) {
80 /* A valid surrogate pair */
81 return(str.Substring (index, 2));
83 /* High surrogate on its own */
84 return(new String (ch, 1));
87 /* Low surrogate on its own */
88 return(new String (ch, 1));
91 /* Look for a base character, which
92 * may or may not be followed by a
93 * series of combining characters
96 if (cat == UnicodeCategory.NonSpacingMark ||
97 cat == UnicodeCategory.SpacingCombiningMark ||
98 cat == UnicodeCategory.EnclosingMark) {
99 /* Not a base character */
100 return(new String (ch, 1));
105 while (index + count < str.Length) {
106 cat = char.GetUnicodeCategory (str[index + count]);
107 if (cat != UnicodeCategory.NonSpacingMark &&
108 cat != UnicodeCategory.SpacingCombiningMark &&
109 cat != UnicodeCategory.EnclosingMark) {
110 /* Finished the sequence */
116 return(str.Substring (index, count));
120 public static TextElementEnumerator GetTextElementEnumerator(string str)
122 if(str == null || str.Length == 0) {
123 throw new ArgumentNullException("string is null");
125 return(new TextElementEnumerator (str, 0));
128 public static TextElementEnumerator GetTextElementEnumerator(string str, int index)
131 throw new ArgumentNullException("string is null");
134 if(index < 0 || index >= str.Length) {
135 throw new ArgumentOutOfRangeException ("Index is not valid");
138 return(new TextElementEnumerator (str, index));
141 public static int[] ParseCombiningCharacters(string str)
144 throw new ArgumentNullException("string is null");
147 ArrayList indices = new ArrayList (str.Length);
148 TextElementEnumerator tee = GetTextElementEnumerator (str);
151 while(tee.MoveNext ()) {
152 indices.Add (tee.ElementIndex);
155 return((int[])indices.ToArray (typeof (int)));