Changes upper case characters to lower case and vice-versa.
Handles Unicode supplementary characters correctly. Uses the
locale-sensitive String.toUpperCase() and String.toLowerCase()
methods.
(defn ^String swap-case
"Changes upper case characters to lower case and vice-versa.
Handles Unicode supplementary characters correctly. Uses the
locale-sensitive String.toUpperCase() and String.toLowerCase()
methods."
[^String s]
(let [buffer (StringBuilder. (.length s))
;; array to make a String from one code point
^"[I" array (make-array Integer/TYPE 1)]
(docodepoints [c s]
(aset-int array 0 c)
(if (Character/isLowerCase c)
;; Character.toUpperCase is not locale-sensitive, but
;; String.toUpperCase is; so we use a String.
(.append buffer (.toUpperCase (String. array 0 1)))
(.append buffer (.toLowerCase (String. array 0 1)))))
(.toString buffer)))
Comments top
No comments for swap-case. Log in to add a comment.