diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index 1adc990d1a..e9829d5343 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -43,6 +43,7 @@ NB: A V string should be/is immutable from the point of view of pub struct string { + is_lit bool pub: str byteptr // points to a C style 0 terminated string of bytes. len int // the length of the .str field, excluding the ending 0 byte. It is always equal to strlen(.str). @@ -104,6 +105,15 @@ pub fn tos3(s charptr) string { } } +pub fn tos_lit(s charptr) string { + return string{ + str: byteptr(s) + len: C.strlen(s) + is_lit:true + } +} + + // string.clone_static returns an independent copy of a given array // It should be used only in -autofree generated code. fn (a string) clone_static() string { @@ -1172,6 +1182,7 @@ pub fn (c byte) is_letter() bool { } pub fn (s &string) free() { + if s.is_lit {return} free(s.str) } diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 6257c11cd0..f4d0e88728 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -1061,7 +1061,6 @@ fn (g &Gen) autofree_variable(v ast.Var) string { } if sym.kind == .string { // Don't free simple string literals. - t := typeof(v.expr) match v.expr { ast.StringLiteral { return '// str literal\n' @@ -1069,6 +1068,7 @@ fn (g &Gen) autofree_variable(v ast.Var) string { else { // NOTE/TODO: assign_stmt multi returns variables have no expr // since the type comes from the called fns return type + t := typeof(v.expr) return '// other ' + t + '\n' } }