Re: RFC: Small mod to fl_utf8len() to detect invalid single char UTF-8 bytes
Greg Ercolano
17:02 Aug 12
On 8/12/26 09:30, Greg Ercolano wrote:
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;
}
[..]
Here's how it helps the test/terminal program which wants to
demonstrate handling of invalid ANSI/XTERM/UTF-8 sequences, showing
the difference between the old fl_utf8len(), which doesn't detect
any of the 0xC0/1 and >0xF4 cases, vs new code suggested above by
codex that detects all the single-byte issues. We probably should
consider this change, as it probably prevents some accidental
overruns from malformed utf-8:
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'.