aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordacctal <donotcontactmevia@email.invalid>2026-06-29 23:58:00 +0000
committerdacctal <donotcontactmevia@email.invalid>2026-06-29 23:58:00 +0000
commite54a7dbdbb7c65d50f713f5f816dcb884259adea (patch)
treebb8345c737bacfae1415cf06a22110b54b6c0c59
parentb8a96b5fa7ccb6b170b1fb40c5ea4efe4519e568 (diff)
string concatenation (probably)
-rw-r--r--TODO.md31
-rw-r--r--include/banned.h67
-rw-r--r--include/cla_parse.h26
-rw-r--r--include/pkgit_globals.h79
-rw-r--r--include/pkgit_string.h43
-rw-r--r--src/cla_parse.c164
-rw-r--r--src/main.c38
-rw-r--r--src/pkgit_globals.c29
-rw-r--r--src/pkgit_string.c95
9 files changed, 519 insertions, 53 deletions
diff --git a/TODO.md b/TODO.md
index fd27956..fc1b4d7 100644
--- a/TODO.md
+++ b/TODO.md
@@ -1,19 +1,18 @@
-- [x] add pkg repo
-- [x] build pkg
-- [x] install pkg repo
-- [x] install pkg name
-- [x] remove pkg
-- [x] lua build systems
-- [x] lua repos
-- [x] CLA parsing
-- [x] lua config primary(/etc) / secondary(.config) checks
-- [x] lua variables for install paths
-- [x] lua dependency listing
-- [x] version management
-- [x] lua uninstall function
-- [x] git depth 1
-- [x] lua on_update function
-- [-] lua-configured privilege escalation
+- [-] cla parsing
+- [ ] lua config primary(/etc) / secondary(.config) checks
+- [ ] lua variables for install paths
+- [ ] lua repos
+- [ ] pkg add repo
+- [ ] lua build systems
+- [ ] pkg build
+- [ ] lua dependency listing
+- [ ] pkg install repo
+- [ ] pkg install name
+- [ ] pkg remove
+- [ ] version management
+- [ ] lua on_update function
+- [ ] lua-configured privilege escalation
+- [ ] git depth 1
- [ ] multiple jobs
- [ ] (maybe) git commit signature checks
diff --git a/include/banned.h b/include/banned.h
new file mode 100644
index 0000000..3ea7dd4
--- /dev/null
+++ b/include/banned.h
@@ -0,0 +1,67 @@
+/*
+
+ pkgit - package it!
+
+ Copyright (C) 2026 dacctal
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+*/
+
+#ifndef PKGIT_BANNED_H
+#define PKGIT_BANNED_H
+
+/*
+ * This header lists functions that have been banned from our code base,
+ * because they're too easy to misuse (and even if used correctly,
+ * complicate audits). Including this header turns them into compile-time
+ * errors.
+ */
+
+#define BANNED(func) sorry_##func##_is_a_banned_function
+
+#undef strcpy
+#define strcpy(x,y) BANNED(strcpy)
+#undef strcat
+#define strcat(x,y) BANNED(strcat)
+#undef strncpy
+#define strncpy(x,y,n) BANNED(strncpy)
+#undef strncat
+#define strncat(x,y,n) BANNED(strncat)
+#undef strtok
+#define strtok(x,y) BANNED(strtok)
+#undef strtok_r
+#define strtok_r(x,y,z) BANNED(strtok_r)
+
+#undef sprintf
+#undef vsprintf
+#define sprintf(...) BANNED(sprintf)
+#define vsprintf(...) BANNED(vsprintf)
+
+#undef gmtime
+#define gmtime(t) BANNED(gmtime)
+#undef localtime
+#define localtime(t) BANNED(localtime)
+#undef ctime
+#define ctime(t) BANNED(ctime)
+#undef ctime_r
+#define ctime_r(t, buf) BANNED(ctime_r)
+#undef asctime
+#define asctime(t) BANNED(asctime)
+#undef asctime_r
+#define asctime_r(t, buf) BANNED(asctime_r)
+
+#undef mktemp
+#define mktemp(x) BANNED(mktemp)
+
+#endif /* BANNED_H */
diff --git a/include/cla_parse.h b/include/cla_parse.h
new file mode 100644
index 0000000..eca1395
--- /dev/null
+++ b/include/cla_parse.h
@@ -0,0 +1,26 @@
+/*
+
+ pkgit - package it!
+
+ Copyright (C) 2026 dacctal
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+*/
+
+#ifndef PKGIT_CLA_PARSE_H
+#define PKGIT_CLA_PARSE_H
+
+void cla_parse(int argc, char **argv);
+
+#endif \ No newline at end of file
diff --git a/include/pkgit_globals.h b/include/pkgit_globals.h
new file mode 100644
index 0000000..b1e76ed
--- /dev/null
+++ b/include/pkgit_globals.h
@@ -0,0 +1,79 @@
+#ifndef PKGIT_GLOBALS_H
+#define PKGIT_GLOBALS_H
+#include "pkgit_string.h"
+
+typedef struct {
+ str_slc name;
+ str_slc url;
+ str_slc version;
+ str_slc target;
+ str_slc src;
+ int is_local;
+} package;
+
+#define VERSION slc_from_cstr("1.4.0_INDEV")
+#define RED slc_from_cstr("\x1b[0;31m")
+#define GREEN slc_from_cstr("\x1b[0;32m")
+#define YELLOW slc_from_cstr("\x1b[0;33m")
+#define BLUE slc_from_cstr("\x1b[0;34m")
+#define MAGENTA slc_from_cstr("\x1b[0;35m")
+#define CYAN slc_from_cstr("\x1b[0;36m")
+#define GRAY slc_from_cstr("\x1b[0;37m")
+#define BRIGHT_RED slc_from_cstr("\x1b[0;91m")
+#define BRIGHT_GREEN slc_from_cstr("\x1b[0;92m")
+#define BRIGHT_YELLOW slc_from_cstr("\x1b[0;93m")
+#define BRIGHT_BLUE slc_from_cstr("\x1b[0;94m")
+#define BRIGHT_MAGENTA slc_from_cstr("\x1b[0;95m")
+#define BRIGHT_CYAN slc_from_cstr("\x1b[0;96m")
+#define BRIGHT_GRAY slc_from_cstr("\x1b[0;97m")
+#define BOLD_RED slc_from_cstr("\x1b[1;31m")
+#define BOLD_GREEN slc_from_cstr("\x1b[1;32m")
+#define BOLD_YELLOW slc_from_cstr("\x1b[1;33m")
+#define BOLD_BLUE slc_from_cstr("\x1b[1;34m")
+#define BOLD_MAGENTA slc_from_cstr("\x1b[1;35m")
+#define BOLD_CYAN slc_from_cstr("\x1b[1;36m")
+#define BOLD_GRAY slc_from_cstr("\x1b[1;37m")
+#define BOLD_WHITE slc_from_cstr("\x1b[1;38m")
+#define BOLD_BRIGHT_RED slc_from_cstr("\x1b[1;91m")
+#define BOLD_BRIGHT_GREEN slc_from_cstr("\x1b[1;92m")
+#define BOLD_BRIGHT_YELLOW slc_from_cstr("\x1b[1;93m")
+#define BOLD_BRIGHT_BLUE slc_from_cstr("\x1b[1;94m")
+#define BOLD_BRIGHT_MAGENTA slc_from_cstr("\x1b[1;95m")
+#define BOLD_BRIGHT_CYAN slc_from_cstr("\x1b[1;96m")
+#define BOLD_BRIGHT_GRAY slc_from_cstr("\x1b[1;97m")
+#define ITALIC slc_from_cstr("\x1b[3m")
+#define COLOR_RESET slc_from_cstr("\x1b[0m")
+
+#define STR_BUFFER slc_from_cstr("")
+
+#define PRINT_PKGIT slc_cat( \
+ slc_cat( \
+ slc_cat(BOLD_YELLOW, slc_from_cstr("[")), \
+ slc_cat(BOLD_MAGENTA, slc_from_cstr("pkgit")), \
+ &STR_BUFFER \
+ ), slc_cat( \
+ slc_cat(BOLD_MAGENTA, slc_from_cstr("]")), \
+ COLOR_RESET, &STR_BUFFER \
+ ), &STR_BUFFER \
+)
+#define PRINT_SUCCESS slc_cat( \
+ slc_cat(PRINT_PKGIT, GREEN), \
+ slc_cat(slc_from_cstr(" [SUCCESS]"), COLOR_RESET) \
+)
+#define PRINT_SKIPPED slc_cat( \
+ slc_cat(PRINT_PKGIT, BLUE), \
+ slc_cat(slc_from_cstr(" [SKIPPED]"), COLOR_RESET) \
+)
+#define PRINT_WARNING slc_cat( \
+ slc_cat(PRINT_PKGIT, YELLOW), \
+ slc_cat(slc_from_cstr(" [WARNING]"), COLOR_RESET) \
+)
+#define PRINT_ERROR slc_cat( \
+ slc_cat(PRINT_PKGIT, RED), \
+ slc_cat(slc_from_cstr(" [ERROR]"), COLOR_RESET) \
+)
+
+extern int is_verbose;
+extern int is_forced;
+
+#endif
diff --git a/include/pkgit_string.h b/include/pkgit_string.h
index ae6098c..c81bcbd 100644
--- a/include/pkgit_string.h
+++ b/include/pkgit_string.h
@@ -1,3 +1,23 @@
+/*
+
+ pkgit - package it!
+
+ Copyright (C) 2026 dacctal
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+*/
+
#ifndef PKGIT_STRING_H
#define PKGIT_STRING_H
#include <stddef.h>
@@ -5,7 +25,7 @@
typedef struct {
char* data;
size_t len;
-} str_s;
+} str_slc;
typedef struct {
char* data;
@@ -13,15 +33,18 @@ typedef struct {
size_t len;
} str;
-#define format_string(s) (int)((s).len), (s).data
-// Usage: printf("%.*s", format_string(ss));
+#define str_fmt(slice) (int)((slice).len), (slice).data
+// Usage: printf("%.*s", format_string(slice));
-void print_slice(str_s s);
-str_s slice_from_cstr(char* cstr);
-int slice_eq(str_s a, str_s b);
-int slice_starts_with(str_s slice, str_s prefix);
-str_s slice_take(str_s slice, size_t n);
-str_s slice_drop(str_s slice, size_t n);
-str_s slice_trim(str_s slice);
+void print_slice(str_slc s);
+str_slc slc_from_cstr(char* cstr);
+#define slc_from_cstr(cstr) slc_from_cstr(cstr)
+int slc_eq(str_slc a, str_slc b);
+int slc_starts_with(str_slc slice, str_slc prefix);
+str_slc slc_take(str_slc slice, size_t n);
+str_slc slc_drop(str_slc slice, size_t n);
+str_slc slc_trim(str_slc slice);
+void slc_cat(str_slc first, str_slc second, str_slc *new_slc);
+//#define slc_cat(first, second, new_slc) slc_cat(first, second, *new_slc)
#endif
diff --git a/src/cla_parse.c b/src/cla_parse.c
new file mode 100644
index 0000000..a0f3d43
--- /dev/null
+++ b/src/cla_parse.c
@@ -0,0 +1,164 @@
+/*
+
+ pkgit - package it!
+
+ Copyright (C) 2026 dacctal
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+*/
+
+#include <stdio.h>
+#include <string.h>
+
+#include "cla_parse.h"
+
+#include "pkgit_globals.h"
+#include "pkgit_string.h"
+
+//#include "declare.h"
+//#include "deps_resolve.h"
+//#include "help.h"
+//#include "name_from_url.h"
+//#include "pkg_build.h"
+//#include "pkg_create.h"
+//#include "pkg_search.h"
+//#include "pkg_install.h"
+//#include "pkg_list.h"
+//#include "pkg_remove.h"
+//#include "pkgit_globals.h"
+//#include "repo_add.h"
+//#include "update.h"
+
+#define COMMAND(large, small, code) \
+ if (slc_eq(slc_from_cstr(argv[i]), slc_from_cstr(large)) \
+ || slc_eq(slc_from_cstr(argv[i]), slc_from_cstr(small))) code
+
+#define NOT_ENOUGH_ARGS(arg, next) \
+ printf("%.*s Not enough arguments! Try: `pkgit %s [%s]`\n", \
+ str_fmt(PRINT_ERROR), arg, next)
+
+void cmd_add(char **argv, int i) {
+ if (argv[i + 1]) {
+ printf("add repo %s", argv[i + 1]);
+ //repo_add(argv[i + 1], name_from_url(argv[i + 1]));
+ } else {
+// NOT_ENOUGH_ARGS(argv[i], "url");
+ }
+}
+
+void cmd_build(int argc, char **argv, int i, package pkg) {
+ if (argv[i + 1]) {
+ for (int j = i + 1; j < argc; j++) {
+ if (argv[j][0] == '-') continue;
+ printf("build pkg %s", argv[j]);
+ //pkg = pkg_create(argv[j]);
+ //pkg_build(pkg);
+ }
+ } else {
+ printf("build pkg .");
+ //pkg = pkg_create(".");
+ //pkg_build(pkg);
+ }
+}
+
+void cmd_install(int argc, char **argv, int i, package pkg) {
+ if (argv[i + 1]) {
+ for (int j = i + 1; j < argc; j++) {
+ if (argv[j][0] == '-') continue;
+ printf("install pkg %s", argv[j]);
+ //pkg = pkg_create(argv[j]);
+ //pkg_install(pkg);
+ }
+ } else {
+ //NOT_ENOUGH_ARGS(argv[i], "url/pkg");
+ }
+}
+
+void cmd_remove(int argc, char **argv, int i, package pkg) {
+ if (argv[i + 1]) {
+ for (int j = i + 1; j < argc; j++) {
+ if (argv[j][0] == '-') continue;
+ printf("remove pkg %s", argv[j]);
+ //pkg = pkg_create(argv[j]);
+ //pkg_remove(pkg);
+ }
+ } else {
+ //NOT_ENOUGH_ARGS(argv[i], "url/pkg");
+ }
+}
+
+void flags_mod(char **argv, int i) {
+ for (size_t j = 1; j < strlen(argv[i]); j++) {
+ switch (argv[i][j]) {
+ case 'q': is_verbose = 0; break;
+ case 'f': is_forced = 1; break;
+ default: break;
+ }
+ }
+}
+
+void flags_cmd(int argc, char **argv, int i, package pkg) {
+ for (size_t j = 1; j < strlen(argv[i]); j++) {
+ switch (argv[i][j]) {
+ case 'a': cmd_add(argv, i); break;
+ case 'b': cmd_build(argc, argv, i, pkg); break;
+ case 'c': /*deps_resolve();*/ printf("deps_resolve\n"); break;
+ case 'd': /*declare();*/ printf("declare\n"); break;
+ case 'i': cmd_install(argc, argv, i, pkg); break;
+ case 'r': cmd_remove(argc, argv, i, pkg); break;
+ case 'u': /*update();*/ printf("update\n"); break;
+ case 'l': /*pkgs_list();*/ printf("list\n"); break;
+ case 's': /*search(argv[i + 1]);*/ printf("search\n"); break;
+ case 'v': printf("%.*s\n", str_fmt(VERSION)); break;
+ case 'h': /*help();*/ printf("help\n"); break;
+ default: break;
+ }
+ }
+}
+
+void flags_parse(int argc, char **argv, package pkg) {
+ for (int i = 1; i < argc; i++) {
+ if (argv[i][0] != '-') continue;
+ if (argv[i][1] == '-') {
+ COMMAND("--quiet", "-q", { is_verbose = 0; });
+ COMMAND("--force", "-f", { is_forced = 1; });
+ } else {
+ flags_mod(argv, i);
+ flags_cmd(argc, argv, i, pkg);
+ }
+ }
+}
+
+void cmds_parse(int argc, char **argv, package pkg) {
+ for (int i = 1; i < argc; i++) {
+ COMMAND("--add", "a", { cmd_add(argv, i); });
+ COMMAND("--build", "b", { cmd_build(argc, argv, i, pkg); });
+ COMMAND("--install", "i", { cmd_install(argc, argv, i, pkg); });
+ COMMAND("--remove", "r", { cmd_remove(argc, argv, i, pkg); });
+ COMMAND("--update", "u", { /*update();*/ });
+ COMMAND("--declare", "d", { /*declare();*/ });
+ COMMAND("--list", "l", { /*pkgs_list();*/ });
+ COMMAND("--search", "s", { /*search(argv[i + 1]);*/ });
+ COMMAND("--version", "v", { printf("%.*s\n", str_fmt(VERSION)); });
+ COMMAND("--help", "h", { /*help();*/ });
+ COMMAND("--check", "c", { /*deps_resolve();*/ });
+ }
+}
+
+void cla_parse(int argc, char **argv) {
+ if (!argv[1]) { /*help();*/ return; }
+ package pkg = {0};
+ flags_parse(argc, argv, pkg);
+ cmds_parse(argc, argv, pkg);
+}
diff --git a/src/main.c b/src/main.c
index fd0732c..eebd9ce 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,9 +1,41 @@
+/*
+ pkgit - package it!
+
+ Copyright (C) 2026 dacctal
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+*/
+
#include <stdio.h>
-#include "pkgit_string.h"
+#include "cla_parse.h"
+#include "pkgit_string.h"
+#include "pkgit_globals.h"
int main(int argc, char** argv) {
- str_s slice = slice_trim(slice_from_cstr(" hello world "));
- printf("[%.*s]\n", format_string(slice));
+ //slc_cat(BOLD_YELLOW, slc_from_cstr("["))
+ //slc_cat(BOLD_MAGENTA, slc_from_cstr("pkgit"))
+ //slc_cat(BOLD_MAGENTA, slc_from_cstr("]"))
+ //COLOR_RESET
+ str_slc hello_world = {"", 0};
+ slc_cat(slc_from_cstr("hello"), slc_from_cstr(" world"), &hello_world);
+ str_slc test = {"hello", 5};
+ printf("%.*s\n", str_fmt(test));
+ printf("%.*s%s%.*s\n", str_fmt(BOLD_YELLOW), "TEST", str_fmt(COLOR_RESET));
+ printf("length: %zu\nstring: %.*s\n", hello_world.len, str_fmt(hello_world));
+ str_slc hello_world_ext = {"", 0};
+ slc_cat(slc_from_cstr(hello_world.data), slc_from_cstr("!!!"), &hello_world_ext);
+ printf("length: %zu\nstring: %.*s\n", hello_world_ext.len, str_fmt(hello_world_ext));
return 0;
}
diff --git a/src/pkgit_globals.c b/src/pkgit_globals.c
new file mode 100644
index 0000000..094eef8
--- /dev/null
+++ b/src/pkgit_globals.c
@@ -0,0 +1,29 @@
+int is_verbose = 1;
+int is_forced = 0;
+
+//void init_vars(void) {
+// char *home = getenv("HOME");
+// if (home) snprintf(home_dir, MAX_PATH_LEN, "%s", home);
+// else snprintf(home_dir, MAX_PATH_LEN, "/root");
+//
+// is_root_config = file_exists(root_config);
+//
+// if (is_root_config) snprintf(config_dir, MAX_PATH_LEN, "/etc/pkgit");
+// else snprintf(config_dir, MAX_PATH_LEN, "%s/.config/pkgit", home_dir);
+//
+// snprintf(config_file, MAX_PATH_LEN, "%s/init.lua", config_dir);
+// snprintf(repo_file, MAX_PATH_LEN, "%s/repos.lua", config_dir);
+//
+// config_exists = file_exists(root_config) || file_exists(home_config());
+//
+// snprintf(bin, MAX_PATH_LEN, "%s/.local/bin", home_dir);
+// snprintf(lib, MAX_PATH_LEN, "%s/.local/lib", home_dir);
+// snprintf(inc, MAX_PATH_LEN, "%s/.local/include", home_dir);
+// snprintf(src, MAX_PATH_LEN, "%s/.local/share/pkgit", home_dir);
+//
+// install_directories[0] = config_dir;
+// install_directories[1] = strdup(get_install_dir("bin"));
+// install_directories[2] = strdup(get_install_dir("lib"));
+// install_directories[3] = strdup(get_install_dir("inc"));
+// install_directories[4] = strdup(get_install_dir("src"));
+//}
diff --git a/src/pkgit_string.c b/src/pkgit_string.c
index cbcc200..6f8a0a6 100644
--- a/src/pkgit_string.c
+++ b/src/pkgit_string.c
@@ -1,23 +1,43 @@
+/*
+
+ pkgit - package it!
+
+ Copyright (C) 2026 dacctal
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+*/
+
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include "pkgit_string.h"
-void print_slice(str_s slice) {
- for (size_t i = 0; i < slice.len; i++) {
- putchar(slice.data[i]);
+void slc_print(str_slc slc) {
+ for (size_t i = 0; i < slc.len; i++) {
+ putchar(slc.data[i]);
}
}
-str_s slice_from_cstr(char* cstr) {
- return (str_s) {
+str_slc slc_from_cstr(char* cstr) {
+ return (str_slc) {
.data = cstr,
.len = strlen(cstr),
};
}
-int slice_eq(str_s a, str_s b) {
+int slc_eq(str_slc a, str_slc b) {
if (a.len != b.len) return 0;
for (size_t i = 0; i < a.len; i++) {
if (a.data[i] != b.data[i]) return 0;
@@ -25,36 +45,63 @@ int slice_eq(str_s a, str_s b) {
return 1;
}
-int slice_starts_with(str_s slice, str_s prefix) {
- if (prefix.len > slice.len) return 0;
+int slc_starts_with(str_slc slc, str_slc prefix) {
+ if (prefix.len > slc.len) return 0;
for (size_t i = 0; i < prefix.len; i++) {
- if (slice.data[i] != prefix.data[i]) return 0;
+ if (slc.data[i] != prefix.data[i]) return 0;
}
return 1;
}
-str_s slice_take(str_s slice, size_t n) {
- if (n > slice.len) n = slice.len;
- return (str_s) {
- .data = slice.data,
+str_slc slc_take(str_slc slc, size_t n) {
+ if (n > slc.len) n = slc.len;
+ return (str_slc) {
+ .data = slc.data,
.len = n
};
}
-str_s slice_drop(str_s slice, size_t n) {
- if (n > slice.len) n = slice.len;
- return (str_s) {
- .data = slice.data + n,
- .len = slice.len - n
+str_slc slc_drop(str_slc slc, size_t n) {
+ if (n > slc.len) n = slc.len;
+ return (str_slc) {
+ .data = slc.data + n,
+ .len = slc.len - n
};
}
-str_s slice_trim(str_s slice) {
- while (slice.len > 0 && isspace((unsigned char)slice.data[0])) {
- slice = slice_drop(slice, 1);
+str_slc slc_trim(str_slc slc) {
+ while (slc.len > 0 && isspace((unsigned char)slc.data[0])) {
+ slc = slc_drop(slc, 1);
+ }
+ while (slc.len > 0 && isspace((unsigned char)slc.data[slc.len - 1])) {
+ slc = slc_take(slc, slc.len - 1);
+ }
+ return slc;
+}
+
+str_slc slc_split(str_slc *slc, char delimiter) {
+ size_t i = 0;
+ while(i < slc->len && slc->data[i] != delimiter) {
+ i++;
+ }
+
+ str_slc result = slc_take(*slc, i);
+ if (i < slc->len) {
+ *slc = slc_drop(*slc, i + i);
+ } else {
+ *slc = slc_drop(*slc, i);
+ }
+ return result;
+}
+
+void slc_cat(str_slc first, str_slc second, str_slc *new_slc) {
+ new_slc->len = first.len + second.len + 1;
+ char buf[new_slc->len];
+ for (size_t i = 0; i < first.len; i++) {
+ buf[i] = first.data[i];
}
- while (slice.len > 0 && isspace((unsigned char)slice.data[slice.len - 1])) {
- slice = slice_take(slice, slice.len - 1);
+ for (size_t i = first.len; i < new_slc->len; i++) {
+ buf[i] = second.data[i-first.len];
}
- return slice;
+ new_slc->data = buf;
}