From 8cfb2ad6c93a92f835490bf4337f63593733e021 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Thu, 7 May 2020 18:05:54 +0200 Subject: [PATCH] string: is_lit --- vlib/builtin/string.v | 11 +++++++++++ vlib/v/gen/cgen.v | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) 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' } }