Given an array data of integers where only the lowest 8 bits of each entry are meaningful, decide whether the bytes form a valid UTF-8 encoding.
A UTF-8 character is 1 to 4 bytes and its leading byte announces the length:
- 1 byte:
0xxxxxxx - 2 bytes:
110xxxxx - 3 bytes:
1110xxxx - 4 bytes:
11110xxx - every continuation byte must look like
10xxxxxx
Any other leading pattern (such as 10xxxxxx in leading position, or 111110xx) is invalid, as is a character whose continuation bytes run off the end of the array. An empty array is valid.