forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharacterProperties.cpp
More file actions
213 lines (188 loc) · 6.75 KB
/
Copy pathCharacterProperties.cpp
File metadata and controls
213 lines (188 loc) · 6.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "hermes/Platform/Unicode/CharacterProperties.h"
#include "hermes/Platform/Unicode/CodePointSet.h"
#include <algorithm>
#include <climits>
#include <iterator>
#include <utility>
namespace hermes {
#include "UnicodeData.inc"
namespace {
struct UnicodeRangeComp {
bool operator()(UnicodeRange p, uint32_t s) const {
return p.second < s;
}
bool operator()(uint32_t s, UnicodeRange p) const {
return s < p.first;
}
};
} // namespace
template <typename UnicodeRangeTable>
inline bool lookup(const UnicodeRangeTable &table, const uint32_t cp) {
return std::binary_search(
std::begin(table), std::end(table), cp, UnicodeRangeComp());
}
bool isUnicodeOnlyLetter(uint32_t cp) {
// "any character in the Unicode categories “Uppercase letter (Lu)”,
// “Lowercase letter (Ll)”, “Titlecase letter (Lt)”, “Modifier letter (Lm)”,
// “Other letter (Lo)”, or “Letter number (Nl)”".
// ASCII characters are not "UnicodeOnly" and so we return false.
if (cp <= 0x7F)
return false;
return lookup(UNICODE_LETTERS, cp);
}
// Special cased due to small number of separate values.
bool isUnicodeOnlySpace(uint32_t cp) {
// "Other category “Zs”: Any other Unicode “space separator”"
// Exclude ASCII.
if (cp <= 0x7F)
return false;
switch (cp) {
case 0xa0:
case 0x1680:
case 0x2000:
case 0x2001:
case 0x2002:
case 0x2003:
case 0x2004:
case 0x2005:
case 0x2006:
case 0x2007:
case 0x2008:
case 0x2009:
case 0x200a:
case 0x202f:
case 0x205f:
case 0x3000:
return true;
default:
return false;
}
}
bool isUnicodeCombiningMark(uint32_t cp) {
// "any character in the Unicode categories “Non-spacing mark (Mn)” or
// “Combining spacing mark (Mc)”"
return lookup(UNICODE_COMBINING_MARK, cp);
}
bool isUnicodeDigit(uint32_t cp) {
// "any character in the Unicode category “Decimal number (Nd)”"
// 0-9 is the common case.
return (cp >= '0' && cp <= '9') || lookup(UNICODE_DIGIT, cp);
};
bool isUnicodeConnectorPunctuation(uint32_t cp) {
// "any character in the Unicode category “Connector punctuation (Pc)"
// _ is the common case.
return cp == '_' || lookup(UNICODE_CONNECTOR_PUNCTUATION, cp);
}
static uint32_t applyTransform(const UnicodeTransformRange &r, uint32_t cp) {
assert(
r.start <= cp && cp < r.start + r.count &&
"range does not contain this cp");
assert(cp <= INT32_MAX && "cp is too big");
// Check if our code point has a mod of 0.
if ((cp - r.start) % r.modulo == 0) {
int32_t cps = static_cast<int32_t>(cp) + r.delta;
assert(cps >= 0 && "delta underflowed");
return static_cast<uint32_t>(cps);
}
return cp;
}
// Predicate used to enable binary search on UnicodeTransformRange.
// Here we return true if the last element of the range is < cp, so <= is
// correct.
static bool operator<(const UnicodeTransformRange &m, uint32_t cp) {
return m.start + m.count <= cp;
}
/// Find all code points which canonicalize to a value in \p range, and add them
/// to \p receiver. This is a slow linear search across all ranges.
static void addPrecanonicalCharacters(
CodePointRange range,
CodePointSet *receiver,
bool unicode) {
if (range.length == 0)
return;
// TODO: if range is ASCII and unicode is not set, we can stop the search
// after the ASCII part as nothing outside ASCII can canonicalize to an ASCII
// character.
const auto start =
unicode ? std::begin(UNICODE_FOLDS) : std::begin(LEGACY_CANONS);
const auto end = unicode ? std::end(UNICODE_FOLDS) : std::end(LEGACY_CANONS);
for (auto iter = start; iter != end; ++iter) {
const UnicodeTransformRange &transform = *iter;
// Get the range of transformed-from and transformed-to characters.
CodePointRange fromRange{transform.start, transform.count};
CodePointRange toRange = fromRange;
toRange.first += transform.delta;
// See if some character in our range will be transformed-to.
if (!range.overlaps(toRange))
continue;
// Looks like it. Add everything.
for (uint32_t cp = fromRange.first; cp < fromRange.end(); cp++) {
uint32_t tcp = applyTransform(transform, cp);
if (tcp != cp && range.first <= tcp && tcp < range.end()) {
receiver->add(cp);
}
}
}
}
/// For each code point in \p range, canonicalize it and add the canonicalized
/// values to \p receiver.
static void
canonicalizeRange(CodePointRange range, CodePointSet *receiver, bool unicode) {
assert(range.length > 0 && "Range should never be empty");
/// Find the first transform that contains or starts after our range.
const auto start =
unicode ? std::begin(UNICODE_FOLDS) : std::begin(LEGACY_CANONS);
const auto end = unicode ? std::end(UNICODE_FOLDS) : std::end(LEGACY_CANONS);
auto transform = std::lower_bound(start, end, range.first);
uint32_t curcp = range.first;
uint32_t endcp = range.end();
while (curcp < endcp && transform != end) {
uint32_t transformEnd = transform->start + transform->count;
assert(transformEnd > curcp && "transform should end after our current cp");
if (transform->start > curcp) {
// Our transform started after the current value, so skip to the
// transform.
curcp = transform->start;
} else {
// Apply this transform for the code points that are in the transform and
// also in our range.
for (; curcp < transformEnd && curcp < endcp; curcp++) {
receiver->add(applyTransform(*transform, curcp));
}
// We have now exhausted this transform; go to the next one.
++transform;
}
}
}
CodePointSet makeCanonicallyEquivalent(const CodePointSet &set, bool unicode) {
// Canonicalize all characters in the set, and then find all characters which
// canonicalize to some element in the set.
CodePointSet canonicalized = set;
for (const auto &range : set.ranges()) {
canonicalizeRange(range, &canonicalized, unicode);
}
CodePointSet result = canonicalized;
for (const auto &range : canonicalized.ranges()) {
addPrecanonicalCharacters(range, &result, unicode);
}
return result;
}
uint32_t canonicalize(uint32_t cp, bool unicode) {
const auto start =
unicode ? std::begin(UNICODE_FOLDS) : std::begin(LEGACY_CANONS);
const auto end = unicode ? std::end(UNICODE_FOLDS) : std::end(LEGACY_CANONS);
auto where = std::lower_bound(start, end, cp);
if (where != end && where->start <= cp && cp < where->start + where->count) {
return applyTransform(*where, cp);
} else {
// No transform for this character, so it canonicalizes to itself.
return cp;
}
}
} // namespace hermes