Debian Codegraph @

scripts/build_punct_map.py

raw
1 #!/usr/bin/env python3
2
3 import os
4 import sys
5 import textwrap
6
7
8 self_path = os.path.dirname(os.path.realpath(__file__));
9 f = open(self_path + "/unicode/DerivedGeneralCategory.txt", "r")
10
11 codepoint_list = []
12
13 # Filter codepoints falling in the right category:
14 for line in f:
15 comment_off = line.find("#")
16 if comment_off >= 0:
17 line = line[:comment_off]
18 line = line.strip()
19 if not line:
20 continue
21
22 char_range, category = line.split(";")
23 char_range = char_range.strip()
24 category = category.strip()
25
26 if category[0] != 'P' and category[0] != 'S':
27 continue
28
29 delim_off = char_range.find("..")
30 if delim_off >= 0:
31 codepoint0 = int(char_range[:delim_off], 16)
32 codepoint1 = int(char_range[delim_off+2:], 16)
33 for codepoint in range(codepoint0, codepoint1 + 1):
34 codepoint_list.append(codepoint)
35 else:
36 codepoint = int(char_range, 16)
37 codepoint_list.append(codepoint)
38 f.close()
39
40
41 codepoint_list.sort()
42
43
44 index0 = 0
45 count = len(codepoint_list)
46
47 records = list()
48 while index0 < count:
49 index1 = index0 + 1
50 while index1 < count and codepoint_list[index1] == codepoint_list[index1-1] + 1:
51 index1 += 1
52
53 if index1 - index0 > 1:
54 # Range of codepoints
55 records.append("R(0x{:04x},0x{:04x})".format(codepoint_list[index0], codepoint_list[index1-1]))
56 else:
57 # Single codepoint
58 records.append("S(0x{:04x})".format(codepoint_list[index0]))
59
60 index0 = index1
61
62 sys.stdout.write("static const unsigned PUNCT_MAP[] = {\n")
63 sys.stdout.write("\n".join(textwrap.wrap(", ".join(records), 110,
64 initial_indent = " ", subsequent_indent=" ")))
65 sys.stdout.write("\n};\n\n")