From 878a878d5085fbf5f9738dccd6ec1df6e777196d Mon Sep 17 00:00:00 2001 From: Matthias Diester Date: Sat, 7 Nov 2020 22:06:04 +0100 Subject: [PATCH] Fix two byte character issue The colored string contains the context of the input string rune by rune with each rune having a respective color setting. However, the parsing transferred the input string byte by byte into the colored string. There is a problem for inputs that contain multi byte characters. Copying them required that the two byte characters are processed at once. Switch copy for look to work on a rune slice rather than bytes. --- parse.go | 8 ++++---- render.go | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/parse.go b/parse.go index 71fd438..fc09d93 100644 --- a/parse.go +++ b/parse.go @@ -68,8 +68,8 @@ func ParseString(input string, opts ...ParseOption) (*String, error) { fullMatchStart, fullMatchEnd := submatch[0], submatch[1] settingsStart, settingsEnd := submatch[2], submatch[3] - for i := pointer; i < fullMatchStart; i++ { - result = append(result, ColoredRune{rune(input[i]), current}) + for _, r := range input[pointer:fullMatchStart] { + result = append(result, ColoredRune{r, current}) } current, err = parseSelectGraphicRenditionEscapeSequence(input[settingsStart:settingsEnd]) @@ -81,8 +81,8 @@ func ParseString(input string, opts ...ParseOption) (*String, error) { } // Flush the remaining input string part into the result - for i := pointer; i < len(input); i++ { - result = append(result, ColoredRune{rune(input[i]), current}) + for _, r := range input[pointer:] { + result = append(result, ColoredRune{r, current}) } // Process optional parser options diff --git a/render.go b/render.go index 8a47a7f..53843fe 100644 --- a/render.go +++ b/render.go @@ -54,7 +54,7 @@ func (s String) String() string { current = coloredRune.Settings } - _ = buffer.WriteByte(byte(coloredRune.Symbol)) + _, _ = buffer.WriteRune(coloredRune.Symbol) } // Make sure to finish with a reset escape sequence