string: is_lit

pull/4776/head
Alexander Medvednikov 2020-05-07 18:05:54 +02:00
parent 1991220797
commit 8cfb2ad6c9
2 changed files with 12 additions and 1 deletions

View File

@ -43,6 +43,7 @@ NB: A V string should be/is immutable from the point of view of
pub struct string { pub struct string {
is_lit bool
pub: pub:
str byteptr // points to a C style 0 terminated string of bytes. 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). 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 // string.clone_static returns an independent copy of a given array
// It should be used only in -autofree generated code. // It should be used only in -autofree generated code.
fn (a string) clone_static() string { fn (a string) clone_static() string {
@ -1172,6 +1182,7 @@ pub fn (c byte) is_letter() bool {
} }
pub fn (s &string) free() { pub fn (s &string) free() {
if s.is_lit {return}
free(s.str) free(s.str)
} }

View File

@ -1061,7 +1061,6 @@ fn (g &Gen) autofree_variable(v ast.Var) string {
} }
if sym.kind == .string { if sym.kind == .string {
// Don't free simple string literals. // Don't free simple string literals.
t := typeof(v.expr)
match v.expr { match v.expr {
ast.StringLiteral { ast.StringLiteral {
return '// str literal\n' return '// str literal\n'
@ -1069,6 +1068,7 @@ fn (g &Gen) autofree_variable(v ast.Var) string {
else { else {
// NOTE/TODO: assign_stmt multi returns variables have no expr // NOTE/TODO: assign_stmt multi returns variables have no expr
// since the type comes from the called fns return type // since the type comes from the called fns return type
t := typeof(v.expr)
return '// other ' + t + '\n' return '// other ' + t + '\n'
} }
} }