Lexicographically earliest valid UTF-8 byte sequence permutation
There are currently 1,114,112 possible Unicode characters (code points). Each character has a unique valid byte sequence in the UTF-8 encoding. Different characters have different length encodings:
- ASCII characters have a 1-byte encoding
00
-7F
. - The next 1920 characters have a 2-byte encoding
C2 80
-DF BF
. - The rest of the BMP has a 3-byte encoding
E0 A0 80
-ED 9F BF
andEE 80 80
-EF BF BF
. - The other planes have a 4-byte encoding
F0 90 80 80
-F4 8F BF BF
.
It's possible for two strings (specific non-normalised sequences of Unicode code points) of Unicode to have byte sequences that are permutations of each other in a number of ways:
- One string could simply be a permutation of the other at the Unicode level, e.g.
ab
(61 62
) andba
(62 61
). - UTF-8 continuation bytes could be switched between two characters, e.g.
¡â
(C2 A1 C3 A2
) and¢á
(C2 A2 C3 A1
). - UTF-8 continuation bytes could be switched within a character, e.g.
ᴵ
(E1 B4 B5
) andᵴ
(E1 B5 B4
).
For this challenge I would like you to write a program or function that finds the string whose UTF-8 byte sequence is lexicographically earliest of all such sequences that are permutations of the UTF-8 byte sequence of a given Unicode string.
For example, if your input is ᵴ¢ába
(E1 B5 B4 C2 A2 C3 A1 62 61
) your output would be ab¡âᴵ
(61 62 C2 A1 C3 A2 E1 B4 B5
).
Note however that some byte sequences are not valid UTF-8 (e.g. E0 80 A0
which is an overlong encoding for a space) so you need to take care to avoid these.
It would be helpful if your "Try It Online" or similar link includes a footer that helps demonstrate the correctness of your output, where this is not obvious from the I/O format or code.
This is code-golf, so the shortest program or function that breaks no standard loopholes wins!