New test.
[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 #if NET_2_0
40         [System.Runtime.InteropServices.ComVisible(true)]
41 #endif
42         public class StringInfo {
43                 public StringInfo()
44                 {
45                 }
46
47                 public static string GetNextTextElement(string str)
48                 {
49                         if(str == null || str.Length == 0) {
50                                 throw new ArgumentNullException("string is null");
51                         }
52                         return(GetNextTextElement (str, 0));
53                 }
54
55                 public static string GetNextTextElement(string str, int index)
56                 {
57                         if(str == null) {
58                                 throw new ArgumentNullException("string is null");
59                         }
60
61                         if(index < 0 || index >= str.Length) {
62                                 throw new ArgumentOutOfRangeException ("Index is not valid");
63                         }
64
65                         /* Find the next base character, surrogate
66                          * pair or combining character sequence
67                          */
68
69                         char ch = str[index];
70                         UnicodeCategory cat = char.GetUnicodeCategory (ch);
71
72                         if (cat == UnicodeCategory.Surrogate) {
73                                 /* Check that it's a high surrogate
74                                  * followed by a low surrogate
75                                  */
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));
82                                         } else {
83                                                 /* High surrogate on its own */
84                                                 return(new String (ch, 1));
85                                         }
86                                 } else {
87                                         /* Low surrogate on its own */
88                                         return(new String (ch, 1));
89                                 }
90                         } else {
91                                 /* Look for a base character, which
92                                  * may or may not be followed by a
93                                  * series of combining characters
94                                  */
95
96                                 if (cat == UnicodeCategory.NonSpacingMark ||
97                                     cat == UnicodeCategory.SpacingCombiningMark ||
98                                     cat == UnicodeCategory.EnclosingMark) {
99                                         /* Not a base character */
100                                         return(new String (ch, 1));
101                                 }
102                                 
103                                 int count = 1;
104
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 */
111                                                 break;
112                                         }
113                                         count++;
114                                 }
115
116                                 return(str.Substring (index, count));
117                         }
118                 }
119
120                 public static TextElementEnumerator GetTextElementEnumerator(string str)
121                 {
122                         if(str == null || str.Length == 0) {
123                                 throw new ArgumentNullException("string is null");
124                         }
125                         return(new TextElementEnumerator (str, 0));
126                 }
127
128                 public static TextElementEnumerator GetTextElementEnumerator(string str, int index)
129                 {
130                         if(str == null) {
131                                 throw new ArgumentNullException("string is null");
132                         }
133
134                         if(index < 0 || index >= str.Length) {
135                                 throw new ArgumentOutOfRangeException ("Index is not valid");
136                         }
137                         
138                         return(new TextElementEnumerator (str, index));
139                 }
140                 
141                 public static int[] ParseCombiningCharacters(string str)
142                 {
143                         if(str == null) {
144                                 throw new ArgumentNullException("string is null");
145                         }
146
147                         ArrayList indices = new ArrayList (str.Length);
148                         TextElementEnumerator tee = GetTextElementEnumerator (str);
149
150                         tee.Reset ();
151                         while(tee.MoveNext ()) {
152                                 indices.Add (tee.ElementIndex);
153                         }
154
155                         return((int[])indices.ToArray (typeof (int)));
156                 }
157         }
158 }