From e9f764db4fd0958071c8f8927f699aacbda08368 Mon Sep 17 00:00:00 2001 From: yuyi Date: Mon, 27 Apr 2020 20:48:28 +0800 Subject: [PATCH] cgen: uniform string output format --- vlib/builtin/array.v | 4 ++-- vlib/builtin/array_test.v | 2 +- vlib/v/gen/cgen.v | 12 ++++++------ vlib/v/tests/array_to_string_test.v | 4 ++-- vlib/v/tests/fixed_array_init_test.v | 4 ++-- vlib/v/tests/fixed_array_to_string_test.v | 4 ++-- vlib/v/tests/map_to_string_test.v | 12 ++++++------ vlib/v/tests/str_gen_test.v | 4 ++-- vlib/v/tests/string_interpolation_array_test.v | 6 +++--- vlib/v/tests/string_interpolation_struct_test.v | 4 ++-- vlib/v/tests/string_interpolation_variadic_test.v | 2 +- 11 files changed, 29 insertions(+), 29 deletions(-) diff --git a/vlib/builtin/array.v b/vlib/builtin/array.v index 5efebe95d9..b3ddbf7681 100644 --- a/vlib/builtin/array.v +++ b/vlib/builtin/array.v @@ -317,9 +317,9 @@ pub fn (a []string) str() string { sb.write('[') for i in 0..a.len { val := a[i] - sb.write('"') + sb.write("\'") sb.write(val) - sb.write('"') + sb.write("\'") if i < a.len - 1 { sb.write(', ') } diff --git a/vlib/builtin/array_test.v b/vlib/builtin/array_test.v index ad7ae7f627..2c4438d40d 100644 --- a/vlib/builtin/array_test.v +++ b/vlib/builtin/array_test.v @@ -168,7 +168,7 @@ fn test_insert() { // } fn test_strings() { a := ['a', 'b', 'c'] - assert a.str() == '["a", "b", "c"]' + assert a.str() == "['a', 'b', 'c']" } /* diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 23329f5a52..354a526d8c 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -159,7 +159,7 @@ pub fn cgen(files []ast.File, table &table.Table, pref &pref.Preferences) string // g.finish() // - + b := strings.new_builder(250000) b.writeln(g.hashes()) b.writeln(g.comptime_defines.str()) @@ -3162,7 +3162,7 @@ fn (mut g Gen) gen_str_for_array(info table.Array, styp, str_fn_name string) { } else { 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}') 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] { g.auto_str_funcs.writeln('\t\tstrings__Builder_write(&sb, _STR("%g", a[i]));') } 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 { 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}') 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('\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\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.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(', ') g.auto_str_funcs.writeln(' &($val_styp[]) { $zero }));') 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') { g.auto_str_funcs.writeln('\t\tstrings__Builder_write(&sb, ${val_styp}_str(it,0));') } else if val_sym.kind in [.f32, .f64] { diff --git a/vlib/v/tests/array_to_string_test.v b/vlib/v/tests/array_to_string_test.v index ee8cd539e3..7627ba4fc8 100644 --- a/vlib/v/tests/array_to_string_test.v +++ b/vlib/v/tests/array_to_string_test.v @@ -1,6 +1,6 @@ fn test_array_to_string_conversion() { a := ['1', '2', '3', '4'] - assert a.str() == '["1", "2", "3", "4"]' + assert a.str() == "['1', '2', '3', '4']" b := [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() { a := ['1', '2', '3'] - assert '$a' == '["1", "2", "3"]' + assert '$a' == "['1', '2', '3']" b := [1, 2, 3, 4] assert '$b' == '[1, 2, 3, 4]' diff --git a/vlib/v/tests/fixed_array_init_test.v b/vlib/v/tests/fixed_array_init_test.v index c990e23138..5bd847acd3 100644 --- a/vlib/v/tests/fixed_array_init_test.v +++ b/vlib/v/tests/fixed_array_init_test.v @@ -1,11 +1,11 @@ fn test_fixed_array_init() { a1 := ['1', '2', '3']!! assert typeof(a1) == '[3]string' - assert '$a1' == '["1", "2", "3"]' + assert '$a1' == "['1', '2', '3']" a2 := ['a', 'b']!! assert typeof(a2) == '[2]string' - assert '$a2' == '["a", "b"]' + assert '$a2' == "['a', 'b']" c1 := [1, 2, 3]!! assert typeof(c1) == '[3]int' diff --git a/vlib/v/tests/fixed_array_to_string_test.v b/vlib/v/tests/fixed_array_to_string_test.v index 17be662907..68fefadf2d 100644 --- a/vlib/v/tests/fixed_array_to_string_test.v +++ b/vlib/v/tests/fixed_array_to_string_test.v @@ -3,12 +3,12 @@ fn test_fixed_array_to_string() { a1[0] = '1' a1[1] = '2' a1[2] = '3' - assert '$a1' == '["1", "2", "3"]' + assert '$a1' == "['1', '2', '3']" mut a2 := [2]string a2[0] = 'a' a2[1] = 'b' - assert '$a2' == '["a", "b"]' + assert '$a2' == "['a', 'b']" mut c1 := [3]int c1[0] = 1 diff --git a/vlib/v/tests/map_to_string_test.v b/vlib/v/tests/map_to_string_test.v index 6d4882aae5..760beced80 100644 --- a/vlib/v/tests/map_to_string_test.v +++ b/vlib/v/tests/map_to_string_test.v @@ -9,30 +9,30 @@ fn test_interpolation_map_to_string() { a['1'] = 'one' a['2'] = 'two' a['3'] = 'three' - assert '$a' == '{"1": "one", "2": "two", "3": "three"}' + assert '$a' == "{'1': 'one', '2': 'two', '3': 'three'}" mut b := map[string]int b['1'] = 1 b['2'] = 2 b['3'] = 3 - assert '$b' == '{"1": 1, "2": 2, "3": 3}' + assert '$b' == "{'1': 1, '2': 2, '3': 3}" mut c := map[string]bool c['1'] = true 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} - 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 e['1'] = Test{true, 0, 'abc'} e['2'] = Test{true, 1, 'def'} e['3'] = Test{false, 2, 'ghi'} s := '$e' - assert s.contains('{"1": Test {') + assert s.contains("{'1': Test {") assert s.contains('a: true') assert s.contains("y: 'abc'") - assert s.contains('}, "2": Test {') + assert s.contains("}, '2': Test {") assert s.contains("y: 'def'") } diff --git a/vlib/v/tests/str_gen_test.v b/vlib/v/tests/str_gen_test.v index 523eed8427..34725bb346 100644 --- a/vlib/v/tests/str_gen_test.v +++ b/vlib/v/tests/str_gen_test.v @@ -50,6 +50,6 @@ fn test_array_of_bytes() { fn test_array_of_strings() { aa := ['aa', 'bb', 'cc'] - assert aa.str() == '["aa", "bb", "cc"]' - assert '$aa' == '["aa", "bb", "cc"]' + assert aa.str() == "['aa', 'bb', 'cc']" + assert '$aa' == "['aa', 'bb', 'cc']" } diff --git a/vlib/v/tests/string_interpolation_array_test.v b/vlib/v/tests/string_interpolation_array_test.v index 97f40fb421..176ad97aaa 100644 --- a/vlib/v/tests/string_interpolation_array_test.v +++ b/vlib/v/tests/string_interpolation_array_test.v @@ -23,11 +23,11 @@ fn test_array_of_structs_interpolation() { assert s.contains('Man {') assert s.contains("name: 'Superman'") assert s.contains('age: 30') - assert s.contains('"being nice"') + assert s.contains("'being nice'") assert s.contains('}, Man {') assert s.contains("name: 'Bilbo Baggins'") assert s.contains('age: 111') - assert s.contains('interests: ["exploring", "hiding"]') + assert s.contains("interests: ['exploring', 'hiding']") assert s.contains('}]') // println(s) } @@ -74,5 +74,5 @@ fn test_array_of_bytes_interpolation() { fn test_array_of_strings_interpolation() { aa := ['aa', 'bb', 'cc'] - assert '$aa' == '["aa", "bb", "cc"]' + assert '$aa' == "['aa', 'bb', 'cc']" } diff --git a/vlib/v/tests/string_interpolation_struct_test.v b/vlib/v/tests/string_interpolation_struct_test.v index c256a23ff8..130d183f29 100644 --- a/vlib/v/tests/string_interpolation_struct_test.v +++ b/vlib/v/tests/string_interpolation_struct_test.v @@ -11,10 +11,10 @@ fn test_default_struct_string_interpolation() { superman := Man{'Superman', 30, ['flying', 'fighting evil', 'being nice']} s := '$superman' assert s.contains('Man {') - assert s.contains('name: \'Superman\'') + assert s.contains("name: 'Superman'") assert s.contains('age: 30') assert s.contains('interests: [') - assert s.contains('"being nice"') + assert s.contains("'being nice'") assert s.contains('}') // println(s) } diff --git a/vlib/v/tests/string_interpolation_variadic_test.v b/vlib/v/tests/string_interpolation_variadic_test.v index 8e61b01994..c6f7c307ea 100644 --- a/vlib/v/tests/string_interpolation_variadic_test.v +++ b/vlib/v/tests/string_interpolation_variadic_test.v @@ -22,7 +22,7 @@ fn test_vargs_string_interpolation() { assert results.contains('age: 30') assert results.contains('}, Man {') // - assert results.contains('interests: ["programming"') + assert results.contains("interests: ['programming'") assert results.contains("name: 'Me'") // assert results.contains('}]')