Skip to content

Commit 30834e4

Browse files
authored
Fix Encoding regression (#111320)
* Fix Encoding regression * Feedback addressing * Fix the test
1 parent a05d34b commit 30834e4

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/libraries/System.Text.Encoding.CodePages/src/System/Text/EncodingCharBuffer.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,14 @@ internal unsafe bool AddChar(char ch)
6868
return AddChar(ch, 1);
6969
}
7070

71-
7271
internal unsafe bool AddChar(char ch1, char ch2, int numBytes)
7372
{
73+
if (_chars is null)
74+
{
75+
_charCountResult += 2;
76+
return true;
77+
}
78+
7479
// Need room for 2 chars
7580
if (_charEnd - _chars < 2)
7681
{

src/libraries/System.Text.Encoding.CodePages/tests/EncodingCodePages.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,7 @@ public static void TestDefaultEncodings()
519519
Assert.Contains(mappedEncoding, CrossplatformDefaultEncodings().Union(CodePageInfo().Select(i => Map((int)i[0], (string)i[1]))));
520520

521521
TestRegister1252();
522+
TestMultiBytesEncodingsSupportSurrogate();
522523
}
523524

524525
private static void ValidateDefaultEncodings()
@@ -639,6 +640,23 @@ public static void TestEncodingDisplayNames(int codePage, string webName, string
639640
Assert.All(name, c => Assert.True(c >= ' ' && c < '~' + 1, "Name: " + name + " contains character: " + c));
640641
}
641642

643+
private static void TestMultiBytesEncodingsSupportSurrogate()
644+
{
645+
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
646+
647+
Encoding encoding = Encoding.GetEncoding("GB18030");
648+
Assert.NotNull(encoding);
649+
650+
string surrogatePair = "\uD840\uDE13"; // Surrogate Pair codepoint '𠈓' \U00020213
651+
byte[] expectedBytes = new byte[] { 0x95, 0x32, 0xB7, 0x37 };
652+
653+
Assert.Equal(expectedBytes, encoding.GetBytes(surrogatePair));
654+
Assert.Equal(expectedBytes.Length, encoding.GetByteCount(surrogatePair));
655+
656+
Assert.Equal(surrogatePair, encoding.GetString(expectedBytes));
657+
Assert.Equal(surrogatePair.Length, encoding.GetCharCount(expectedBytes));
658+
}
659+
642660
// This test is run as part of the default mappings test, since it modifies global state which that test
643661
// depends on.
644662
private static void TestRegister1252()

0 commit comments

Comments
 (0)