diff --git a/query/query.go b/query/query.go index 5d0178a..7959317 100644 --- a/query/query.go +++ b/query/query.go @@ -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 } diff --git a/query/query_test.go b/query/query_test.go index eb2968c..78f9e42 100644 --- a/query/query_test.go +++ b/query/query_test.go @@ -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) {