From 5aae180c180f3eb2e9b0fd69dc1f482e6d3141b6 Mon Sep 17 00:00:00 2001 From: speedy-lex Date: Mon, 30 Jun 2025 18:07:20 +0200 Subject: [PATCH] Implement nn_unicode_wlen --- src/unicode.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/unicode.c b/src/unicode.c index 957eec2..73094ce 100644 --- a/src/unicode.c +++ b/src/unicode.c @@ -8,7 +8,6 @@ static bool nn_unicode_is_continuation(unsigned char byte) { } bool nn_unicode_validate(const char *b) { - // TODO: validate UTF-8-ness const unsigned char* s = (const unsigned char*)b; while (*s) { if(s[0] <= 0x7F) { @@ -168,12 +167,24 @@ const char *nn_unicode_codepointToChar(unsigned int codepoint, size_t *len) { return buffer; } +size_t nn_unicode_charWidth(unsigned int codepoint) { + // TODO: implement this + return 1; +} + +size_t nn_unicode_wlen(const char *s) { + size_t wlen = 0; + while (*s) { + unsigned int codepoint = nn_unicode_codepointAt(s, 0); + size_t codepointSize = nn_unicode_codepointSize(codepoint); + wlen += nn_unicode_charWidth(codepoint); + s += codepointSize; + } + return wlen; +} + // NOT IMPLEMENTED YET -size_t nn_unicode_charWidth(unsigned int codepoint); - -size_t nn_unicode_wlen(const char *s); - unsigned int nn_unicode_upperCodepoint(unsigned int codepoint); char *nn_unicode_upper(const char *s); unsigned int nn_unicode_lowerCodepoint(unsigned int codepoint);