| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
# Copyright (C) 2010, 2013-2017 Apple Inc. All rights reserved. |
| 4 |
# |
| 5 |
# Redistribution and use in source and binary forms, with or without |
| 6 |
# modification, are permitted provided that the following conditions |
| 7 |
# are met: |
| 8 |
# 1. Redistributions of source code must retain the above copyright |
| 9 |
# notice, this list of conditions and the following disclaimer. |
| 10 |
# 2. Redistributions in binary form must reproduce the above copyright |
| 11 |
# notice, this list of conditions and the following disclaimer in the |
| 12 |
# documentation and/or other materials provided with the distribution. |
| 13 |
# |
| 14 |
# THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 15 |
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 17 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 18 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 19 |
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 20 |
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 21 |
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 22 |
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 |
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 |
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 |
|
| 26 |
import os |
| 27 |
import sys |
| 28 |
|
| 29 |
types = { |
| 30 |
"wordchar": { "UseTable" : True, "data": ['_', ('0','9'), ('A', 'Z'), ('a','z')]}, |
| 31 |
"wordUnicodeIgnoreCaseChar": { "UseTable" : False, "data": ['_', ('0', '9'), ('A', 'Z'), ('a', 'z'), 0x017f, 0x212a]}, |
| 32 |
"nonwordchar": { "UseTable" : True, "Inverse": "wordchar", "data": ['`', (0, ord('0') - 1), (ord('9') + 1, ord('A') - 1), (ord('Z') + 1, ord('_') - 1), (ord('z') + 1, 0x10ffff)]}, |
| 33 |
"nonwordUnicodeIgnoreCaseChar": { "UseTable" : False, "Inverse": "wordUnicodeIgnoreCaseChar", "data": ['`', (0, ord('0') - 1), (ord('9') + 1, ord('A') - 1), (ord('Z') + 1, ord('_') - 1), (ord('z') + 1, 0x017e), (0x0180, 0x2129), (0x212b, 0x10ffff)]}, |
| 34 |
"newline": { "UseTable" : False, "data": ['\n', '\r', 0x2028, 0x2029]}, |
| 35 |
"spaces": { "UseTable" : True, "data": [' ', ('\t', '\r'), 0xa0, 0x1680, 0x2028, 0x2029, 0x202f, 0x205f, 0x3000, (0x2000, 0x200a), 0xfeff]}, |
| 36 |
"nonspaces": { "UseTable" : True, "Inverse": "spaces", "data": [(0, ord('\t') - 1), (ord('\r') + 1, ord(' ') - 1), (ord(' ') + 1, 0x009f), (0x00a1, 0x167f), (0x1681, 0x180d), (0x180f, 0x1fff), (0x200b, 0x2027), (0x202a, 0x202e), (0x2030, 0x205e), (0x2060, 0x2fff), (0x3001, 0xfefe), (0xff00, 0x10ffff)]}, |
| 37 |
"digits": { "UseTable" : False, "data": [('0', '9')]}, |
| 38 |
"nondigits": { "UseTable" : False, "Inverse": "digits", "data": [(0, ord('0') - 1), (ord('9') + 1, 0x10ffff)] } |
| 39 |
} |
| 40 |
entriesPerLine = 50 |
| 41 |
arrays = ""; |
| 42 |
functions = ""; |
| 43 |
emitTables = (len(sys.argv) < 2 or sys.argv[1] != "--no-tables") |
| 44 |
|
| 45 |
for name, classes in types.items(): |
| 46 |
ranges = []; |
| 47 |
size = 0; |
| 48 |
for _class in classes["data"]: |
| 49 |
if type(_class) == str: |
| 50 |
ranges.append((ord(_class), ord(_class))) |
| 51 |
elif type(_class) == int: |
| 52 |
ranges.append((_class, _class)) |
| 53 |
else: |
| 54 |
(min, max) = _class; |
| 55 |
if type(min) == str: |
| 56 |
min = ord(min) |
| 57 |
if type(max) == str: |
| 58 |
max = ord(max) |
| 59 |
if max > 0x7f and min <= 0x7f: |
| 60 |
ranges.append((min, 0x7f)) |
| 61 |
min = 0x80 |
| 62 |
ranges.append((min,max)) |
| 63 |
ranges.sort(); |
| 64 |
|
| 65 |
if emitTables and classes["UseTable"] and (not "Inverse" in classes): |
| 66 |
array = ("static const char _%sData[65536] = {\n" % name); |
| 67 |
i = 0 |
| 68 |
for (min,max) in ranges: |
| 69 |
while i < min: |
| 70 |
i = i + 1 |
| 71 |
array += ('0,') |
| 72 |
if (i % entriesPerLine == 0) and (i != 0): |
| 73 |
array += ('\n') |
| 74 |
while i <= max: |
| 75 |
i = i + 1 |
| 76 |
if (i == 65536): |
| 77 |
array += ("1") |
| 78 |
else: |
| 79 |
array += ('1,') |
| 80 |
if (i % entriesPerLine == 0) and (i != 0): |
| 81 |
array += ('\n') |
| 82 |
while i < 0xffff: |
| 83 |
array += ("0,") |
| 84 |
i = i + 1; |
| 85 |
if (i % entriesPerLine == 0) and (i != 0): |
| 86 |
array += ('\n') |
| 87 |
if i == 0xffff: |
| 88 |
array += ("0") |
| 89 |
array += ("\n};\n\n"); |
| 90 |
arrays += array |
| 91 |
|
| 92 |
# Generate createFunction: |
| 93 |
function = ""; |
| 94 |
function += ("std::unique_ptr<CharacterClass> %sCreate()\n" % name) |
| 95 |
function += ("{\n") |
| 96 |
if emitTables and classes["UseTable"]: |
| 97 |
if "Inverse" in classes: |
| 98 |
function += (" auto characterClass = std::make_unique<CharacterClass>(_%sData, true);\n" % (classes["Inverse"])) |
| 99 |
else: |
| 100 |
function += (" auto characterClass = std::make_unique<CharacterClass>(_%sData, false);\n" % (name)) |
| 101 |
else: |
| 102 |
function += (" auto characterClass = std::make_unique<CharacterClass>();\n") |
| 103 |
hasNonBMPCharacters = False |
| 104 |
for (min, max) in ranges: |
| 105 |
if (min == max): |
| 106 |
if (min > 127): |
| 107 |
function += (" characterClass->m_matchesUnicode.append(0x%04x);\n" % min) |
| 108 |
else: |
| 109 |
function += (" characterClass->m_matches.append(0x%02x);\n" % min) |
| 110 |
continue |
| 111 |
if (min > 127) or (max > 127): |
| 112 |
function += (" characterClass->m_rangesUnicode.append(CharacterRange(0x%04x, 0x%04x));\n" % (min, max)) |
| 113 |
else: |
| 114 |
function += (" characterClass->m_ranges.append(CharacterRange(0x%02x, 0x%02x));\n" % (min, max)) |
| 115 |
if max >= 0x10000: |
| 116 |
hasNonBMPCharacters = True |
| 117 |
function += (" characterClass->m_hasNonBMPCharacters = %s;\n" % ("true" if hasNonBMPCharacters else "false")) |
| 118 |
function += (" return characterClass;\n") |
| 119 |
function += ("}\n\n") |
| 120 |
functions += function |
| 121 |
|
| 122 |
if (len(sys.argv) > 1): |
| 123 |
path = sys.argv[-1] |
| 124 |
dirname = os.path.dirname(path) |
| 125 |
if not os.path.isdir(dirname): |
| 126 |
os.makedirs(dirname) |
| 127 |
f = open(path, "w") |
| 128 |
f.write(arrays) |
| 129 |
f.write(functions) |
| 130 |
f.close() |
| 131 |
else: |
| 132 |
print(arrays) |
| 133 |
print(functions) |
| 134 |
|