1#include <wchar.h>23#include "../libc.h"45struct range {6 unsigned long begin, end;7 int valid;8 int nbytes;9};1011int12_validutf8(wchar_t wc, int *nbytes)13{14 static struct range ranges[] = {15 {0, 0x80, 1, 1},16 {0x80, 0x800, 1, 2},17 {0x800, 0xD800, 1, 3},18 {0xD800, 0xDD00, 0, 3},19 {0xDD00, 0x10000, 1, 3},20 {0x10000, 0x110000, 1, 4},21 {0x110000, -1ul, 0, 0},22 };23 struct range *bp;2425 for (bp = ranges; bp->begin > wc || bp->end <= wc; ++bp)26 ;27 *nbytes = bp->nbytes;2829 return bp->valid;30}