more string fns
This commit is contained in:
@@ -345,8 +345,8 @@ typedef struct Codepoint {
|
||||
} Codepoint;
|
||||
|
||||
typedef struct TextPos {
|
||||
i64 line;
|
||||
i64 column;
|
||||
u32 line;
|
||||
u32 column;
|
||||
} TextPos;
|
||||
|
||||
typedef struct TextRange {
|
||||
@@ -680,6 +680,9 @@ fn String stringFromRawCodepoint(Arena* a, u32 c);
|
||||
fn bool stringInsertCodepointAtByte(String* s, Codepoint c, u32 byte_offset, bool keep_trailing_zero);
|
||||
fn u8 stringDeleteCodepointAtByte(String* s, u32 byte_offset);
|
||||
fn bool stringDeleteCodepointsBetweenByteOffsetsInclusive(String* s, u32 start, u32 end);
|
||||
fn u32 stringCountLines(String s);
|
||||
fn u32 stringLineLen(String s, u32 line_number);
|
||||
fn u32 stringLineStartByteOffset(String s, u32 line_number);
|
||||
|
||||
///// OS-wrapped apis
|
||||
void osInit();
|
||||
|
||||
@@ -428,3 +428,41 @@ fn StringUTF16Const str16FromStr8(Arena* arena, String string) {
|
||||
return result;
|
||||
}
|
||||
|
||||
fn u32 stringCountLines(String s) {
|
||||
u32 result = 1;
|
||||
for (u32 i = 0; i < s.length; i++) {
|
||||
if (s.bytes[i] == '\n') {
|
||||
result += 1;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
fn u32 stringLineLen(String s, u32 line_number) {
|
||||
u32 result = 0;
|
||||
u32 current_line = 0;
|
||||
for (u32 i = 0; i < s.length && current_line <= line_number; i++) {
|
||||
if (current_line == line_number) {
|
||||
result += 1;
|
||||
}
|
||||
if (s.bytes[i] == '\n') {
|
||||
current_line += 1;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
fn u32 stringLineStartByteOffset(String s, u32 line_number) {
|
||||
u32 current_line = 0;
|
||||
for (u32 i = 0; i < s.length; i++) {
|
||||
// do this check first, so we won't return '\n' character pos, but the following char pos
|
||||
if (current_line == line_number) {
|
||||
return i;
|
||||
}
|
||||
if (s.bytes[i] == '\n') {
|
||||
current_line += 1;
|
||||
}
|
||||
}
|
||||
return s.length;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user