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\$

closed as unclear what you're asking by Shaggy, caird coinheringaahing, nwellnhof, Sriotchilism O'Zaic, Xcali Oct 2 at 18:38

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\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 at 1:03
  • 3
    \$\begingroup\$ Some more test cases would be good \$\endgroup\$ – Jo King Oct 2 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 at 14:59
  • \$\begingroup\$ @nwellnhof I improved the question accordingly. Now vote for reopen, please? \$\endgroup\$ – Dannyu NDos Oct 2 at 23:52