Skip to content

Commit

Permalink
Uppercase and Lowercase can now handle unicode
Browse files Browse the repository at this point in the history
  • Loading branch information
StavromulaBeta committed Aug 20, 2024
1 parent 2be65c2 commit c039f0b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/cognac.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
31 changes: 23 additions & 8 deletions src/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#define _FORTIFY_SOURCE 2

#include <stddef.h>
#include <wchar.h>
#include <stdio.h>
#include <assert.h>
#include <limits.h>
Expand Down Expand Up @@ -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;
}

/*
Expand Down
4 changes: 2 additions & 2 deletions tests/strings.cog
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit c039f0b

Please sign in to comment.