toml.scanner: make end_of_text, at, next and peek return u32 (#13998)
parent
fa66183f43
commit
1938bc48e7
|
@ -10,7 +10,7 @@ import toml.util
|
||||||
|
|
||||||
pub const (
|
pub const (
|
||||||
digit_extras = [`_`, `.`, `x`, `o`, `b`, `e`, `E`]
|
digit_extras = [`_`, `.`, `x`, `o`, `b`, `e`, `E`]
|
||||||
end_of_text = -1
|
end_of_text = math.max_u32
|
||||||
)
|
)
|
||||||
|
|
||||||
// Scanner contains the necessary fields for the state of the scan process.
|
// Scanner contains the necessary fields for the state of the scan process.
|
||||||
|
@ -263,7 +263,7 @@ pub fn (s &Scanner) remaining() int {
|
||||||
// next returns the next character code from the input text.
|
// next returns the next character code from the input text.
|
||||||
// next returns `end_of_text` if it can't reach the next character.
|
// next returns `end_of_text` if it can't reach the next character.
|
||||||
[direct_array_access; inline]
|
[direct_array_access; inline]
|
||||||
pub fn (mut s Scanner) next() int {
|
pub fn (mut s Scanner) next() u32 {
|
||||||
if s.pos < s.text.len {
|
if s.pos < s.text.len {
|
||||||
opos := s.pos
|
opos := s.pos
|
||||||
s.pos++
|
s.pos++
|
||||||
|
@ -299,7 +299,7 @@ pub fn (mut s Scanner) skip_n(n int) {
|
||||||
// at returns `end_of_text` if it can't get the current character.
|
// at returns `end_of_text` if it can't get the current character.
|
||||||
// unlike `next()`, `at()` does not change the state of the scanner.
|
// unlike `next()`, `at()` does not change the state of the scanner.
|
||||||
[direct_array_access; inline]
|
[direct_array_access; inline]
|
||||||
pub fn (s &Scanner) at() int {
|
pub fn (s &Scanner) at() u32 {
|
||||||
if s.pos < s.text.len {
|
if s.pos < s.text.len {
|
||||||
return s.text[s.pos]
|
return s.text[s.pos]
|
||||||
}
|
}
|
||||||
|
@ -315,7 +315,7 @@ fn (s Scanner) at_crlf() bool {
|
||||||
// peek returns the character code from the input text at position + `n`.
|
// peek returns the character code from the input text at position + `n`.
|
||||||
// peek returns `end_of_text` if it can't peek `n` characters ahead.
|
// peek returns `end_of_text` if it can't peek `n` characters ahead.
|
||||||
[direct_array_access; inline]
|
[direct_array_access; inline]
|
||||||
pub fn (s &Scanner) peek(n int) int {
|
pub fn (s &Scanner) peek(n int) u32 {
|
||||||
if s.pos + n < s.text.len {
|
if s.pos + n < s.text.len {
|
||||||
// Allow peeking back - needed for spaces between date and time in RFC 3339 format :/
|
// Allow peeking back - needed for spaces between date and time in RFC 3339 format :/
|
||||||
if n - 1 < 0 && s.pos + n - 1 >= 0 {
|
if n - 1 < 0 && s.pos + n - 1 >= 0 {
|
||||||
|
|
|
@ -25,9 +25,9 @@ fn test_next() {
|
||||||
assert s.next() == `a`
|
assert s.next() == `a`
|
||||||
assert s.next() == `b`
|
assert s.next() == `b`
|
||||||
assert s.next() == `c`
|
assert s.next() == `c`
|
||||||
assert s.next() == -1
|
assert s.next() == scanner.end_of_text
|
||||||
assert s.next() == -1
|
assert s.next() == scanner.end_of_text
|
||||||
assert s.next() == -1
|
assert s.next() == scanner.end_of_text
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_skip() {
|
fn test_skip() {
|
||||||
|
@ -35,14 +35,14 @@ fn test_skip() {
|
||||||
assert s.next() == `a`
|
assert s.next() == `a`
|
||||||
s.skip()
|
s.skip()
|
||||||
assert s.next() == `c`
|
assert s.next() == `c`
|
||||||
assert s.next() == -1
|
assert s.next() == scanner.end_of_text
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_skip_n() {
|
fn test_skip_n() {
|
||||||
mut s := scanner.new_scanner(input: scan_input) or { panic(err) }
|
mut s := scanner.new_scanner(input: scan_input) or { panic(err) }
|
||||||
s.skip_n(2)
|
s.skip_n(2)
|
||||||
assert s.next() == `c`
|
assert s.next() == `c`
|
||||||
assert s.next() == -1
|
assert s.next() == scanner.end_of_text
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_at() {
|
fn test_at() {
|
||||||
|
@ -54,7 +54,7 @@ fn test_at() {
|
||||||
assert s.next() == `a`
|
assert s.next() == `a`
|
||||||
assert s.next() == `b`
|
assert s.next() == `b`
|
||||||
assert s.next() == `c`
|
assert s.next() == `c`
|
||||||
assert s.next() == -1
|
assert s.next() == scanner.end_of_text
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_peek() {
|
fn test_peek() {
|
||||||
|
@ -62,13 +62,13 @@ fn test_peek() {
|
||||||
assert s.peek(0) == `a`
|
assert s.peek(0) == `a`
|
||||||
assert s.peek(1) == `b`
|
assert s.peek(1) == `b`
|
||||||
assert s.peek(2) == `c`
|
assert s.peek(2) == `c`
|
||||||
assert s.peek(3) == -1
|
assert s.peek(3) == scanner.end_of_text
|
||||||
assert s.peek(4) == -1
|
assert s.peek(4) == scanner.end_of_text
|
||||||
//
|
//
|
||||||
assert s.next() == `a`
|
assert s.next() == `a`
|
||||||
assert s.next() == `b`
|
assert s.next() == `b`
|
||||||
assert s.next() == `c`
|
assert s.next() == `c`
|
||||||
assert s.next() == -1
|
assert s.next() == scanner.end_of_text
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_reset() {
|
fn test_reset() {
|
||||||
|
@ -76,7 +76,7 @@ fn test_reset() {
|
||||||
assert s.next() == `a`
|
assert s.next() == `a`
|
||||||
s.next()
|
s.next()
|
||||||
s.next()
|
s.next()
|
||||||
assert s.next() == -1
|
assert s.next() == scanner.end_of_text
|
||||||
s.reset()
|
s.reset()
|
||||||
assert s.next() == `a`
|
assert s.next() == `a`
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue