From 576ce0d67de5fcb0a4d7523c7517f1425093ee02 Mon Sep 17 00:00:00 2001 From: Shad Storhaug Date: Wed, 9 Oct 2019 18:43:05 +0700 Subject: [PATCH] ICU4N.Globalization.UChar: Renamed ToString() > ConvertFromUtf32(), GetCodePoint() > ConvertToUtf32() to match System.Char. Added overload of ConvertToUtf32(string, int). (#4) --- src/ICU4N/Globalization/UCharacter.cs | 65 ++++++++++++------- src/ICU4N/Impl/PunycodeExtension.cs | 8 +-- src/ICU4N/Impl/PunycodeExtension.tt | 2 +- .../Dev/Test/Collate/CollationIteratorTest.cs | 2 +- .../Dev/Test/Lang/UCharacterTest.cs | 14 ++-- tests/ICU4N.Tests/Dev/Test/Lang/UTF16Test.cs | 8 +-- .../Dev/Test/Normalizers/BasicTest.cs | 4 +- 7 files changed, 61 insertions(+), 42 deletions(-) diff --git a/src/ICU4N/Globalization/UCharacter.cs b/src/ICU4N/Globalization/UCharacter.cs index b3ea40581..c0a8b3e5a 100644 --- a/src/ICU4N/Globalization/UCharacter.cs +++ b/src/ICU4N/Globalization/UCharacter.cs @@ -3948,25 +3948,24 @@ public static bool IsUpper(string s, int index) // ICU4N specific overload to co /// this implementation differs in that it returns null rather than throwing exceptions if /// the input is not a valid code point. /// - /// Code point. + /// Code point. /// String representation of the code point, null if code point is not /// defined in unicode. /// /// ICU 2.1 - // ICU4N TODO: API - to cover Char, add overload for (string, int) - public static string ToString(int ch) // ICU4N TODO: API - Rename ConvertFromUtf32 to cover Char + public static string ConvertFromUtf32(int utf32) // ICU4N: Renamed from ToString to cover System.Char API { - if (ch < MinValue || ch > MaxValue) + if (utf32 < MinValue || utf32 > MaxValue) { return null; } - if (ch < SupplementaryMinValue) + if (utf32 < SupplementaryMinValue) { - return new string(new char[] { (char)ch }); + return new string(new char[] { (char)utf32 }); } - return new string(Character.ToChars(ch)); + return new string(Character.ToChars(utf32)); } /// @@ -4877,17 +4876,16 @@ internal static int GetPropertyValueEnumNoThrow(UProperty property, ICharSequenc /// /// Returns a code point corresponding to the two surrogate code units. /// - /// The lead char. - /// The trail char. + /// The lead char (high surrogate). + /// The trail char (low surrogate). /// Code point if surrogate characters are valid. /// Thrown when the code units do not form a valid code point. /// ICU 2.1 - public static int GetCodePoint(char lead, char trail) // ICU4N TODO: API - rename ConvertToUtf32 to match Char + public static int ConvertToUtf32(char highSurrogate, char lowSurrogate) // ICU4N specific - renamed from GetCodePoint() to match System.Char { - // ICU4N TODO: Perhaps we should just call char.ConvertToUtf32, since this is a duplicate of that functionality - if (char.IsSurrogatePair(lead, trail)) + if (char.IsSurrogatePair(highSurrogate, lowSurrogate)) { - return Character.ToCodePoint(lead, trail); + return Character.ToCodePoint(highSurrogate, lowSurrogate); } throw new ArgumentException("Illegal surrogate characters"); } @@ -4900,7 +4898,7 @@ public static int GetCodePoint(char lead, char trail) // ICU4N TODO: API - renam /// Code point if argument is a valid character. /// Thrown when the code units do not form a valid code point. /// ICU 2.1 - public static int GetCodePoint(char char16) // ICU4N TODO: API - rename ConvertToUtf32 to match Char + public static int ConvertToUtf32(char char16) // ICU4N specific - renamed from GetCodePoint() to match System.Char { if (UChar.IsLegal(char16)) { @@ -4909,6 +4907,27 @@ public static int GetCodePoint(char char16) // ICU4N TODO: API - rename ConvertT throw new ArgumentException("Illegal codepoint"); } + /// + /// + /// Returns the code point corresponding to the BMP code point. + /// + /// A . + /// The position of the character to evaluate in . + /// Code point if char at is a valid character. + /// Thrown when the code units do not form a valid code point. + /// ICU4N 60.1 + // ICU4N TODO: Tests + public static int ConvertToUtf32(string s, int index) // ICU4N specific overload to cover System.Char + { + if (s == null) + throw new ArgumentNullException(nameof(s)); + if (((uint)index) >= ((uint)s.Length)) + { + throw new ArgumentOutOfRangeException(nameof(index)); + } + return ConvertToUtf32(s[index]); + } + /// /// Returns the uppercase version of the argument string. /// Casing is dependent on the current culture and context-sensitive. @@ -5980,17 +5999,17 @@ public static string GetStringPropertyValue(UProperty propertyEnum, int codepoin { case UProperty.Age: return GetAge(codepoint).ToString(); case UProperty.ISO_Comment: return GetISOComment(codepoint); - case UProperty.Bidi_Mirroring_Glyph: return ToString(GetMirror(codepoint)); - case UProperty.Case_Folding: return ToString(FoldCase(codepoint, true)); - case UProperty.Lowercase_Mapping: return ToString(ToLower(codepoint)); + case UProperty.Bidi_Mirroring_Glyph: return ConvertFromUtf32(GetMirror(codepoint)); + case UProperty.Case_Folding: return ConvertFromUtf32(FoldCase(codepoint, true)); + case UProperty.Lowercase_Mapping: return ConvertFromUtf32(ToLower(codepoint)); case UProperty.Name: return GetName(codepoint); - case UProperty.Simple_Case_Folding: return ToString(FoldCase(codepoint, true)); - case UProperty.Simple_Lowercase_Mapping: return ToString(ToLower(codepoint)); - case UProperty.Simple_Titlecase_Mapping: return ToString(ToTitleCase(codepoint)); - case UProperty.Simple_Uppercase_Mapping: return ToString(ToUpper(codepoint)); - case UProperty.Titlecase_Mapping: return ToString(ToTitleCase(codepoint)); + case UProperty.Simple_Case_Folding: return ConvertFromUtf32(FoldCase(codepoint, true)); + case UProperty.Simple_Lowercase_Mapping: return ConvertFromUtf32(ToLower(codepoint)); + case UProperty.Simple_Titlecase_Mapping: return ConvertFromUtf32(ToTitleCase(codepoint)); + case UProperty.Simple_Uppercase_Mapping: return ConvertFromUtf32(ToUpper(codepoint)); + case UProperty.Titlecase_Mapping: return ConvertFromUtf32(ToTitleCase(codepoint)); case UProperty.Unicode_1_Name: return GetName1_0(codepoint); - case UProperty.Uppercase_Mapping: return ToString(ToUpper(codepoint)); + case UProperty.Uppercase_Mapping: return ConvertFromUtf32(ToUpper(codepoint)); } throw new ArgumentException("Illegal Property Enum"); } diff --git a/src/ICU4N/Impl/PunycodeExtension.cs b/src/ICU4N/Impl/PunycodeExtension.cs index f031fd681..e99d78f82 100644 --- a/src/ICU4N/Impl/PunycodeExtension.cs +++ b/src/ICU4N/Impl/PunycodeExtension.cs @@ -56,7 +56,7 @@ public static StringBuilder Encode(string src, bool[] caseFlags) { ++j; - n |= UChar.GetCodePoint(c, c2); + n |= UChar.ConvertToUtf32(c, c2); } else { @@ -209,7 +209,7 @@ public static StringBuilder Encode(StringBuilder src, bool[] caseFlags) { ++j; - n |= UChar.GetCodePoint(c, c2); + n |= UChar.ConvertToUtf32(c, c2); } else { @@ -362,7 +362,7 @@ public static StringBuilder Encode(char[] src, bool[] caseFlags) { ++j; - n |= UChar.GetCodePoint(c, c2); + n |= UChar.ConvertToUtf32(c, c2); } else { @@ -515,7 +515,7 @@ internal static StringBuilder Encode(ICharSequence src, bool[] caseFlags) { ++j; - n |= UChar.GetCodePoint(c, c2); + n |= UChar.ConvertToUtf32(c, c2); } else { diff --git a/src/ICU4N/Impl/PunycodeExtension.tt b/src/ICU4N/Impl/PunycodeExtension.tt index 324783648..383e2d297 100644 --- a/src/ICU4N/Impl/PunycodeExtension.tt +++ b/src/ICU4N/Impl/PunycodeExtension.tt @@ -76,7 +76,7 @@ namespace ICU4N.Impl { ++j; - n |= UChar.GetCodePoint(c, c2); + n |= UChar.ConvertToUtf32(c, c2); } else { diff --git a/tests/ICU4N.Tests.Collation/Dev/Test/Collate/CollationIteratorTest.cs b/tests/ICU4N.Tests.Collation/Dev/Test/Collate/CollationIteratorTest.cs index 765d1c11b..46fde8edd 100644 --- a/tests/ICU4N.Tests.Collation/Dev/Test/Collate/CollationIteratorTest.cs +++ b/tests/ICU4N.Tests.Collation/Dev/Test/Collate/CollationIteratorTest.cs @@ -184,7 +184,7 @@ public void TestMaxExpansion(/* char* par */) + " maximum expansion count == 1"); } - str = UChar.ToString(unassigned); + str = UChar.ConvertFromUtf32(unassigned); iter.SetText(str); temporder = iter.Previous(); diff --git a/tests/ICU4N.Tests/Dev/Test/Lang/UCharacterTest.cs b/tests/ICU4N.Tests/Dev/Test/Lang/UCharacterTest.cs index ac3560f4b..cc1038b00 100644 --- a/tests/ICU4N.Tests/Dev/Test/Lang/UCharacterTest.cs +++ b/tests/ICU4N.Tests/Dev/Test/Lang/UCharacterTest.cs @@ -1649,7 +1649,7 @@ public void TestCodePoint() { for (char j = (char)0xDC00; j <= 0xDFFF; j++) { - if (UChar.GetCodePoint(i, j) != ch) + if (UChar.ConvertToUtf32(i, j) != ch) { Errln("Error getting codepoint for surrogate " + "characters \\u" @@ -1661,7 +1661,7 @@ public void TestCodePoint() } try { - UChar.GetCodePoint((char)0xD7ff, (char)0xDC00); + UChar.ConvertToUtf32((char)0xD7ff, (char)0xDC00); Errln("Invalid surrogate characters should not form a " + "supplementary"); } @@ -1677,7 +1677,7 @@ public void TestCodePoint() // not a character try { - UChar.GetCodePoint(i); + UChar.ConvertToUtf32(i); Errln("Not a character is not a valid codepoint"); } catch (Exception e) @@ -1686,7 +1686,7 @@ public void TestCodePoint() } else { - if (UChar.GetCodePoint(i) != i) + if (UChar.ConvertToUtf32(i) != i) { Errln("A valid codepoint should return itself"); } @@ -3179,7 +3179,7 @@ public void TestToString() for (int i = 0; i < valid_tests.Length; i++) { - if (UChar.ToString(valid_tests[i]) == null) + if (UChar.ConvertFromUtf32(valid_tests[i]) == null) { Errln("UCharacter.toString(int) was not suppose to return " + "null because it was given a valid parameter. Value passed: " + @@ -3189,11 +3189,11 @@ public void TestToString() for (int i = 0; i < invalid_tests.Length; i++) { - if (UChar.ToString(invalid_tests[i]) != null) + if (UChar.ConvertFromUtf32(invalid_tests[i]) != null) { Errln("UCharacter.toString(int) was suppose to return " + "null because it was given an invalid parameter. Value passed: " + - invalid_tests[i] + ". Got: " + UChar.ToString(invalid_tests[i])); + invalid_tests[i] + ". Got: " + UChar.ConvertFromUtf32(invalid_tests[i])); } } } diff --git a/tests/ICU4N.Tests/Dev/Test/Lang/UTF16Test.cs b/tests/ICU4N.Tests/Dev/Test/Lang/UTF16Test.cs index f7ae5a4bb..056339e79 100644 --- a/tests/ICU4N.Tests/Dev/Test/Lang/UTF16Test.cs +++ b/tests/ICU4N.Tests/Dev/Test/Lang/UTF16Test.cs @@ -1028,7 +1028,7 @@ public void TestSetCharAt() [Test] public void TestValueOf() { - if (UChar.GetCodePoint('\ud800', '\udc00') != 0x10000) + if (UChar.ConvertToUtf32('\ud800', '\udc00') != 0x10000) { Errln("FAIL: getCodePoint('\ud800','\udc00')"); } @@ -1143,7 +1143,7 @@ public void TestIndexOf() // int testChar3 = 0xdc02; // int testChar4 = 0xd841; String test3 = "\ud841\udc02\u0071\udc02\ud841\u0071\ud841\udc02\u0071\u0072\ud841\udc02\u0071\ud841\udc02\u0071\udc02\ud841\u0073"; - String test4 = UChar.ToString(testChar2); + String test4 = UChar.ConvertFromUtf32(testChar2); if (UTF16.IndexOf(test1, test2, StringComparison.Ordinal) != 0 || UTF16.IndexOf(test1, test2, 0, StringComparison.Ordinal) != 0) @@ -1446,7 +1446,7 @@ public void TestIndexOf() if (UTF16.IndexOf(INDEXOF_SUPPLEMENTARY_STRING_, ch, index) != expected || UTF16.IndexOf(INDEXOF_SUPPLEMENTARY_STRING_, - UChar.ToString(ch), index, StringComparison.Ordinal) != + UChar.ConvertFromUtf32(ch), index, StringComparison.Ordinal) != expected) { Errln("Failed finding index for supplementary 0x" + @@ -1460,7 +1460,7 @@ public void TestIndexOf() if (UTF16.LastIndexOf(INDEXOF_SUPPLEMENTARY_STRING_, ch, index) != expected || UTF16.LastIndexOf(INDEXOF_SUPPLEMENTARY_STRING_, - UChar.ToString(ch), index, StringComparison.Ordinal) + UChar.ConvertFromUtf32(ch), index, StringComparison.Ordinal) != expected) { Errln("Failed finding last index for supplementary 0x" + diff --git a/tests/ICU4N.Tests/Dev/Test/Normalizers/BasicTest.cs b/tests/ICU4N.Tests/Dev/Test/Normalizers/BasicTest.cs index 7a0d9ae34..c552613ba 100644 --- a/tests/ICU4N.Tests/Dev/Test/Normalizers/BasicTest.cs +++ b/tests/ICU4N.Tests/Dev/Test/Normalizers/BasicTest.cs @@ -1268,7 +1268,7 @@ public void TestDebugIterOld() "' (" + Hex(ch) + ")" + " at index " + index); break; } - got.Append(UChar.ToString(ch)); + got.Append(UChar.ConvertFromUtf32(ch)); index++; } if (!expected.Equals(got.ToString())) @@ -1294,7 +1294,7 @@ public void TestDebugIterOld() + "' (" + Hex(ch) + ")" + " at index " + index); break; } - got.Append(UChar.ToString(ch)); + got.Append(UChar.ConvertFromUtf32(ch)); } if (!expectedReverse.Equals(got.ToString())) {