Up down case (#1248)

* feat!: implemented String.ascii-to-lower and String.ascii-to-upper

* fix: corrected docstrings

* fix: added unit test for ascii-to-lower and ascii-to-upper

* fix: moved tolower- and toupper from String to Byte module

Co-authored-by: guberatsie <gunnar.bernhardt@siemens.com>
This commit is contained in:
guberathome 2021-06-16 09:45:32 +02:00 committed by GitHub
parent 38951efc7d
commit 5e5cf8d4c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 0 deletions

View File

@ -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)))))
)
)

View File

@ -323,4 +323,12 @@
&[\<5C> \<5C>]
&(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" )
)