mirror of
https://github.com/peco/peco.git
synced 2026-09-10 07:16:29 -04:00
Add bounds validation
This commit is contained in:
parent
717dfca53b
commit
136b4a80e9
|
|
@ -40,16 +40,20 @@ func (q *Text) SaveQuery() {
|
|||
func (q *Text) DeleteRange(start, end int) {
|
||||
q.mutex.Lock()
|
||||
defer q.mutex.Unlock()
|
||||
if start == -1 {
|
||||
if start < 0 {
|
||||
return
|
||||
}
|
||||
|
||||
l := len(q.query)
|
||||
if start > l {
|
||||
return
|
||||
}
|
||||
|
||||
if end > l {
|
||||
end = l
|
||||
}
|
||||
|
||||
if start > end {
|
||||
if start >= end {
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -94,7 +98,12 @@ func (q *Text) InsertAt(ch rune, where int) {
|
|||
q.mutex.Lock()
|
||||
defer q.mutex.Unlock()
|
||||
|
||||
if where == len(q.query) {
|
||||
l := len(q.query)
|
||||
if where < 0 || where > l {
|
||||
return
|
||||
}
|
||||
|
||||
if where == l {
|
||||
q.query = append(q.query, ch)
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -97,6 +97,9 @@ func TestQueryDeleteRange(t *testing.T) {
|
|||
{"delete all", "abcdef", 0, 6, ""},
|
||||
{"delete single char", "abcdef", 2, 3, "abdef"},
|
||||
{"start is -1 (no-op)", "abcdef", -1, 3, "abcdef"},
|
||||
{"start is negative (no-op)", "abcdef", -5, 3, "abcdef"},
|
||||
{"start beyond length (no-op)", "abcdef", 10, 15, "abcdef"},
|
||||
{"start equals end (no-op)", "abcdef", 3, 3, "abcdef"},
|
||||
{"start > end (no-op)", "abcdef", 4, 2, "abcdef"},
|
||||
{"end beyond length (clamped)", "abcdef", 4, 100, "abcd"},
|
||||
{"unicode delete", "あいうえお", 1, 3, "あえお"},
|
||||
|
|
@ -185,6 +188,8 @@ func TestQueryInsertAt(t *testing.T) {
|
|||
{"insert in middle", "hello", 'X', 2, "heXllo"},
|
||||
{"insert into empty", "", 'X', 0, "X"},
|
||||
{"insert unicode", "hello", 'あ', 2, "heあllo"},
|
||||
{"insert at negative index (no-op)", "hello", 'X', -1, "hello"},
|
||||
{"insert beyond length (no-op)", "hello", 'X', 10, "hello"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue