Skip to content
Myr

gen_utf8_tables.py

This file

Path

scripts/gen_utf8_tables.py

31 lines · 1000 bytes · Python

Last changed by

Not known.

History of this file

Take it

1#!/usr/bin/python23# This program was used to generate jv_utf8_tables.gen.h45mask = lambda n: (1 << n) - 1678def print_table(type, name, t):9    print("static const %s %s[] =" % (type, name))10    for i in range(0,len(t),16):11        print ((" {" if i == 0 else "  ") +12               ", ".join("0x%02x"%n for n in t[i:i+16]) +13               ("," if i + 16 < len(t) else "};"))141516def utf8info(c):17    if c < 0x80: return 1, mask(7)18    if 0x80 <= c <= 0xBF: return 255, mask(6)19    if 0xC0 <= c <= 0xC1: return 0, 020    if 0xC2 <= c <= 0xDF: return 2, mask(5)21    if 0xE0 <= c <= 0xEF: return 3, mask(4)22    if 0xF0 <= c <= 0xF4: return 4, mask(3)23    if 0xF4 <= c <= 0xFF: return 0, 02425table = lambda i: [utf8info(c)[i] for c in range(256)]2627print("#define UTF8_CONTINUATION_BYTE ((unsigned char)255)")2829print_table("unsigned char", "utf8_coding_length", table(0))30print_table("unsigned char", "utf8_coding_bits", table(1))31print_table("int", "utf8_first_codepoint", [0, 0x0, 0x80, 0x800, 0x10000])