News for hackers. Stuff that matters.
Re:Fast UTF-8 validation by rectang on Tuesday October 20, 2020 @04:05PM (#24841825)
I tried to simplify the example code for the sake of clarity.

While I acknowledge it would be unusual to provide a library API for finding the value of the last code point specifically, most string libraries provide something like a "codePointAt" function to return the code point at a specific offset. You'll run into the same problem with DECODE_UTF8 there.

    int32_t
    code_point_at(const char *s, size_t len, size_t offset) {
        const char *end = s + len;
        while (s < end && offset > 0) {
            // Move forward by 1-4 bytes depending on header byte value.
            s += UTF8_SKIP(s);
            offset--;
        }
        if (s >= end) {
            return -1;
        }
        return DECODE_UTF8(s); // BOOM! May read beyond string end
    }

There are no comments on this comment yet.