Skip to content

Commit

Permalink
Add Uppercase & Lowercase functions
Browse files Browse the repository at this point in the history
  • Loading branch information
hedyhli authored and StavromulaBeta committed Aug 20, 2024
1 parent 99bded5 commit 2be65c2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
{.name="ordinal", .calltype=call, .argc=1, .args={string}, .returns=true, .rettype=number},
{.name="character", .calltype=call, .argc=1, .args={number}, .returns=true, .rettype=string},
{.name="split", .calltype=call, .argc=2, .args={string, string}, .returns=true, .rettype=list},
{.name="uppercase", .calltype=call, .argc=1, .args={string}, .returns=true, .rettype=string},
{.name="lowercase", .calltype=call, .argc=1, .args={string}, .returns=true, .rettype=string},
{.name="floor", .calltype=call, .argc=1, .args={number}, .returns=true, .rettype=number},
{.name="round", .calltype=call, .argc=1, .args={number}, .returns=true, .rettype=number},
{.name="ceiling", .calltype=call, .argc=1, .args={number}, .returns=true, .rettype=number},
Expand Down
16 changes: 16 additions & 0 deletions src/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -1653,6 +1653,22 @@ static LIST ___split(STRING sep, STRING str)
return prev;
}

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;
}

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;
}

/*
static BLOCK ___remember(BLOCK b)
{
Expand Down
14 changes: 13 additions & 1 deletion tests/strings.cog
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,16 @@ else
Print If == List ("Hello" "world!") Split on " " with "Hello world!"
"PASS: Splitting a string"
else
"FAIL: Splitting a string"
"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"
"PASS: Converting a string to lowercase"
else
"FAIL: Converting a string to lowercase";

Print If == "123 ♥ ÆABCDE" Uppercase "123 ♥ ÆaBcde"
"PASS: Converting a string to uppercase"
else
"FAIL: Converting a string to uppercase";

0 comments on commit 2be65c2

Please sign in to comment.