Skip to content

Commit

Permalink
ICU4N.Globalization.UChar: Renamed ToString() > ConvertFromUtf32(), G…
Browse files Browse the repository at this point in the history
…etCodePoint() > ConvertToUtf32() to match System.Char. Added overload of ConvertToUtf32(string, int). (#4)
  • Loading branch information
NightOwl888 committed Oct 9, 2019
1 parent 4040a98 commit 576ce0d
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 42 deletions.
65 changes: 42 additions & 23 deletions src/ICU4N/Globalization/UCharacter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
/// </summary>
/// <param name="ch">Code point.</param>
/// <param name="utf32">Code point.</param>
/// <returns>String representation of the code point, null if code point is not
/// defined in unicode.
/// </returns>
/// <stable>ICU 2.1</stable>
// 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));
}

/// <summary>
Expand Down Expand Up @@ -4877,17 +4876,16 @@ internal static int GetPropertyValueEnumNoThrow(UProperty property, ICharSequenc
/// <summary>
/// Returns a code point corresponding to the two surrogate code units.
/// </summary>
/// <param name="lead">The lead char.</param>
/// <param name="trail">The trail char.</param>
/// <param name="highSurrogate">The lead char (high surrogate).</param>
/// <param name="lowSurrogate">The trail char (low surrogate).</param>
/// <returns>Code point if surrogate characters are valid.</returns>
/// <exception cref="ArgumentException">Thrown when the code units do not form a valid code point.</exception>
/// <stable>ICU 2.1</stable>
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");
}
Expand All @@ -4900,7 +4898,7 @@ public static int GetCodePoint(char lead, char trail) // ICU4N TODO: API - renam
/// <returns>Code point if argument is a valid character.</returns>
/// <exception cref="ArgumentException">Thrown when the code units do not form a valid code point.</exception>
/// <stable>ICU 2.1</stable>
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))
{
Expand All @@ -4909,6 +4907,27 @@ public static int GetCodePoint(char char16) // ICU4N TODO: API - rename ConvertT
throw new ArgumentException("Illegal codepoint");
}

/// <icu/>
/// <summary>
/// Returns the code point corresponding to the BMP code point.
/// </summary>
/// <param name="s">A <see cref="string"/>.</param>
/// <param name="index">The position of the character to evaluate in <paramref name="s"/>.</param>
/// <returns>Code point if char at <paramref name="index"/> is a valid character.</returns>
/// <exception cref="ArgumentException">Thrown when the code units do not form a valid code point.</exception>
/// <draft>ICU4N 60.1</draft>
// 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]);
}

/// <summary>
/// Returns the uppercase version of the argument string.
/// Casing is dependent on the current culture and context-sensitive.
Expand Down Expand Up @@ -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");
}
Expand Down
8 changes: 4 additions & 4 deletions src/ICU4N/Impl/PunycodeExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static StringBuilder Encode(string src, bool[] caseFlags)
{
++j;

n |= UChar.GetCodePoint(c, c2);
n |= UChar.ConvertToUtf32(c, c2);
}
else
{
Expand Down Expand Up @@ -209,7 +209,7 @@ public static StringBuilder Encode(StringBuilder src, bool[] caseFlags)
{
++j;

n |= UChar.GetCodePoint(c, c2);
n |= UChar.ConvertToUtf32(c, c2);
}
else
{
Expand Down Expand Up @@ -362,7 +362,7 @@ public static StringBuilder Encode(char[] src, bool[] caseFlags)
{
++j;

n |= UChar.GetCodePoint(c, c2);
n |= UChar.ConvertToUtf32(c, c2);
}
else
{
Expand Down Expand Up @@ -515,7 +515,7 @@ internal static StringBuilder Encode(ICharSequence src, bool[] caseFlags)
{
++j;

n |= UChar.GetCodePoint(c, c2);
n |= UChar.ConvertToUtf32(c, c2);
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/ICU4N/Impl/PunycodeExtension.tt
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ namespace ICU4N.Impl
{
++j;

n |= UChar.GetCodePoint(c, c2);
n |= UChar.ConvertToUtf32(c, c2);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
14 changes: 7 additions & 7 deletions tests/ICU4N.Tests/Dev/Test/Lang/UCharacterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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");
}
Expand All @@ -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)
Expand All @@ -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");
}
Expand Down Expand Up @@ -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: " +
Expand All @@ -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]));
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions tests/ICU4N.Tests/Dev/Test/Lang/UTF16Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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')");
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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" +
Expand All @@ -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" +
Expand Down
4 changes: 2 additions & 2 deletions tests/ICU4N.Tests/Dev/Test/Normalizers/BasicTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand All @@ -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()))
{
Expand Down

0 comments on commit 576ce0d

Please sign in to comment.