2 * This code was written by Rich Felker in 2010; no copyright is claimed.
3 * This code is in the public domain. Attribution is appreciated but
14 /* Decodes UTF-8 byte-by-byte. The c argument must be initialized to 0
15 * to begin decoding; when finished it will contain the Unicode scalar
16 * value decoded. Return value is 1 if finished, 0 if in-progress, and
17 * -1 if an invalid sequence was encountered. After an invalid sequence,
18 * the state (in c) automatically resets to 0 if a continuation byte was
19 * expected to facilitate a calling idiom of immediately retrying a
20 * failed decode call after processing the invalid sequence. If the
21 * second try fails, the byte is invalid as a starter as well.
23 * A trivial usage idiom is:
24 * while (src<end && (n=decode(dst, *src))>=0) 1[dst+=n]=0, src++;
27 int decode(unsigned *c, unsigned b)
33 } else if (b-SA >= SB-SA) {
46 return !(*c&(1U<<31));