From 4cfb4f74a64ab59023f55675c389d758db8d81c9 Mon Sep 17 00:00:00 2001 From: IonutParau Date: Thu, 29 May 2025 12:12:32 +0200 Subject: [PATCH] start of unicode lib --- build.zig | 1 + src/emulator.c | 6 +++++ src/neonucleus.h | 14 ++++++++++++ src/unicode.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+) create mode 100644 src/unicode.c diff --git a/build.zig b/build.zig index cd8a907..a08ee32 100644 --- a/build.zig +++ b/build.zig @@ -12,6 +12,7 @@ fn addEngineSources(c: *std.Build.Step.Compile) void { "src/component.c", "src/computer.c", "src/universe.c", + "src/unicode.c", // components "src/components/eeprom.c", "src/components/filesystem.c", diff --git a/src/emulator.c b/src/emulator.c index 440f44a..795addf 100644 --- a/src/emulator.c +++ b/src/emulator.c @@ -191,6 +191,12 @@ bool ne_fs_isDirectory(nn_component *component, ne_fs *fs, const char *path) { return DirectoryExists(p); } +bool ne_fs_makeDirectory(nn_component *component, ne_fs *fs, const char *path) { + const char *p = ne_fs_diskPath(component, path); + + return MakeDirectory(p) == 0; +} + int main() { printf("Setting up universe\n"); nn_universe *universe = nn_newUniverse(); diff --git a/src/neonucleus.h b/src/neonucleus.h index 51a1a6d..6a9327a 100644 --- a/src/neonucleus.h +++ b/src/neonucleus.h @@ -153,6 +153,20 @@ bool nn_removeRef(nn_refc *refc, size_t count); /* Returns true if the object should be freed */ bool nn_decRef(nn_refc *refc); +bool nn_unicode_validate(const char *s); +// returned string must be nn_free()'d +char *nn_unicode_char(int *codepoints, size_t codepointCount); +// returned array must be nn_free()'d +int *nn_unicode_codepoints(const char *s); +size_t nn_unicode_len(const char *s); +int nn_unicode_codepointAt(const char *s, size_t byteOffset); +size_t nn_unicode_codepointSize(int codepoint); +const char *nn_unicode_codepointToChar(int codepoint, size_t *len); +size_t nn_unicode_charWidth(int codepoint); +size_t nn_unicode_wlen(const char *s); +void nn_unicode_upper(char *s); +void nn_unicode_lower(char *s); + double nn_realTime(); double nn_realTimeClock(void *_); /* Will busy-loop until the time passes. This is meant for computed latencies in components. */ diff --git a/src/unicode.c b/src/unicode.c new file mode 100644 index 0000000..a4cf285 --- /dev/null +++ b/src/unicode.c @@ -0,0 +1,57 @@ +#include "neonucleus.h" +#include + +bool nn_unicode_validate(const char *s) { + // TODO: validate UTF-8-ness + return true; +} + +// A general unicode library, which assumes unicode encoding. +// It is used to power the Lua architecture's Unicode API re-implementation. +// It can also just be used to deal with unicode. + +char *nn_unicode_char(int *codepoints, size_t codepointCount) { + size_t len = 0; + for(size_t i = 0; i < codepointCount; i++) { + int codepoint = codepoints[i]; + len += nn_unicode_codepointSize(codepoint); + } + + char *buf = nn_malloc(len+1); + if(buf == NULL) return buf; + buf[len] = '\0'; + + size_t j = 0; + for(size_t i = 0; i < codepointCount; i++) { + int codepoint = codepoints[i]; + size_t codepointLen = 0; + const char *c = nn_unicode_codepointToChar(codepoint, &codepointLen); + memcpy(buf + j, c, codepointLen); + j += codepointLen; + } + + return buf; +} + +int *nn_unicode_codepoints(const char *s); + +size_t nn_unicode_len(const char *s) { + size_t count = 0; + while (*s) { + count += (*s++ & 0xC0) != 0x80; + } + return count; +} + +int nn_unicode_codepointAt(const char *s, size_t byteOffset); + +size_t nn_unicode_codepointSize(int codepoint); + +const char *nn_unicode_codepointToChar(int codepoint, size_t *len); + +size_t nn_unicode_charWidth(int codepoint); + +size_t nn_unicode_wlen(const char *s); + +void nn_unicode_upper(char *s); +void nn_unicode_lower(char *s);