summaryrefslogtreecommitdiff
path: root/AK/UnicodeUtils.h
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2023-01-07 14:48:20 -0500
committerLinus Groh <mail@linusgroh.de>2023-01-08 12:13:15 +0100
commit39bda0073edb4a95d3192879157c48e094bd89af (patch)
tree05d1a61da492eaac3cce7901ebd9cf78f3589cf3 /AK/UnicodeUtils.h
parentaee7c44064b4ad24709e3b5c5b5bb63b3b2d7354 (diff)
downloadserenity-39bda0073edb4a95d3192879157c48e094bd89af.zip
AK: Make StringBuilder::try_append_code_point actually fallible
It currently uses the non-fallible `append` method to append each UTF-8 encoded byte of the code point.
Diffstat (limited to 'AK/UnicodeUtils.h')
-rw-r--r--AK/UnicodeUtils.h30
1 files changed, 30 insertions, 0 deletions
diff --git a/AK/UnicodeUtils.h b/AK/UnicodeUtils.h
index c0e2a13ad2..fd55dd611d 100644
--- a/AK/UnicodeUtils.h
+++ b/AK/UnicodeUtils.h
@@ -6,6 +6,8 @@
#pragma once
+#include <AK/Concepts.h>
+#include <AK/Error.h>
#include <AK/Forward.h>
namespace AK::UnicodeUtils {
@@ -35,4 +37,32 @@ template<typename Callback>
return -1;
}
+template<FallibleFunction<char> Callback>
+[[nodiscard]] ErrorOr<int> try_code_point_to_utf8(u32 code_point, Callback&& callback)
+{
+ if (code_point <= 0x7f) {
+ TRY(callback(static_cast<char>(code_point)));
+ return 1;
+ }
+ if (code_point <= 0x07ff) {
+ TRY(callback(static_cast<char>((((code_point >> 6) & 0x1f) | 0xc0))));
+ TRY(callback(static_cast<char>((((code_point >> 0) & 0x3f) | 0x80))));
+ return 2;
+ }
+ if (code_point <= 0xffff) {
+ TRY(callback(static_cast<char>((((code_point >> 12) & 0x0f) | 0xe0))));
+ TRY(callback(static_cast<char>((((code_point >> 6) & 0x3f) | 0x80))));
+ TRY(callback(static_cast<char>((((code_point >> 0) & 0x3f) | 0x80))));
+ return 3;
+ }
+ if (code_point <= 0x10ffff) {
+ TRY(callback(static_cast<char>((((code_point >> 18) & 0x07) | 0xf0))));
+ TRY(callback(static_cast<char>((((code_point >> 12) & 0x3f) | 0x80))));
+ TRY(callback(static_cast<char>((((code_point >> 6) & 0x3f) | 0x80))));
+ TRY(callback(static_cast<char>((((code_point >> 0) & 0x3f) | 0x80))));
+ return 4;
+ }
+ return -1;
+}
+
}