2005-01-31 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / corlib / System.Globalization / StringInfo.cs
1 //
2 // System.Globalization.StringInfo.cs
3 //
4 // Author:
5 //      Dick Porter (dick@ximian.com)
6 //
7 // (C) 2002 Ximian, Inc.
8 // (C) 2004 Novell, Inc.
9 //
10
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
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:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
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.
32 //
33
34 using System.Collections;
35
36 namespace System.Globalization {
37
38         [Serializable]
39         public class StringInfo {
40                 public StringInfo()
41                 {
42                 }
43
44                 public static string GetNextTextElement(string str)
45                 {
46                         if(str == null || str.Length == 0) {
47                                 throw new ArgumentNullException("string is null");
48                         }
49                         return(GetNextTextElement (str, 0));
50                 }
51
52                 public static string GetNextTextElement(string str, int index)
53                 {
54                         if(str == null) {
55                                 throw new ArgumentNullException("string is null");
56                         }
57
58                         if(index < 0 || index >= str.Length) {
59                                 throw new ArgumentOutOfRangeException ("Index is not valid");
60                         }
61
62                         /* Find the next base character, surrogate
63                          * pair or combining character sequence
64                          */
65
66                         char ch = str[index];
67                         UnicodeCategory cat = char.GetUnicodeCategory (ch);
68
69                         if (cat == UnicodeCategory.Surrogate) {
70                                 /* Check that it's a high surrogate
71                                  * followed by a low surrogate
72                                  */
73                                 if (ch >= 0xD800 && ch <= 0xDBFF) {
74                                         if ((index + 1) < str.Length &&
75                                             str[index + 1] >= 0xDC00 &&
76                                             str[index + 1] <= 0xDFFF) {
77                                                 /* A valid surrogate pair */
78                                                 return(str.Substring (index, 2));
79                                         } else {
80                                                 /* High surrogate on its own */
81                                                 return(new String (ch, 1));
82                                         }
83                                 } else {
84                                         /* Low surrogate on its own */
85                                         return(new String (ch, 1));
86                                 }
87                         } else {
88                                 /* Look for a base character, which
89                                  * may or may not be followed by a
90                                  * series of combining characters
91                                  */
92
93                                 if (cat == UnicodeCategory.NonSpacingMark ||
94                                     cat == UnicodeCategory.SpacingCombiningMark ||
95                                     cat == UnicodeCategory.EnclosingMark) {
96                                         /* Not a base character */
97                                         return(new String (ch, 1));
98                                 }
99                                 
100                                 int count = 1;
101
102                                 while (index + count < str.Length) {
103                                         cat = char.GetUnicodeCategory (str[index + count]);
104                                         if (cat != UnicodeCategory.NonSpacingMark &&
105                                             cat != UnicodeCategory.SpacingCombiningMark &&
106                                             cat != UnicodeCategory.EnclosingMark) {
107                                                 /* Finished the sequence */
108                                                 break;
109                                         }
110                                         count++;
111                                 }
112
113                                 return(str.Substring (index, count));
114                         }
115                 }
116
117                 public static TextElementEnumerator GetTextElementEnumerator(string str)
118                 {
119                         if(str == null || str.Length == 0) {
120                                 throw new ArgumentNullException("string is null");
121                         }
122                         return(new TextElementEnumerator (str, 0));
123                 }
124
125                 public static TextElementEnumerator GetTextElementEnumerator(string str, int index)
126                 {
127                         if(str == null) {
128                                 throw new ArgumentNullException("string is null");
129                         }
130
131                         if(index < 0 || index >= str.Length) {
132                                 throw new ArgumentOutOfRangeException ("Index is not valid");
133                         }
134                         
135                         return(new TextElementEnumerator (str, index));
136                 }
137                 
138                 public static int[] ParseCombiningCharacters(string str)
139                 {
140                         if(str == null) {
141                                 throw new ArgumentNullException("string is null");
142                         }
143
144                         ArrayList indices = new ArrayList (str.Length);
145                         TextElementEnumerator tee = GetTextElementEnumerator (str);
146
147                         tee.Reset ();
148                         while(tee.MoveNext ()) {
149                                 indices.Add (tee.ElementIndex);
150                         }
151
152                         return((int[])indices.ToArray (typeof (int)));
153                 }
154         }
155 }