Your task is to golf a program that prints which character encoding, if any, the data read from the input represents. It should be able to detect the following encodings:
One of the above names of character encodings should be printed (in the same way) if it can be determined which is used. If the input is not ASCII, but it's ambiguous which of the other encodings is used, a
? should be printed. If none of the encodings can be valid,
X must be printed.
Furthermore, the following should be taken into account:
- If the input is valid ASCII, ASCII should be printed. Even though this means the other encodings are also valid. When the input is not ASCII,
?should printed in case of any ambiguity.
- The UTF's are not required to start with a BOM.
- UTF-16 can be either big or little endian. This means there are actually five different encodings to detect.
- If UTF-8 or UTF-16 data encodes a number between 0 and 0x10FFFF you may consider it valid, even if it represents a Unicode code point that is technically not allowed in this encoding. Therefore you do not have to worry about unassigned code points and such.
- ISO 8859-1 is considered invalid when any of its undefined bytes (positions 0-31 and 127-159, except 9, 10 and 13) are used.
- This is code golf.