-
Notifications
You must be signed in to change notification settings - Fork 326
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
Changes from 3 commits
77fbb68
02f3b4c
69bee70
744a742
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
| } | ||
|
Comment on lines
+60
to
+65
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't look right. It effectively counts every surrogate pair as 1 cell in terminal display, but that's not the case for almost all surrogate pairs. For example, 😀 and 👉 are both 2-cell width. I intended to rewrite the length calculation using the lib Wcwidth and rewrite the cursor movement code using
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The root cause lies in The '❌' emoji is the Unicode point |
||
| else if (c == 0x1b && (i + 1) < end && str[i + 1] == '[') | ||
| { | ||
| // Simple escape sequence skipping | ||
| i += 2; | ||
|
|
@@ -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])) | ||
|
andyleejordan marked this conversation as resolved.
Outdated
|
||
| { | ||
| sum++; | ||
| i++; // Skip the low surrogate | ||
| continue; | ||
| } | ||
| else if (c == 0x1b && (i + 1) < end && sb[i + 1] == '[') | ||
| { | ||
| // Simple escape sequence skipping | ||
| i += 2; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.