Skip to content

Commit

Permalink
strings.textscanner: fix off-by-one error in skip method (#21045)
Browse files Browse the repository at this point in the history
  • Loading branch information
kimshrier authored Mar 17, 2024
1 parent 549654a commit d7cc6f5
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion vlib/strings/textscanner/textscanner.v
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub fn (mut ss TextScanner) next() int {
// `skip()` does not return a result.
@[inline]
pub fn (mut ss TextScanner) skip() {
if ss.pos + 1 < ss.ilen {
if ss.pos < ss.ilen {
ss.pos++
}
}
Expand Down
26 changes: 26 additions & 0 deletions vlib/strings/textscanner/textscanner_test.js.v
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,39 @@ fn test_skip() {
s.skip()
assert s.next() == `c`
assert s.next() == -1

s.reset()
assert s.peek() == `a`
s.skip()
assert s.peek() == `b`
s.skip()
assert s.peek() == `c`
s.skip()
assert s.peek() == -1
}

fn test_skip_n() {
mut s := textscanner.new('abc')
s.skip_n(2)
assert s.next() == `c`
assert s.next() == -1

s.reset()
assert s.peek() == `a`
s.skip_n(2)
assert s.peek() == `c`
s.skip_n(2)
assert s.peek() == -1

s.reset()
assert s.peek() == `a`
s.skip_n(3)
assert s.peek() == -1

s.reset()
assert s.peek() == `a`
s.skip_n(4)
assert s.peek() == -1
}

fn test_peek() {
Expand Down
26 changes: 26 additions & 0 deletions vlib/strings/textscanner/textscanner_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,39 @@ fn test_skip() {
s.skip()
assert s.next() == `c`
assert s.next() == -1

s.reset()
assert s.peek() == `a`
s.skip()
assert s.peek() == `b`
s.skip()
assert s.peek() == `c`
s.skip()
assert s.peek() == -1
}

fn test_skip_n() {
mut s := textscanner.new('abc')
s.skip_n(2)
assert s.next() == `c`
assert s.next() == -1

s.reset()
assert s.peek() == `a`
s.skip_n(2)
assert s.peek() == `c`
s.skip_n(2)
assert s.peek() == -1

s.reset()
assert s.peek() == `a`
s.skip_n(3)
assert s.peek() == -1

s.reset()
assert s.peek() == `a`
s.skip_n(4)
assert s.peek() == -1
}

fn test_peek() {
Expand Down

0 comments on commit d7cc6f5

Please sign in to comment.