summaryrefslogtreecommitdiff
path: root/src/std/str.c
diff options
context:
space:
mode:
authorsegunkayode1 <segunkayode456@gmail.com>2024-02-26 20:42:44 +1100
committersegunkayode1 <segunkayode456@gmail.com>2024-02-26 20:42:44 +1100
commit055b213beac82f1ca0fb10b2eebce6df3cb216b2 (patch)
tree84935541a3740305aa3dc7b6cbc9565b112af211 /src/std/str.c
parent7e4e763d8f2e15c4f2b2a77402f4fe0c4e8b5f68 (diff)
update
Diffstat (limited to 'src/std/str.c')
-rw-r--r--src/std/str.c22
1 files changed, 13 insertions, 9 deletions
diff --git a/src/std/str.c b/src/std/str.c
index 1e092bc..7dafef1 100644
--- a/src/std/str.c
+++ b/src/std/str.c
@@ -1,6 +1,6 @@
#include "str.h"
#include <string.h>
-
+#include <assert.h>
#include "mem.h"
str8 str8_create(u8* buf, size_t len) { return (str8){ .buf = buf, .len = len }; }
@@ -43,25 +43,29 @@ str8 str8_concat(arena* a, str8 left, str8 right) {
return str8_create(dest, n_bytes);
}
-str8 str8_substr(str8 s, u64 min, u64 max){
- s.buf = s.buf + (ptrdiff_t)min;
- s.len = max - min;
- return s;
+str8 str8_substr(str8 s, u64 min, u64 max) {
+ assert(min >= 0);
+ assert(min < s.len);
+ assert(max >= 0);
+ assert(max <= s.len);
+ uint8_t * start = s.buf + (ptrdiff_t)min;
+ size_t new_len = max - min;
+ return (str8) {.buf = start, .len = new_len };
}
-str8 str8_take(str8 s, u64 first_n){
+str8 str8_take(str8 s, u64 first_n) {
return str8_substr(s, 0, first_n);
}
-str8 str8_drop(str8 s, u64 last_n){
+str8 str8_drop(str8 s, u64 last_n) {
return str8_substr(s, s.len - last_n, s.len);
}
-str8 str8_skip(str8 s, u64 n){
+str8 str8_skip(str8 s, u64 n) {
return str8_substr(s, n, s.len);
}
-str8 str8_chop(str8 s, u64 n){
+str8 str8_chop(str8 s, u64 n) {
return str8_substr(s, 0, s.len - n);
}