From 580f4a3a19dc014b3c2bf5d4ac9e1e96857bbc12 Mon Sep 17 00:00:00 2001 From: vylsaz Date: Tue, 7 Jan 2025 03:00:59 +0000 Subject: [PATCH] Windows: utf-16 util --- src/windows/utf16.c | 76 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/windows/utf16.c diff --git a/src/windows/utf16.c b/src/windows/utf16.c new file mode 100644 index 00000000..99cbc642 --- /dev/null +++ b/src/windows/utf16.c @@ -0,0 +1,76 @@ +#include "../utils/talloc.h" +#include + +static u64 utf16lenB(B x) { // doesn't consume + assert(isArr(x)); + SGetU(x) + usz ia = IA(x); + u64 res = 0; + for (usz i = 0; i < ia; ++i) { + u32 c = o2c(GetU(x,i)); + res+= 1+(c > 0xFFFF); + } + return res; +} + +FORCE_INLINE void utf16_w(WCHAR** buf_i, u32 c) +{ + WCHAR* buf = *buf_i; + if (c<=0xFFFF) { + *buf++ = c; + } else { + assert(c <= 0x10FFFF); + *buf++ = ((c-0x10000) >> 10)+0xD800; + *buf++ = ((c-0x10000)&0x3FF)+0xDC00; + } + *buf_i = buf; +} + +void toUTF16(B x, WCHAR* p) { + SGetU(x) + usz ia = IA(x); + for (u64 i = 0; i < ia; ++i) utf16_w(&p, o2cG(GetU(x,i))); +} + +B utf16Decode(const WCHAR* s, i64 len) { +#define UTF16_MASK 0xFC00 +#define UTF16_IS_HI(WC) ((UTF16_MASK&(WC))==0xD800) /* 0xD800..0xDBFF */ +#define UTF16_IS_LO(WC) ((UTF16_MASK&(WC))==0xDC00) /* 0xDC00..0xDFFF */ +#define UTF16_SURROGATE(HI, LO) (0x10000+(((HI)-0xD800) << 10)+((LO)-0xDC00)) + u64 sz = 0; + for (i64 j = 0; ; ++j) { + if (j>=len) { + if (j!=len) assert(0); + break; + } + if (UTF16_IS_HI(s[j]) && j+1