Skip to content

Commit

Permalink
Fix Unicode BiDi Category (dotnet#55790)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarekgh committed Jul 16, 2021
1 parent fb2e673 commit 62fa1f8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ private static void Parse(List<CharUnicodeInfoTestCase> testCases, string line)
string charName = data[1];
string charCategoryString = data[2];
string numericValueString = data[8];
StrongBidiCategory bidiCategory = data[4] == "L" ? StrongBidiCategory.StrongLeftToRight :
data[4] == "R" || data[4] == "AL" ? StrongBidiCategory.StrongRightToLeft : StrongBidiCategory.Other;

int codePoint = int.Parse(charValueString, NumberStyles.HexNumber);
Parse(testCases, codePoint, charCategoryString, numericValueString);
Parse(testCases, codePoint, charCategoryString, numericValueString, bidiCategory);

if (charName.EndsWith("First>"))
{
Expand All @@ -59,7 +61,7 @@ private static void Parse(List<CharUnicodeInfoTestCase> testCases, string line)
{
// Assumes that all code points in the range have the same numeric value
// and general category
Parse(testCases, rangeCodePoint, charCategoryString, numericValueString);
Parse(testCases, rangeCodePoint, charCategoryString, numericValueString, bidiCategory);
}
}
}
Expand Down Expand Up @@ -99,7 +101,7 @@ private static void Parse(List<CharUnicodeInfoTestCase> testCases, string line)
["Lu"] = UnicodeCategory.UppercaseLetter
};

private static void Parse(List<CharUnicodeInfoTestCase> testCases, int codePoint, string charCategoryString, string numericValueString)
private static void Parse(List<CharUnicodeInfoTestCase> testCases, int codePoint, string charCategoryString, string numericValueString, StrongBidiCategory bidiCategory)
{
string codeValueRepresentation = codePoint > char.MaxValue ? char.ConvertFromUtf32(codePoint) : ((char)codePoint).ToString();
double numericValue = ParseNumericValueString(numericValueString);
Expand All @@ -110,7 +112,8 @@ private static void Parse(List<CharUnicodeInfoTestCase> testCases, int codePoint
Utf32CodeValue = codeValueRepresentation,
GeneralCategory = generalCategory,
NumericValue = numericValue,
CodePoint = codePoint
CodePoint = codePoint,
BidiCategory = bidiCategory
});
}

Expand Down Expand Up @@ -141,11 +144,19 @@ private static double ParseNumericValueString(string numericValueString)
}
}

public enum StrongBidiCategory
{
Other = 0x00,
StrongLeftToRight = 0x20,
StrongRightToLeft = 0x40,
}

public class CharUnicodeInfoTestCase
{
public string Utf32CodeValue { get; set; }
public int CodePoint { get; set; }
public UnicodeCategory GeneralCategory { get; set; }
public double NumericValue { get; set; }
public StrongBidiCategory BidiCategory { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using Xunit;
using System.Reflection;

namespace System.Globalization.Tests
{
Expand All @@ -12,6 +13,9 @@ public partial class CharUnicodeInfoTests
[Fact]
public void GetUnicodeCategory()
{
MethodInfo GetBidiCategory = Type.GetType("System.Globalization.CharUnicodeInfo").GetMethod("GetBidiCategoryNoBoundsChecks", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance | BindingFlags.InvokeMethod);
object [] parameters = new object [] { 0 };

foreach (CharUnicodeInfoTestCase testCase in CharUnicodeInfoTestData.TestCases)
{
if (testCase.Utf32CodeValue.Length == 1)
Expand All @@ -22,6 +26,9 @@ public void GetUnicodeCategory()
// Test the string overload for a surrogate pair or a single char
GetUnicodeCategoryTest_String(testCase.Utf32CodeValue, new UnicodeCategory[] { testCase.GeneralCategory });
Assert.Equal(testCase.GeneralCategory, CharUnicodeInfo.GetUnicodeCategory(testCase.CodePoint));

parameters[0] = (uint)testCase.CodePoint;
Assert.Equal(testCase.BidiCategory, (StrongBidiCategory)GetBidiCategory.Invoke(null, parameters));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace System.Globalization
internal enum StrongBidiCategory
{
Other = 0x00,
StrongLeftToRight = 0x10,
StrongRightToLeft = 0x20,
StrongLeftToRight = 0x20,
StrongRightToLeft = 0x40,
}
}

0 comments on commit 62fa1f8

Please sign in to comment.