1 //===--- UnicodeNameMappingGenerator.cpp - Unicode name data generator ---===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file is used to generate lib/Support/UnicodeNameToCodepointGenerated.cpp
10 // using UnicodeData.txt and NameAliases.txt available at
11 // https://unicode.org/Public/15.0.0/ucd/
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/ADT/StringExtras.h"
16 #include "llvm/ADT/StringRef.h"
25 #include <unordered_map>
29 static const llvm::StringRef Letters
=
30 " _-ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
32 // Collect names UnicodeData.txt and AliasNames.txt
33 // There may be multiple names per code points.
34 static std::unordered_multimap
<char32_t
, std::string
>
35 loadDataFiles(const std::string
&NamesFile
, const std::string
&AliasesFile
) {
36 std::unordered_multimap
<char32_t
, std::string
> CollectedCharacters
;
37 auto FromFile
= [&](const std::string
&File
, bool IsAliasFile
= false) {
38 std::ifstream
InputFile(File
);
39 for (std::string Line
; getline(InputFile
, Line
);) {
40 if (Line
.empty() || !isxdigit(Line
[0]))
42 auto FirstSemiPos
= Line
.find(';');
43 if (FirstSemiPos
== std::string::npos
)
45 auto SecondSemiPos
= Line
.find(';', FirstSemiPos
+ 1);
46 if (SecondSemiPos
== std::string::npos
)
48 unsigned long long CodePoint
;
49 if (llvm::getAsUnsignedInteger(
50 llvm::StringRef(Line
.c_str(), FirstSemiPos
), 16, CodePoint
)) {
55 Line
.substr(FirstSemiPos
+ 1, SecondSemiPos
- FirstSemiPos
- 1);
57 if (!Name
.empty() && Name
[0] == '<') {
58 // Ignore ranges of characters, as their name is either absent or
63 // Some aliases are ignored for compatibility with C++
65 std::string Kind
= Line
.substr(SecondSemiPos
+ 1);
66 if (Kind
!= "control" && Kind
!= "correction" && Kind
!= "alternate")
70 auto InsertUnique
= [&](char32_t CP
, std::string Name
) {
71 auto It
= CollectedCharacters
.find(CP
);
72 while (It
!= std::end(CollectedCharacters
) && It
->first
== CP
) {
73 if (It
->second
== Name
)
77 CollectedCharacters
.insert({CP
, std::move(Name
)});
79 InsertUnique(CodePoint
, std::move(Name
));
84 FromFile(AliasesFile
, true);
85 return CollectedCharacters
;
92 // When inserting named codepoint
93 // We create a node per character in the name.
94 // SPARKLE becomes S <- P <- A <- R <- K <- L <- E
95 // Once all characters are inserted, the tree is compacted
96 void insert(llvm::StringRef Name
, char32_t Codepoint
) {
98 for (auto Ch
: Name
) {
99 std::string
Label(1, Ch
);
100 auto It
= llvm::find_if(N
->Children
,
101 [&](const auto &C
) { return C
->Name
== Label
; });
102 if (It
== N
->Children
.end()) {
103 It
= N
->Children
.insert(It
, std::make_unique
<Node
>(Label
, N
));
107 N
->Value
= Codepoint
;
110 void compact() { compact(Root
.get()); }
112 // This creates 2 arrays of bytes from the tree:
113 // A serialized dictionary of node labels,
114 // And the nodes themselves.
115 // The name of each label is found by indexing into the dictionary.
116 // The longest names are inserted first into the dictionary,
117 // in the hope it will contain shorter labels as substring,
118 // thereby reducing duplication.
119 // We could theorically be more clever by trying to minimizing the size
120 // of the dictionary.
121 std::pair
<std::string
, std::vector
<uint8_t>> serialize() {
122 std::set
<std::string
> Names
= this->getNameFragments();
123 std::vector
<std::string
> Sorted(Names
.begin(), Names
.end());
124 llvm::sort(Sorted
, [](const auto &a
, const auto &b
) {
125 return a
.size() > b
.size();
127 std::string
Dict(Letters
.begin(), Letters
.end());
129 for (const std::string
&Name
: Sorted
) {
130 if (Name
.size() <= 1)
132 if (Dict
.find(Name
) != std::string::npos
)
137 if (Dict
.size() >= std::numeric_limits
<uint16_t>::max()) {
138 fprintf(stderr
, "Dictionary too big to be serialized");
142 auto Bytes
= dumpIndex(Dict
);
143 return {Dict
, Bytes
};
146 std::set
<std::string
> getNameFragments() {
147 std::set
<std::string
> Keys
;
148 collectKeys(Root
.get(), Keys
);
152 // Maps a valid char in an Unicode character name
153 // To a 6 bits index.
154 static uint8_t letter(char C
) {
155 auto Pos
= Letters
.find(C
);
156 assert(Pos
!= std::string::npos
&&
157 "Invalid letter in Unicode character name");
162 // +================+============+======================+=============+========+===+==============+===============+
163 // | 0 | 1 | 2-7 (6) | 8-23 | 24-44 | | 46 | 47 |
164 // +================+============+======================+=============+========+===+==============+===============+
165 // | Has Value | Has Long Name | Letter OR Name Size | Dict Index | Value | | Has Sibling | Has Children |
166 // +----------------+------------+----------------------+-------------+--------+---+--------------+---------------+
169 std::vector
<uint8_t> dumpIndex(const std::string
&Dict
) {
170 struct ChildrenOffset
{
176 // Keep track of the start of each node
177 // position in the serialized data.
178 std::unordered_map
<Node
*, int32_t> Offsets
;
180 // Keep track of where to write the index
181 // of the first children
182 std::vector
<ChildrenOffset
> ChildrenOffsets
;
183 std::unordered_map
<Node
*, bool> SiblingTracker
;
184 std::deque
<Node
*> AllNodes
;
185 std::vector
<uint8_t> Bytes
;
186 Bytes
.reserve(250'000);
187 // This leading byte is used by the reading code to detect the root node.
190 auto CollectChildren
= [&SiblingTracker
, &AllNodes
](const auto &Children
) {
191 for (std::size_t Index
= 0; Index
< Children
.size(); Index
++) {
192 const std::unique_ptr
<Node
> &Child
= Children
[Index
];
193 AllNodes
.push_back(Child
.get());
194 if (Index
!= Children
.size() - 1)
195 SiblingTracker
[Child
.get()] = true;
198 CollectChildren(Root
->Children
);
200 while (!AllNodes
.empty()) {
201 const std::size_t Offset
= Bytes
.size();
202 Node
*const N
= AllNodes
.front();
203 AllNodes
.pop_front();
205 assert(!N
->Name
.empty());
208 uint8_t FirstByte
= (!!N
->Value
) ? 0x80 : 0;
209 // Single letter node are indexed in 6 bits
210 if (N
->Name
.size() == 1) {
211 FirstByte
|= letter(N
->Name
[0]);
212 Bytes
.push_back(FirstByte
);
214 // Otherwise we use a 16 bits index
215 FirstByte
= FirstByte
| uint8_t(N
->Name
.size()) | 0x40;
216 Bytes
.push_back(FirstByte
);
217 auto PosInDict
= Dict
.find(N
->Name
);
218 assert(PosInDict
!= std::string::npos
);
219 uint8_t Low
= PosInDict
;
220 uint8_t High
= ((PosInDict
>> 8) & 0xFF);
221 Bytes
.push_back(High
);
222 Bytes
.push_back(Low
);
225 const bool HasSibling
= SiblingTracker
.count(N
) != 0;
226 const bool HasChildren
= N
->Children
.size() != 0;
229 uint32_t Value
= (*(N
->Value
) << 3);
230 uint8_t H
= ((Value
>> 16) & 0xFF);
231 uint8_t M
= ((Value
>> 8) & 0xFF);
232 uint8_t L
= (Value
& 0xFF) | uint8_t(HasSibling
? 0x01 : 0) |
233 uint8_t(HasChildren
? 0x02 : 0);
240 ChildrenOffsets
.push_back(
241 ChildrenOffset
{N
->Children
[0].get(), Bytes
.size(), true});
242 // index of the first children
243 Bytes
.push_back(0x00);
244 Bytes
.push_back(0x00);
245 Bytes
.push_back(0x00);
248 // When there is no value (that's most intermediate nodes)
249 // Dispense of the 3 values bytes, and only store
250 // 1 byte to track whether the node has sibling and children
251 // + 2 bytes for the index of the first children if necessary.
252 // That index also uses bytes 0-6 of the previous byte.
254 uint8_t(HasSibling
? 0x80 : 0) | uint8_t(HasChildren
? 0x40 : 0);
255 Bytes
.push_back(Byte
);
257 ChildrenOffsets
.emplace_back(
258 ChildrenOffset
{N
->Children
[0].get(), Bytes
.size() - 1, false});
259 Bytes
.push_back(0x00);
260 Bytes
.push_back(0x00);
263 CollectChildren(N
->Children
);
266 // Once all the nodes are in the inndex
267 // Fill the bytes we left to indicate the position
269 for (const ChildrenOffset
&Parent
: ChildrenOffsets
) {
270 const auto It
= Offsets
.find(Parent
.FirstChild
);
271 assert(It
!= Offsets
.end());
272 std::size_t Pos
= It
->second
;
273 if (Parent
.HasValue
) {
274 Bytes
[Parent
.Offset
] = ((Pos
>> 16) & 0xFF);
276 Bytes
[Parent
.Offset
] =
277 Bytes
[Parent
.Offset
] | uint8_t((Pos
>> 16) & 0xFF);
279 Bytes
[Parent
.Offset
+ 1] = ((Pos
>> 8) & 0xFF);
280 Bytes
[Parent
.Offset
+ 2] = Pos
& 0xFF;
283 // Add some padding so that the deserialization code
284 // doesn't try to read past the enf of the array.
296 void collectKeys(Node
*N
, std::set
<std::string
> &Keys
) {
297 Keys
.insert(N
->Name
);
298 for (const std::unique_ptr
<Node
> &Child
: N
->Children
) {
299 collectKeys(Child
.get(), Keys
);
303 // Merge sequences of 1-character nodes
304 // This greatly reduce the total number of nodes,
305 // and therefore the size of the index.
306 // When the tree gets serialized, we only have 5 bytes to store the
307 // size of a name. Overlong names (>32 characters) are therefore
308 // kep into separate nodes
309 void compact(Node
*N
) {
310 for (auto &&Child
: N
->Children
) {
311 compact(Child
.get());
313 if (N
->Parent
&& N
->Parent
->Children
.size() == 1 && !N
->Parent
->Value
&&
314 (N
->Parent
->Name
.size() + N
->Name
.size() <= 32)) {
315 N
->Parent
->Value
= N
->Value
;
316 N
->Parent
->Name
+= N
->Name
;
317 N
->Parent
->Children
= std::move(N
->Children
);
318 for (std::unique_ptr
<Node
> &c
: N
->Parent
->Children
) {
319 c
->Parent
= N
->Parent
;
324 Node(std::string Name
, Node
*Parent
= nullptr)
325 : Name(Name
), Parent(Parent
) {}
327 std::vector
<std::unique_ptr
<Node
>> Children
;
329 Node
*Parent
= nullptr;
330 std::optional
<char32_t
> Value
;
333 std::unique_ptr
<Node
> Root
= std::make_unique
<Node
>("");
336 extern const char *UnicodeLicense
;
338 int main(int argc
, char **argv
) {
339 printf("Unicode name -> codepoint mapping generator\n"
340 "Usage: %s UnicodeData.txt NameAliases.txt output\n\n",
342 printf("NameAliases.txt can be found at "
343 "https://unicode.org/Public/15.0.0/ucd/NameAliases.txt\n"
344 "UnicodeData.txt can be found at "
345 "https://unicode.org/Public/15.0.0/ucd/UnicodeData.txt\n\n");
350 FILE *Out
= fopen(argv
[3], "w");
352 printf("Error creating output file.\n");
357 uint32_t NameCount
= 0;
358 std::size_t LongestName
= 0;
359 auto Entries
= loadDataFiles(argv
[1], argv
[2]);
360 for (const std::pair
<const char32_t
, std::string
> &Entry
: Entries
) {
361 char32_t Codepoint
= Entry
.first
;
362 const std::string
&Name
= Entry
.second
;
363 // Ignore names which are not valid.
365 !llvm::all_of(Name
, [](char C
) { return Letters
.contains(C
); })) {
368 printf("%06x: %s\n", static_cast<unsigned int>(Codepoint
), Name
.c_str());
369 T
.insert(Name
, Codepoint
);
371 std::max(LongestName
, std::size_t(llvm::count_if(Name
, llvm::isAlnum
)));
376 std::pair
<std::string
, std::vector
<uint8_t>> Data
= T
.serialize();
377 const std::string
&Dict
= Data
.first
;
378 const std::vector
<uint8_t> &Tree
= Data
.second
;
381 //===------------- Support/UnicodeNameToCodepointGenerated.cpp ------------===//
382 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
383 // See https://llvm.org/LICENSE.txt for license information.
384 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
386 //===----------------------------------------------------------------------===//
388 // This file implements mapping the name of a unicode code point to its value.
390 // This file was generated using %s.
391 // Do not edit manually.
393 //===----------------------------------------------------------------------===//
398 #include "llvm
/Support
/Compiler
.h
"
402 argv
[0], UnicodeLicense
);
405 "namespace llvm { namespace sys { namespace unicode { \n"
406 "extern const char *UnicodeNameToCodepointDict;\n"
407 "extern const uint8_t *UnicodeNameToCodepointIndex;\n"
408 "extern const std::size_t UnicodeNameToCodepointIndexSize;\n"
409 "extern const std::size_t UnicodeNameToCodepointLargestNameSize;\n");
411 fprintf(Out
, "const char* UnicodeNameToCodepointDict = \"%s\";\n",
414 fprintf(Out
, "uint8_t UnicodeNameToCodepointIndex_[%zu] = {\n",
417 for (auto Byte
: Tree
) {
418 fprintf(Out
, "0x%02x,", Byte
);
422 fprintf(Out
, "const uint8_t* UnicodeNameToCodepointIndex = "
423 "UnicodeNameToCodepointIndex_; \n");
424 fprintf(Out
, "const std::size_t UnicodeNameToCodepointIndexSize = %zu;\n",
427 "const std::size_t UnicodeNameToCodepointLargestNameSize = %zu;\n",
429 fprintf(Out
, "\n}}}\n");
431 printf("Generated %s: %u Files.\nIndex: %f kB, Dictionary: %f kB.\nDone\n\n",
432 argv
[3], NameCount
, Tree
.size() / 1024.0, Dict
.size() / 1024.0);
435 const char *UnicodeLicense
= R
"(
437 UNICODE, INC. LICENSE AGREEMENT - DATA FILES AND SOFTWARE
439 See Terms of Use <https://www.unicode.org/copyright.html>
440 for definitions of Unicode Inc.’s Data Files and Software.
442 NOTICE TO USER: Carefully read the following legal agreement.
443 BY DOWNLOADING, INSTALLING, COPYING OR OTHERWISE USING UNICODE INC.'S
444 DATA FILES ("DATA FILES
"), AND/OR SOFTWARE ("SOFTWARE
"),
445 YOU UNEQUIVOCALLY ACCEPT, AND AGREE TO BE BOUND BY, ALL OF THE
446 TERMS AND CONDITIONS OF THIS AGREEMENT.
447 IF YOU DO NOT AGREE, DO NOT DOWNLOAD, INSTALL, COPY, DISTRIBUTE OR USE
448 THE DATA FILES OR SOFTWARE.
450 COPYRIGHT AND PERMISSION NOTICE
452 Copyright © 1991-2022 Unicode, Inc. All rights reserved.
453 Distributed under the Terms of Use in https://www.unicode.org/copyright.html.
455 Permission is hereby granted, free of charge, to any person obtaining
456 a copy of the Unicode data files and any associated documentation
457 (the "Data Files
") or Unicode software and any associated documentation
458 (the "Software
") to deal in the Data Files or Software
459 without restriction, including without limitation the rights to use,
460 copy, modify, merge, publish, distribute, and/or sell copies of
461 the Data Files or Software, and to permit persons to whom the Data Files
462 or Software are furnished to do so, provided that either
463 (a) this copyright and permission notice appear with all copies
464 of the Data Files or Software, or
465 (b) this copyright and permission notice appear in associated
468 THE DATA FILES AND SOFTWARE ARE PROVIDED "AS IS
", WITHOUT WARRANTY OF
469 ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
470 WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
471 NONINFRINGEMENT OF THIRD PARTY RIGHTS.
472 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS
473 NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL
474 DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
475 DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
476 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
477 PERFORMANCE OF THE DATA FILES OR SOFTWARE.
479 Except as contained in this notice, the name of a copyright holder
480 shall not be used in advertising or otherwise to promote the sale,
481 use or other dealings in these Data Files or Software without prior
482 written authorization of the copyright holder.