octets arrived over the network: each element is a number from 0 to 255, that is, exactly one byte. Check whether the stream is well-formed UTF-8, meaning it splits completely into characters according to the rules below.
A character takes 1 to 4 bytes:
| Bytes per character | Binary layout |
|---|---|
| 1 | 0xxxxxxx |
| 2 | 110xxxxx 10xxxxxx |
| 3 | 1110xxxx 10xxxxxx 10xxxxxx |
| 4 | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx |
Here x is any bit. In other words, the first byte of a k-byte character (k ≥ 2) starts with k ones followed by a zero, and each of the remaining k − 1 bytes starts with 10. A one-byte character starts with 0. Nothing else is allowed; you do not need to check what the characters mean.
Return True if the stream is well formed and False otherwise.octets=[226, 130, 172, 65] → True
226 = 11100010 starts a three-byte character; 130 = 10000010 and 172 = 10101100 are valid continuations. 65 = 01000001 is a one-byte character. The stream is well formed.
octets=[195, 65] → False
195 = 11000011 announces a two-byte character, but the next byte 65 = 01000001 does not start with 10.
octets=[128] → False
128 = 10000000 is a continuation byte, and no character may start with one.
octets.length ≤ 2·10⁴octets[i] ≤ 255octets=[226, 130, 172, 65]
True
Run checks the first 3 tests in your browser. Submit checks all 137 tests on the server, hidden ones included.