cgen: uniform string output format

pull/4624/head
yuyi 2020-04-27 20:48:28 +08:00 committed by GitHub
parent eb8973c362
commit e9f764db4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 29 additions and 29 deletions

View File

@ -317,9 +317,9 @@ pub fn (a []string) str() string {
sb.write('[') sb.write('[')
for i in 0..a.len { for i in 0..a.len {
val := a[i] val := a[i]
sb.write('"') sb.write("\'")
sb.write(val) sb.write(val)
sb.write('"') sb.write("\'")
if i < a.len - 1 { if i < a.len - 1 {
sb.write(', ') sb.write(', ')
} }

View File

@ -168,7 +168,7 @@ fn test_insert() {
// } // }
fn test_strings() { fn test_strings() {
a := ['a', 'b', 'c'] a := ['a', 'b', 'c']
assert a.str() == '["a", "b", "c"]' assert a.str() == "['a', 'b', 'c']"
} }
/* /*

View File

@ -3162,7 +3162,7 @@ fn (mut g Gen) gen_str_for_array(info table.Array, styp, str_fn_name string) {
} else { } else {
g.auto_str_funcs.writeln('\t\tstrings__Builder_write(&sb, ${field_styp}_str(it));') g.auto_str_funcs.writeln('\t\tstrings__Builder_write(&sb, ${field_styp}_str(it));')
} }
g.auto_str_funcs.writeln('\t\tif (i != a.len-1) {') g.auto_str_funcs.writeln('\t\tif (i < a.len-1) {')
g.auto_str_funcs.writeln('\t\t\tstrings__Builder_write(&sb, tos3(", "));') g.auto_str_funcs.writeln('\t\t\tstrings__Builder_write(&sb, tos3(", "));')
g.auto_str_funcs.writeln('\t\t}') g.auto_str_funcs.writeln('\t\t}')
g.auto_str_funcs.writeln('\t}') g.auto_str_funcs.writeln('\t}')
@ -3187,11 +3187,11 @@ fn (mut g Gen) gen_str_for_array_fixed(info table.ArrayFixed, styp, str_fn_name
} else if sym.kind in [.f32, .f64] { } else if sym.kind in [.f32, .f64] {
g.auto_str_funcs.writeln('\t\tstrings__Builder_write(&sb, _STR("%g", a[i]));') g.auto_str_funcs.writeln('\t\tstrings__Builder_write(&sb, _STR("%g", a[i]));')
} else if sym.kind == .string { } else if sym.kind == .string {
g.auto_str_funcs.writeln('\t\tstrings__Builder_write(&sb, _STR("\\"%.*s\\"", a[i].len, a[i].str));') g.auto_str_funcs.writeln('\t\tstrings__Builder_write(&sb, _STR("\'%.*s\'", a[i].len, a[i].str));')
} else { } else {
g.auto_str_funcs.writeln('\t\tstrings__Builder_write(&sb, ${field_styp}_str(a[i]));') g.auto_str_funcs.writeln('\t\tstrings__Builder_write(&sb, ${field_styp}_str(a[i]));')
} }
g.auto_str_funcs.writeln('\t\tif (i != $info.size-1) {') g.auto_str_funcs.writeln('\t\tif (i < ${info.size-1}) {')
g.auto_str_funcs.writeln('\t\t\tstrings__Builder_write(&sb, tos3(", "));') g.auto_str_funcs.writeln('\t\t\tstrings__Builder_write(&sb, tos3(", "));')
g.auto_str_funcs.writeln('\t\t}') g.auto_str_funcs.writeln('\t\t}')
g.auto_str_funcs.writeln('\t}') g.auto_str_funcs.writeln('\t}')
@ -3218,14 +3218,14 @@ fn (mut g Gen) gen_str_for_map(info table.Map, styp, str_fn_name string) {
g.auto_str_funcs.writeln('\tstrings__Builder_write(&sb, tos3("{"));') g.auto_str_funcs.writeln('\tstrings__Builder_write(&sb, tos3("{"));')
g.auto_str_funcs.writeln('\tfor (unsigned int i = 0; i < m.key_values.size; i++) {') g.auto_str_funcs.writeln('\tfor (unsigned int i = 0; i < m.key_values.size; i++) {')
g.auto_str_funcs.writeln('\t\tstring key = (*(string*)DenseArray_get(m.key_values, i));') g.auto_str_funcs.writeln('\t\tstring key = (*(string*)DenseArray_get(m.key_values, i));')
g.auto_str_funcs.writeln('\t\tstrings__Builder_write(&sb, _STR("\\"%.*s\\"", key.len, key.str));') g.auto_str_funcs.writeln('\t\tstrings__Builder_write(&sb, _STR("\'%.*s\'", key.len, key.str));')
g.auto_str_funcs.writeln('\t\tstrings__Builder_write(&sb, tos3(": "));') g.auto_str_funcs.writeln('\t\tstrings__Builder_write(&sb, tos3(": "));')
g.auto_str_funcs.write('\t$val_styp it = (*($val_styp*)map_get3(') g.auto_str_funcs.write('\t$val_styp it = (*($val_styp*)map_get3(')
g.auto_str_funcs.write('m, (*(string*)DenseArray_get(m.key_values, i))') g.auto_str_funcs.write('m, (*(string*)DenseArray_get(m.key_values, i))')
g.auto_str_funcs.write(', ') g.auto_str_funcs.write(', ')
g.auto_str_funcs.writeln(' &($val_styp[]) { $zero }));') g.auto_str_funcs.writeln(' &($val_styp[]) { $zero }));')
if val_sym.kind == .string { if val_sym.kind == .string {
g.auto_str_funcs.writeln('\t\tstrings__Builder_write(&sb, _STR("\\"%.*s\\"", it.len, it.str));') g.auto_str_funcs.writeln('\t\tstrings__Builder_write(&sb, _STR("\'%.*s\'", it.len, it.str));')
} else if val_sym.kind == .struct_ && !val_sym.has_method('str') { } else if val_sym.kind == .struct_ && !val_sym.has_method('str') {
g.auto_str_funcs.writeln('\t\tstrings__Builder_write(&sb, ${val_styp}_str(it,0));') g.auto_str_funcs.writeln('\t\tstrings__Builder_write(&sb, ${val_styp}_str(it,0));')
} else if val_sym.kind in [.f32, .f64] { } else if val_sym.kind in [.f32, .f64] {

View File

@ -1,6 +1,6 @@
fn test_array_to_string_conversion() { fn test_array_to_string_conversion() {
a := ['1', '2', '3', '4'] a := ['1', '2', '3', '4']
assert a.str() == '["1", "2", "3", "4"]' assert a.str() == "['1', '2', '3', '4']"
b := [1, 2, 3, 4] b := [1, 2, 3, 4]
assert b.str() == '[1, 2, 3, 4]' assert b.str() == '[1, 2, 3, 4]'
@ -17,7 +17,7 @@ fn test_array_to_string_conversion() {
fn test_interpolation_array_to_string() { fn test_interpolation_array_to_string() {
a := ['1', '2', '3'] a := ['1', '2', '3']
assert '$a' == '["1", "2", "3"]' assert '$a' == "['1', '2', '3']"
b := [1, 2, 3, 4] b := [1, 2, 3, 4]
assert '$b' == '[1, 2, 3, 4]' assert '$b' == '[1, 2, 3, 4]'

View File

@ -1,11 +1,11 @@
fn test_fixed_array_init() { fn test_fixed_array_init() {
a1 := ['1', '2', '3']!! a1 := ['1', '2', '3']!!
assert typeof(a1) == '[3]string' assert typeof(a1) == '[3]string'
assert '$a1' == '["1", "2", "3"]' assert '$a1' == "['1', '2', '3']"
a2 := ['a', 'b']!! a2 := ['a', 'b']!!
assert typeof(a2) == '[2]string' assert typeof(a2) == '[2]string'
assert '$a2' == '["a", "b"]' assert '$a2' == "['a', 'b']"
c1 := [1, 2, 3]!! c1 := [1, 2, 3]!!
assert typeof(c1) == '[3]int' assert typeof(c1) == '[3]int'

View File

@ -3,12 +3,12 @@ fn test_fixed_array_to_string() {
a1[0] = '1' a1[0] = '1'
a1[1] = '2' a1[1] = '2'
a1[2] = '3' a1[2] = '3'
assert '$a1' == '["1", "2", "3"]' assert '$a1' == "['1', '2', '3']"
mut a2 := [2]string mut a2 := [2]string
a2[0] = 'a' a2[0] = 'a'
a2[1] = 'b' a2[1] = 'b'
assert '$a2' == '["a", "b"]' assert '$a2' == "['a', 'b']"
mut c1 := [3]int mut c1 := [3]int
c1[0] = 1 c1[0] = 1

View File

@ -9,30 +9,30 @@ fn test_interpolation_map_to_string() {
a['1'] = 'one' a['1'] = 'one'
a['2'] = 'two' a['2'] = 'two'
a['3'] = 'three' a['3'] = 'three'
assert '$a' == '{"1": "one", "2": "two", "3": "three"}' assert '$a' == "{'1': 'one', '2': 'two', '3': 'three'}"
mut b := map[string]int mut b := map[string]int
b['1'] = 1 b['1'] = 1
b['2'] = 2 b['2'] = 2
b['3'] = 3 b['3'] = 3
assert '$b' == '{"1": 1, "2": 2, "3": 3}' assert '$b' == "{'1': 1, '2': 2, '3': 3}"
mut c := map[string]bool mut c := map[string]bool
c['1'] = true c['1'] = true
c['2'] = false c['2'] = false
assert '$c' == '{"1": true, "2": false}' assert '$c' == "{'1': true, '2': false}"
d := {'f1': 1.1, 'f2': 2.2, 'f3': 3.3, 'f4': 4.4} d := {'f1': 1.1, 'f2': 2.2, 'f3': 3.3, 'f4': 4.4}
assert '$d' == '{"f1": 1.1, "f2": 2.2, "f3": 3.3, "f4": 4.4}' assert '$d' == "{'f1': 1.1, 'f2': 2.2, 'f3': 3.3, 'f4': 4.4}"
mut e := map[string]Test mut e := map[string]Test
e['1'] = Test{true, 0, 'abc'} e['1'] = Test{true, 0, 'abc'}
e['2'] = Test{true, 1, 'def'} e['2'] = Test{true, 1, 'def'}
e['3'] = Test{false, 2, 'ghi'} e['3'] = Test{false, 2, 'ghi'}
s := '$e' s := '$e'
assert s.contains('{"1": Test {') assert s.contains("{'1': Test {")
assert s.contains('a: true') assert s.contains('a: true')
assert s.contains("y: 'abc'") assert s.contains("y: 'abc'")
assert s.contains('}, "2": Test {') assert s.contains("}, '2': Test {")
assert s.contains("y: 'def'") assert s.contains("y: 'def'")
} }

View File

@ -50,6 +50,6 @@ fn test_array_of_bytes() {
fn test_array_of_strings() { fn test_array_of_strings() {
aa := ['aa', 'bb', 'cc'] aa := ['aa', 'bb', 'cc']
assert aa.str() == '["aa", "bb", "cc"]' assert aa.str() == "['aa', 'bb', 'cc']"
assert '$aa' == '["aa", "bb", "cc"]' assert '$aa' == "['aa', 'bb', 'cc']"
} }

View File

@ -23,11 +23,11 @@ fn test_array_of_structs_interpolation() {
assert s.contains('Man {') assert s.contains('Man {')
assert s.contains("name: 'Superman'") assert s.contains("name: 'Superman'")
assert s.contains('age: 30') assert s.contains('age: 30')
assert s.contains('"being nice"') assert s.contains("'being nice'")
assert s.contains('}, Man {') assert s.contains('}, Man {')
assert s.contains("name: 'Bilbo Baggins'") assert s.contains("name: 'Bilbo Baggins'")
assert s.contains('age: 111') assert s.contains('age: 111')
assert s.contains('interests: ["exploring", "hiding"]') assert s.contains("interests: ['exploring', 'hiding']")
assert s.contains('}]') assert s.contains('}]')
// println(s) // println(s)
} }
@ -74,5 +74,5 @@ fn test_array_of_bytes_interpolation() {
fn test_array_of_strings_interpolation() { fn test_array_of_strings_interpolation() {
aa := ['aa', 'bb', 'cc'] aa := ['aa', 'bb', 'cc']
assert '$aa' == '["aa", "bb", "cc"]' assert '$aa' == "['aa', 'bb', 'cc']"
} }

View File

@ -11,10 +11,10 @@ fn test_default_struct_string_interpolation() {
superman := Man{'Superman', 30, ['flying', 'fighting evil', 'being nice']} superman := Man{'Superman', 30, ['flying', 'fighting evil', 'being nice']}
s := '$superman' s := '$superman'
assert s.contains('Man {') assert s.contains('Man {')
assert s.contains('name: \'Superman\'') assert s.contains("name: 'Superman'")
assert s.contains('age: 30') assert s.contains('age: 30')
assert s.contains('interests: [') assert s.contains('interests: [')
assert s.contains('"being nice"') assert s.contains("'being nice'")
assert s.contains('}') assert s.contains('}')
// println(s) // println(s)
} }

View File

@ -22,7 +22,7 @@ fn test_vargs_string_interpolation() {
assert results.contains('age: 30') assert results.contains('age: 30')
assert results.contains('}, Man {') assert results.contains('}, Man {')
// //
assert results.contains('interests: ["programming"') assert results.contains("interests: ['programming'")
assert results.contains("name: 'Me'") assert results.contains("name: 'Me'")
// //
assert results.contains('}]') assert results.contains('}]')