Author: atsushi
Date: 2006-07-24 05:17:17 -0400 (Mon, 24 Jul 2006)
New Revision: 62909
Modified:
trunk/mcs/class/corlib/System/ChangeLog
trunk/mcs/class/corlib/System/Char.cs
trunk/mcs/class/corlib/Test/System/ChangeLog
trunk/mcs/class/corlib/Test/System/CharTest.cs
Log:
2006-07-24 Atsushi Enomoto <[EMAIL PROTECTED]>
* Char.cs : implemented utf32 conversion methods thus fixed bug #78856.
* CharTest.cs : added tests for utf32 conversion methods.
Modified: trunk/mcs/class/corlib/System/ChangeLog
===================================================================
--- trunk/mcs/class/corlib/System/ChangeLog 2006-07-24 07:56:09 UTC (rev
62908)
+++ trunk/mcs/class/corlib/System/ChangeLog 2006-07-24 09:17:17 UTC (rev
62909)
@@ -1,3 +1,7 @@
+2006-07-24 Atsushi Enomoto <[EMAIL PROTECTED]>
+
+ * Char.cs : implemented utf32 conversion methods thus fixed bug #78856.
+
2006-07-19 John Luke <[EMAIL PROTECTED]>
* TermInfoDriver.cs: switch order of alt and control when
Modified: trunk/mcs/class/corlib/System/Char.cs
===================================================================
--- trunk/mcs/class/corlib/System/Char.cs 2006-07-24 07:56:09 UTC (rev
62908)
+++ trunk/mcs/class/corlib/System/Char.cs 2006-07-24 09:17:17 UTC (rev
62909)
@@ -117,10 +117,64 @@
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 ()
Modified: trunk/mcs/class/corlib/Test/System/ChangeLog
===================================================================
--- trunk/mcs/class/corlib/Test/System/ChangeLog 2006-07-24 07:56:09 UTC
(rev 62908)
+++ trunk/mcs/class/corlib/Test/System/ChangeLog 2006-07-24 09:17:17 UTC
(rev 62909)
@@ -1,3 +1,7 @@
+2006-07-24 Atsushi Enomoto <[EMAIL PROTECTED]>
+
+ * CharTest.cs : added tests for utf32 conversion methods.
+
2006-07-19 Korn�l P�l <[EMAIL PROTECTED]>
* StringTest.cs: Added some more TestSbytePtrConstructorNegative tests
Modified: trunk/mcs/class/corlib/Test/System/CharTest.cs
===================================================================
--- trunk/mcs/class/corlib/Test/System/CharTest.cs 2006-07-24 07:56:09 UTC
(rev 62908)
+++ trunk/mcs/class/corlib/Test/System/CharTest.cs 2006-07-24 09:17:17 UTC
(rev 62909)
@@ -521,6 +521,49 @@
Assert(c1.GetTypeCode().Equals(TypeCode.Char));
}
+#if NET_2_0
+ public void TestConvertFromUtf32 ()
+ {
+ AssertEquals ("#1", "A", Char.ConvertFromUtf32 (0x41));
+ AssertEquals ("#2", "\uD800\uDC00", Char.ConvertFromUtf32
(0x10000));
+ }
+
+ [ExpectedException (typeof (ArgumentOutOfRangeException))]
+ public void TestConvertFromUtf32Fail1 ()
+ {
+ Char.ConvertFromUtf32 (-1);
+ }
+
+ [ExpectedException (typeof (ArgumentOutOfRangeException))]
+ public void TestConvertFromUtf32Fail2 ()
+ {
+ Char.ConvertFromUtf32 (0x110001);
+ }
+
+ [ExpectedException (typeof (ArgumentOutOfRangeException))]
+ public void TestConvertFromUtf32Fail3 ()
+ {
+ Char.ConvertFromUtf32 (0xD800);
+ }
+
+ public void TestConvertToUtf32 ()
+ {
+ AssertEquals ("#1", 0x10000, Char.ConvertToUtf32 ('\uD800',
'\uDC00'));
+ AssertEquals ("#2", 0x10FFFF, Char.ConvertToUtf32 ('\uDBFF',
'\uDFFF'));
+ }
+
+ [ExpectedException (typeof (ArgumentOutOfRangeException))]
+ public void TestConvertToUtf32Fail1 ()
+ {
+ Char.ConvertToUtf32 ('A', '\uDC00');
+ }
+
+ [ExpectedException (typeof (ArgumentOutOfRangeException))]
+ public void TestConvertUtf32Fail2 ()
+ {
+ Char.ConvertToUtf32 ('\uD800', '\uD800');
+ }
+#endif
}
}
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches