From 01c7b280adf5120d77640f217ab9afaa5d76d21a Mon Sep 17 00:00:00 2001 From: "Adam D. Ruppe" Date: Thu, 21 Mar 2024 11:23:13 -0400 Subject: [PATCH] surrogate pairs --- webview.d | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/webview.d b/webview.d index f734cc7..492ae34 100644 --- a/webview.d +++ b/webview.d @@ -205,10 +205,37 @@ string toGC(scope StringMethod dg) { string s; - // FIXME: encode properly in UTF-8 while(*t) { - s ~= *t; + import std.utf; + char[4] buffer; + wchar item = *t; t++; + if(item >= 0xD800 && item <= 0xDFFF) { + wchar second = *t; + t++; + wchar low, high; + if(item >= 0xD800 && item <= 0xDBFF) { + high = item; + low = second; + } else { + high = second; + low = item; + } + + if( + high >= 0xD800 && high <= 0xDBFF + && + low >= 0xDC00 && low <= 0xDCFF + ) { + dchar d = (high - 0xD800) * 0x400 + (low - 0xDC00) + 0x10000; + + s ~= buffer[0 .. encode(buffer, d)]; + } else { + // we could probably throw something tbh + } + } else { + s ~= buffer[0 .. encode(buffer, item)]; + } } auto ret = s;