Skip to content

Commit 99fd84d

Browse files
authored
toml: fix hex values starting with a, e or E and comments ending with crlf (#12367)
1 parent 3fdbfca commit 99fd84d

4 files changed

Lines changed: 34 additions & 6 deletions

File tree

vlib/toml/checker/checker.v

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,6 @@ fn (c Checker) check_number(num ast.Number) ? {
141141
is_oct = lit_sans_sign.starts_with('0o')
142142
is_hex = lit_sans_sign.starts_with('0x')
143143

144-
third := lit[2]
145-
if third in scanner.digit_extras {
146-
ascii = byte(third).ascii_str()
147-
return error(@MOD + '.' + @STRUCT + '.' + @FN +
148-
' numbers like "$lit" (hex, octal and binary) can not have `$ascii` in ...${c.excerpt(num.pos)}...')
149-
}
150144
lit_sans_sign_and_type_prefix := lit_sans_sign[2..]
151145

152146
if lit_sans_sign_and_type_prefix.starts_with('_')

vlib/toml/scanner/scanner.v

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,12 @@ pub fn (s &Scanner) at() byte {
273273
return byte(-1)
274274
}
275275

276+
// at_crlf returns `true` if the scanner is at a `\r` character
277+
// and the next character is a `\n`.
278+
fn (s Scanner) at_crlf() bool {
279+
return s.at() == `\r` && s.peek(1) == `\n`
280+
}
281+
276282
// peek returns the character code from the input text at position + `n`.
277283
// peek returns `-1` if it can't peek `n` characters ahead.
278284
[direct_array_access; inline]
@@ -322,6 +328,10 @@ fn (mut s Scanner) ignore_line() ?string {
322328
for c := s.at(); c != -1 && c != `\n`; c = s.at() {
323329
s.next()
324330
util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'skipping "${byte(c).ascii_str()}"')
331+
if s.at_crlf() {
332+
util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'letting `\\r\\n` slip through')
333+
return s.text[start..s.pos]
334+
}
325335
}
326336
return s.text[start..s.pos]
327337
}

vlib/toml/tests/crlf_test.v

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import toml
2+
3+
fn test_crlf() {
4+
str_value := 'test string'
5+
mut toml_txt := 'crlf_string = "test string"
6+
# Comment with CRLF\r\n'
7+
toml_doc := toml.parse(toml_txt) or { panic(err) }
8+
9+
value := toml_doc.value('crlf_string')
10+
assert value == toml.Any(str_value)
11+
assert value as string == str_value
12+
assert value.string() == str_value
13+
}

vlib/toml/tests/types_test.v

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,14 @@ open_sourced = "Jun 22 2019 20:20:28"'
6868
value := toml_doc.value('v.open_sourced').string()
6969
assert value == 'Jun 22 2019 20:20:28'
7070
}
71+
72+
fn test_hex_values() {
73+
// Regression test
74+
// '0xb' is carefully chosen to include the 'b' character that also denotes binary via 0b prefix.
75+
toml_txt := 'hex = 0xb'
76+
toml_doc := toml.parse(toml_txt) or { panic(err) }
77+
78+
value := toml_doc.value('hex')
79+
assert value as i64 == 11
80+
assert value.i64() == 11
81+
}

0 commit comments

Comments
 (0)