1 hour ago · Tech · hide · 0 comments

In part VII of the series, we saw how to validate a UTF-8 sequence, that is to ensure it can be safely decoded into a Unicode code point. In part VI, we set validation aside and saw how a non-validating decoder would look like. Now it is time to investigate a decoding validator - e.g. decode a UTF-8 sequence without assuming it is valid.An obvious approach would be to perform the two steps sequentially: first validate the sequence, then decode it using the non‑validating decoder. That is a reasonable strategy, especially if the string will be decoded more than once or if subsequent operations assume valid UTF‑8. However, if all we want to do is decode a byte sequence once, we can do better by validating while decoding.Intermixing decoding with validation is not hard, once we have figured out both steps. We can start from the validation algorithm and add composition of the code point value in the “valid sequence” branches. Here is how it is implemented in the utfcpp library: template…

No comments yet. Log in to reply on the Fediverse. Comments will appear here.