From 16d409c382295fa9ab777835d7874a38a53959c2 Mon Sep 17 00:00:00 2001 From: Lassi Kortela Date: Mon, 26 Aug 2019 11:47:56 +0300 Subject: [PATCH] Add string-upcase, string-downcase builtins Simple ASCII for now. Add some Unicode smarts later. --- c/string.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/c/string.c b/c/string.c index b52c536..99eac91 100644 --- a/c/string.c +++ b/c/string.c @@ -292,6 +292,34 @@ value_t builtin_char_downcase(value_t *args, uint32_t nargs) return mk_wchar(towlower(*(int32_t *)cp_data(cp))); } +value_t string_map_chars(int (*mapfun)(int), char *os) +{ + char *ns; + value_t nv; + size_t n, i; + + n = strlen(os); + nv = cvalue_string(n); + ns = cv_data((struct cvalue *)ptr(nv)); + memcpy(ns, os, n); + for (i = 0; i < n; i++) { + ns[i] = mapfun(ns[i]); + } + return nv; +} + +value_t builtin_string_upcase(value_t *args, uint32_t nargs) +{ + argcount("string-upcase", nargs, 1); + return string_map_chars(toupper, tostring(args[0], "string-upcase")); +} + +value_t builtin_string_downcase(value_t *args, uint32_t nargs) +{ + argcount("string-downcase", nargs, 1); + return string_map_chars(tolower, tostring(args[0], "string-downcase")); +} + value_t builtin_char_alphabetic(value_t *args, uint32_t nargs) { struct cprim *cp; @@ -501,6 +529,9 @@ static struct builtinspec stringfunc_info[] = { { "char-downcase", builtin_char_downcase }, { "char-alphabetic?", builtin_char_alphabetic }, + { "string-upcase", builtin_string_upcase }, + { "string-downcase", builtin_string_downcase }, + { "number->string", fl_numbertostring }, { "string->number", fl_stringtonumber },