diff options
| author | dacctal <donotcontactmevia@email.invalid> | 2026-06-29 18:29:38 +0000 |
|---|---|---|
| committer | dacctal <donotcontactmevia@email.invalid> | 2026-06-29 18:29:38 +0000 |
| commit | b8a96b5fa7ccb6b170b1fb40c5ea4efe4519e568 (patch) | |
| tree | df85e5ad0cbfcc78da3e7450672e1a753b7cd9b0 /src/pkgit_string.c | |
| parent | d85350854dca4c6495cb78c89ff4934c7a908509 (diff) | |
initialized rewrite
Diffstat (limited to 'src/pkgit_string.c')
| -rw-r--r-- | src/pkgit_string.c | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/src/pkgit_string.c b/src/pkgit_string.c new file mode 100644 index 0000000..cbcc200 --- /dev/null +++ b/src/pkgit_string.c @@ -0,0 +1,60 @@ +#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]); + } +} + +str_s slice_from_cstr(char* cstr) { + return (str_s) { + .data = cstr, + .len = strlen(cstr), + }; +} + +int slice_eq(str_s a, str_s 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; + } + return 1; +} + +int slice_starts_with(str_s slice, str_s prefix) { + if (prefix.len > slice.len) return 0; + for (size_t i = 0; i < prefix.len; i++) { + if (slice.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, + .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_s slice_trim(str_s slice) { + while (slice.len > 0 && isspace((unsigned char)slice.data[0])) { + slice = slice_drop(slice, 1); + } + while (slice.len > 0 && isspace((unsigned char)slice.data[slice.len - 1])) { + slice = slice_take(slice, slice.len - 1); + } + return slice; +} |
