# Unicode-aware string functions?

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

In[31]:= ToUpperCase@{"éàÇœßþσς", "ĳķǌđӽծ", "ÿ"}
Out[31]= {"ÉÀÇŒSSÞ∑∑", "ĳķǌđӽծ", "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):

-
Since this issue is listed in the 'Possible Issues' section of the documentation I don't think it's likely there will be an internal solution, otherwise they would have noted that. If the list of problem cases is restricted couldn't you just redefine ToLowerCase for those cases? –  Sjoerd C. de Vries Mar 27 '12 at 21:16
First, the “problem” noted in the documentation is not my bug: the doc states that lowercase then uppercase is not necessarily a round trip, which is expected. I just mentioned that they unfortunately use one buggy case as part of their remark… I'll try to make my question clearer. –  F'x Mar 27 '12 at 21:21
Perhaps this W3.org note is related: "The CSS2 specification only requires that text-transform work for 'the Latin1 repertoire'. It is not clear what characters the term 'Latin1' covers. This test assumes that it means the Basic Latin and Latin-1 Supplement blocks of the Unicode Standard (equivalent to ISO-8859-1). There is one character in this set, ÿ, that only occurs in the lower-case form." –  Sjoerd C. de Vries Mar 27 '12 at 21:29
@SjoerdC.deVries yes, I actually tried ÿ because I know it's the only Latin-1 character whose uppercase variant is not included in Latin-1, so it the past it was always an interesting corner case. But Mathematica's string support is not Latin-1 based, as far as I understand. Or is it? –  F'x Mar 27 '12 at 21:32
And Wikipedia: "The ISO 8859-1 character encoding includes the letters ä, ë, ï, ö, ü, and their respective capital forms, as well as ÿ in lower case only, with Ÿ added in the revised edition ISO 8859-15." –  Sjoerd C. de Vries Mar 27 '12 at 21:34
show 1 more comment

See if this helps:

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

-
It works for me –  Sjoerd C. de Vries Mar 27 '12 at 21:37
… and it works for me too! Clever use of Java link… –  F'x Mar 27 '12 at 21:39
@Sjoerd, F'x Great! –  Leonid Shifrin Mar 27 '12 at 21:44
Another way of doing the same thing could be with MakeJavaObject[s] instead of JavaNew –  Rojo Mar 28 '12 at 0:11
@Rojo I prefer explicit class name, since I am using a particular method of the class, so anyone interested can jump right to Javadocs for Java String. –  Leonid Shifrin Mar 28 '12 at 10:34
show 1 more comment

I had started working on a homegrown solution to this issue, directly by downloading Unicode data from the source. I’ll post it here, as it may be expanded to other functions were Java might not come and save the day!

unicodeData = StringSplit[#, ";"] & /@
StringSplit[Import["ftp://ftp.unicode.org/Public/UNIDATA/UnicodeData.txt"], "\n"];
upperCaseData = (FromDigits[#, 16] & /@ # &) /@
Select[unicodeData, (Length[#] > 12) && (StringLength[#[[13]]] > 0) &][[;; , {1, 13}]];

unichar[s_] := FromCharacterCode[FromDigits[s, 16], "Unicode"];
upperCaseChar[i_] := Module[{r},
r = Select[upperCaseData, #[[1]] == i &];
Return[If[Length[r] > 0, FromCharacterCode[r[[1, 2]]], FromCharacterCode[i]]];
]
upperCase[s_] := StringJoin[upperCaseChar /@ ToCharacterCode[s, "Unicode"]];


Which works:

In[127]:= upperCase["foéàçÿœÆĳķǌđӽծÿ"]
Out[127]= "FOÉÀÇŸŒÆĲĶǊĐӼԾŸ"
`
-