commit 4764c14

Alexey Yerin  ·  2021-05-17 17:35:53 +0000 UTC
parent e906b7e
ui/editor: add boundary checks for word actions

Prior to this, if the input is spaced out (whitespace skip), word
movements caused senpai to crash because there were no boundary checks.
1 files changed,  +3, -3
+3, -3
 1@@ -128,7 +128,7 @@ func (e *Editor) RemWord() (ok bool) {
 2 	// Hello world|
 3 	// Hello |
 4 	// |
 5-	for line[e.cursorIdx-1] == ' ' {
 6+	for e.cursorIdx > 0 && line[e.cursorIdx-1] == ' ' {
 7 		e.remRuneAt(e.cursorIdx - 1)
 8 		e.Left()
 9 	}
10@@ -186,7 +186,7 @@ func (e *Editor) RightWord() {
11 		return
12 	}
13 
14-	for line[e.cursorIdx] == ' ' {
15+	for e.cursorIdx < len(line) && line[e.cursorIdx] == ' ' {
16 		e.Right()
17 	}
18 	for i := e.cursorIdx; i < len(line) && line[i] != ' '; i += 1 {
19@@ -214,7 +214,7 @@ func (e *Editor) LeftWord() {
20 
21 	line := e.text[e.lineIdx]
22 
23-	for line[e.cursorIdx-1] == ' ' {
24+	for e.cursorIdx > 0 && line[e.cursorIdx-1] == ' ' {
25 		e.Left()
26 	}
27 	for i := e.cursorIdx - 1; i >= 0 && line[i] != ' '; i -= 1 {