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:

  1. If the text validates as UTF-8, it is UTF-8. This is a relatively inexpensive pass on the data.
  2. (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.
  3. 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.)


In reply to Re: Encoding Detection by gaal
in thread Encoding Detection by shlomoy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.