UTF-1 is one of the methods to transform ISO/IEC 10646 and Unicode into a sequence of bytes. It was originally for ISO 10646.
The UTF-1 encodes every character with variable length of byte sequence, just like UTF-8 does. It was designed to avoid control codes. But it was not designed to avoid duplication of a slash character ('/'), which is path delimiter for many operating systems. Additionally it takes a bit long to process the algorithm, because it is based on modulo 190, which is not power of two. Eventually UTF-8 was better than that.
In this challenge we are reviving UTF-1.
How it works
Every constant value in tables on this section is represented in hexadecimal value.
The symbol / is the integer division operator, % is the integer modulo operator, and ^ means power. The precedence of operators is ^ first, / second, % third, and others last. Here is how to convert each codepoint to UTF-1 (sorry for typo last time):
codepoint x | UTF-1 |
---|---|
U+0000 to U+009F | (x) |
U+00A0 to U+00FF | (A0,x) |
U+0100 to U+4015 | y=x-100 in (A1+y/BE,T(y%BE)) |
U+4016 to U+38E2D | y=x-4016 in (F6+y/BE^2,T(y/BE%BE),T(y%BE)) |
U+38E2E to U+7FFFFFFF | y=x-38E2E in (FC+y/BE^4,T(y/BE^3%BE),T(y/BE^2%BE),T(y/BE%BE),T(y%BE)) |
T(z) is a function such that:
z | T(z) |
---|---|
0 to 5D | z+21 |
5E to BD | z+42 |
BE to DE | z-BE |
DF to FF | z-60 |
Challenge
Your task is to implement a program or a function or a subroutine that takes one integer, who represents the codepoint of a character, to return a sequence of integers that represents its corresponding UTF-1 value.
Constraints
Input shall be an nonnegative integer up to 0x7FFFFFFF.
Rules
- Standard loopholes apply.
- Standard I/O rules apply.
- Shortest code wins.
Test cases
Taken from the Wikipedia article.
U+007F 7F
U+0080 80
U+009F 9F
U+00A0 A0 A0
U+00BF A0 BF
U+00C0 A0 C0
U+00FF A0 FF
U+0100 A1 21
U+015D A1 7E
U+015E A1 A0
U+01BD A1 FF
U+01BE A2 21
U+07FF AA 72
U+0800 AA 73
U+0FFF B5 48
U+1000 B5 49
U+4015 F5 FF
U+4016 F6 21 21
U+D7FF F7 2F C3
U+E000 F7 3A 79
U+F8FF F7 5C 3C
U+FDD0 F7 62 BA
U+FDEF F7 62 D9
U+FEFF F7 64 4C
U+FFFD F7 65 AD
U+FFFE F7 65 AE
U+FFFF F7 65 AF
U+10000 F7 65 B0
U+38E2D FB FF FF
U+38E2E FC 21 21 21 21
U+FFFFF FC 21 37 B2 7A
U+100000 FC 21 37 B2 7B
U+10FFFF FC 21 39 6E 6C
U+7FFFFFFF FD BD 2B B9 40
x
in(A1+x/BE,T(y%BE))
a typo? Did you meanA1+y/BE
? \$\endgroup\$T
will be less thanBE
since they are mod ofBE
? Then the 3rd and 4th rules ofT
seems useless. \$\endgroup\$