From b3345df5bf97b14acf02ebc8ba35c545603d0201 Mon Sep 17 00:00:00 2001 From: Kevin Trogant Date: Fri, 12 Dec 2025 11:27:16 +0100 Subject: [PATCH] S8Split2 --- rtcore.h | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/rtcore.h b/rtcore.h index 5114914..bdc4672 100644 --- a/rtcore.h +++ b/rtcore.h @@ -212,6 +212,10 @@ RTC_API u8 *S8End(s8 s); /* Splits the given string s at the first occurence of c. * If c does not occur, split_result.first contains the whole string */ RTC_API split_result S8Split(s8 s, u8 c); +/* Splits the given string s at the first occurence of c. + * If c does not occur, split_result.first contains the whole string. + * This version also consumes repeating occurences of c */ +RTC_API split_result S8Split2(s8 s, u8 c); /* Creates a clone of string s on arena a */ RTC_API s8 S8Clone(s8 s, arena *a); @@ -254,6 +258,43 @@ RTC_API b32 WriteEntireFile(s8 path, byte *data, isize length); #define AtomicLoadAcquire(_ptr) _InterlockedOr_HLEAcquire(_ptr, 0) #endif +/* Aliases for intrinsics */ +#if defined(__GNUC__) || defined(__clang__) + #define CTZ32(ui) __builtin_ctz(ui) + #define CTZ64(ul) __builtin_ctzl(ul) + #define CLZ32(ui) __builtin_clz(ui) + #define CLZ64(ul) __builtin_clzl(ul) + #define PopCount32(_x) __builtin_popcount(_x) + #define PopCount64(_x) __builtin_popcountl(_x) +#elif defined(_MSC_VER) +static force_inline unsigned int CTZ32(u32 x) +{ + unsigned int index; + _BitScanReverse(&index, x); + return index; +} +static force_inline unsigned int CTZ64(u64 x) +{ + unsigned int index; + _BitScanReverse64(&index, x); + return index; +} +static force_inline unsigned int CLZ32(u32 x) +{ + unsigned int index; + _BitScanForward(&index, x); + return index; +} +static force_inline unsigned int CLZ64(u64 x) +{ + unsigned int index; + _BitScanForward64(&index, x); + return index; +} + #define PopCount32(_x) __popcnt(_x) + #define PopCount64(_x) __popcnt64(_x) +#endif + /* Threading wrapper */ typedef struct thread thread; @@ -391,6 +432,21 @@ S8Split(s8 s, u8 c) S8Span(chr + 1, S8End(s) + 1), }; } +RTC_API split_result +S8Split2(s8 s, u8 c) +{ + u8 *chr = S8Chr(s, c); + if (!chr) + return (split_result){s}; + split_result r = { + .first = S8Span(s.data, chr), + }; + u8 *end = S8End(s) + 1; + while (chr != end && *chr == c) + ++chr; + r.second = S8Span(chr, end); + return r; +} RTC_API s8 S8Clone(s8 s, arena *a) {