summaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
authorpommicket <pommicket@gmail.com>2023-09-07 14:06:16 -0400
committerpommicket <pommicket@gmail.com>2023-09-07 14:06:16 -0400
commitbb4b7774aabc2686f936935b02899b1aa0bf1f2b (patch)
treed218d61e8163bf2e7cbdc352ab86744e8f250be8 /util.c
parent7b8b4d4495164250c71582347dd0338d387c4e98 (diff)
use LSPString in requests too for consistency
Diffstat (limited to 'util.c')
-rw-r--r--util.c33
1 files changed, 18 insertions, 15 deletions
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;
}