Debian Codegraph @

scripts/build_whitespace_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 category_list = [ "Zs" ]
13
14 # Filter codepoints falling in the right category:
15 for line in f:
16 comment_off = line.find("#")
17 if comment_off >= 0:
18 line = line[:comment_off]
19 line = line.strip()
20 if not line:
21 continue
22
23 char_range, category = line.split(";")
24 char_range = char_range.strip()
25 category = category.strip()
26
27 if not category in category_list:
28 continue
29
30 delim_off = char_range.find("..")
31 if delim_off >= 0:
32 codepoint0 = int(char_range[:delim_off], 16)
33 codepoint1 = int(char_range[delim_off+2:], 16)
34 for codepoint in range(codepoint0, codepoint1 + 1):
35 codepoint_list.append(codepoint)
36 else:
37 codepoint = int(char_range, 16)
38 codepoint_list.append(codepoint)
39 f.close()
40
41
42 codepoint_list.sort()
43
44
45 index0 = 0
46 count = len(codepoint_list)
47
48 records = list()
49 while index0 < count:
50 index1 = index0 + 1
51 while index1 < count and codepoint_list[index1] == codepoint_list[index1-1] + 1:
52 index1 += 1
53
54 if index1 - index0 > 1:
55 # Range of codepoints
56 records.append("R(0x{:04x},0x{:04x})".format(codepoint_list[index0], codepoint_list[index1-1]))
57 else:
58 # Single codepoint
59 records.append("S(0x{:04x})".format(codepoint_list[index0]))
60
61 index0 = index1
62
63 sys.stdout.write("static const unsigned WHITESPACE_MAP[] = {\n")
64 sys.stdout.write("\n".join(textwrap.wrap(", ".join(records), 110,
65 initial_indent = " ", subsequent_indent=" ")))
66 sys.stdout.write("\n};\n\n")