diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 337dcde2d4..23329f5a52 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -1127,6 +1127,12 @@ fn (mut g Gen) expr(node ast.Expr) { // `C.printf("hi")` => `printf("hi");` g.write('"$escaped_val"') } else { + // TODO calculate the literal's length in V, it's a bit tricky with all the + // escape characters. + // Clang and GCC optimize `strlen("lorem ipsum")` to `11` + // g.write('tos4("$escaped_val", strlen("$escaped_val"))') + // g.write('tos4("$escaped_val", $it.val.len)') + // g.write('_SLIT("$escaped_val")') g.write('tos3("$escaped_val")') } } @@ -2727,7 +2733,7 @@ fn (g Gen) type_default(typ table.Type) string { } */ match sym.name { - 'string' { return 'tos3("")' } + 'string' { return '(string){.str=""}' } 'rune' { return '0' } else {} } diff --git a/vlib/v/gen/cheaders.v b/vlib/v/gen/cheaders.v index 2731c46c71..a728ed6b86 100644 --- a/vlib/v/gen/cheaders.v +++ b/vlib/v/gen/cheaders.v @@ -173,6 +173,8 @@ extern wchar_t **_wenviron; //============================== HELPER C MACROS =============================*/ +//#define tos4(s, slen) ((string){.str=(s), .len=(slen)}) +#define _SLIT(s) ((string){.str=(s), .len=(strlen(s))}) #define _PUSH_MANY(arr, val, tmp, tmp_typ) {tmp_typ tmp = (val); array_push_many(arr, tmp.data, tmp.len);} #define _IN(typ, val, arr) array_##typ##_contains(arr, val) #define _IN_MAP(val, m) map_exists(m, val)