more string shit

This commit is contained in:
Tenari
2026-05-24 20:53:11 -05:00
parent 02a44bcfe3
commit 314f30d632
2 changed files with 22 additions and 3 deletions
+3 -1
View File
@@ -675,9 +675,11 @@ fn bool codepointIsWhitespace(Codepoint c);
fn Codepoint codepointFromBytes(ptr bytes, u32 offset); fn Codepoint codepointFromBytes(ptr bytes, u32 offset);
fn Codepoint codepointFromBytesBefore(ptr bytes, u32 offset); fn Codepoint codepointFromBytesBefore(ptr bytes, u32 offset);
fn Codepoint codepointFromRawInt(u32 c); fn Codepoint codepointFromRawInt(u32 c);
fn void codepointFillBuf(Codepoint cp, ptr buf);
fn String stringFromRawCodepoint(Arena* a, u32 c); fn String stringFromRawCodepoint(Arena* a, u32 c);
fn bool stringInsertCodepointAtByte(String* s, Codepoint c, u32 byte_offset); fn bool stringInsertCodepointAtByte(String* s, Codepoint c, u32 byte_offset);
fn bool stringDeleteCodepointAtByte(String* s, u32 byte_offset); fn u8 stringDeleteCodepointAtByte(String* s, u32 byte_offset);
fn bool stringDeleteCodepointsBetweenByteOffsetsInclusive(String* s, u32 start, u32 end);
///// OS-wrapped apis ///// OS-wrapped apis
void osInit(); void osInit();
+19 -2
View File
@@ -261,8 +261,10 @@ fn bool stringInsertCodepointAtByte(String* s, Codepoint c, u32 byte_offset) {
return true; return true;
} }
fn bool stringDeleteCodepointAtByte(String* s, u32 byte_offset) { fn u8 stringDeleteCodepointAtByte(String* s, u32 byte_offset) {
if (s->length < byte_offset) return false; // returns number of bytes deleted
if (s->length < byte_offset) return 0;
if (s->length == 0) return 0;
Codepoint cp = codepointFromBytes(s->bytes, byte_offset); Codepoint cp = codepointFromBytes(s->bytes, byte_offset);
// shift all the bytes from the back towards the byte_offset // shift all the bytes from the back towards the byte_offset
@@ -272,6 +274,21 @@ fn bool stringDeleteCodepointAtByte(String* s, u32 byte_offset) {
} }
s->length -= 1; s->length -= 1;
} }
return cp.size;
}
fn bool stringDeleteCodepointsBetweenByteOffsetsInclusive(String* s, u32 start, u32 end) {
if (end < start) return false;
if (s->length < start || s->length < end) return false;
if (s->length == 0) return false;
u32 codepoint_size = 0;
for (u32 i = 0; i < (end - start); i+=codepoint_size) {
codepoint_size = stringDeleteCodepointAtByte(s, start);
if (codepoint_size == 0) {
return false;
}
}
return true; return true;
} }