2 # Copyright (c) 2010 Google Inc. All rights reserved.
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above
11 # copyright notice, this list of conditions and the following disclaimer
12 # in the documentation and/or other materials provided with the
14 # * Neither the name of Google Inc. nor the names of its
15 # contributors may be used to endorse or promote products derived from
16 # this software without specific prior written permission.
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 def convert_entity_to_cpp_name(entity):
39 postfix = "EntityName"
41 return "%sSemicolon%s" % (entity[:-1], postfix)
42 return "%s%s" % (entity, postfix)
45 def convert_entity_to_uchar_array(entity):
46 return "{'%s'}" % "', '".join(entity)
49 def convert_value_to_int(value):
50 assert(value[0] == "U")
51 assert(value[1] == "+")
52 return "0x" + value[2:]
55 def offset_table_entry(offset):
56 return " &staticEntityTable[%s]," % offset
59 program_name = os.path.basename(__file__)
60 if len(sys.argv) < 4 or sys.argv[1] != "-o":
61 # Python 3, change to: print("Usage: %s -o OUTPUT_FILE INPUT_FILE" % program_name, file=sys.stderr)
62 sys.stderr.write("Usage: %s -o OUTPUT_FILE INPUT_FILE\n" % program_name)
65 output_path = sys.argv[2]
66 input_path = sys.argv[3]
68 html_entity_names_file = open(input_path)
69 entries = list(csv.reader(html_entity_names_file))
70 html_entity_names_file.close()
72 entries.sort(key = lambda entry: entry[ENTITY])
73 entity_count = len(entries)
75 output_file = open(output_path, "w")
77 output_file.write("""/*
78 * Copyright (C) 2010 Google, Inc. All Rights Reserved.
80 * Redistribution and use in source and binary forms, with or without
81 * modification, are permitted provided that the following conditions
83 * 1. Redistributions of source code must retain the above copyright
84 * notice, this list of conditions and the following disclaimer.
85 * 2. Redistributions in binary form must reproduce the above copyright
86 * notice, this list of conditions and the following disclaimer in the
87 * documentation and/or other materials provided with the distribution.
89 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
90 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
91 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
92 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
93 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
94 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
95 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
96 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
97 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
98 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
99 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
102 // THIS FILE IS GENERATED BY WebCore/html/parser/create-html-entity-table
103 // DO NOT EDIT (unless you are a ninja)!
106 #include "HTMLEntityTable.h"
113 for entry in entries:
114 output_file.write("static const UChar %s[] = %s;\n" % (
115 convert_entity_to_cpp_name(entry[ENTITY]),
116 convert_entity_to_uchar_array(entry[ENTITY])))
118 output_file.write("""
119 static const HTMLEntityTableEntry staticEntityTable[%s] = {\n""" % entity_count)
123 for entry in entries:
124 letter = entry[ENTITY][0]
125 if not index.get(letter):
126 index[letter] = offset
127 output_file.write(' { %s, %s, %s },\n' % (
128 convert_entity_to_cpp_name(entry[ENTITY]),
130 convert_value_to_int(entry[VALUE])))
133 output_file.write("""};
137 output_file.write("static const HTMLEntityTableEntry* uppercaseOffset[] = {\n")
138 for letter in string.ascii_uppercase:
139 output_file.write("%s\n" % offset_table_entry(index[letter]))
140 output_file.write("%s\n" % offset_table_entry(index['a']))
141 output_file.write("""};
143 static const HTMLEntityTableEntry* lowercaseOffset[] = {\n""")
144 for letter in string.ascii_lowercase:
145 output_file.write("%s\n" % offset_table_entry(index[letter]))
146 output_file.write("%s\n" % offset_table_entry(entity_count))
147 output_file.write("""};
151 const HTMLEntityTableEntry* HTMLEntityTable::firstEntryStartingWith(UChar c)
153 if (c >= 'A' && c <= 'Z')
154 return uppercaseOffset[c - 'A'];
155 if (c >= 'a' && c <= 'z')
156 return lowercaseOffset[c - 'a'];
160 const HTMLEntityTableEntry* HTMLEntityTable::lastEntryStartingWith(UChar c)
162 if (c >= 'A' && c <= 'Z')
163 return uppercaseOffset[c - 'A' + 1] - 1;
164 if (c >= 'a' && c <= 'z')
165 return lowercaseOffset[c - 'a' + 1] - 1;
169 const HTMLEntityTableEntry* HTMLEntityTable::firstEntry()
171 return &staticEntityTable[0];
174 const HTMLEntityTableEntry* HTMLEntityTable::lastEntry()
176 return &staticEntityTable[%s - 1];