You may not be able to tell for a particular text whether it is in Windows-1255 or in ISO-8859-8, but the good news is that in those cases there isn't any difference in interpretation.
Likewise, English text may be either ASCII or UTF-8 in some cases with no interpretation difference.
I'd suggest the following, which is not optimal but wins for simplicity:
- If the text validates as UTF-8, it is UTF-8. This is a relatively inexpensive pass on the data.
- (From here on be in octet mode.) If the text contains 0xDF, interpret is a DOUBLE LOW LINE. It is in ISO-8859-8; this code point is not defined in Windows-1255.
- Actually, from here you can assume Windows-1255, which is a superset of ISO-8859-8 apart from the undefined character in the previous item.
This suggests an efficient algorithm; interleave a UTF-8 validator with a 0xDF detector; if you can assume your input is what you say it is, you have a fast one-pass function.
References:
Unicode's official notion of ISO-8859-8:
ftp://ftp.unicode.org/Public/MAPPINGS/ISO8859/8859-8.TXT
Windows-1255 reference (Unicode):
ftp://ftp.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP1255.TXT
Windows-1255 reference (Microsoft):
http://www.microsoft.com/typography/unicode/1255.htm
(Credit to some of the research behind this to Anatoly Vorobey.) |