aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-07-26 13:28:18 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-07-26 13:28:18 -0400
commitcfe46ee393aa3c9a2344a916bccfc4a69d4b8b77 (patch)
treea5953090e0cb9c9d66b622d91012111ab30a89ea
parent87785555eca3b7166d5f1af658a08f58a64340ed (diff)
Replace heap_strn() with GC_strndup()
-rw-r--r--builtins/util.c14
-rw-r--r--builtins/util.h1
-rw-r--r--parse.c22
-rw-r--r--tomo.c4
-rw-r--r--typecheck.c4
5 files changed, 17 insertions, 28 deletions
diff --git a/builtins/util.c b/builtins/util.c
index f7fa9f92..598ab259 100644
--- a/builtins/util.c
+++ b/builtins/util.c
@@ -11,20 +11,10 @@
public bool USE_COLOR;
-public char *heap_strn(const char *str, size_t len)
-{
- if (!str) return NULL;
- if (len == 0) return "";
- char *heaped = GC_MALLOC_ATOMIC(len + 1);
- memcpy(heaped, str, len);
- heaped[len] = '\0';
- return heaped;
-}
-
public char *heap_str(const char *str)
{
if (!str) return NULL;
- return heap_strn(str, strlen(str));
+ return GC_strndup(str, strlen(str));
}
public char *heap_strf(const char *fmt, ...)
@@ -35,7 +25,7 @@ public char *heap_strf(const char *fmt, ...)
int len = vasprintf(&tmp, fmt, args);
if (len < 0) return NULL;
va_end(args);
- char *ret = heap_strn(tmp, (size_t)len);
+ char *ret = GC_strndup(tmp, (size_t)len);
free(tmp);
return ret;
}
diff --git a/builtins/util.h b/builtins/util.h
index 5b71fa2b..08de9779 100644
--- a/builtins/util.h
+++ b/builtins/util.h
@@ -25,7 +25,6 @@
extern bool USE_COLOR;
-char *heap_strn(const char *str, size_t len);
char *heap_str(const char *str);
char *heap_strf(const char *fmt, ...);
CORD CORD_asprintf(CORD fmt, ...);
diff --git a/parse.c b/parse.c
index 71f85e66..017dd739 100644
--- a/parse.c
+++ b/parse.c
@@ -165,20 +165,20 @@ const char *unescape(const char **out) {
uint8_t buf[bufsize];
(void)u32_to_u8(ustr, bufsize, buf, &bufsize);
*endpos = endptr;
- return heap_strn((char*)buf, bufsize);
+ return GC_strndup((char*)buf, bufsize);
} else if (escape[1] == 'x' && escape[2] && escape[3]) {
char *endptr = NULL;
char c = (char)strtol(escape+2, &endptr, 16);
*endpos = escape + 4;
- return heap_strn(&c, 1);
+ return GC_strndup(&c, 1);
} else if ('0' <= escape[1] && escape[1] <= '7' && '0' <= escape[2] && escape[2] <= '7' && '0' <= escape[3] && escape[3] <= '7') {
char *endptr = NULL;
char c = (char)strtol(escape+1, &endptr, 8);
*endpos = escape + 4;
- return heap_strn(&c, 1);
+ return GC_strndup(&c, 1);
} else {
*endpos = escape + 2;
- return heap_strn(escape+1, 1);
+ return GC_strndup(escape+1, 1);
}
}
@@ -356,7 +356,7 @@ const char *get_word(const char **inout) {
break;
}
*inout = (const char*)pos;
- return heap_strn(word, (size_t)((const char*)pos - word));
+ return GC_strndup(word, (size_t)((const char*)pos - word));
}
const char *get_id(const char **inout) {
@@ -2015,7 +2015,7 @@ PARSER(parse_inline_c) {
CORD c_code = CORD_EMPTY;
while (get_indent(ctx, pos) > indent) {
size_t line_len = strcspn(pos, "\r\n");
- c_code = CORD_all(c_code, heap_strn(pos, line_len), "\n");
+ c_code = CORD_all(c_code, GC_strndup(pos, line_len), "\n");
pos += line_len;
if (whitespace(&pos) == 0) break;
}
@@ -2043,7 +2043,7 @@ PARSER(parse_doctest) {
*output_end = pos + strcspn(pos, "\r\n");
if (output_end <= output_start)
parser_err(ctx, output_start, output_end, "You're missing expected output here");
- output = heap_strn(output_start, (size_t)(output_end - output_start));
+ output = GC_strndup(output_start, (size_t)(output_end - output_start));
pos = output_end;
} else {
pos = expr->end;
@@ -2107,7 +2107,7 @@ PARSER(parse_use) {
size_t name_len = strcspn(pos, " \t\r\n;");
if (name_len < 1)
parser_err(ctx, start, pos, "There is no module name here to use");
- char *name = heap_strn(pos, name_len);
+ char *name = GC_strndup(pos, name_len);
pos += name_len;
while (match(&pos, ";")) continue;
return NewAST(ctx->file, start, pos, Use, .name=name);
@@ -2120,7 +2120,7 @@ PARSER(parse_import) {
size_t path_len = strcspn(pos, " \t\r\n;");
if (path_len < 1)
parser_err(ctx, start, pos, "There is no path here to import");
- char *path = heap_strn(pos, path_len);
+ char *path = GC_strndup(pos, path_len);
pos += path_len;
while (match(&pos, ";")) continue;
return NewAST(ctx->file, start, pos, Import, .path=path);
@@ -2131,7 +2131,7 @@ PARSER(parse_linker) {
if (!match_word(&pos, "!link")) return NULL;
spaces(&pos);
size_t len = strcspn(pos, "\r\n");
- const char *directive = heap_strn(pos, len);
+ const char *directive = GC_strndup(pos, len);
pos += len;
return NewAST(ctx->file, start, pos, LinkerDirective, .directive=directive);
}
@@ -2149,7 +2149,7 @@ ast_t *parse_file(const char *path, jmp_buf *on_err) {
if (path[0] == '<') {
const char *endbracket = strchr(path, '>');
if (!endbracket) return NULL;
- file = spoof_file(heap_strn(path, (size_t)(endbracket + 1 - path)), endbracket + 1);
+ file = spoof_file(GC_strndup(path, (size_t)(endbracket + 1 - path)), endbracket + 1);
} else {
file = load_file(path);
if (!file) return NULL;
diff --git a/tomo.c b/tomo.c
index 631192e4..1e62667b 100644
--- a/tomo.c
+++ b/tomo.c
@@ -282,7 +282,7 @@ int main(int argc, char *argv[])
if (mode == MODE_COMPILE_EXE || executable_status != 0)
return executable_status;
- char *exe_name = heap_strn(filename, strlen(filename) - strlen(".tm"));
+ char *exe_name = GC_strndup(filename, strlen(filename) - strlen(".tm"));
int num_args = argc - after_flags - 1;
char *prog_args[num_args + 2];
prog_args[0] = exe_name;
@@ -459,7 +459,7 @@ int compile_executable(env_t *base_env, const char *filename, CORD object_files)
errx(1, "No main() function has been defined for %s, so it can't be run!", filename);
}
- const char *bin_name = heap_strn(filename, strlen(filename) - strlen(".tm"));
+ const char *bin_name = GC_strndup(filename, strlen(filename) - strlen(".tm"));
FILE *runner = CORD_RUN(autofmt, " | ", cc, " ", cflags, " ", ldflags, " ", ldlibs, " ", object_files, " -x c - -o ", bin_name);
CORD program = CORD_all(
diff --git a/typecheck.c b/typecheck.c
index 0dc7cdc0..52af8371 100644
--- a/typecheck.c
+++ b/typecheck.c
@@ -22,7 +22,7 @@ type_t *parse_type_ast(env_t *env, type_ast_t *ast)
type_t *t = Table$str_get(*env->types, name);
if (t) return t;
while (strchr(name, '.')) {
- char *module_name = heap_strn(name, strcspn(name, "."));
+ char *module_name = GC_strndup(name, strcspn(name, "."));
binding_t *b = get_binding(env, module_name);
if (!b || b->type->tag != ModuleType)
code_err(ast, "I don't know a module with the name '%s'", module_name);
@@ -139,7 +139,7 @@ static env_t *load_module(env_t *env, ast_t *module_ast)
*module_env->libname = (CORD)libname_id;
for (int64_t i = 1; i <= files_f->num_lines; i++) {
const char *line = get_line(files_f, i);
- line = heap_strn(line, strcspn(line, "\r\n"));
+ line = GC_strndup(line, strcspn(line, "\r\n"));
if (!line || line[0] == '\0') continue;
const char *tm_path = resolve_path(line, resolved_path, ".");
if (!tm_path) errx(1, "Couldn't find library %s dependency: %s", libname, line);