Re: RFC: Small mod to fl_utf8len() to detect invalid single char UTF-8 bytes
Greg Ercolano
16:30 Aug 12
On 8/12/26 03:40, 'Albrecht Schlosser'
via fltk.coredev wrote:
I'm not
sure if it is now a good time (before the release of 1.5) to
change such a function in the way I would suggest for correctness
(handling length 5 and 6 as error and returning -1 instead), and I
didn't check FLTK's code for any side effects, but I think it
would be OK.
OK, yeah, I didn't want to change much, so I think the one line
change is the right one "for now".
Later we can figure out how to make it more correct.
I should mention though; FWIW, codex initially suggested
rewriting the code to be as follows, as per RFC 3629:
int fl_utf8len(char c) { const unsigned char u = (unsigned char)c; if (u <= 0x7f) return 1; if (u >= 0xc2 && u <= 0xdf) return 2; if (u >= 0xe0 && u <= 0xef) return 3; if (u >= 0xf0 && u <= 0xf4) return 4; return -1; }
int fl_utf8len1(char c) {
const int len = fl_utf8len(c);
return len < 0 ? 1 : len;
}
..but I didn't want to make such a large change, and asked for a
simpler modification retaining the spirit and formatting of the old
code, so it suggested that one line mod for the return 6 case
instead.
However, using the above code, it seems to properly detect all
possible "invalid bytes", not only for the first byte in the string
(to determine length), but actually anywhere in the utf8 string.
I tested the above code against our old code, and its results
are exactly the same, except for detecting these as
invalid bytes:
0xc0, 0xc1
0xf5 thru 0xff
And RFC 3629 confirms explicitly that those particular values
are in fact invalid:
..that last bit being relevant, and seems to be exactly what the
new code (above) does.
So I'm kinda liking that as the way to go.
So I can either add that in a future commit (+1), or my one line
change (also +1), vs not making any change (-1).
Comments are owned by the poster. All other content is copyright 1998-2026 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.