2
\$\begingroup\$

Given a UTF-16 string, test its endianness.

Rules:

  1. As long as the input is in UTF-16 without BOM, its type doesn't matter. In C++ on Windows, it can be std::wstring or std::u16string. In Haskell, it can be [Int16] or [Word16].

  2. The endianness is tested by noncharacters. If the input contains a noncharacter, it is in wrong endianness.

  3. If the original version and the endianness-swapped version both or neither contain a noncharacter, the test is inconclusive.

  4. The type of the output must be well-ordered. Output 1 (or an equivalent enumerator) when the input is in right endianness, or -1 (or an equivalent enumerator) when the input must be endianness-swapped, or 0 (or an equivalent enumerator) when the test is inconclusive. For example, in Haskell, Ordering is a valid output type.

  5. Wrong UTF-16 sequences are also treated as noncharacters. (So the noncharacters are: Encodings of code point from U+FDD0 to U+FDEF, encodings of code point U+XXFFFE and U+XXFFFF with XX from 00 to 10, and unpaired surrogates.)

  6. As this is a code golf, the shortest code in bytes wins.

Examples:

Assume we're in Haskell, the system is a big-endian, and Ordering is the output type. When given the following byte sequence:

00 00 DB FF DF FE

Because it encodes the noncharacter U+10FFFE, the output must be:

LT

For the following byte sequence:

00 00 00 00 00 00

The output must be:

EQ

For the following byte sequence:

FE FF

Since the endianness-swapped version encodes the noncharacter U+FFFE, the output must be:

GT
\$\endgroup\$
  • \$\begingroup\$ @JoKing We output 0 when the test is inconclusive (see Rule #3). A non-UTF character is treated as a noncharacter also in the flipped version (see Rule #5). \$\endgroup\$ – Dannyu NDos Oct 2 '19 at 1:03
  • 3
    \$\begingroup\$ Some more test cases would be good \$\endgroup\$ – Jo King Oct 2 '19 at 1:14
  • 2
    \$\begingroup\$ You should include a description of noncharacters and invalid UTF-16 sequences in your challenge, or at least point to the specification. \$\endgroup\$ – nwellnhof Oct 2 '19 at 14:59
  • 1
    \$\begingroup\$ @nwellnhof I improved the question accordingly. Now vote for reopen, please? \$\endgroup\$ – Dannyu NDos Oct 2 '19 at 23:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.