From c039f0b23475c1a283a6d0f74c3ae84fc6bc7fdf Mon Sep 17 00:00:00 2001 From: Finn Barber Date: Tue, 20 Aug 2024 15:31:19 +0100 Subject: [PATCH] Uppercase and Lowercase can now handle unicode --- src/cognac.c | 1 + src/runtime.h | 31 +++++++++++++++++++++++-------- tests/strings.cog | 4 ++-- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/cognac.c b/src/cognac.c index 0b4a5d1..fc67ba3 100755 --- a/src/cognac.c +++ b/src/cognac.c @@ -414,6 +414,7 @@ module_t* create_module(char* path) module_t* mod = alloc(sizeof *mod); mod->path = path; mod->file = fopen(path, "r"); + if (!mod->file) { fprintf(stderr, "Can't open file '%s'\n", path); exit(EXIT_FAILURE); } char* path2 = strdup(path); char* path3 = path2; for (char* s = path2 ; *s ; ++s) if (*s == '/') path2 = s+1; diff --git a/src/runtime.h b/src/runtime.h index 39ba27a..2d9d06f 100644 --- a/src/runtime.h +++ b/src/runtime.h @@ -3,6 +3,7 @@ #define _FORTIFY_SOURCE 2 #include +#include #include #include #include @@ -1655,18 +1656,32 @@ static LIST ___split(STRING sep, STRING str) static STRING ___uppercase(STRING str) { - char* converted = gc_strdup(str); - for (char* c = converted; *c; c += mblen(str, MB_CUR_MAX)) - *c = toupper(*c); - return converted; + char* converted = gc_strdup(str); + int len = 0; + for (char* c = converted; *c; c += len) + { + wchar_t chr = 0; + len = mblen(c, MB_CUR_MAX); + mbtowc(&chr, c, len); + chr = towupper(chr); + wctomb(c, chr); + } + return converted; } static STRING ___lowercase(STRING str) { - char* converted = gc_strdup(str); - for (char* c = converted; *c; c += mblen(str, MB_CUR_MAX)) - *c = tolower(*c); - return converted; + char* converted = gc_strdup(str); + int len = 0; + for (char* c = converted; *c; c += len) + { + wchar_t chr = 0; + len = mblen(c, MB_CUR_MAX); + mbtowc(&chr, c, len); + chr = towlower(chr); + wctomb(c, chr); + } + return converted; } /* diff --git a/tests/strings.cog b/tests/strings.cog index 41fa70f..a022dbb 100644 --- a/tests/strings.cog +++ b/tests/strings.cog @@ -44,8 +44,8 @@ else "FAIL: Splitting a string"; ~~ TODO: Only ASCII conversions are supported right now. -~~ Print If == "123 áæćde" Lowercase "123 ÁÆĆdE" -Print If == "123 ♥ æabcde" Lowercase "123 ♥ æAbCdE" +Print If == "123 áæćde" Lowercase "123 ÁÆĆdE" +~~Print If == "123 ♥ æabcde" Lowercase "123 ♥ æAbCdE" "PASS: Converting a string to lowercase" else "FAIL: Converting a string to lowercase";