From bb4b7774aabc2686f936935b02899b1aa0bf1f2b Mon Sep 17 00:00:00 2001 From: pommicket Date: Thu, 7 Sep 2023 14:06:16 -0400 Subject: use LSPString in requests too for consistency --- util.c | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) (limited to 'util.c') diff --git a/util.c b/util.c index b146c82..1b9b9f3 100644 --- a/util.c +++ b/util.c @@ -1018,24 +1018,27 @@ String32 str32_from_utf8(const char *utf8) { return string; } -// returns a null-terminated UTF-8 string -// the string returned should be free'd -// this will return NULL on failure + +bool str32_to_utf8_cstr_in_place(String32 s, char *out) { + char *p = out; + for (size_t i = 0; i < s.len; ++i) { + size_t bytes = unicode_utf32_to_utf8(p, s.str[i]); + if (bytes == (size_t)-1) { + // invalid UTF-32 code point + *p = '\0'; + return false; + } else { + p += bytes; + } + } + *p = '\0'; + return true; +} + char *str32_to_utf8_cstr(String32 s) { char *utf8 = calloc(4 * s.len + 1, 1); // each codepoint takes up at most 4 bytes in UTF-8, + we need a terminating null byte if (utf8) { - char *p = utf8; - for (size_t i = 0; i < s.len; ++i) { - size_t bytes = unicode_utf32_to_utf8(p, s.str[i]); - if (bytes == (size_t)-1) { - // invalid UTF-32 code point - free(utf8); - return NULL; - } else { - p += bytes; - } - } - *p = '\0'; + str32_to_utf8_cstr_in_place(s, utf8); } return utf8; } -- cgit v1.2.3