New test.
[mono.git] / mcs / class / corlib / System / Char.cs
index 4ba730a0aaeaeb43e682cb9e4df90a5d55e4b0ac..fc5e0de8e412cb89e8a4dc94bc089a1a8a179cc7 100644 (file)
@@ -117,10 +117,64 @@ namespace System
                                return -1;
                }
 
+               public static string ConvertFromUtf32 (int utf32)
+               {
+                       if (utf32 < 0 || utf32 > 0x10FFFF)
+                               throw new ArgumentOutOfRangeException ("utf32", "The argument must be from 0 to 0x10FFFF.");
+                       if (0xD800 <= utf32 && utf32 <= 0xDFFF)
+                               throw new ArgumentOutOfRangeException ("utf32", "The argument must not be in surrogate pair range.");
+                       if (utf32 < 0x10000)
+                               return new string ((char) utf32, 1);
+                       utf32 -= 0x10000;
+                       return new string (
+                               new char [] {(char) ((utf32 >> 10) + 0xD800),
+                               (char) (utf32 % 0x0400 + 0xDC00)});
+               }
+
+               public static int ConvertToUtf32 (char highSurrogate, char lowSurrogate)
+               {
+                       if (highSurrogate < 0xD800 || 0xDBFF < highSurrogate)
+                               throw new ArgumentOutOfRangeException ("highSurrogate");
+                       if (lowSurrogate < 0xDC00 || 0xDFFF < lowSurrogate)
+                               throw new ArgumentOutOfRangeException ("lowSurrogate");
+
+                       return 0x10000 + ((highSurrogate - 0xD800) << 10) + (lowSurrogate - 0xDC00);
+               }
+
+               public static int ConvertToUtf32 (string s, int index)
+               {
+                       if (s == null)
+                               throw new ArgumentNullException ("s");
+                       if (index < 0 || index >= s.Length)
+                               throw new ArgumentOutOfRangeException ("index");
+                       if (!Char.IsSurrogate (s [index]))
+                               return s [index];
+                       if (!Char.IsHighSurrogate (s [index])
+                           || index == s.Length - 1
+                           || !Char.IsLowSurrogate (s [index + 1]))
+                               throw new ArgumentException (String.Format ("The string contains invalid surrogate pair character at {0}", index));
+                       return ConvertToUtf32 (s [index], s [index + 1]);
+               }
+
                public bool Equals (char value)
                {
                        return m_value == value;
                }
+
+               public static bool IsSurrogatePair (char high, char low)
+               {
+                       return '\uD800' <= high && high <= '\uDBFF'
+                               && '\uDC00' <= low && low <= '\uDFFF';
+               }
+
+               public static bool IsSurrogatePair (string s, int index)
+               {
+                       if (s == null)
+                               throw new ArgumentNullException ("s");
+                       if (index < 0 || index >= s.Length)
+                               throw new ArgumentOutOfRangeException ("index");
+                       return index + 1 < s.Length && IsSurrogatePair (s [index], s [index + 1]);
+               }
 #endif
 
                public override int GetHashCode ()
@@ -214,6 +268,24 @@ namespace System
                        return IsDigit (str[index]);
                }
 
+#if NET_2_0
+               public static bool IsHighSurrogate (char c)
+               {
+                       return c >= '\uD800' && c <= '\uDBFF';
+               }
+
+               public static bool IsHighSurrogate (string s, int index)
+               {
+                       if (s == null) 
+                               throw new ArgumentNullException ("s");
+                       
+                       if (index < 0 || index >= s.Length)
+                               throw new ArgumentOutOfRangeException ("index");
+                       
+                       return IsHighSurrogate (s [index]);
+               }
+#endif
+
                public static bool IsLetter (char c)
                {
                        unsafe {
@@ -292,6 +364,24 @@ namespace System
                        return IsLower (str[index]);
                }
 
+#if NET_2_0
+               public static bool IsLowSurrogate (char c)
+               {
+                       return c >= '\uDC00' && c <= '\uDFFF';
+               }
+
+               public static bool IsLowSurrogate (string s, int index)
+               {
+                       if (s == null) 
+                               throw new ArgumentNullException ("s");
+                       
+                       if (index < 0 || index >= s.Length)
+                               throw new ArgumentOutOfRangeException ("index");
+                       
+                       return IsLowSurrogate (s [index]);
+               }
+#endif
+
                public static bool IsNumber (char c)
                {
                        unsafe {
@@ -480,6 +570,19 @@ namespace System
                        return IsWhiteSpace (str[index]);
                }
 
+#if NET_2_0
+               public static bool TryParse (string s, out char result)
+               {
+                       if (s == null || s.Length != 1) {
+                               result = (char) 0;
+                               return false;
+                       }
+
+                       result = s [0];
+                       return true;
+               }
+#endif
+
                public static char Parse (string str)
                {
                        if (str == null) 
@@ -636,11 +739,6 @@ namespace System
                        throw new InvalidCastException();
                }
                
-               string IConvertible.ToString (IFormatProvider provider)
-               {
-                       return ToString(provider);
-               }
-
                ushort IConvertible.ToUInt16 (IFormatProvider provider)
                {
                        return System.Convert.ToUInt16(m_value);