From fccb06b2cdbe4fa67e8f584d2f9416a11ed5bc3c Mon Sep 17 00:00:00 2001 From: Daniel Bertalan Date: Mon, 11 Oct 2021 07:08:08 +0200 Subject: [PATCH] AK: Use UnicodeUtils::code_point_to_utf8 in StringBuilder --- AK/StringBuilder.cpp | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/AK/StringBuilder.cpp b/AK/StringBuilder.cpp index 8a697f8ebc..df4331f9ca 100644 --- a/AK/StringBuilder.cpp +++ b/AK/StringBuilder.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -97,21 +98,8 @@ void StringBuilder::clear() void StringBuilder::append_code_point(u32 code_point) { - if (code_point <= 0x7f) { - append((char)code_point); - } else if (code_point <= 0x07ff) { - append((char)(((code_point >> 6) & 0x1f) | 0xc0)); - append((char)(((code_point >> 0) & 0x3f) | 0x80)); - } else if (code_point <= 0xffff) { - append((char)(((code_point >> 12) & 0x0f) | 0xe0)); - append((char)(((code_point >> 6) & 0x3f) | 0x80)); - append((char)(((code_point >> 0) & 0x3f) | 0x80)); - } else if (code_point <= 0x10ffff) { - append((char)(((code_point >> 18) & 0x07) | 0xf0)); - append((char)(((code_point >> 12) & 0x3f) | 0x80)); - append((char)(((code_point >> 6) & 0x3f) | 0x80)); - append((char)(((code_point >> 0) & 0x3f) | 0x80)); - } else { + auto nwritten = AK::UnicodeUtils::code_point_to_utf8(code_point, [this](char c) { append(c); }); + if (nwritten < 0) { append(0xef); append(0xbf); append(0xbd);