Skip to main content

character encoding - Unicode-aware string functions?


I've recently discovered that ToUpperCase is quite unreliable on non-ASCII input:


see code below



In[31]:= ToUpperCase@{"éàÇœßþσς", "ijķnjđӽծ", "ÿ"}
Out[31]= {"ÉÀÇŒSSÞ∑∑", "ijķnjđӽծ", "Y"}

So: it handles some accented ASCII, ligatures, Western European stuff, greek and cyrillic, while it fails on others (returning the lowercase characters themselves when they have valid uppercase variants). In at least one case, it fails utterly (the uppercase version of ÿ is Ÿ, not Y).


Mathematica’s documentation is very short on specifics on Unicode support. It broadly states that:



Mathematica has efficient systemwide 16-bit Unicode support, allowing a full range of international, technical and other character sets and character encodings.



which of course I very much doubt after seeing the above. More specifically, the ToUpperCase doc does not include a mention of Unicode.





So, after that bleak assessment, the question is the following: how do I get better Unicode-compliant string manipulation functions? (mostly ToUpperCase and ToLowerCase for now, but I'm sure others will be a problem for me in the future). Are there internal functions that do this, or external packages? Have I missed something in M’s documentation?




On a humorous note: the documentation for ToUpperCase even uses my example of a buggy transform (ÿ) in an example. It is, however, used about an unrelated issue, to warn users that “for a few characters, ToLowerCase is not the inverse of ToUpperCase” (as might be expected by Unicode-unaware programmers):


enter image description here




Answer



See if this helps:


Needs["JLink`"];
ClearAll[toUpperCase];
toUpperCase[s_String] :=
JavaBlock[JavaNew["java.lang.String", s]@toUpperCase[]];

Comments