This is not a question along the lines of, "what is an overlong?" or, "what should I do with overlongs?" as I understand what an overlong is and I understand how they should be handled. This is a question possibly about history and possibly about some limitation I don't understand.
In the UTF-8 encoding scheme you could encode the same binary sequence in multiple ways, for example:
00101010,
11000000 10101010,
11100000 10000000 10101010, and
11110000 10000000 10000000 10101010
All technically decode to the same binary sequence 101010, which all represent the number 42 just with a variable amount of leading zeros. Of course the only valid encoding in UTF-8 is the shortest. The rest are called overlongs and are strictly not valid UTF-8.
But
It seems like this is both:
- Space wasteful
- Parser complicating
If instead each multi-byte sequence was given a starting integer offset then it seems there would be:
- No such thing as an overlong
- Simpler logic for parsers to implement
- More available numbers to represent characters
The offsets would simply be the next possible integer to represent.
| byte length | offset | usable bits |
|---|---|---|
| 1 | 0 | 7 |
| 2 | 2^7 = 128 | 11 |
| 3 | 2^11 = 2048 | 16 |
| 4 | 2^16 = 65536 | 21 |
Then the sequences listed above would all have different values:
00101010= 4211000000 10101010= 128 + 4211100000 10000000 10101010= 2048 + 4211110000 10000000 10000000 10101010= 65536 + 42
and the maximum UTF-8 value would go from 2^21 to 2^21 + 65536.
Is there a technical or historical reason that this isn't the case?