1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

toml: improve number conversion (#12509)

This commit is contained in:
Larpon
2021-11-18 18:46:19 +01:00
committed by GitHub
parent 3caeadfa0d
commit 96554fad71
4 changed files with 41 additions and 31 deletions

View File

@@ -22,11 +22,8 @@ const (
'string/escape-tricky.toml',
'string/multiline.toml',
// Integer
'integer/literals.toml',
'integer/long.toml',
// Float
'float/exponent.toml',
'float/underscore.toml',
'float/inf-and-nan.toml',
// Comment
'comment/tricky.toml',
@@ -241,12 +238,18 @@ fn to_burntsushi(value ast.Value) string {
return '{ "type": "null", "value": "$json_text" }'
}
ast.Number {
if value.text.contains('.') || value.text.to_lower().contains('e') {
json_text := value.text.f64()
return '{ "type": "float", "value": "$json_text" }'
if value.text.contains('inf') || value.text.contains('nan') {
return '{ "type": "float", "value": "$value.text" }'
}
i64_ := strconv.parse_int(value.text, 0, 0) or { i64(0) }
return '{ "type": "integer", "value": "$i64_" }'
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'
}
return '{ "type": "float", "value": "$val" }'
}
return '{ "type": "integer", "value": "$value.i64()" }'
}
map[string]ast.Value {
mut str := '{ '