Drop more obsolete defines
[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 using System.Runtime.InteropServices;
36
37 namespace System.Globalization {
38
39         [Serializable]
40         [ComVisible(true)]
41         public class StringInfo {
42                 public StringInfo()
43                 {
44                 }
45
46                 string s;
47                 int length;
48
49                 public StringInfo (string value)
50                 {
51                         // Argument check in property
52                         String = value;
53                 }
54
55                 [ComVisible (false)]
56                 public override bool Equals (object value)
57                 {
58                         StringInfo other = value as StringInfo;
59                         return other != null && s == other.s;
60                 }
61
62                 [ComVisible (false)]
63                 public override int GetHashCode ()
64                 {
65                         return s.GetHashCode ();
66                 }
67
68                 public int LengthInTextElements {
69                         get {
70                                 if (length < 0) {
71                                         length = 0;
72                                         for (int idx = 0; idx < s.Length; length++)
73                                                 idx += GetNextTextElementLength (s, idx);
74                                 }
75                                 return length;
76                         }
77                 }
78
79                 public string String {
80                         get { return s; }
81                         set {
82                                 if (value == null)
83                                         throw new ArgumentNullException ("value");
84                                 length = -1;
85                                 s = value;
86                         }
87                 }
88
89                 public string SubstringByTextElements (int startingTextElement)
90                 {
91                         if (startingTextElement < 0 || s.Length == 0)
92                                 throw new ArgumentOutOfRangeException ("startingTextElement");
93                         int idx = 0;
94                         for (int i = 0; i < startingTextElement; i++) {
95                                 if (idx >= s.Length)
96                                         throw new ArgumentOutOfRangeException ("startingTextElement");
97                                 idx += GetNextTextElementLength (s, idx);
98                         }
99                         return s.Substring (idx);
100                 }
101
102                 public string SubstringByTextElements (int startingTextElement, int lengthInTextElements)
103                 {
104                         if (startingTextElement < 0 || s.Length == 0)
105                                 throw new ArgumentOutOfRangeException ("startingTextElement");
106                         if (lengthInTextElements < 0)
107                                 throw new ArgumentOutOfRangeException ("lengthInTextElements");
108                         int idx = 0;
109                         for (int i = 0; i < startingTextElement; i++) {
110                                 if (idx >= s.Length)
111                                         throw new ArgumentOutOfRangeException ("startingTextElement");
112                                 idx += GetNextTextElementLength (s, idx);
113                         }
114                         int start = idx;
115                         for (int i = 0; i < lengthInTextElements; i++) {
116                                 if (idx >= s.Length)
117                                         throw new ArgumentOutOfRangeException ("lengthInTextElements");
118                                 idx += GetNextTextElementLength (s, idx);
119                         }
120                         return s.Substring (start, idx - start);
121                 }
122
123                 public static string GetNextTextElement(string str)
124                 {
125                         if(str == null || str.Length == 0) {
126                                 throw new ArgumentNullException("string is null");
127                         }
128                         return(GetNextTextElement (str, 0));
129                 }
130
131                 public static string GetNextTextElement(string str, int index)
132                 {
133                         int len = GetNextTextElementLength (str, index);
134                         return len != 1 ? str.Substring (index, len) : new string (str [index], 1);
135                 }
136                 
137                 static int GetNextTextElementLength(string str, int index)
138                 {
139                         if(str == null) {
140                                 throw new ArgumentNullException("string is null");
141                         }
142
143                         if(index >= str.Length)
144                                 return 0;
145                         if(index < 0)
146                                 throw new ArgumentOutOfRangeException ("Index is not valid");
147
148                         /* Find the next base character, surrogate
149                          * pair or combining character sequence
150                          */
151
152                         char ch = str[index];
153                         UnicodeCategory cat = char.GetUnicodeCategory (ch);
154
155                         if (cat == UnicodeCategory.Surrogate) {
156                                 /* Check that it's a high surrogate
157                                  * followed by a low surrogate
158                                  */
159                                 if (ch >= 0xD800 && ch <= 0xDBFF) {
160                                         if ((index + 1) < str.Length &&
161                                             str[index + 1] >= 0xDC00 &&
162                                             str[index + 1] <= 0xDFFF) {
163                                                 /* A valid surrogate pair */
164                                                 return 2;
165                                         } else {
166                                                 /* High surrogate on its own */
167                                                 return 1;
168                                         }
169                                 } else {
170                                         /* Low surrogate on its own */
171                                         return 1;
172                                 }
173                         } else {
174                                 /* Look for a base character, which
175                                  * may or may not be followed by a
176                                  * series of combining characters
177                                  */
178
179                                 if (cat == UnicodeCategory.NonSpacingMark ||
180                                     cat == UnicodeCategory.SpacingCombiningMark ||
181                                     cat == UnicodeCategory.EnclosingMark) {
182                                         /* Not a base character */
183                                         return 1;
184                                 }
185                                 
186                                 int count = 1;
187
188                                 while (index + count < str.Length) {
189                                         cat = char.GetUnicodeCategory (str[index + count]);
190                                         if (cat != UnicodeCategory.NonSpacingMark &&
191                                             cat != UnicodeCategory.SpacingCombiningMark &&
192                                             cat != UnicodeCategory.EnclosingMark) {
193                                                 /* Finished the sequence */
194                                                 break;
195                                         }
196                                         count++;
197                                 }
198
199                                 return count;
200                         }
201                 }
202
203                 public static TextElementEnumerator GetTextElementEnumerator(string str)
204                 {
205                         if(str == null || str.Length == 0) {
206                                 throw new ArgumentNullException("string is null");
207                         }
208                         return(new TextElementEnumerator (str, 0));
209                 }
210
211                 public static TextElementEnumerator GetTextElementEnumerator(string str, int index)
212                 {
213                         if(str == null) {
214                                 throw new ArgumentNullException("string is null");
215                         }
216
217                         if(index < 0 || index >= str.Length) {
218                                 throw new ArgumentOutOfRangeException ("Index is not valid");
219                         }
220                         
221                         return(new TextElementEnumerator (str, index));
222                 }
223                 
224                 public static int[] ParseCombiningCharacters(string str)
225                 {
226                         if(str == null) {
227                                 throw new ArgumentNullException("string is null");
228                         }
229
230                         ArrayList indices = new ArrayList (str.Length);
231                         TextElementEnumerator tee = GetTextElementEnumerator (str);
232
233                         tee.Reset ();
234                         while(tee.MoveNext ()) {
235                                 indices.Add (tee.ElementIndex);
236                         }
237
238                         return((int[])indices.ToArray (typeof (int)));
239                 }
240         }
241 }