User:Wtrmute/UTF-8

The regular language UTF-8
When deciding whether a UTF-8 string is valid, one must look for whether the byte classes (marked by the prefixes as designed by Thompson, etc) follow one another in a correct way, but one must also guard against overlong forms. Finally, in principle, one must also reject the encoding of the surrogate pair code points, though this is generally not done on purpose so the UTF-8 may encode invalid UTF-16 strings reversibly ("WTF-8"). The resulting language, even applying these restrictions, is still regular, as the below table shows:

An UTF-8 string is a sequence of any of the byte sequences above, and therefore a valid UTF-8 string (more properly, WTF-8 string) matches the following language:

If we want to refuse the codification of Surrogate Pairs, then the 3-byte forms have to be split:

The language of all UTF-8 strings, as per RFC 3629, is therefore:

Finally, in CESU-8, all four-byte sequences are replaced by equivalent six-byte sequences:

A lot of this complexity was generated to accomodate UTF-16 and its surrogate pair mechanism. If this were not the case, the language would be quite a bit simpler.