Skip to content

Fix handling of surrogate pairs in length calculation #4462

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
16 changes: 14 additions & 2 deletions PSReadLine/Render.Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,13 @@ internal static int LengthInBufferCells(string str, int start, int end)
for (var i = start; i < end; i++)
{
var c = str[i];
if (c == 0x1b && (i+1) < end && str[i+1] == '[')
if (char.IsHighSurrogate(c) && (i + 1) < end && char.IsLowSurrogate(str[i + 1]))
{
sum++;
i++; // Skip the low surrogate
continue;
}
else if (c == 0x1b && (i + 1) < end && str[i + 1] == '[')
{
// Simple escape sequence skipping
i += 2;
Expand All @@ -77,7 +83,13 @@ internal static int LengthInBufferCells(StringBuilder sb, int start, int end)
for (var i = start; i < end; i++)
{
var c = sb[i];
if (c == 0x1b && (i + 1) < end && sb[i + 1] == '[')
if (char.IsHighSurrogate(c) && (i + 1) < end && char.IsLowSurrogate(sb[i + 1]))
{
sum++;
i++; // Skip the low surrogate
continue;
}
else if (c == 0x1b && (i + 1) < end && sb[i + 1] == '[')
{
// Simple escape sequence skipping
i += 2;
Expand Down