From 77fbb68a8058fa54561daa6a2b1579003a4422b0 Mon Sep 17 00:00:00 2001 From: Norman Dankert Date: Fri, 7 Feb 2025 23:31:54 +0100 Subject: [PATCH] Fix handling of surrogate pairs in length calculation --- PSReadLine/Render.Helper.cs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/PSReadLine/Render.Helper.cs b/PSReadLine/Render.Helper.cs index 8fc56bea1..8fa9ca642 100644 --- a/PSReadLine/Render.Helper.cs +++ b/PSReadLine/Render.Helper.cs @@ -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; @@ -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;