Skip to content

Commit 62fa1f8

Browse files
authored
Fix Unicode BiDi Category (dotnet#55790)
1 parent fb2e673 commit 62fa1f8

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

src/libraries/System.Globalization/tests/System/Globalization/CharUnicodeInfoTestData.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,11 @@ private static void Parse(List<CharUnicodeInfoTestCase> testCases, string line)
4444
string charName = data[1];
4545
string charCategoryString = data[2];
4646
string numericValueString = data[8];
47+
StrongBidiCategory bidiCategory = data[4] == "L" ? StrongBidiCategory.StrongLeftToRight :
48+
data[4] == "R" || data[4] == "AL" ? StrongBidiCategory.StrongRightToLeft : StrongBidiCategory.Other;
4749

4850
int codePoint = int.Parse(charValueString, NumberStyles.HexNumber);
49-
Parse(testCases, codePoint, charCategoryString, numericValueString);
51+
Parse(testCases, codePoint, charCategoryString, numericValueString, bidiCategory);
5052

5153
if (charName.EndsWith("First>"))
5254
{
@@ -59,7 +61,7 @@ private static void Parse(List<CharUnicodeInfoTestCase> testCases, string line)
5961
{
6062
// Assumes that all code points in the range have the same numeric value
6163
// and general category
62-
Parse(testCases, rangeCodePoint, charCategoryString, numericValueString);
64+
Parse(testCases, rangeCodePoint, charCategoryString, numericValueString, bidiCategory);
6365
}
6466
}
6567
}
@@ -99,7 +101,7 @@ private static void Parse(List<CharUnicodeInfoTestCase> testCases, string line)
99101
["Lu"] = UnicodeCategory.UppercaseLetter
100102
};
101103

102-
private static void Parse(List<CharUnicodeInfoTestCase> testCases, int codePoint, string charCategoryString, string numericValueString)
104+
private static void Parse(List<CharUnicodeInfoTestCase> testCases, int codePoint, string charCategoryString, string numericValueString, StrongBidiCategory bidiCategory)
103105
{
104106
string codeValueRepresentation = codePoint > char.MaxValue ? char.ConvertFromUtf32(codePoint) : ((char)codePoint).ToString();
105107
double numericValue = ParseNumericValueString(numericValueString);
@@ -110,7 +112,8 @@ private static void Parse(List<CharUnicodeInfoTestCase> testCases, int codePoint
110112
Utf32CodeValue = codeValueRepresentation,
111113
GeneralCategory = generalCategory,
112114
NumericValue = numericValue,
113-
CodePoint = codePoint
115+
CodePoint = codePoint,
116+
BidiCategory = bidiCategory
114117
});
115118
}
116119

@@ -141,11 +144,19 @@ private static double ParseNumericValueString(string numericValueString)
141144
}
142145
}
143146

147+
public enum StrongBidiCategory
148+
{
149+
Other = 0x00,
150+
StrongLeftToRight = 0x20,
151+
StrongRightToLeft = 0x40,
152+
}
153+
144154
public class CharUnicodeInfoTestCase
145155
{
146156
public string Utf32CodeValue { get; set; }
147157
public int CodePoint { get; set; }
148158
public UnicodeCategory GeneralCategory { get; set; }
149159
public double NumericValue { get; set; }
160+
public StrongBidiCategory BidiCategory { get; set; }
150161
}
151162
}

src/libraries/System.Globalization/tests/System/Globalization/CharUnicodeInfoTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using Xunit;
5+
using System.Reflection;
56

67
namespace System.Globalization.Tests
78
{
@@ -12,6 +13,9 @@ public partial class CharUnicodeInfoTests
1213
[Fact]
1314
public void GetUnicodeCategory()
1415
{
16+
MethodInfo GetBidiCategory = Type.GetType("System.Globalization.CharUnicodeInfo").GetMethod("GetBidiCategoryNoBoundsChecks", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance | BindingFlags.InvokeMethod);
17+
object [] parameters = new object [] { 0 };
18+
1519
foreach (CharUnicodeInfoTestCase testCase in CharUnicodeInfoTestData.TestCases)
1620
{
1721
if (testCase.Utf32CodeValue.Length == 1)
@@ -22,6 +26,9 @@ public void GetUnicodeCategory()
2226
// Test the string overload for a surrogate pair or a single char
2327
GetUnicodeCategoryTest_String(testCase.Utf32CodeValue, new UnicodeCategory[] { testCase.GeneralCategory });
2428
Assert.Equal(testCase.GeneralCategory, CharUnicodeInfo.GetUnicodeCategory(testCase.CodePoint));
29+
30+
parameters[0] = (uint)testCase.CodePoint;
31+
Assert.Equal(testCase.BidiCategory, (StrongBidiCategory)GetBidiCategory.Invoke(null, parameters));
2532
}
2633
}
2734

src/libraries/System.Private.CoreLib/src/System/Globalization/StrongBidiCategory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace System.Globalization
1010
internal enum StrongBidiCategory
1111
{
1212
Other = 0x00,
13-
StrongLeftToRight = 0x10,
14-
StrongRightToLeft = 0x20,
13+
StrongLeftToRight = 0x20,
14+
StrongRightToLeft = 0x40,
1515
}
1616
}

0 commit comments

Comments
 (0)