Skip to content

Commit

Permalink
Fix Encoding regression (#111320)
Browse files Browse the repository at this point in the history
* Fix Encoding regression

* Feedback addressing

* Fix the test
  • Loading branch information
tarekgh authored Jan 14, 2025
1 parent a05d34b commit 30834e4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,14 @@ internal unsafe bool AddChar(char ch)
return AddChar(ch, 1);
}


internal unsafe bool AddChar(char ch1, char ch2, int numBytes)
{
if (_chars is null)
{
_charCountResult += 2;
return true;
}

// Need room for 2 chars
if (_charEnd - _chars < 2)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ public static void TestDefaultEncodings()
Assert.Contains(mappedEncoding, CrossplatformDefaultEncodings().Union(CodePageInfo().Select(i => Map((int)i[0], (string)i[1]))));

TestRegister1252();
TestMultiBytesEncodingsSupportSurrogate();
}

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

private static void TestMultiBytesEncodingsSupportSurrogate()
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

Encoding encoding = Encoding.GetEncoding("GB18030");
Assert.NotNull(encoding);

string surrogatePair = "\uD840\uDE13"; // Surrogate Pair codepoint '𠈓' \U00020213
byte[] expectedBytes = new byte[] { 0x95, 0x32, 0xB7, 0x37 };

Assert.Equal(expectedBytes, encoding.GetBytes(surrogatePair));
Assert.Equal(expectedBytes.Length, encoding.GetByteCount(surrogatePair));

Assert.Equal(surrogatePair, encoding.GetString(expectedBytes));
Assert.Equal(surrogatePair.Length, encoding.GetCharCount(expectedBytes));
}

// This test is run as part of the default mappings test, since it modifies global state which that test
// depends on.
private static void TestRegister1252()
Expand Down

0 comments on commit 30834e4

Please sign in to comment.