Move utf8 back into util;

This commit is contained in:
bjorn 2021-02-08 20:55:51 -07:00
parent 907cf35dda
commit 84f65c26e4
9 changed files with 72 additions and 78 deletions

View File

@ -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

View File

@ -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

View File

@ -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));
}
}

View File

@ -1,5 +0,0 @@
#include <stddef.h>
#include <stdint.h>
size_t utf8_decode(const char *s, const char *e, unsigned *pch);
void utf8_encode(uint32_t codepoint, char str[4]);

View File

@ -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));
}
}

View File

@ -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]);

View File

@ -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 <msdfgen-c.h>

View File

@ -2,7 +2,6 @@
#include "thread/thread.h"
#include "core/os.h"
#include "core/util.h"
#include "core/utf.h"
#include <stdlib.h>
#include <string.h>

View File

@ -3,7 +3,6 @@
#include "data/rasterizer.h"
#include "data/image.h"
#include "core/map.h"
#include "core/utf.h"
#include <string.h>
#include <stdlib.h>