From 3fd4be7cb2744d852cf99e627f7bfea3fd83bd3c Mon Sep 17 00:00:00 2001 From: Anton Kling Date: Mon, 14 Oct 2024 16:37:18 +0200 Subject: libc: Copy string views to preallocated buffers --- userland/libc/tb/sv.c | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'userland/libc/tb/sv.c') diff --git a/userland/libc/tb/sv.c b/userland/libc/tb/sv.c index 3f947ef..4ff24fb 100644 --- a/userland/libc/tb/sv.c +++ b/userland/libc/tb/sv.c @@ -11,6 +11,15 @@ char *SV_TO_C(struct sv s) { return c_string; } +size_t sv_to_cstring_buffer(struct sv s, char *buffer, size_t length) { + if (0 == length || length - 1 < s.length) { + return s.length; + } + memcpy(buffer, s.s, s.length); + buffer[s.length] = '\0'; + return s.length; +} + struct sv sv_next(struct sv s) { if (0 == s.length) { return s; -- cgit v1.2.3