From 27e04748eda562fafc7f7a43a24d73e385407cf2 Mon Sep 17 00:00:00 2001 From: yuyi Date: Sun, 19 Apr 2020 16:42:34 +0800 Subject: [PATCH] cgen:gen_str_for_array --- vlib/builtin/array.v | 62 ------------------- vlib/v/gen/cgen.v | 34 +++++----- vlib/v/tests/str_gen_test.v | 45 ++++++++++++++ ...st.v => string_interpolation_array_test.v} | 47 +++++++++++++- 4 files changed, 111 insertions(+), 77 deletions(-) rename vlib/v/tests/{string_interpolation_array_of_structs_test.v => string_interpolation_array_test.v} (54%) diff --git a/vlib/builtin/array.v b/vlib/builtin/array.v index 2d45188847..d29824fe3b 100644 --- a/vlib/builtin/array.v +++ b/vlib/builtin/array.v @@ -377,68 +377,6 @@ pub fn (a []string) str() string { return sb.str() } -// []byte.str returns a string representation of the array of bytes -// => '[`a`, `b`, `c`]' -pub fn (a []byte) str() string { - mut sb := strings.new_builder(a.len * 3) - sb.write('[') - for i in 0..a.len { - val := a[i].str() - sb.write('`') - sb.write(val) - sb.write('`') - if a[i] != 0 { - val.free() - } - if i < a.len - 1 { - sb.write(', ') - } - } - sb.write(']') - return sb.str() -} - -// []int.str returns a string representation of the array of ints -// => '[1, 2, 3]' -pub fn (a []int) str() string { - mut sb := strings.new_builder(a.len * 13) - sb.write('[') - for i in 0..a.len { - val := a[i].str() - sb.write(val) - //println('"$val"') - if a[i] != 0 { - val.free() - } - if i < a.len - 1 { - sb.write(', ') - } - } - sb.write(']') - return sb.str() -} - -// []bool.str returns a string representation of the array of bools -// => '[true, true, false]' -pub fn (a []bool) str() string { - mut sb := strings.new_builder(a.len * 3) - sb.write('[') - for i in 0..a.len { - val := a[i] - if val { - sb.write('true') - } - else { - sb.write('false') - } - if i < a.len - 1 { - sb.write(', ') - } - } - sb.write(']') - return sb.str() -} - // []byte.hex returns a string with the hexadecimal representation // of the byte elements of the array pub fn (b []byte) hex() string { diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 82ec688697..8db288e8c0 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -3003,23 +3003,29 @@ fn (var g Gen) gen_str_for_struct(info table.Struct, styp string) { fn (var g Gen) gen_str_for_array(info table.Array, styp string) { s := styp.replace('.', '__') sym := g.table.get_type_symbol(info.elem_type) + field_styp := g.typ(info.elem_type) if sym.kind == .struct_ && !sym.has_method('str') { - field_styp := g.typ(info.elem_type) g.gen_str_for_type(sym, field_styp) - g.definitions.write('string ${s}_str($styp a) {\n') - g.definitions.write('\tstrings__Builder sb = strings__new_builder(a.len * 10);\n') - g.definitions.write('\tstrings__Builder_write(&sb, tos3("["));\n') - g.definitions.write('\tfor (int i = 0; i < a.len; i++) {\n') - g.definitions.write('\t\t${field_styp} it = (*(${field_styp}*)array_get(a, i));\n') - g.definitions.write('\t\tif (i != a.len-1) {\n') - g.definitions.write('\t\t\tstrings__Builder_write(&sb, ${field_styp}_str(it,0));\n') - g.definitions.write('\t\t\tstrings__Builder_write(&sb, tos3(", "));\n') - g.definitions.write('\t\t} else {\n') - g.definitions.write('\t\t\tstrings__Builder_write(&sb, ${field_styp}_str(it,0));\n\t\t}\n\t}\n') - g.definitions.write('\tstrings__Builder_write(&sb, tos3("]"));\n') - g.definitions.write('\treturn strings__Builder_str(&sb);\n') - g.definitions.write('}\n') } + g.definitions.writeln('string ${s}_str($styp a) {') + g.definitions.writeln('\tstrings__Builder sb = strings__new_builder(a.len * 10);') + g.definitions.writeln('\tstrings__Builder_write(&sb, tos3("["));') + g.definitions.writeln('\tfor (int i = 0; i < a.len; i++) {') + g.definitions.writeln('\t\t${field_styp} it = (*(${field_styp}*)array_get(a, i));') + if sym.kind == .struct_ && !sym.has_method('str') { + g.definitions.writeln('\t\t\tstrings__Builder_write(&sb, ${field_styp}_str(it,0));') + } else if sym.kind in [.f32, .f64] { + g.definitions.writeln('\t\t\tstrings__Builder_write(&sb, _STR("%g", it));') + } else { + g.definitions.writeln('\t\t\tstrings__Builder_write(&sb, ${field_styp}_str(it));') + } + g.definitions.writeln('\t\tif (i != a.len-1) {') + g.definitions.writeln('\t\t\tstrings__Builder_write(&sb, tos3(", "));') + g.definitions.writeln('\t\t}') + g.definitions.writeln('\t}') + g.definitions.writeln('\tstrings__Builder_write(&sb, tos3("]"));') + g.definitions.writeln('\treturn strings__Builder_str(&sb);') + g.definitions.writeln('}') } fn (g Gen) type_to_fmt(typ table.Type) string { diff --git a/vlib/v/tests/str_gen_test.v b/vlib/v/tests/str_gen_test.v index 33874bf006..a900dc72ec 100644 --- a/vlib/v/tests/str_gen_test.v +++ b/vlib/v/tests/str_gen_test.v @@ -16,3 +16,48 @@ fn test_array_str() { // assert n2.str() == '[4, 5, 6]' println(n2) } + +fn test_array_of_floats() { + // f64 array + aa := [1.2, 3.4, 5.67] + assert '$aa' == '[1.2, 3.4, 5.67]' + // f32 array + bb := [f32(1.2), 3.4, 5.67] + assert '$bb' == '[1.2, 3.4, 5.67]' +} + +fn test_array_of_bools() { + aa := [true, false, true] + assert '$aa' == '[true, false, true]' +} + +fn test_array_of_ints() { + // int + a1 := [11, 22, 33] + assert '$a1' == '[11, 22, 33]' + // u32 + a2 := [u32(11), 22, 33] + assert '$a2' == '[11, 22, 33]' + // i16 + b1 := [i16(11), 22, 33] + assert '$b1' == '[11, 22, 33]' + // u16 + b2 := [u16(11), 22, 33] + assert '$b2' == '[11, 22, 33]' + // i64 + c1 := [i64(11), 22, 33] + assert '$c1' == '[11, 22, 33]' + // u64 + c2 := [u64(11), 22, 33] + assert '$c2' == '[11, 22, 33]' +} + +fn test_array_of_bytes() { + aa := [`a`, `b`, `c`] + assert '$aa' == '[a, b, c]' +} + +fn test_array_of_strings() { + aa := ['aa', 'bb', 'cc'] + assert '$aa' == '["aa", "bb", "cc"]' +} diff --git a/vlib/v/tests/string_interpolation_array_of_structs_test.v b/vlib/v/tests/string_interpolation_array_test.v similarity index 54% rename from vlib/v/tests/string_interpolation_array_of_structs_test.v rename to vlib/v/tests/string_interpolation_array_test.v index 0baf26647f..97f40fb421 100644 --- a/vlib/v/tests/string_interpolation_array_of_structs_test.v +++ b/vlib/v/tests/string_interpolation_array_test.v @@ -14,7 +14,7 @@ struct Man { interests []string } -fn test_default_struct_array_of_structs_interpolation() { +fn test_array_of_structs_interpolation() { people := [ Man{'Superman', 30, ['flying','fighting evil','being nice']}, Man{'Bilbo Baggins', 111, ['exploring', 'hiding']}, @@ -31,3 +31,48 @@ fn test_default_struct_array_of_structs_interpolation() { assert s.contains('}]') // println(s) } + +fn test_array_of_floats_interpolation() { + // f64 array + aa := [1.2, 3.4, 5.67] + assert '$aa' == '[1.2, 3.4, 5.67]' + // f32 array + bb := [f32(1.2), 3.4, 5.67] + assert '$bb' == '[1.2, 3.4, 5.67]' +} + +fn test_array_of_bools_interpolation() { + aa := [true, false, true] + assert '$aa' == '[true, false, true]' +} + +fn test_array_of_ints_interpolation() { + // int + a1 := [11, 22, 33] + assert '$a1' == '[11, 22, 33]' + // u32 + a2 := [u32(11), 22, 33] + assert '$a2' == '[11, 22, 33]' + // i16 + b1 := [i16(11), 22, 33] + assert '$b1' == '[11, 22, 33]' + // u16 + b2 := [u16(11), 22, 33] + assert '$b2' == '[11, 22, 33]' + // i64 + c1 := [i64(11), 22, 33] + assert '$c1' == '[11, 22, 33]' + // u64 + c2 := [u64(11), 22, 33] + assert '$c2' == '[11, 22, 33]' +} + +fn test_array_of_bytes_interpolation() { + aa := [`a`, `b`, `c`] + assert '$aa' == '[a, b, c]' +} + +fn test_array_of_strings_interpolation() { + aa := ['aa', 'bb', 'cc'] + assert '$aa' == '["aa", "bb", "cc"]' +}