From 84f65c26e4ef7d934605e7066b7fb3fdae8c8484 Mon Sep 17 00:00:00 2001 From: bjorn Date: Mon, 8 Feb 2021 20:55:51 -0700 Subject: [PATCH] Move utf8 back into util; --- CMakeLists.txt | 1 - Tupfile | 1 - src/core/utf.c | 68 ----------------------------------- src/core/utf.h | 5 --- src/core/util.c | 68 +++++++++++++++++++++++++++++++++++ src/core/util.h | 4 +++ src/modules/data/rasterizer.c | 1 - src/modules/event/event.c | 1 - src/modules/graphics/font.c | 1 - 9 files changed, 72 insertions(+), 78 deletions(-) delete mode 100644 src/core/utf.c delete mode 100644 src/core/utf.h diff --git a/CMakeLists.txt b/CMakeLists.txt index c073391c..6c50c7bb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -267,7 +267,6 @@ set(LOVR_SRC src/main.c src/core/fs.c src/core/map.c - src/core/utf.c src/core/util.c src/core/zip.c src/api/api.c diff --git a/Tupfile b/Tupfile index c2539ebc..c3c33e48 100644 --- a/Tupfile +++ b/Tupfile @@ -9,7 +9,6 @@ SRC += src/core/map.c ifneq (@(PICO),y) SRC += src/core/os_$(PLATFORM).c endif -SRC += src/core/utf.c SRC += src/core/util.c SRC += src/core/zip.c diff --git a/src/core/utf.c b/src/core/utf.c deleted file mode 100644 index ea3f5099..00000000 --- a/src/core/utf.c +++ /dev/null @@ -1,68 +0,0 @@ -#include "utf.h" - -// https://github.com/starwing/luautf8 -size_t utf8_decode(const char *s, const char *e, unsigned *pch) { - unsigned ch; - - if (s >= e) { - *pch = 0; - return 0; - } - - ch = (unsigned char)s[0]; - if (ch < 0xC0) goto fallback; - if (ch < 0xE0) { - if (s+1 >= e || (s[1] & 0xC0) != 0x80) - goto fallback; - *pch = ((ch & 0x1F) << 6) | - (s[1] & 0x3F); - return 2; - } - if (ch < 0xF0) { - if (s+2 >= e || (s[1] & 0xC0) != 0x80 - || (s[2] & 0xC0) != 0x80) - goto fallback; - *pch = ((ch & 0x0F) << 12) | - ((s[1] & 0x3F) << 6) | - (s[2] & 0x3F); - return 3; - } - { - int count = 0; /* to count number of continuation bytes */ - unsigned res = 0; - while ((ch & 0x40) != 0) { /* still have continuation bytes? */ - int cc = (unsigned char)s[++count]; - if ((cc & 0xC0) != 0x80) /* not a continuation byte? */ - goto fallback; /* invalid byte sequence, fallback */ - res = (res << 6) | (cc & 0x3F); /* add lower 6 bits from cont. byte */ - ch <<= 1; /* to test next bit */ - } - if (count > 5) - goto fallback; /* invalid byte sequence */ - res |= ((ch & 0x7F) << (count * 5)); /* add first byte */ - *pch = res; - return count+1; - } - -fallback: - *pch = ch; - return 1; -} - -void utf8_encode(uint32_t c, char s[4]) { - if (c <= 0x7f) { - s[0] = c; - } else if (c <= 0x7ff) { - s[0] = (0xc0 | ((c >> 6) & 0x1f)); - s[1] = (0x80 | (c & 0x3f)); - } else if (c <= 0xffff) { - s[0] = (0xe0 | ((c >> 12) & 0x0f)); - s[1] = (0x80 | ((c >> 6) & 0x3f)); - s[2] = (0x80 | (c & 0x3f)); - } else if (c <= 0x10ffff) { - s[1] = (0xf0 | ((c >> 18) & 0x07)); - s[1] = (0x80 | ((c >> 12) & 0x3f)); - s[2] = (0x80 | ((c >> 6) & 0x3f)); - s[3] = (0x80 | (c & 0x3f)); - } -} diff --git a/src/core/utf.h b/src/core/utf.h deleted file mode 100644 index 7663c35e..00000000 --- a/src/core/utf.h +++ /dev/null @@ -1,5 +0,0 @@ -#include -#include - -size_t utf8_decode(const char *s, const char *e, unsigned *pch); -void utf8_encode(uint32_t codepoint, char str[4]); diff --git a/src/core/util.c b/src/core/util.c index d2abced9..f440ec78 100644 --- a/src/core/util.c +++ b/src/core/util.c @@ -40,3 +40,71 @@ void lovrLog(int level, const char* tag, const char* format, ...) { lovrLogCallback(lovrLogUserdata, level, tag, format, args); va_end(args); } + +// UTF-8 +// https://github.com/starwing/luautf8 +size_t utf8_decode(const char *s, const char *e, unsigned *pch) { + unsigned ch; + + if (s >= e) { + *pch = 0; + return 0; + } + + ch = (unsigned char)s[0]; + if (ch < 0xC0) goto fallback; + if (ch < 0xE0) { + if (s+1 >= e || (s[1] & 0xC0) != 0x80) + goto fallback; + *pch = ((ch & 0x1F) << 6) | + (s[1] & 0x3F); + return 2; + } + if (ch < 0xF0) { + if (s+2 >= e || (s[1] & 0xC0) != 0x80 + || (s[2] & 0xC0) != 0x80) + goto fallback; + *pch = ((ch & 0x0F) << 12) | + ((s[1] & 0x3F) << 6) | + (s[2] & 0x3F); + return 3; + } + { + int count = 0; /* to count number of continuation bytes */ + unsigned res = 0; + while ((ch & 0x40) != 0) { /* still have continuation bytes? */ + int cc = (unsigned char)s[++count]; + if ((cc & 0xC0) != 0x80) /* not a continuation byte? */ + goto fallback; /* invalid byte sequence, fallback */ + res = (res << 6) | (cc & 0x3F); /* add lower 6 bits from cont. byte */ + ch <<= 1; /* to test next bit */ + } + if (count > 5) + goto fallback; /* invalid byte sequence */ + res |= ((ch & 0x7F) << (count * 5)); /* add first byte */ + *pch = res; + return count+1; + } + +fallback: + *pch = ch; + return 1; +} + +void utf8_encode(uint32_t c, char s[4]) { + if (c <= 0x7f) { + s[0] = c; + } else if (c <= 0x7ff) { + s[0] = (0xc0 | ((c >> 6) & 0x1f)); + s[1] = (0x80 | (c & 0x3f)); + } else if (c <= 0xffff) { + s[0] = (0xe0 | ((c >> 12) & 0x0f)); + s[1] = (0x80 | ((c >> 6) & 0x3f)); + s[2] = (0x80 | (c & 0x3f)); + } else if (c <= 0x10ffff) { + s[1] = (0xf0 | ((c >> 18) & 0x07)); + s[1] = (0x80 | ((c >> 12) & 0x3f)); + s[2] = (0x80 | ((c >> 6) & 0x3f)); + s[3] = (0x80 | (c & 0x3f)); + } +} diff --git a/src/core/util.h b/src/core/util.h index e450917f..7c8cc92b 100644 --- a/src/core/util.h +++ b/src/core/util.h @@ -83,3 +83,7 @@ static inline void _arr_reserve(void** data, size_t n, size_t* capacity, size_t *data = allocator(*data, *capacity * stride); lovrAssert(*data, "Out of memory"); } + +// UTF-8 +size_t utf8_decode(const char *s, const char *e, unsigned *pch); +void utf8_encode(uint32_t codepoint, char str[4]); diff --git a/src/modules/data/rasterizer.c b/src/modules/data/rasterizer.c index ff665298..16a9a847 100644 --- a/src/modules/data/rasterizer.c +++ b/src/modules/data/rasterizer.c @@ -2,7 +2,6 @@ #include "data/blob.h" #include "data/image.h" #include "resources/VarelaRound.ttf.h" -#include "core/utf.h" #include "core/util.h" #include "lib/stb/stb_truetype.h" #include diff --git a/src/modules/event/event.c b/src/modules/event/event.c index e050ac1c..0b71c92f 100644 --- a/src/modules/event/event.c +++ b/src/modules/event/event.c @@ -2,7 +2,6 @@ #include "thread/thread.h" #include "core/os.h" #include "core/util.h" -#include "core/utf.h" #include #include diff --git a/src/modules/graphics/font.c b/src/modules/graphics/font.c index 4a45cd5f..e29a7041 100644 --- a/src/modules/graphics/font.c +++ b/src/modules/graphics/font.c @@ -3,7 +3,6 @@ #include "data/rasterizer.h" #include "data/image.h" #include "core/map.h" -#include "core/utf.h" #include #include