summaryrefslogtreecommitdiff
path: root/zap/source/any/str/utf8enclen.c
diff options
context:
space:
mode:
Diffstat (limited to 'zap/source/any/str/utf8enclen.c')
-rw-r--r--zap/source/any/str/utf8enclen.c29
1 files changed, 29 insertions, 0 deletions
diff --git a/zap/source/any/str/utf8enclen.c b/zap/source/any/str/utf8enclen.c
new file mode 100644
index 0000000..f49031e
--- /dev/null
+++ b/zap/source/any/str/utf8enclen.c
@@ -0,0 +1,29 @@
+/*
+ Copyright 2022-2023 Gabriel Jensen.
+ This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
+ If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>.
+*/
+
+#include <zap/str.h>
+
+zap_sz zap_utf8enclen(zap_i02 const * buf) {
+ zap_sz len = 0x0u;
+ for (;;++buf) {
+ zap_i02 const chr = *buf;
+ if (chr == 0x0u) break;
+ if (chr >= 0x10000u) {
+ len += 0x4u;
+ continue;
+ }
+ if (chr >= 0x800u) {
+ len += 0x3u;
+ continue;
+ }
+ if (chr >= 0x80u) {
+ len += 0x2u;
+ continue;
+ }
+ ++len;
+ }
+ return len;
+}