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

toml: add value decoding (#12521)

This commit is contained in:
Larpon
2021-11-20 18:48:44 +01:00
committed by GitHub
parent 4b9e8e243c
commit f1dd0e3355
8 changed files with 206 additions and 64 deletions

View File

@ -19,7 +19,6 @@ const (
valid_value_exceptions = [
// String
'string/escapes.toml',
'string/escape-tricky.toml',
'string/multiline.toml',
// Integer
'integer/long.toml',
@ -199,13 +198,7 @@ fn test_burnt_sushi_tomltest() {
fn to_burntsushi(value ast.Value) string {
match value {
ast.Quoted {
mut json_text := ''
if value.quote == `"` {
json_text = toml_to_json_escapes(value) or { '<error>' }
} else {
json_text = json2.Any(value.text).json_str()
}
json_text := json2.Any(value.text).json_str()
return '{ "type": "string", "value": "$json_text" }'
}
ast.DateTime {
@ -271,49 +264,3 @@ fn to_burntsushi(value ast.Value) string {
}
return '<error>'
}
// toml_to_json_escapes is a utility function for normalizing
// TOML basic string to JSON string
fn toml_to_json_escapes(q ast.Quoted) ?string {
mut s := scanner.new_simple(q.text) ?
mut r := ''
for {
ch := s.next()
if ch == scanner.end_of_text {
break
}
ch_byte := byte(ch)
if ch == `"` {
if byte(s.peek(-1)) != `\\` {
r += '\\'
}
}
if ch == `\\` {
next_ch := byte(s.at())
escape := ch_byte.ascii_str() + next_ch.ascii_str()
if escape.to_lower() == '\\u' {
mut b := s.next()
mut unicode_point := ''
for {
b = s.next()
if b != ` ` && b != scanner.end_of_text {
unicode_point += byte(b).ascii_str()
} else {
break
}
}
if unicode_point.len < 8 {
unicode_point = '0'.repeat(8 - unicode_point.len) + unicode_point
}
rn := rune(strconv.parse_int(unicode_point, 16, 0) ?)
r += '$rn'
continue
}
}
r += ch_byte.ascii_str()
}
return r
}

View File

@ -72,9 +72,9 @@ fn test_unicode_escapes() {
mut toml_doc := toml.parse(toml_unicode_escapes) or { panic(err) }
mut value := toml_doc.value('short')
assert value.string() == r'\u03B4'
assert value.string() == '\u03B4' // <- This escape is handled by V
value = toml_doc.value('long')
assert value.string() == r'\U000003B4'
assert value.string() == 'δ' // <- for the long escape we compare with the unicode point
}
fn test_literal_strings() {