/* unicode.c -- utilities for Unicode written by N. Tsuchimura */ #include #include #include #include /* for fprintf() */ /* determine if UTF-8 character or not */ boolean isUTF8(int length, int nth, int c) { c &= 0xff; switch (length * 8 + nth) { case 011: return (0x00 <= c && c < 0x80); case 021: return (0xc2 <= c && c < 0xe0); case 031: return (0xe0 <= c && c < 0xf0); case 041: return (0xf0 <= c && c < 0xf5); case 022: case 032: case 033: case 042: case 043: case 044: return (0x80 <= c && c < 0xc0); default: fprintf(stderr, "isUTF8: unexpected param length=%d, nth=%d\n", length, nth); } return false; } int UTF8length(int first_byte) { first_byte &= 0xff; if (first_byte < 0x80) return 1; if (first_byte < 0xc2) return -2; /* illegal */ if (first_byte < 0xe0) return 2; if (first_byte < 0xf0) return 3; if (first_byte < 0xf5) return 4; return -1; /* reserved/undefined */ } /* with strict range check */ int UTF8Slength(unsigned char *buff, int buff_len) { int i, len; len = UTF8length(buff[0]); if (len < 0) return -2; /* illegal */ if (len > buff_len) return -3; /* overflow */ for (i=0; iUCS_MAX) return uptex % UCS_MAX; /* for OTF package */ return uptex; }