From 1f0533482b988d534fd50cfff1e5263cac3cda54 Mon Sep 17 00:00:00 2001 From: takase1121 <20792268+takase1121@users.noreply.github.com> Date: Tue, 9 Jul 2024 18:04:37 +0800 Subject: [PATCH] utfconv: add functions that use arena_allocator --- src/utfconv.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/utfconv.h b/src/utfconv.h index 59f98e4a..d5d9092e 100644 --- a/src/utfconv.h +++ b/src/utfconv.h @@ -12,8 +12,34 @@ #include #include +#include "arena_allocator.h" + #define UTFCONV_ERROR_INVALID_CONVERSION "Input contains invalid byte sequences." +#define utfconv_fromutf8(A, str) utfconv_fromlutf8(A, str, -1) + +static UNUSED LPWSTR utfconv_fromlutf8(lxl_arena *A, const char *str, int len) { + int output_len = MultiByteToWideChar(CP_UTF8, 0, str, len, NULL, 0); + if (!output_len) return NULL; + LPWSTR output = lxl_arena_malloc(A, sizeof(WCHAR) * output_len); + if (!output) return NULL; + output_len = MultiByteToWideChar(CP_UTF8, 0, str, len, output, output_len); + if (!output_len) return (lxl_arena_free(A, output), NULL); + return output; +} + +#define utfconv_fromwstr(A, str) utfconv_fromlwstr(A, str, -1) + +static UNUSED const char *utfconv_fromlwstr(lxl_arena *A, LPWSTR str, int len) { + int output_len = WideCharToMultiByte(CP_UTF8, 0, str, len, NULL, 0, NULL, NULL); + if (!output_len) return NULL; + char *output = lxl_arena_malloc(A, sizeof(char) * output_len); + if (!output) return NULL; + output_len = WideCharToMultiByte(CP_UTF8, 0, str, len, output, output_len, NULL, NULL); + if (!output_len) return (lxl_arena_free(A, output), NULL); + return (const char *) output; +} + static UNUSED LPWSTR utfconv_utf8towc(const char *str) { LPWSTR output; int len;