start of unicode lib

This commit is contained in:
IonutParau 2025-05-29 12:12:32 +02:00
parent 7daf034d25
commit 4cfb4f74a6
4 changed files with 78 additions and 0 deletions

View File

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

View File

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

View File

@ -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. */

57
src/unicode.c Normal file
View File

@ -0,0 +1,57 @@
#include "neonucleus.h"
#include <string.h>
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);