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;
35 using System.Runtime.InteropServices;
37 namespace System.Globalization {
41 public class StringInfo {
43 public StringInfo () : this (string.Empty)
50 public StringInfo (string value)
52 // Argument check in property
57 public override bool Equals (object value)
59 StringInfo other = value as StringInfo;
60 return other != null && s == other.s;
64 public override int GetHashCode ()
66 return s.GetHashCode ();
69 public int LengthInTextElements {
73 for (int idx = 0; idx < s.Length; length++)
74 idx += GetNextTextElementLength (s, idx);
80 public string String {
84 throw new ArgumentNullException ("value");
90 public string SubstringByTextElements (int startingTextElement)
92 if (startingTextElement < 0 || s.Length == 0)
93 throw new ArgumentOutOfRangeException ("startingTextElement");
95 for (int i = 0; i < startingTextElement; i++) {
97 throw new ArgumentOutOfRangeException ("startingTextElement");
98 idx += GetNextTextElementLength (s, idx);
100 return s.Substring (idx);
103 public string SubstringByTextElements (int startingTextElement, int lengthInTextElements)
105 if (startingTextElement < 0 || s.Length == 0)
106 throw new ArgumentOutOfRangeException ("startingTextElement");
107 if (lengthInTextElements < 0)
108 throw new ArgumentOutOfRangeException ("lengthInTextElements");
110 for (int i = 0; i < startingTextElement; i++) {
112 throw new ArgumentOutOfRangeException ("startingTextElement");
113 idx += GetNextTextElementLength (s, idx);
116 for (int i = 0; i < lengthInTextElements; i++) {
118 throw new ArgumentOutOfRangeException ("lengthInTextElements");
119 idx += GetNextTextElementLength (s, idx);
121 return s.Substring (start, idx - start);
124 public static string GetNextTextElement(string str)
126 if(str == null || str.Length == 0) {
127 throw new ArgumentNullException("string is null");
129 return(GetNextTextElement (str, 0));
132 public static string GetNextTextElement(string str, int index)
134 int len = GetNextTextElementLength (str, index);
135 return len != 1 ? str.Substring (index, len) : new string (str [index], 1);
138 static int GetNextTextElementLength(string str, int index)
141 throw new ArgumentNullException("string is null");
144 if(index >= str.Length)
147 throw new ArgumentOutOfRangeException ("Index is not valid");
149 /* Find the next base character, surrogate
150 * pair or combining character sequence
153 char ch = str[index];
154 UnicodeCategory cat = char.GetUnicodeCategory (ch);
156 if (cat == UnicodeCategory.Surrogate) {
157 /* Check that it's a high surrogate
158 * followed by a low surrogate
160 if (ch >= 0xD800 && ch <= 0xDBFF) {
161 if ((index + 1) < str.Length &&
162 str[index + 1] >= 0xDC00 &&
163 str[index + 1] <= 0xDFFF) {
164 /* A valid surrogate pair */
167 /* High surrogate on its own */
171 /* Low surrogate on its own */
175 /* Look for a base character, which
176 * may or may not be followed by a
177 * series of combining characters
180 if (cat == UnicodeCategory.NonSpacingMark ||
181 cat == UnicodeCategory.SpacingCombiningMark ||
182 cat == UnicodeCategory.EnclosingMark) {
183 /* Not a base character */
189 while (index + count < str.Length) {
190 cat = char.GetUnicodeCategory (str[index + count]);
191 if (cat != UnicodeCategory.NonSpacingMark &&
192 cat != UnicodeCategory.SpacingCombiningMark &&
193 cat != UnicodeCategory.EnclosingMark) {
194 /* Finished the sequence */
204 public static TextElementEnumerator GetTextElementEnumerator(string str)
206 if(str == null || str.Length == 0) {
207 throw new ArgumentNullException("string is null");
209 return(new TextElementEnumerator (str, 0));
212 public static TextElementEnumerator GetTextElementEnumerator(string str, int index)
215 throw new ArgumentNullException("string is null");
218 if(index < 0 || index >= str.Length) {
219 throw new ArgumentOutOfRangeException ("Index is not valid");
222 return(new TextElementEnumerator (str, index));
225 public static int[] ParseCombiningCharacters(string str)
228 throw new ArgumentNullException("string is null");
231 ArrayList indices = new ArrayList (str.Length);
232 TextElementEnumerator tee = GetTextElementEnumerator (str);
235 while(tee.MoveNext ()) {
236 indices.Add (tee.ElementIndex);
239 return((int[])indices.ToArray (typeof (int)));