Skip to content

Commit 4c3895e

Browse files
chqrliebvdberg
authored andcommitted
Compiler: improve split_paths
* factorize code in `split_paths()` * rename `handle_path` as more explicit `Compiler.addLibPath()`
1 parent adb108a commit 4c3895e

File tree

2 files changed

+7
-12
lines changed

2 files changed

+7
-12
lines changed

common/string_utils.c2

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,12 @@ public type PathHandler fn void (void* arg, const char* path, u32 len);
5858
public fn void split_paths(const char* input, void* arg, PathHandler handler) @(unused) {
5959
// split by ':'
6060
const char* start = input;
61-
const char* cp = start;
62-
while (1) {
63-
if (*cp == 0) {
61+
for (const char* cp = start;; cp++) {
62+
if (*cp == '\0' || *cp == ':') {
6463
if (cp != start) handler(arg, start, (u32)(cp - start));
65-
return;
64+
if (*cp == '\0') return;
65+
start = cp + 1;
6666
}
67-
if (*cp == ':') {
68-
if (cp != start) handler(arg, start, (u32)(cp - start));
69-
start = cp+1;
70-
}
71-
cp++;
7267
}
7368
}
7469

compiler/compiler.c2

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public type Options struct {
8484
bool asan;
8585
bool msan;
8686
bool ubsan;
87-
const char* libdir; // no ownership, from environment varible C2_LIBDIR
87+
const char* libdir; // no ownership, from environment variable C2_LIBDIR
8888
char *target_triple;
8989
}
9090

@@ -280,7 +280,7 @@ fn void Compiler.build(Compiler* c,
280280
}
281281
if (c.opts.libdir) {
282282
// parse into multiple, add each to string-pool
283-
string_utils.split_paths(c.opts.libdir, c, handle_path);
283+
string_utils.split_paths(c.opts.libdir, c, Compiler.addLibPath);
284284
}
285285
c.targetInfo.getNative();
286286
}
@@ -684,7 +684,7 @@ fn void Compiler.addGlobalDefine(Compiler* c, const char* prefix, const char* ta
684684
}
685685

686686
// NOTE: paths are not 0-terminated
687-
fn void handle_path(void* arg, const char* path, u32 len) {
687+
fn void Compiler.addLibPath(void* arg, const char* path, u32 len) {
688688
Compiler* c = arg;
689689
u32 path_idx = c.auxPool.add(path, len, true);
690690
//stdio.printf("PATH [%s]\n", c.auxPool.idx2str(path_idx));

0 commit comments

Comments
 (0)