Skip to content

[release/9.0] Fix Encoding regression #111367

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading