string: make replace() clone for now; parser: comptime method

pull/5020/head
Alexander Medvednikov 2020-05-25 08:17:36 +02:00
parent 1b36d7a037
commit f41e2c0a4b
3 changed files with 51 additions and 4 deletions

View File

@ -156,7 +156,7 @@ pub fn (s string) replace_once(rep, with string) string {
pub fn (s string) replace(rep, with string) string {
if s.len == 0 || rep.len == 0 {
return s
return s.clone()
}
// TODO PERF Allocating ints is expensive. Should be a stack array
// Get locations of all reps within this string
@ -172,7 +172,7 @@ pub fn (s string) replace(rep, with string) string {
}
// Dont change the string if there's nothing to replace
if idxs.len == 0 {
return s
return s.clone()
}
// Now we know the number of replacements we need to do and we can calc the len of the new string
new_len := s.len + idxs.len * (with.len - rep.len)
@ -1229,7 +1229,7 @@ pub fn (s string) all_after_last(dot string) string {
return s.right(pos + dot.len)
}
pub fn (s string) after(dot string) string {
pub fn (s string) after(dot string) string {
return s.all_after_last(dot)
}

View File

@ -1158,8 +1158,16 @@ 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)
/*
f := v.name[0]
if
//!(f >= `a` && f <= `d`) {
//f != `c` {
v.name!='cvar_name' {
t := typeof(v.expr)
return '// other ' + t + '\n'
}
*/
}
}
return g.autofree_var_call('string_free', v)

View File

@ -6,6 +6,7 @@ module parser
import v.ast
import v.pref
import v.vmod
import v.table
const (
supported_platforms = ['windows', 'mac', 'macos', 'darwin', 'linux', 'freebsd', 'openbsd',
@ -189,3 +190,41 @@ fn os_from_string(os string) pref.OS {
// println('bad os $os') // todo panic?
return .linux
}
// `user.$method()` (`method` is a string)
fn (mut p Parser) comptime_method_call(typ table.Type) {
p.check(.dollar)
method_name := p.check_name()
_ = method_name
mut j := 0
sym := p.table.get_type_symbol(typ)
if sym.kind != .struct_ {
p.error('not a struct')
}
// info := sym.info as table.Struct
for method in sym.methods {
if method.return_type != table.void_type {
continue
}
/*
receiver := method.args[0]
if !p.expr_var.ptr {
p.error('`$p.expr_var.name` needs to be a reference')
}
amp := if receiver.is_mut && !p.expr_var.ptr { '&' } else { '' }
if j > 0 {
p.gen(' else ')
}
p.genln('if (string_eq($method_name, _STR("$method.name")) ) ' + '${typ.name}_$method.name ($amp $p.expr_var.name);')
*/
j++
}
p.check(.lpar)
p.check(.rpar)
if p.tok.kind == .key_orelse {
p.check(.key_orelse)
// p.genln('else {')
p.check(.lcbr)
// p.statements()
}
}