Skip to content

Commit

Permalink
minor optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Apr 23, 2024
1 parent 83560fd commit 05d0907
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/julec/compile.jule
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ fn push_comp_cmd_gcc(mut &cmd: str) {
// Generate compile command for backend-compiler.
fn gen_compile_cmd(source_path: str, &ir: &IR): (str, str) {
let &compiler = env::COMPILER_PATH
let mut cmd = ""
let mut cmd = make(str, 1 << 6)

match env::COMPILER {
| "gcc":
Expand Down
2 changes: 1 addition & 1 deletion src/julec/obj/cxx/expr.jule
Original file line number Diff line number Diff line change
Expand Up @@ -1186,7 +1186,7 @@ fn sbtoa(b: byte): str {
}

fn cstr_bytes(bytes: []byte): str {
let mut lit = ""
let mut lit = make(str, bytes.len)
for _, b in bytes {
lit += sbtoa(b)
}
Expand Down
9 changes: 8 additions & 1 deletion src/julec/obj/cxx/object.jule
Original file line number Diff line number Diff line change
Expand Up @@ -1032,7 +1032,14 @@ fn prepare_structure(mut &s: &Struct) {

// Concatenate all strings into single string.
fn concat_all_parts(parts: ...&Token): str {
let mut s = ""
let mut n = 0
for _, part in parts {
n += part.kind.len
}
if n == 0 {
ret ""
}
let mut s = make(str, n)
for _, p in parts {
s += p.kind
}
Expand Down
17 changes: 1 addition & 16 deletions std/fs/path/path_unix.jule
Original file line number Diff line number Diff line change
Expand Up @@ -70,25 +70,10 @@ fn __abs(path: str): (str, ok: bool) {
ret __join(wd, path), true
}

fn str_join(elems: []str, sep: str): str {
match elems.len {
| 0:
ret ""
| 1:
ret elems[0]
}

let mut s = elems[0]
for _, elem in elems[1:] {
s += sep + elem
}
ret s
}

fn __join(elem: ...str): str {
for i, e in elem {
if e != "" {
ret clean(str_join(elem[i:], str(SEPARATOR)))
ret clean(strings::join(elem[i:], str(SEPARATOR)))
}
}
ret ""
Expand Down
6 changes: 5 additions & 1 deletion std/jule/parser/parser.jule
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,11 @@ unsafe fn push_suggestion(mut log: *Log, fmt: LogMsg, args: ...any) {
}

fn tokstoa(&tokens: []&Token): str {
let mut s = ""
let mut n = 0
for _, token in tokens {
n += token.kind.len
}
let mut s = make(str, n)
for _, token in tokens {
s += token.kind
}
Expand Down
26 changes: 22 additions & 4 deletions std/strings/strings.jule
Original file line number Diff line number Diff line change
Expand Up @@ -347,11 +347,15 @@ pub fn replace(s: str, sub: str, new: str, mut n: int): str {
ret s
}

if n < 0 {
n = s.len
let m = count(s, sub)
if m == 0 {
ret s
}
if n < 0 || m < n {
n = m
}

let mut ss = make(str, s.len)
let mut ss = make(str, s.len + n * (new.len - sub.len))
let mut i = 0
for n > 0; n-- {
let j = find_at(s, sub, i)
Expand Down Expand Up @@ -454,7 +458,21 @@ pub fn join(parts: []str, sep: str): str {
if parts.len == 0 {
ret ""
}
let mut s = parts[0]
let mut n = 0
if sep.len > 0 {
if sep.len >= int.MAX/parts.len-1 {
panic("std::strings: join: output length overflow")
}
n += sep.len * (parts.len - 1)
}
for _, part in parts {
if part.len > int.MAX-n {
panic("std::strings: join: output length overflow")
}
n += part.len
}
let mut s = make(str, n)
s += parts[0]
for _, part in parts[1:] {
s += sep
s += part
Expand Down

0 comments on commit 05d0907

Please sign in to comment.