cgen:gen_str_for_array
parent
3ee858cd79
commit
27e04748ed
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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"]'
|
||||
}
|
||||
|
|
|
@ -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"]'
|
||||
}
|
Loading…
Reference in New Issue