summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/string.c23
-rw-r--r--src/string.h7
2 files changed, 30 insertions, 0 deletions
diff --git a/src/string.c b/src/string.c
index 0925a43..f9340dd 100644
--- a/src/string.c
+++ b/src/string.c
@@ -138,6 +138,12 @@ char *strng_malloc_str(strng_t const *string) {
return s;
}
+void strng_map(strng_t *string, char (F)(char c)) {
+ for (u64 i = 0; i < string->len; i++) {
+ strng_set_ic(string, i, F(*(STRNG_TO(string)+i)));
+ }
+}
+
strng_view_t sv_from_chars(char const* c) { u64 l = strlen(c);
return (strng_view_t){
.start=0,
@@ -203,6 +209,23 @@ void sv_trim_strt_prvc(strng_view_t *sv, char n) {
}
}
+u64 sv_find_from_end_c(strng_view_t const *sv, char n, u64 skip) {
+ if (sv->start > sv->end) return 0;
+
+ u64 i = sv->end;
+ while (i > sv->start) {
+ i--;
+ if (*(sv->string + i) == n) {
+ if (skip) {
+ skip--;
+ continue;
+ }
+ return sv->end - i;
+ }
+ }
+ return sv_len(sv);
+}
+
void sv_set_len_left(strng_view_t *sv, u64 n) {
if (sv->start > sv->end) return;
diff --git a/src/string.h b/src/string.h
index 13a433a..2259582 100644
--- a/src/string.h
+++ b/src/string.h
@@ -33,6 +33,9 @@ strng_t * strng_dup(smrt_arena_t *arena, strng_t const *src);
char *strng_malloc_str(strng_t const *string);
+// Applies F to each char in string and updates the value
+void strng_map(strng_t *string, char (F)(char c));
+
#define STRNG_TO(s) (char *)((u8*)(s)+STRNG_BASE_POS)
#define STRNG_FMT(s) (i32)s->len, (char *)((u8*)(s)+STRNG_BASE_POS)
@@ -99,6 +102,10 @@ void sv_trim_end_nextc(strng_view_t *sv, char n);
// Does nothing if there is no prev char n
void sv_trim_strt_prvc(strng_view_t *sv, char n);
+// Goes backwards from end until character n (skipping n `skip` times)
+// and returns the distance from end
+u64 sv_find_from_end_c(strng_view_t const *sv, char n, u64 skip);
+
// Sets length keeping start
void sv_set_len_left (strng_view_t *sv, u64 n);
// Sets length keeping end