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
{
39 public class StringInfo
{
44 public static string GetNextTextElement(string str
)
46 if(str
== null || str
.Length
== 0) {
47 throw new ArgumentNullException("string is null");
49 return(GetNextTextElement (str
, 0));
52 public static string GetNextTextElement(string str
, int index
)
55 throw new ArgumentNullException("string is null");
58 if(index
< 0 || index
>= str
.Length
) {
59 throw new ArgumentOutOfRangeException ("Index is not valid");
62 /* Find the next base character, surrogate
63 * pair or combining character sequence
67 UnicodeCategory cat
= char.GetUnicodeCategory (ch
);
69 if (cat
== UnicodeCategory
.Surrogate
) {
70 /* Check that it's a high surrogate
71 * followed by a low surrogate
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));
80 /* High surrogate on its own */
81 return(new String (ch
, 1));
84 /* Low surrogate on its own */
85 return(new String (ch
, 1));
88 /* Look for a base character, which
89 * may or may not be followed by a
90 * series of combining characters
93 if (cat
== UnicodeCategory
.NonSpacingMark
||
94 cat
== UnicodeCategory
.SpacingCombiningMark
||
95 cat
== UnicodeCategory
.EnclosingMark
) {
96 /* Not a base character */
97 return(new String (ch
, 1));
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 */
113 return(str
.Substring (index
, count
));
117 public static TextElementEnumerator
GetTextElementEnumerator(string str
)
119 if(str
== null || str
.Length
== 0) {
120 throw new ArgumentNullException("string is null");
122 return(new TextElementEnumerator (str
, 0));
125 public static TextElementEnumerator
GetTextElementEnumerator(string str
, int index
)
128 throw new ArgumentNullException("string is null");
131 if(index
< 0 || index
>= str
.Length
) {
132 throw new ArgumentOutOfRangeException ("Index is not valid");
135 return(new TextElementEnumerator (str
, index
));
138 public static int[] ParseCombiningCharacters(string str
)
141 throw new ArgumentNullException("string is null");
144 ArrayList indices
= new ArrayList (str
.Length
);
145 TextElementEnumerator tee
= GetTextElementEnumerator (str
);
148 while(tee
.MoveNext ()) {
149 indices
.Add (tee
.ElementIndex
);
152 return((int[])indices
.ToArray (typeof (int)));