Skip to content

Commit

Permalink
perf(strings): reduce split function time complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
pmqueiroz committed Dec 1, 2024
1 parent 2a92b0a commit 856cc93
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 19 deletions.
17 changes: 16 additions & 1 deletion interpreter/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,12 @@ func Evaluate(expression ast.Expression, env *environment.Environment) (interfac
return nil, err
}

defaultError := exception.NewRuntimeError("RT028", types.SafeParseUmbraType(value), types.SafeParseUmbraType(expr.Type.Type))
expectedType, err := types.ParseTokenType(expr.Type.Type)
if err != nil {
return nil, err
}

defaultError := exception.NewRuntimeError("RT028", types.SafeParseUmbraType(value), expectedType)

switch expr.Type.Type {
case tokens.STR_TYPE:
Expand All @@ -523,6 +528,16 @@ func Evaluate(expression ast.Expression, env *environment.Environment) (interfac
return strconv.FormatFloat(v, 'f', -1, 64), nil
case bool:
return strconv.FormatBool(v), nil
case []interface{}:
var strElements []string
for _, elem := range v {
strElem, ok := elem.(string)
if !ok {
return nil, exception.NewRuntimeError("RT028", types.SafeParseUmbraType(elem), "<str>")
}
strElements = append(strElements, strElem)
}
return strings.Join(strElements, ""), nil
}
return nil, defaultError
case tokens.CHAR_TYPE:
Expand Down
58 changes: 40 additions & 18 deletions lib/strings.u
Original file line number Diff line number Diff line change
@@ -1,42 +1,63 @@
import "io"

def substring(string str, start num, end num) {
mut result str = ""
if (start < 0 or end > ~string or start > end) {
return result
def join(array arr, separator str) str {
if (~array == 0) {
return ""
}

for mut i num = 0, ~range string - 1 {
if (i < start) {
continue
mut totalLength num = 0

for mut i num = 0, ~array - 1 {
totalLength = totalLength + ~array[i]
}

totalLength = totalLength + (~separator * (~array - 1))

mut buffer arr = []

mut index num = 0
for mut i num = 0, ~array - 1 {
for mut j num = 0, ~array[i] - 1 {
buffer[index] = (range array[i])[j]
index = index + 1
}

if (i >= end) {
break
if (i < ~array - 1) {
for mut k num = 0, ~separator - 1 {
buffer[index] = (range separator)[k]
index = index + 1
}
}

result = result + (range string)[i]
}

return result
return str(buffer)
}

def substring(string str, start num, end num) {
mut result arr = []
if (start < 0 or end > ~string or start > end) {
return ""
}

for mut i num = start, end - 1 {
result[~result] = (range string)[i]
}

return join(result, "")
}

def split(string str, separator char) {
mut result arr = []
mut start num = 0
mut end num = 0

for mut i num = 0, ~range string - 1 {
if (char((range string)[i]) == separator) {
result[~result] = substring(string, start, end)
result[~result] = substring(string, start, i)
start = i + 1
end = i + 1
} else {
end = i + 1
}
}

result[~result] = substring(string, start, end)
result[~result] = substring(string, start, ~range string)
return result
}

Expand Down Expand Up @@ -71,4 +92,5 @@ pub {
split
toUpper
toLower
join
}

0 comments on commit 856cc93

Please sign in to comment.