toml: add better float validation (#12640)
parent
f7926ec9a4
commit
9cf7af0c75
|
@ -210,6 +210,14 @@ fn (c Checker) check_number(num ast.Number) ? {
|
|||
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
||||
' numbers like "$lit" (float) can not have decimal points on either side of the exponent notation in ...${c.excerpt(num.pos)}...')
|
||||
}
|
||||
// Check if it contains other chars than the allowed
|
||||
for r in lit {
|
||||
if r !in [`0`, `1`, `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `.`, `e`, `E`, `-`, `+`,
|
||||
`_`] {
|
||||
return error(@MOD + '.' + @STRUCT + '.' + @FN +
|
||||
' numbers like "$lit" (float) can not contain `${byte(r).ascii_str()}` in ...${c.excerpt(num.pos)}...')
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if lit.len > 1 && lit.starts_with('0') && lit[1] !in [`b`, `o`, `x`] {
|
||||
ascii = byte(lit[0]).ascii_str()
|
||||
|
|
|
@ -17,15 +17,12 @@ const (
|
|||
]
|
||||
invalid_exceptions = [
|
||||
'invalid/string-bad-line-ending-escape.toml',
|
||||
'invalid/float-no-suffix.toml',
|
||||
]
|
||||
|
||||
valid_value_exceptions = [
|
||||
'valid/unicode-escape.toml',
|
||||
// These have correct values, and should've passed, but the format of arrays is *mixed* in the JSON ??
|
||||
'valid/example2.toml',
|
||||
// Float
|
||||
'valid/float-exponent.toml',
|
||||
]
|
||||
|
||||
// These have correct values, and should've passed as-is, but the format of arrays changes in the JSON ??
|
||||
|
@ -259,14 +256,16 @@ fn to_alexcrichton(value ast.Value, array_type int) string {
|
|||
return '{ "type": "null", "value": "$json_text" }'
|
||||
}
|
||||
ast.Number {
|
||||
if value.text.contains('inf') || value.text.contains('nan') {
|
||||
text := value.text
|
||||
if text.contains('inf') || text.contains('nan') {
|
||||
return '{ "type": "float", "value": "$value.text" }'
|
||||
}
|
||||
if !value.text.starts_with('0x')
|
||||
&& (value.text.contains('.') || value.text.to_lower().contains('e')) {
|
||||
mut val := '$value.f64()'.replace('.e+', '.0e') // json notation
|
||||
if !val.contains('.') && val != '0' { // json notation
|
||||
val += '.0'
|
||||
if !text.starts_with('0x') && (text.contains('.') || text.to_lower().contains('e')) {
|
||||
mut val := ''
|
||||
if text.to_lower().contains('e') && !text.contains('-') {
|
||||
val = '${value.f64():.1f}'
|
||||
} else {
|
||||
val = '$value.f64()'
|
||||
}
|
||||
return '{ "type": "float", "value": "$val" }'
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue