| 1 |
#!/usr/bin/env python3 |
| 2 |
# |
| 3 |
# Script to generate libcpp/printable-chars.inc |
| 4 |
# |
| 5 |
# This file is part of GCC. |
| 6 |
# |
| 7 |
# GCC is free software; you can redistribute it and/or modify it under |
| 8 |
# the terms of the GNU General Public License as published by the Free |
| 9 |
# Software Foundation; either version 3, or (at your option) any later |
| 10 |
# version. |
| 11 |
# |
| 12 |
# GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 13 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 |
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 15 |
# for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with GCC; see the file COPYING3. If not see |
| 19 |
# <http://www.gnu.org/licenses/>. |
| 20 |
|
| 21 |
from pprint import pprint |
| 22 |
import unicodedata |
| 23 |
|
| 24 |
def is_printable_char(code_point) -> bool: |
| 25 |
category = unicodedata.category(chr(code_point)) |
| 26 |
# "Cc" is "control" and "Cf" is "format" |
| 27 |
return category[0] != 'C' |
| 28 |
|
| 29 |
class Range: |
| 30 |
def __init__(self, start, end, value): |
| 31 |
self.start = start |
| 32 |
self.end = end |
| 33 |
self.value = value |
| 34 |
|
| 35 |
def __repr__(self): |
| 36 |
return f'Range({self.start:x}, {self.end:x}, {self.value})' |
| 37 |
|
| 38 |
def make_ranges(value_callback): |
| 39 |
ranges = [] |
| 40 |
for code_point in range(0x10FFFF): |
| 41 |
value = is_printable_char(code_point) |
| 42 |
if 0: |
| 43 |
print(f'{code_point=:x} {value=}') |
| 44 |
if ranges and ranges[-1].value == value: |
| 45 |
# Extend current range |
| 46 |
ranges[-1].end = code_point |
| 47 |
else: |
| 48 |
# Start a new range |
| 49 |
ranges.append(Range(code_point, code_point, value)) |
| 50 |
return ranges |
| 51 |
|
| 52 |
ranges = make_ranges(is_printable_char) |
| 53 |
if 0: |
| 54 |
pprint(ranges) |
| 55 |
|
| 56 |
print(f"/* Generated by contrib/unicode/gen-printable-chars.py") |
| 57 |
print(f" using version {unicodedata.unidata_version}" |
| 58 |
" of the Unicode standard. */") |
| 59 |
print("\nstatic const cppchar_t printable_range_ends[] = {", end="") |
| 60 |
for i, r in enumerate(ranges): |
| 61 |
if i % 8: |
| 62 |
print(" ", end="") |
| 63 |
else: |
| 64 |
print("\n ", end="") |
| 65 |
print("0x%x," % r.end, end="") |
| 66 |
print("\n};\n") |
| 67 |
print("static const bool is_printable[] = {", end="") |
| 68 |
for i, r in enumerate(ranges): |
| 69 |
if i % 24: |
| 70 |
print(" ", end="") |
| 71 |
else: |
| 72 |
print("\n ", end="") |
| 73 |
if r.value: |
| 74 |
print("1,", end="") |
| 75 |
else: |
| 76 |
print("0,", end="") |
| 77 |
print("\n};") |