another helper for strings

This commit is contained in:
Tenari
2026-06-06 11:35:30 -05:00
parent 22dec9e742
commit 9c6c72f964
2 changed files with 28 additions and 2 deletions
+2
View File
@@ -684,6 +684,8 @@ fn u32 stringCountLines(String s);
fn u32 stringLineLen(String s, u32 line_number); fn u32 stringLineLen(String s, u32 line_number);
fn u32 stringLineStartByteOffset(String s, u32 line_number); fn u32 stringLineStartByteOffset(String s, u32 line_number);
fn bool stringInsertBytesAt(String* s, str bytes, u32 at); fn bool stringInsertBytesAt(String* s, str bytes, u32 at);
fn bool stringInsertBytesAtMaybeAlloc(Arena* a, String* s, str bytes, u32 at);
fn u32 stringFindNextCharPosFrom(String s, u32 from, u8 c);
///// OS-wrapped apis ///// OS-wrapped apis
void osInit(); void osInit();
+26 -2
View File
@@ -472,8 +472,10 @@ fn bool stringInsertBytesAt(String* s, str bytes, u32 at) {
return false; return false;
} }
for (u32 i = 0; i < len; i++) { for (u32 i = 0; i < len; i++) {
// move the original byte over by `len` // shift all the following bytes over by one, starting from the end and working toward `at`
s->bytes[at + i + len] = s->bytes[at + i]; for (u32 ii = s->length; ii > at+i; ii--) {
s->bytes[ii] = s->bytes[ii-1];
}
// copy the new byte in // copy the new byte in
s->bytes[at + i] = bytes[i]; s->bytes[at + i] = bytes[i];
s->length += 1; s->length += 1;
@@ -481,3 +483,25 @@ fn bool stringInsertBytesAt(String* s, str bytes, u32 at) {
return true; return true;
} }
// will alloc a new string over top of s.bytes if it didnt have enough space
// this is destructive of the pointer
// may cause mem-leak if you arent clearing the arena correctly
fn bool stringInsertBytesAtMaybeAlloc(Arena* a, String* s, str bytes, u32 at) {
bool is_already_enough_space = stringInsertBytesAt(s, bytes, at);
if (!is_already_enough_space) {
ptr old_bytes = s->bytes;
s->bytes = arenaAllocZero(a, s->capacity + 64);
MemoryCopy(s->bytes, old_bytes, s->length);
return stringInsertBytesAt(s, bytes, at);
}
return true;
}
fn u32 stringFindNextCharPosFrom(String s, u32 from, u8 c) {
for (u32 i = from; i < s.length; i++) {
if (s.bytes[i] == c) {
return i;
}
}
return s.length;
}