diff options
author | Luke T. Shumaker <lukeshu@lukeshu.com> | 2025-01-12 23:08:14 -0700 |
---|---|---|
committer | Luke T. Shumaker <lukeshu@lukeshu.com> | 2025-01-19 15:43:29 -0700 |
commit | cd9667d6be072a9c736e49c679b55852f7b30a6c (patch) | |
tree | c99c10d4c0caea96cc5a5420532eccaa60efe976 /lib9p/9p.c | |
parent | 6381e08fd03e322d7d95973ca00c5605afb67707 (diff) |
lib9p: Don't nul-terminate strings, add some string utils
Diffstat (limited to 'lib9p/9p.c')
-rw-r--r-- | lib9p/9p.c | 29 |
1 files changed, 29 insertions, 0 deletions
@@ -35,6 +35,35 @@ const char *lib9p_msgtype_str(enum lib9p_version ver, enum lib9p_msg_type typ) { return _lib9p_table_msg_name[ver][typ] ?: const_byte_str(typ); } +struct lib9p_s lib9p_str(char *s) { + if (!s) + return (struct lib9p_s){0}; + return (struct lib9p_s){ + .len = strlen(s), + .utf8 = s, + }; +} +struct lib9p_s lib9p_strn(char *s, size_t maxlen) { + if (maxlen == 0 || !s) + return (struct lib9p_s){0}; + return (struct lib9p_s){ + .len = strnlen(s, maxlen), + .utf8 = s, + }; +} +struct lib9p_s lib9p_str_slice(struct lib9p_s s, uint16_t beg, uint16_t end) { + assert(s.len == 0 || s.utf8); + assert(beg <= end && end <= s.len); + return (struct lib9p_s){ + .len = end - beg, + .utf8 = &s.utf8[beg], + }; +} +bool lib9p_str_eq(struct lib9p_s a, struct lib9p_s b) { + return a.len == b.len && + (a.len == 0 || memcmp(a.utf8, b.utf8, a.len) == 0); +} + /* ctx ************************************************************************/ void lib9p_ctx_clear_error(struct lib9p_ctx *ctx) { |