Skip to content

Commit 52df19e

Browse files
authored
toml: check for invalid placement of underscores around exponent (#12303)
1 parent f14dabc commit 52df19e

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

vlib/toml/checker/checker.v

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ fn has_repeating(str string, repeats []rune) bool {
6161

6262
fn (c Checker) check_number(num ast.Number) ? {
6363
lit := num.text
64+
lit_lower_case := lit.to_lower()
6465
if lit in ['0', '0.0', '+0', '-0', '+0.0', '-0.0', '0e0', '+0e0', '-0e0', '0e00'] {
6566
return
6667
}
@@ -78,8 +79,8 @@ fn (c Checker) check_number(num ast.Number) ? {
7879

7980
mut hex_bin_oct := is_hex_bin_oct(lit)
8081
mut is_bin, mut is_oct, mut is_hex := false, false, false
81-
is_float := lit.to_lower().all_before('e').contains('.')
82-
has_exponent_notation := lit.to_lower().contains('e')
82+
is_float := lit_lower_case.all_before('e').contains('.')
83+
has_exponent_notation := lit_lower_case.contains('e')
8384
float_decimal_index := lit.index('.') or { -1 }
8485
// mut is_first_digit := byte(lit[0]).is_digit()
8586
mut ascii := byte(lit[0]).ascii_str()
@@ -160,15 +161,16 @@ fn (c Checker) check_number(num ast.Number) ? {
160161
}
161162

162163
if has_exponent_notation {
163-
if lit.to_lower().all_after('e').starts_with('_') {
164+
if lit_lower_case.all_after('e').starts_with('_')
165+
|| lit_lower_case.all_before('e').ends_with('_') {
164166
return error(@MOD + '.' + @STRUCT + '.' + @FN +
165-
' the exponent in "$lit" can not start with an underscore in ...${c.excerpt(num.pos)}...')
167+
' the exponent in "$lit" can not start nor end with an underscore in ...${c.excerpt(num.pos)}...')
166168
}
167-
if lit.to_lower().all_after('e').contains('.') {
169+
if lit_lower_case.all_after('e').contains('.') {
168170
return error(@MOD + '.' + @STRUCT + '.' + @FN +
169171
' numbers like "$lit" (with exponent) can not have a decimal point in ...${c.excerpt(num.pos)}...')
170172
}
171-
if !is_hex && lit.to_lower().count('e') > 1 {
173+
if !is_hex && lit_lower_case.count('e') > 1 {
172174
return error(@MOD + '.' + @STRUCT + '.' + @FN +
173175
' numbers like "$lit" (with exponent) can only have one exponent in ...${c.excerpt(num.pos)}...')
174176
}
@@ -189,9 +191,9 @@ fn (c Checker) check_number(num ast.Number) ? {
189191
return error(@MOD + '.' + @STRUCT + '.' + @FN +
190192
' numbers like "$lit" (float) can not have underscores before or after the decimal point in ...${c.excerpt(num.pos)}...')
191193
}
192-
if lit.contains('e.') || lit.contains('.e') || lit.contains('E.') || lit.contains('.E') {
194+
if lit_lower_case.contains('e.') || lit.contains('.e') {
193195
return error(@MOD + '.' + @STRUCT + '.' + @FN +
194-
' numbers like "$lit" (float) can not have underscores before or after the decimal point in ...${c.excerpt(num.pos)}...')
196+
' numbers like "$lit" (float) can not have decimal points on either side of the exponent notation in ...${c.excerpt(num.pos)}...')
195197
}
196198
} else {
197199
if lit.len > 1 && lit.starts_with('0') && lit[1] !in [`b`, `o`, `x`] {

vlib/toml/tests/burntsushi.toml-test_test.v

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ const (
1919
'string/basic-out-of-range-unicode-escape-1.toml',
2020
'string/basic-out-of-range-unicode-escape-2.toml',
2121
'string/bad-uni-esc.toml',
22-
// Float
23-
'float/trailing-us-exp.toml',
2422
// Encoding
2523
'encoding/bad-utf8-in-comment.toml',
2624
'encoding/bad-utf8-in-string.toml',

0 commit comments

Comments
 (0)