From a5788ba94cbbd0c165462e64d66e3063a5d5e524 Mon Sep 17 00:00:00 2001 From: Tenari Date: Tue, 2 Jun 2026 07:33:28 -0500 Subject: [PATCH] more string fns --- all.h | 7 +++++-- string.c | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/all.h b/all.h index d126b95..3378a4a 100644 --- a/all.h +++ b/all.h @@ -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(); diff --git a/string.c b/string.c index 57659c2..2f701e6 100644 --- a/string.c +++ b/string.c @@ -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; +} +