widget: do not allow invalid utf8 in editor

This commit replaces invalid UTF8 codepoints with the replacement character
when they are inserted into the editor. This ensures that the editor never
moves the editing gap to an invalid location and reads its contents.

Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
This commit is contained in:
Chris Waldon
2022-11-14 09:53:10 -05:00
committed by Elias Naur
parent 4f5a6b3212
commit 5c84cf7e90
+6
View File
@@ -6,6 +6,8 @@ import (
"io"
"strings"
"unicode/utf8"
"golang.org/x/text/runes"
)
// editBuffer implements a gap buffer for text editing.
@@ -162,6 +164,10 @@ func (e *editBuffer) String() string {
}
func (e *editBuffer) prepend(caret int, s string) {
if !utf8.ValidString(s) {
s = runes.ReplaceIllFormed().String(s)
}
e.moveGap(caret, len(s))
copy(e.text[caret:], s)
e.gapstart += len(s)