scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

_validutf8.c (552B)


      1 #include <wchar.h>
      2 
      3 #include "../libc.h"
      4 
      5 struct range {
      6 	unsigned long begin, end;
      7 	int valid;
      8 	int nbytes;
      9 };
     10 
     11 int
     12 _validutf8(wchar_t wc, int *nbytes)
     13 {
     14 
     15 	static struct range ranges[] = {
     16 		{0,         0x80,       1, 1},
     17 		{0x80,      0x800,      1, 2},
     18 		{0x800,     0xD800,     1, 3},
     19 		{0xD800,    0xDD00,     0, 3},
     20 		{0xDD00,    0x10000,    1, 3},
     21 		{0x10000,   0x110000,   1, 4},
     22 		{0x11000,   -1ul,       0, 0},
     23 	};
     24 	struct range *bp;
     25 
     26 	for (bp = ranges; bp->begin <= wc && bp->end > wc; ++bp)
     27 		;
     28 	*nbytes = bp->nbytes;
     29 
     30 	return bp->valid;
     31 }