diff options
| author | Jake Koroman <jake@jakekoroman.com> | 2026-06-24 20:44:07 -0400 |
|---|---|---|
| committer | Jake Koroman <jake@jakekoroman.com> | 2026-06-24 20:44:07 -0400 |
| commit | 9ce4e557d79b86fcf5ff03209816d11934d7fc81 (patch) | |
| tree | aab7f7edb7e3a112e1ea52fc090effb45f65f06b | |
| parent | fda86fa4895e17d7b93b62454f2ae7bdb5b81ce3 (diff) | |
As I target C99 it was annoying to be forced to define
_POSIX_C_SOURCE=200809L just for strnlen. Especially because it was called
only once throughout the whole library. I just copy pasted the strnlen
implementation used by musl as I would still like to take advantage of
libc's memchr optimizations. Same code but no POSIX extension required,
a true win.
| -rw-r--r-- | jrk.h | 17 |
1 files changed, 11 insertions, 6 deletions
@@ -1,9 +1,4 @@ /* - * requires POSIX.1-2008 - * #define _POSIX_C_SOURCE 200809L - */ - -/* * TODO: * - add a log interface to give user control of logging. * disable completely, log to file, log format, etc. @@ -16,6 +11,7 @@ */ #include <stdbool.h> +#include <stddef.h> #include <stdint.h> typedef int8_t i8; @@ -85,6 +81,7 @@ void *jrk_arena_resize(jrk_Arena*, void*, u64, u64); i32 jrk_rand_num(i32); i32 jrk_rand_num_range(i32, i32); +size_t jrk_strnlen(const char *, size_t); jrk_fd jrk_fd_open_read(const char*); jrk_fd jrk_fd_open_write(const char*); @@ -876,7 +873,7 @@ jrk_string_from_cstr(const char *data) { jrk_String result = {0}; result.data = data; - result.len = strnlen(data, _JRK_MAX_STRNLEN); + result.len = jrk_strnlen(data, _JRK_MAX_STRNLEN); return result; } @@ -990,4 +987,12 @@ jrk_rand_num_range(i32 min, i32 max) return rand() % (max - min + 1) + min; } +/* musl implementation */ +size_t +jrk_strnlen(const char *s, size_t n) +{ + const char *p = memchr(s, 0, n); + return p ? (size_t) (p-s) : n; +} + #endif /* JRK_IMPLEMENTATION */ |
