diff --git a/core/String.carp b/core/String.carp index d6316502..0cdea927 100644 --- a/core/String.carp +++ b/core/String.carp @@ -1,5 +1,13 @@ (system-include "carp_string.h") +(system-include "ctype.h") +(defmodule Byte + (hidden tolower-) ; helper func for String.ascii-to-lower + (register tolower- (Fn [Byte] Byte) "tolower") + (hidden toupper-) ; helper func for String.ascii-to-upper + (register toupper- (Fn [Byte] Byte) "toupper") +) + (defmodule String (register = (Fn [&String &String] Bool)) @@ -169,6 +177,13 @@ (doc contains? "Checks whether the string `s` contains the character `c`.") (defn contains? [s c] (> (index-of s c) -1)) + + (doc ascii-to-lower "converts each character in this string to lower case using tolower() from standard C library. Note: this will only work for ASCII characters.") + (defn ascii-to-lower [s] + (String.from-bytes &(Array.endo-map &(fn [c] (Byte.tolower- c)) (String.to-bytes s))) ) + (doc ascii-to-upper "converts each character in this string to upper case using toupper() from standard C library. Note: this will only work for ASCII characters.") + (defn ascii-to-upper [s] + (String.from-bytes &(Array.endo-map &(fn [c] (Byte.toupper- c)) (String.to-bytes s))) ) ) (defmodule StringCopy @@ -501,3 +516,4 @@ (cons (String.head s) (String.to-list (String.tail s))))) ) ) + diff --git a/test/string.carp b/test/string.carp index 0ca339d0..1598a29c 100644 --- a/test/string.carp +++ b/test/string.carp @@ -323,4 +323,12 @@ &[\� \�] &(chars &(from-bytes &[255b 255b])) "check for invalid UTF-8 sequences") + (assert-equal test + "hällo wörld" + &(ascii-to-lower "HälLo WöRld") + "ascii-to-lower works for valid UTF-8" ) + (assert-equal test + "HäLLO WöRLD" + &(ascii-to-upper "HälLo WöRld") + "ascii-to-upper works for valid UTF-8" ) )