cld3 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/Gemfile +18 -0
- data/LICENSE +204 -0
- data/LICENSE_CLD3 +203 -0
- data/README.md +22 -0
- data/cld3.gemspec +35 -0
- data/ext/cld3/base.cc +36 -0
- data/ext/cld3/base.h +106 -0
- data/ext/cld3/casts.h +98 -0
- data/ext/cld3/embedding_feature_extractor.cc +51 -0
- data/ext/cld3/embedding_feature_extractor.h +182 -0
- data/ext/cld3/embedding_network.cc +196 -0
- data/ext/cld3/embedding_network.h +186 -0
- data/ext/cld3/embedding_network_params.h +285 -0
- data/ext/cld3/extconf.rb +49 -0
- data/ext/cld3/feature_extractor.cc +137 -0
- data/ext/cld3/feature_extractor.h +633 -0
- data/ext/cld3/feature_extractor.proto +50 -0
- data/ext/cld3/feature_types.cc +72 -0
- data/ext/cld3/feature_types.h +158 -0
- data/ext/cld3/fixunicodevalue.cc +55 -0
- data/ext/cld3/fixunicodevalue.h +69 -0
- data/ext/cld3/float16.h +58 -0
- data/ext/cld3/fml_parser.cc +308 -0
- data/ext/cld3/fml_parser.h +123 -0
- data/ext/cld3/generated_entities.cc +296 -0
- data/ext/cld3/generated_ulscript.cc +678 -0
- data/ext/cld3/generated_ulscript.h +142 -0
- data/ext/cld3/getonescriptspan.cc +1109 -0
- data/ext/cld3/getonescriptspan.h +124 -0
- data/ext/cld3/integral_types.h +37 -0
- data/ext/cld3/lang_id_nn_params.cc +57449 -0
- data/ext/cld3/lang_id_nn_params.h +178 -0
- data/ext/cld3/language_identifier_features.cc +165 -0
- data/ext/cld3/language_identifier_features.h +116 -0
- data/ext/cld3/nnet_language_identifier.cc +380 -0
- data/ext/cld3/nnet_language_identifier.h +175 -0
- data/ext/cld3/nnet_language_identifier_c.cc +72 -0
- data/ext/cld3/offsetmap.cc +478 -0
- data/ext/cld3/offsetmap.h +168 -0
- data/ext/cld3/port.h +143 -0
- data/ext/cld3/registry.cc +28 -0
- data/ext/cld3/registry.h +242 -0
- data/ext/cld3/relevant_script_feature.cc +89 -0
- data/ext/cld3/relevant_script_feature.h +49 -0
- data/ext/cld3/script_detector.h +156 -0
- data/ext/cld3/sentence.proto +77 -0
- data/ext/cld3/sentence_features.cc +29 -0
- data/ext/cld3/sentence_features.h +35 -0
- data/ext/cld3/simple_adder.h +72 -0
- data/ext/cld3/stringpiece.h +81 -0
- data/ext/cld3/task_context.cc +161 -0
- data/ext/cld3/task_context.h +81 -0
- data/ext/cld3/task_context_params.cc +74 -0
- data/ext/cld3/task_context_params.h +54 -0
- data/ext/cld3/task_spec.proto +98 -0
- data/ext/cld3/text_processing.cc +245 -0
- data/ext/cld3/text_processing.h +30 -0
- data/ext/cld3/unicodetext.cc +96 -0
- data/ext/cld3/unicodetext.h +144 -0
- data/ext/cld3/utf8acceptinterchange.h +486 -0
- data/ext/cld3/utf8prop_lettermarkscriptnum.h +1631 -0
- data/ext/cld3/utf8repl_lettermarklower.h +758 -0
- data/ext/cld3/utf8scannot_lettermarkspecial.h +1455 -0
- data/ext/cld3/utf8statetable.cc +1344 -0
- data/ext/cld3/utf8statetable.h +285 -0
- data/ext/cld3/utils.cc +241 -0
- data/ext/cld3/utils.h +144 -0
- data/ext/cld3/workspace.cc +64 -0
- data/ext/cld3/workspace.h +177 -0
- data/lib/cld3.rb +99 -0
- metadata +158 -0
@@ -0,0 +1,1344 @@
|
|
1
|
+
// Copyright 2013 Google Inc. All Rights Reserved.
|
2
|
+
//
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
// you may not use this file except in compliance with the License.
|
5
|
+
// You may obtain a copy of the License at
|
6
|
+
//
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
//
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
// See the License for the specific language governing permissions and
|
13
|
+
// limitations under the License.
|
14
|
+
|
15
|
+
//
|
16
|
+
// State Table follower for scanning UTF-8 strings without converting to
|
17
|
+
// 32- or 16-bit Unicode values.
|
18
|
+
//
|
19
|
+
|
20
|
+
#ifdef COMPILER_MSVC
|
21
|
+
// MSVC warns: warning C4309: 'initializing' : truncation of constant value
|
22
|
+
// But the value is in fact not truncated. 0xFF still comes out 0xFF at
|
23
|
+
// runtime.
|
24
|
+
#pragma warning ( disable : 4309 )
|
25
|
+
#endif
|
26
|
+
|
27
|
+
#include "utf8statetable.h"
|
28
|
+
|
29
|
+
#include <stdint.h> // for uintptr_t
|
30
|
+
#include <string.h> // for NULL, memcpy, memmove
|
31
|
+
|
32
|
+
#include "integral_types.h" // for uint8, uint32, int8
|
33
|
+
#include "offsetmap.h"
|
34
|
+
#include "port.h"
|
35
|
+
#include "stringpiece.h"
|
36
|
+
|
37
|
+
namespace chrome_lang_id {
|
38
|
+
namespace CLD2 {
|
39
|
+
|
40
|
+
static const int kReplaceAndResumeFlag = 0x80; // Bit in del byte to distinguish
|
41
|
+
// optional next-state field
|
42
|
+
// after replacement text
|
43
|
+
static const int kHtmlPlaintextFlag = 0x80; // Bit in add byte to distinguish
|
44
|
+
// HTML replacement vs. plaintext
|
45
|
+
|
46
|
+
/**
|
47
|
+
* This code implements a little interpreter for UTF8 state
|
48
|
+
* tables. There are three kinds of quite-similar state tables,
|
49
|
+
* property, scanning, and replacement. Each state in one of
|
50
|
+
* these tables consists of an array of 256 or 64 one-byte
|
51
|
+
* entries. The state is subscripted by an incoming source byte,
|
52
|
+
* and the entry either specifies the next state or specifies an
|
53
|
+
* action. Space-optimized tables have full 256-entry states for
|
54
|
+
* the first byte of a UTF-8 character, but only 64-entry states
|
55
|
+
* for continuation bytes. Space-optimized tables may only be
|
56
|
+
* used with source input that has been checked to be
|
57
|
+
* structurally- (or stronger interchange-) valid.
|
58
|
+
*
|
59
|
+
* A property state table has an unsigned one-byte property for
|
60
|
+
* each possible UTF-8 character. One-byte character properties
|
61
|
+
* are in the state[0] array, while for other lengths the
|
62
|
+
* state[0] array gives the next state, which contains the
|
63
|
+
* property value for two-byte characters or yet another state
|
64
|
+
* for longer ones. The code simply loads the right number of
|
65
|
+
* next-state values, then returns the final byte as property
|
66
|
+
* value. There are no actions specified in property tables.
|
67
|
+
* States are typically shared for multi-byte UTF-8 characters
|
68
|
+
* that all have the same property value.
|
69
|
+
*
|
70
|
+
* A scanning state table has entries that are either a
|
71
|
+
* next-state specifier for bytes that are accepted by the
|
72
|
+
* scanner, or an exit action for the last byte of each
|
73
|
+
* character that is rejected by the scanner.
|
74
|
+
*
|
75
|
+
* Scanning long strings involves a tight loop that picks up one
|
76
|
+
* byte at a time and follows next-state value back to state[0]
|
77
|
+
* for each accepted UTF-8 character. Scanning stops at the end
|
78
|
+
* of the string or at the first character encountered that has
|
79
|
+
* an exit action such as "reject". Timing information is given
|
80
|
+
* below.
|
81
|
+
*
|
82
|
+
* Since so much of Google's text is 7-bit-ASCII values
|
83
|
+
* (approximately 94% of the bytes of web documents), the
|
84
|
+
* scanning interpreter has two speed optimizations. One checks
|
85
|
+
* 8 bytes at a time to see if they are all in the range lo..hi,
|
86
|
+
* as specified in constants in the overall statetable object.
|
87
|
+
* The check involves ORing together four 4-byte values that
|
88
|
+
* overflow into the high bit of some byte when a byte is out of
|
89
|
+
* range. For seven-bit-ASCII, lo is 0x20 and hi is 0x7E. This
|
90
|
+
* loop is about 8x faster than the one-byte-at-a-time loop.
|
91
|
+
*
|
92
|
+
* If checking for exit bytes in the 0x00-0x1F and 7F range is
|
93
|
+
* unneeded, an even faster loop just looks at the high bits of
|
94
|
+
* 8 bytes at once, and is about 1.33x faster than the lo..hi
|
95
|
+
* loop.
|
96
|
+
*
|
97
|
+
* Exit from the scanning routines backs up to the first byte of
|
98
|
+
* the rejected character, so the text spanned is always a
|
99
|
+
* complete number of UTF-8 characters. The normal scanning exit
|
100
|
+
* is at the first rejected character, or at the end of the
|
101
|
+
* input text. Scanning also exits on any detected ill-formed
|
102
|
+
* character or at a special do-again action built into some
|
103
|
+
* exit-optimized tables. The do-again action gets back to the
|
104
|
+
* top of the scanning loop to retry eight-byte ASCII scans. It
|
105
|
+
* is typically put into state tables after four seven-bit-ASCII
|
106
|
+
* characters in a row are seen, to allow restarting the fast
|
107
|
+
* scan after some slower processing of multi-byte characters.
|
108
|
+
*
|
109
|
+
* A replacement state table is similar to a scanning state
|
110
|
+
* table but has more extensive actions. The default
|
111
|
+
* byte-at-a-time loop copies one byte from source to
|
112
|
+
* destination and goes to the next state. The replacement
|
113
|
+
* actions overwrite 1-3 bytes of the destination with different
|
114
|
+
* bytes, possibly shortening the output by 1 or 2 bytes. The
|
115
|
+
* replacement bytes come from within the state table, from
|
116
|
+
* dummy states inserted just after any state that contains a
|
117
|
+
* replacement action. This gives a quick address calculation for
|
118
|
+
* the replacement byte(s) and gives some cache locality.
|
119
|
+
*
|
120
|
+
* Additional replacement actions use one or two bytes from
|
121
|
+
* within dummy states to index a side table of more-extensive
|
122
|
+
* replacements. The side table specifies a length of 0..15
|
123
|
+
* destination bytes to overwrite and a length of 0..127 bytes
|
124
|
+
* to overwrite them with, plus the actual replacement bytes.
|
125
|
+
*
|
126
|
+
* This side table uses one extra bit to specify a pair of
|
127
|
+
* replacements, the first to be used in an HTML context and the
|
128
|
+
* second to be used in a plaintext context. This allows
|
129
|
+
* replacements that are spelled with "<" in the former
|
130
|
+
* context and "<" in the latter.
|
131
|
+
*
|
132
|
+
* The side table also uses an extra bit to specify a non-zero
|
133
|
+
* next state after a replacement. This allows a combination
|
134
|
+
* replacement and state change, used to implement a limited
|
135
|
+
* version of the Boyer-Moore algorithm for multi-character
|
136
|
+
* replacement without backtracking. This is useful when there
|
137
|
+
* are overlapping replacements, such as ch => x and also c =>
|
138
|
+
* y, the latter to be used only if the character after c is not
|
139
|
+
* h. in this case, the state[0] table's entry for c would
|
140
|
+
* change c to y and also have a next-state of say n, and the
|
141
|
+
* state[n] entry for h would specify a replacement of the two
|
142
|
+
* bytes yh by x. No backtracking is needed.
|
143
|
+
*
|
144
|
+
* A replacement table may also include the exit actions of a
|
145
|
+
* scanning state table, so some character sequences can
|
146
|
+
* terminate early.
|
147
|
+
*
|
148
|
+
* During replacement, an optional data structure called an
|
149
|
+
* offset map can be updated to reflect each change in length
|
150
|
+
* between source and destination. This offset map can later be
|
151
|
+
* used to map destination-string offsets to corresponding
|
152
|
+
* source-string offsets or vice versa.
|
153
|
+
*
|
154
|
+
* The routines below also have variants in which state-table
|
155
|
+
* entries are all two bytes instead of one byte. This allows
|
156
|
+
* tables with more than 240 total states, but takes up twice as
|
157
|
+
* much space per state.
|
158
|
+
*
|
159
|
+
**/
|
160
|
+
|
161
|
+
// Return true if current Tbl pointer is within state0 range
|
162
|
+
// Note that unsigned compare checks both ends of range simultaneously
|
163
|
+
static inline bool InStateZero(const UTF8ScanObj* st, const uint8* Tbl) {
|
164
|
+
const uint8* Tbl0 = &st->state_table[st->state0];
|
165
|
+
return (static_cast<uint32>(Tbl - Tbl0) < st->state0_size);
|
166
|
+
}
|
167
|
+
|
168
|
+
static inline bool InStateZero_2(const UTF8ReplaceObj_2* st,
|
169
|
+
const unsigned short int* Tbl) {
|
170
|
+
const unsigned short int* Tbl0 = &st->state_table[st->state0];
|
171
|
+
// Word difference, not byte difference
|
172
|
+
return (static_cast<uint32>(Tbl - Tbl0) < st->state0_size);
|
173
|
+
}
|
174
|
+
|
175
|
+
// Look up property of one UTF-8 character and advance over it
|
176
|
+
// Return 0 if input length is zero
|
177
|
+
// Return 0 and advance one byte if input is ill-formed
|
178
|
+
uint8 UTF8GenericProperty(const UTF8PropObj* st,
|
179
|
+
const uint8** src,
|
180
|
+
int* srclen) {
|
181
|
+
if (*srclen <= 0) {
|
182
|
+
return 0;
|
183
|
+
}
|
184
|
+
|
185
|
+
const uint8* lsrc = *src;
|
186
|
+
const uint8* Tbl_0 = &st->state_table[st->state0];
|
187
|
+
const uint8* Tbl = Tbl_0;
|
188
|
+
int e;
|
189
|
+
int eshift = st->entry_shift;
|
190
|
+
|
191
|
+
// Short series of tests faster than switch, optimizes 7-bit ASCII
|
192
|
+
unsigned char c = lsrc[0];
|
193
|
+
if (static_cast<signed char>(c) >= 0) { // one byte
|
194
|
+
e = Tbl[c];
|
195
|
+
*src += 1;
|
196
|
+
*srclen -= 1;
|
197
|
+
} else if (((c & 0xe0) == 0xc0) && (*srclen >= 2)) { // two bytes
|
198
|
+
e = Tbl[c];
|
199
|
+
Tbl = &Tbl_0[e << eshift];
|
200
|
+
e = Tbl[lsrc[1]];
|
201
|
+
*src += 2;
|
202
|
+
*srclen -= 2;
|
203
|
+
} else if (((c & 0xf0) == 0xe0) && (*srclen >= 3)) { // three bytes
|
204
|
+
e = Tbl[c];
|
205
|
+
Tbl = &Tbl_0[e << eshift];
|
206
|
+
e = Tbl[lsrc[1]];
|
207
|
+
Tbl = &Tbl_0[e << eshift];
|
208
|
+
e = Tbl[lsrc[2]];
|
209
|
+
*src += 3;
|
210
|
+
*srclen -= 3;
|
211
|
+
}else if (((c & 0xf8) == 0xf0) && (*srclen >= 4)) { // four bytes
|
212
|
+
e = Tbl[c];
|
213
|
+
Tbl = &Tbl_0[e << eshift];
|
214
|
+
e = Tbl[lsrc[1]];
|
215
|
+
Tbl = &Tbl_0[e << eshift];
|
216
|
+
e = Tbl[lsrc[2]];
|
217
|
+
Tbl = &Tbl_0[e << eshift];
|
218
|
+
e = Tbl[lsrc[3]];
|
219
|
+
*src += 4;
|
220
|
+
*srclen -= 4;
|
221
|
+
} else { // Ill-formed
|
222
|
+
e = 0;
|
223
|
+
*src += 1;
|
224
|
+
*srclen -= 1;
|
225
|
+
}
|
226
|
+
return e;
|
227
|
+
}
|
228
|
+
|
229
|
+
bool UTF8HasGenericProperty(const UTF8PropObj& st, const char* src) {
|
230
|
+
const uint8* lsrc = reinterpret_cast<const uint8*>(src);
|
231
|
+
const uint8* Tbl_0 = &st.state_table[st.state0];
|
232
|
+
const uint8* Tbl = Tbl_0;
|
233
|
+
int e;
|
234
|
+
int eshift = st.entry_shift;
|
235
|
+
|
236
|
+
// Short series of tests faster than switch, optimizes 7-bit ASCII
|
237
|
+
unsigned char c = lsrc[0];
|
238
|
+
if (static_cast<signed char>(c) >= 0) { // one byte
|
239
|
+
e = Tbl[c];
|
240
|
+
} else if ((c & 0xe0) == 0xc0) { // two bytes
|
241
|
+
e = Tbl[c];
|
242
|
+
Tbl = &Tbl_0[e << eshift];
|
243
|
+
e = Tbl[lsrc[1]];
|
244
|
+
} else if ((c & 0xf0) == 0xe0) { // three bytes
|
245
|
+
e = Tbl[c];
|
246
|
+
Tbl = &Tbl_0[e << eshift];
|
247
|
+
e = Tbl[lsrc[1]];
|
248
|
+
Tbl = &Tbl_0[e << eshift];
|
249
|
+
e = Tbl[lsrc[2]];
|
250
|
+
} else { // four bytes
|
251
|
+
e = Tbl[c];
|
252
|
+
Tbl = &Tbl_0[e << eshift];
|
253
|
+
e = Tbl[lsrc[1]];
|
254
|
+
Tbl = &Tbl_0[e << eshift];
|
255
|
+
e = Tbl[lsrc[2]];
|
256
|
+
Tbl = &Tbl_0[e << eshift];
|
257
|
+
e = Tbl[lsrc[3]];
|
258
|
+
}
|
259
|
+
|
260
|
+
// Comparing against 0 to avoid a warning due to implicit conversion.
|
261
|
+
return (e != 0);
|
262
|
+
}
|
263
|
+
|
264
|
+
|
265
|
+
// BigOneByte versions are needed for tables > 240 states, but most
|
266
|
+
// won't need the TwoByte versions.
|
267
|
+
// Internally, to next-to-last offset is multiplied by 16 and the last
|
268
|
+
// offset is relative instead of absolute.
|
269
|
+
// Look up property of one UTF-8 character and advance over it
|
270
|
+
// Return 0 if input length is zero
|
271
|
+
// Return 0 and advance one byte if input is ill-formed
|
272
|
+
uint8 UTF8GenericPropertyBigOneByte(const UTF8PropObj* st,
|
273
|
+
const uint8** src,
|
274
|
+
int* srclen) {
|
275
|
+
if (*srclen <= 0) {
|
276
|
+
return 0;
|
277
|
+
}
|
278
|
+
|
279
|
+
const uint8* lsrc = *src;
|
280
|
+
const uint8* Tbl_0 = &st->state_table[st->state0];
|
281
|
+
const uint8* Tbl = Tbl_0;
|
282
|
+
int e;
|
283
|
+
int eshift = st->entry_shift;
|
284
|
+
|
285
|
+
// Short series of tests faster than switch, optimizes 7-bit ASCII
|
286
|
+
unsigned char c = lsrc[0];
|
287
|
+
if (static_cast<signed char>(c) >= 0) { // one byte
|
288
|
+
e = Tbl[c];
|
289
|
+
*src += 1;
|
290
|
+
*srclen -= 1;
|
291
|
+
} else if (((c & 0xe0) == 0xc0) && (*srclen >= 2)) { // two bytes
|
292
|
+
e = Tbl[c];
|
293
|
+
Tbl = &Tbl_0[e << eshift];
|
294
|
+
e = Tbl[lsrc[1]];
|
295
|
+
*src += 2;
|
296
|
+
*srclen -= 2;
|
297
|
+
} else if (((c & 0xf0) == 0xe0) && (*srclen >= 3)) { // three bytes
|
298
|
+
e = Tbl[c];
|
299
|
+
Tbl = &Tbl_0[e << (eshift + 4)]; // 16x the range
|
300
|
+
e = (reinterpret_cast<const int8*>(Tbl))[lsrc[1]];
|
301
|
+
Tbl = &Tbl[e << eshift]; // Relative +/-
|
302
|
+
e = Tbl[lsrc[2]];
|
303
|
+
*src += 3;
|
304
|
+
*srclen -= 3;
|
305
|
+
}else if (((c & 0xf8) == 0xf0) && (*srclen >= 4)) { // four bytes
|
306
|
+
e = Tbl[c];
|
307
|
+
Tbl = &Tbl_0[e << eshift];
|
308
|
+
e = Tbl[lsrc[1]];
|
309
|
+
Tbl = &Tbl_0[e << (eshift + 4)]; // 16x the range
|
310
|
+
e = (reinterpret_cast<const int8*>(Tbl))[lsrc[2]];
|
311
|
+
Tbl = &Tbl[e << eshift]; // Relative +/-
|
312
|
+
e = Tbl[lsrc[3]];
|
313
|
+
*src += 4;
|
314
|
+
*srclen -= 4;
|
315
|
+
} else { // Ill-formed
|
316
|
+
e = 0;
|
317
|
+
*src += 1;
|
318
|
+
*srclen -= 1;
|
319
|
+
}
|
320
|
+
return e;
|
321
|
+
}
|
322
|
+
|
323
|
+
// BigOneByte versions are needed for tables > 240 states, but most
|
324
|
+
// won't need the TwoByte versions.
|
325
|
+
bool UTF8HasGenericPropertyBigOneByte(const UTF8PropObj& st, const char* src) {
|
326
|
+
const uint8* lsrc = reinterpret_cast<const uint8*>(src);
|
327
|
+
const uint8* Tbl_0 = &st.state_table[st.state0];
|
328
|
+
const uint8* Tbl = Tbl_0;
|
329
|
+
int e;
|
330
|
+
int eshift = st.entry_shift;
|
331
|
+
|
332
|
+
// Short series of tests faster than switch, optimizes 7-bit ASCII
|
333
|
+
unsigned char c = lsrc[0];
|
334
|
+
if (static_cast<signed char>(c) >= 0) { // one byte
|
335
|
+
e = Tbl[c];
|
336
|
+
} else if ((c & 0xe0) == 0xc0) { // two bytes
|
337
|
+
e = Tbl[c];
|
338
|
+
Tbl = &Tbl_0[e << eshift];
|
339
|
+
e = Tbl[lsrc[1]];
|
340
|
+
} else if ((c & 0xf0) == 0xe0) { // three bytes
|
341
|
+
e = Tbl[c];
|
342
|
+
Tbl = &Tbl_0[e << (eshift + 4)]; // 16x the range
|
343
|
+
e = (reinterpret_cast<const int8*>(Tbl))[lsrc[1]];
|
344
|
+
Tbl = &Tbl[e << eshift]; // Relative +/-
|
345
|
+
e = Tbl[lsrc[2]];
|
346
|
+
} else { // four bytes
|
347
|
+
e = Tbl[c];
|
348
|
+
Tbl = &Tbl_0[e << eshift];
|
349
|
+
e = Tbl[lsrc[1]];
|
350
|
+
Tbl = &Tbl_0[e << (eshift + 4)]; // 16x the range
|
351
|
+
e = (reinterpret_cast<const int8*>(Tbl))[lsrc[2]];
|
352
|
+
Tbl = &Tbl[e << eshift]; // Relative +/-
|
353
|
+
e = Tbl[lsrc[3]];
|
354
|
+
}
|
355
|
+
|
356
|
+
// Comparing against 0 to avoid implicit conversion and a warning.
|
357
|
+
return (e != 0);
|
358
|
+
}
|
359
|
+
|
360
|
+
|
361
|
+
// TwoByte versions are needed for tables > 240 states
|
362
|
+
// Look up property of one UTF-8 character and advance over it
|
363
|
+
// Return 0 if input length is zero
|
364
|
+
// Return 0 and advance one byte if input is ill-formed
|
365
|
+
uint8 UTF8GenericPropertyTwoByte(const UTF8PropObj_2* st,
|
366
|
+
const uint8** src,
|
367
|
+
int* srclen) {
|
368
|
+
if (*srclen <= 0) {
|
369
|
+
return 0;
|
370
|
+
}
|
371
|
+
|
372
|
+
const uint8* lsrc = *src;
|
373
|
+
const unsigned short* Tbl_0 = &st->state_table[st->state0];
|
374
|
+
const unsigned short* Tbl = Tbl_0;
|
375
|
+
int e;
|
376
|
+
int eshift = st->entry_shift;
|
377
|
+
|
378
|
+
// Short series of tests faster than switch, optimizes 7-bit ASCII
|
379
|
+
unsigned char c = lsrc[0];
|
380
|
+
if (static_cast<signed char>(c) >= 0) { // one byte
|
381
|
+
e = Tbl[c];
|
382
|
+
*src += 1;
|
383
|
+
*srclen -= 1;
|
384
|
+
} else if (((c & 0xe0) == 0xc0) && (*srclen >= 2)) { // two bytes
|
385
|
+
e = Tbl[c];
|
386
|
+
Tbl = &Tbl_0[e << eshift];
|
387
|
+
e = Tbl[lsrc[1]];
|
388
|
+
*src += 2;
|
389
|
+
*srclen -= 2;
|
390
|
+
} else if (((c & 0xf0) == 0xe0) && (*srclen >= 3)) { // three bytes
|
391
|
+
e = Tbl[c];
|
392
|
+
Tbl = &Tbl_0[e << eshift];
|
393
|
+
e = Tbl[lsrc[1]];
|
394
|
+
Tbl = &Tbl_0[e << eshift];
|
395
|
+
e = Tbl[lsrc[2]];
|
396
|
+
*src += 3;
|
397
|
+
*srclen -= 3;
|
398
|
+
}else if (((c & 0xf8) == 0xf0) && (*srclen >= 4)) { // four bytes
|
399
|
+
e = Tbl[c];
|
400
|
+
Tbl = &Tbl_0[e << eshift];
|
401
|
+
e = Tbl[lsrc[1]];
|
402
|
+
Tbl = &Tbl_0[e << eshift];
|
403
|
+
e = Tbl[lsrc[2]];
|
404
|
+
Tbl = &Tbl_0[e << eshift];
|
405
|
+
e = Tbl[lsrc[3]];
|
406
|
+
*src += 4;
|
407
|
+
*srclen -= 4;
|
408
|
+
} else { // Ill-formed
|
409
|
+
e = 0;
|
410
|
+
*src += 1;
|
411
|
+
*srclen -= 1;
|
412
|
+
}
|
413
|
+
return e;
|
414
|
+
}
|
415
|
+
|
416
|
+
// TwoByte versions are needed for tables > 240 states
|
417
|
+
bool UTF8HasGenericPropertyTwoByte(const UTF8PropObj_2& st, const char* src) {
|
418
|
+
const uint8* lsrc = reinterpret_cast<const uint8*>(src);
|
419
|
+
const unsigned short* Tbl_0 = &st.state_table[st.state0];
|
420
|
+
const unsigned short* Tbl = Tbl_0;
|
421
|
+
int e;
|
422
|
+
int eshift = st.entry_shift;
|
423
|
+
|
424
|
+
// Short series of tests faster than switch, optimizes 7-bit ASCII
|
425
|
+
unsigned char c = lsrc[0];
|
426
|
+
if (static_cast<signed char>(c) >= 0) { // one byte
|
427
|
+
e = Tbl[c];
|
428
|
+
} else if ((c & 0xe0) == 0xc0) { // two bytes
|
429
|
+
e = Tbl[c];
|
430
|
+
Tbl = &Tbl_0[e << eshift];
|
431
|
+
e = Tbl[lsrc[1]];
|
432
|
+
} else if ((c & 0xf0) == 0xe0) { // three bytes
|
433
|
+
e = Tbl[c];
|
434
|
+
Tbl = &Tbl_0[e << eshift];
|
435
|
+
e = Tbl[lsrc[1]];
|
436
|
+
Tbl = &Tbl_0[e << eshift];
|
437
|
+
e = Tbl[lsrc[2]];
|
438
|
+
} else { // four bytes
|
439
|
+
e = Tbl[c];
|
440
|
+
Tbl = &Tbl_0[e << eshift];
|
441
|
+
e = Tbl[lsrc[1]];
|
442
|
+
Tbl = &Tbl_0[e << eshift];
|
443
|
+
e = Tbl[lsrc[2]];
|
444
|
+
Tbl = &Tbl_0[e << eshift];
|
445
|
+
e = Tbl[lsrc[3]];
|
446
|
+
}
|
447
|
+
|
448
|
+
// Comparing against 0 to avoid implicit conversion and a warning.
|
449
|
+
return (e != 0);
|
450
|
+
}
|
451
|
+
|
452
|
+
|
453
|
+
// Approximate speeds on 2.8 GHz Pentium 4:
|
454
|
+
// GenericScan 1-byte loop 300 MB/sec *
|
455
|
+
// GenericScan 4-byte loop 1200 MB/sec
|
456
|
+
// GenericScan 8-byte loop 2400 MB/sec *
|
457
|
+
// GenericScanFastAscii 4-byte loop 3000 MB/sec
|
458
|
+
// GenericScanFastAscii 8-byte loop 3200 MB/sec *
|
459
|
+
//
|
460
|
+
// * Implemented below. FastAscii loop is memory-bandwidth constrained.
|
461
|
+
|
462
|
+
// Scan a UTF-8 stringpiece based on state table.
|
463
|
+
// Always scan complete UTF-8 characters
|
464
|
+
// Set number of bytes scanned. Return reason for exiting
|
465
|
+
int UTF8GenericScan(const UTF8ScanObj* st,
|
466
|
+
const StringPiece& str,
|
467
|
+
int* bytes_consumed) {
|
468
|
+
int eshift = st->entry_shift; // 6 (space optimized) or 8
|
469
|
+
// int nEntries = (1 << eshift); // 64 or 256 entries per state
|
470
|
+
|
471
|
+
const uint8* isrc =
|
472
|
+
reinterpret_cast<const uint8*>(str.data());
|
473
|
+
const uint8* src = isrc;
|
474
|
+
const int len = str.length();
|
475
|
+
const uint8* srclimit = isrc + len;
|
476
|
+
const uint8* srclimit8 = srclimit - 7;
|
477
|
+
*bytes_consumed = 0;
|
478
|
+
if (len == 0) return kExitOK;
|
479
|
+
|
480
|
+
const uint8* Tbl_0 = &st->state_table[st->state0];
|
481
|
+
|
482
|
+
DoAgain:
|
483
|
+
// Do state-table scan
|
484
|
+
int e = 0;
|
485
|
+
uint8 c;
|
486
|
+
|
487
|
+
// Do fast for groups of 8 identity bytes.
|
488
|
+
// This covers a lot of 7-bit ASCII ~8x faster than the 1-byte loop,
|
489
|
+
// including slowing slightly on cr/lf/ht
|
490
|
+
//----------------------------
|
491
|
+
const uint8* Tbl2 = &st->fast_state[0];
|
492
|
+
uint32 losub = st->losub;
|
493
|
+
uint32 hiadd = st->hiadd;
|
494
|
+
while (src < srclimit8) {
|
495
|
+
const uint32* src32 = reinterpret_cast<const uint32 *>(src);
|
496
|
+
uint32 s0123 = UNALIGNED_LOAD32(&src32[0]);
|
497
|
+
uint32 s4567 = UNALIGNED_LOAD32(&src32[1]);
|
498
|
+
src += 8;
|
499
|
+
// This is a fast range check for all bytes in [lowsub..0x80-hiadd)
|
500
|
+
uint32 temp = (s0123 - losub) | (s0123 + hiadd) |
|
501
|
+
(s4567 - losub) | (s4567 + hiadd);
|
502
|
+
if ((temp & 0x80808080) != 0) {
|
503
|
+
// We typically end up here on cr/lf/ht; src was incremented
|
504
|
+
int e0123 = (Tbl2[src[-8]] | Tbl2[src[-7]]) |
|
505
|
+
(Tbl2[src[-6]] | Tbl2[src[-5]]);
|
506
|
+
if (e0123 != 0) {src -= 8; break;} // Exit on Non-interchange
|
507
|
+
e0123 = (Tbl2[src[-4]] | Tbl2[src[-3]]) |
|
508
|
+
(Tbl2[src[-2]] | Tbl2[src[-1]]);
|
509
|
+
if (e0123 != 0) {src -= 4; break;} // Exit on Non-interchange
|
510
|
+
// Else OK, go around again
|
511
|
+
}
|
512
|
+
}
|
513
|
+
//----------------------------
|
514
|
+
|
515
|
+
// Byte-at-a-time scan
|
516
|
+
//----------------------------
|
517
|
+
const uint8* Tbl = Tbl_0;
|
518
|
+
while (src < srclimit) {
|
519
|
+
c = *src;
|
520
|
+
e = Tbl[c];
|
521
|
+
src++;
|
522
|
+
if (e >= kExitIllegalStructure) {break;}
|
523
|
+
Tbl = &Tbl_0[e << eshift];
|
524
|
+
}
|
525
|
+
//----------------------------
|
526
|
+
|
527
|
+
|
528
|
+
// Exit possibilities:
|
529
|
+
// Some exit code, !state0, back up over last char
|
530
|
+
// Some exit code, state0, back up one byte exactly
|
531
|
+
// source consumed, !state0, back up over partial char
|
532
|
+
// source consumed, state0, exit OK
|
533
|
+
// For illegal byte in state0, avoid backup up over PREVIOUS char
|
534
|
+
// For truncated last char, back up to beginning of it
|
535
|
+
|
536
|
+
if (e >= kExitIllegalStructure) {
|
537
|
+
// Back up over exactly one byte of rejected/illegal UTF-8 character
|
538
|
+
src--;
|
539
|
+
// Back up more if needed
|
540
|
+
if (!InStateZero(st, Tbl)) {
|
541
|
+
do {src--;} while ((src > isrc) && ((src[0] & 0xc0) == 0x80));
|
542
|
+
}
|
543
|
+
} else if (!InStateZero(st, Tbl)) {
|
544
|
+
// Back up over truncated UTF-8 character
|
545
|
+
e = kExitIllegalStructure;
|
546
|
+
do {src--;} while ((src > isrc) && ((src[0] & 0xc0) == 0x80));
|
547
|
+
} else {
|
548
|
+
// Normal termination, source fully consumed
|
549
|
+
e = kExitOK;
|
550
|
+
}
|
551
|
+
|
552
|
+
if (e == kExitDoAgain) {
|
553
|
+
// Loop back up to the fast scan
|
554
|
+
goto DoAgain;
|
555
|
+
}
|
556
|
+
|
557
|
+
*bytes_consumed = src - isrc;
|
558
|
+
return e;
|
559
|
+
}
|
560
|
+
|
561
|
+
// Scan a UTF-8 stringpiece based on state table.
|
562
|
+
// Always scan complete UTF-8 characters
|
563
|
+
// Set number of bytes scanned. Return reason for exiting
|
564
|
+
// OPTIMIZED for case of 7-bit ASCII 0000..007f all valid
|
565
|
+
int UTF8GenericScanFastAscii(const UTF8ScanObj* st,
|
566
|
+
const StringPiece& str,
|
567
|
+
int* bytes_consumed) {
|
568
|
+
const uint8* isrc =
|
569
|
+
reinterpret_cast<const uint8*>(str.data());
|
570
|
+
const uint8* src = isrc;
|
571
|
+
const int len = str.length();
|
572
|
+
const uint8* srclimit = isrc + len;
|
573
|
+
const uint8* srclimit8 = srclimit - 7;
|
574
|
+
*bytes_consumed = 0;
|
575
|
+
if (len == 0) return kExitOK;
|
576
|
+
|
577
|
+
int n;
|
578
|
+
int rest_consumed;
|
579
|
+
int exit_reason;
|
580
|
+
do {
|
581
|
+
// Skip 8 bytes of ASCII at a whack; no endianness issue
|
582
|
+
while ((src < srclimit8) &&
|
583
|
+
(((UNALIGNED_LOAD32(&reinterpret_cast<const uint32*>(src)[0]) |
|
584
|
+
UNALIGNED_LOAD32(&reinterpret_cast<const uint32*>(src)[1]))
|
585
|
+
& 0x80808080) == 0)) {
|
586
|
+
src += 8;
|
587
|
+
}
|
588
|
+
// Run state table on the rest
|
589
|
+
n = src - isrc;
|
590
|
+
StringPiece str2(str.data() + n, str.length() - n);
|
591
|
+
exit_reason = UTF8GenericScan(st, str2, &rest_consumed);
|
592
|
+
src += rest_consumed;
|
593
|
+
} while ( exit_reason == kExitDoAgain );
|
594
|
+
|
595
|
+
*bytes_consumed = src - isrc;
|
596
|
+
return exit_reason;
|
597
|
+
}
|
598
|
+
|
599
|
+
// Hack to change halfwidth katakana to match an old UTF8CharToLower()
|
600
|
+
|
601
|
+
// Return number of src bytes skipped
|
602
|
+
static int DoSpecialFixup(const unsigned char c,
|
603
|
+
const unsigned char** srcp, const unsigned char* srclimit,
|
604
|
+
unsigned char** dstp, unsigned char* dstlimit) {
|
605
|
+
return 0;
|
606
|
+
}
|
607
|
+
|
608
|
+
|
609
|
+
// Scan a UTF-8 stringpiece based on state table, copying to output stringpiece
|
610
|
+
// and doing text replacements.
|
611
|
+
// DO NOT CALL DIRECTLY. Use UTF8GenericReplace() below
|
612
|
+
// Needs caller to loop on kExitDoAgain
|
613
|
+
static int UTF8GenericReplaceInternal(const UTF8ReplaceObj* st,
|
614
|
+
const StringPiece& istr,
|
615
|
+
StringPiece& ostr,
|
616
|
+
bool is_plain_text,
|
617
|
+
int* bytes_consumed,
|
618
|
+
int* bytes_filled,
|
619
|
+
int* chars_changed,
|
620
|
+
OffsetMap* offsetmap) {
|
621
|
+
int eshift = st->entry_shift;
|
622
|
+
int nEntries = (1 << eshift); // 64 or 256 entries per state
|
623
|
+
const uint8* isrc = reinterpret_cast<const uint8*>(istr.data());
|
624
|
+
const int ilen = istr.length();
|
625
|
+
const uint8* copystart = isrc;
|
626
|
+
const uint8* src = isrc;
|
627
|
+
const uint8* srclimit = src + ilen;
|
628
|
+
*bytes_consumed = 0;
|
629
|
+
*bytes_filled = 0;
|
630
|
+
*chars_changed = 0;
|
631
|
+
|
632
|
+
const uint8* odst = reinterpret_cast<const uint8*>(ostr.data());
|
633
|
+
const int olen = ostr.length();
|
634
|
+
uint8* dst = const_cast<uint8*>(odst);
|
635
|
+
uint8* dstlimit = dst + olen;
|
636
|
+
|
637
|
+
int total_changed = 0;
|
638
|
+
|
639
|
+
// Invariant condition during replacements:
|
640
|
+
// remaining dst size >= remaining src size
|
641
|
+
if ((dstlimit - dst) < (srclimit - src)) {
|
642
|
+
if (offsetmap != NULL) {
|
643
|
+
offsetmap->Copy(src - copystart);
|
644
|
+
copystart = src;
|
645
|
+
}
|
646
|
+
return kExitDstSpaceFull;
|
647
|
+
}
|
648
|
+
const uint8* Tbl_0 = &st->state_table[st->state0];
|
649
|
+
|
650
|
+
Do_state_table:
|
651
|
+
// Do state-table scan, copying as we go
|
652
|
+
const uint8* Tbl = Tbl_0;
|
653
|
+
int e = 0;
|
654
|
+
uint8 c = 0;
|
655
|
+
|
656
|
+
Do_state_table_newe:
|
657
|
+
|
658
|
+
//----------------------------
|
659
|
+
while (src < srclimit) {
|
660
|
+
c = *src;
|
661
|
+
e = Tbl[c];
|
662
|
+
*dst = c;
|
663
|
+
src++;
|
664
|
+
dst++;
|
665
|
+
if (e >= kExitIllegalStructure) {break;}
|
666
|
+
Tbl = &Tbl_0[e << eshift];
|
667
|
+
}
|
668
|
+
//----------------------------
|
669
|
+
|
670
|
+
// Exit possibilities:
|
671
|
+
// Replacement code, do the replacement and loop
|
672
|
+
// Some other exit code, state0, back up one byte exactly
|
673
|
+
// Some other exit code, !state0, back up over last char
|
674
|
+
// source consumed, state0, exit OK
|
675
|
+
// source consumed, !state0, back up over partial char
|
676
|
+
// For illegal byte in state0, avoid backup up over PREVIOUS char
|
677
|
+
// For truncated last char, back up to beginning of it
|
678
|
+
|
679
|
+
if (e >= kExitIllegalStructure) {
|
680
|
+
// Switch on exit code; most loop back to top
|
681
|
+
int offset = 0;
|
682
|
+
switch (e) {
|
683
|
+
// These all make the output string the same size or shorter
|
684
|
+
// No checking needed
|
685
|
+
case kExitReplace31: // del 2, add 1 bytes to change
|
686
|
+
dst -= 2;
|
687
|
+
if (offsetmap != NULL) {
|
688
|
+
offsetmap->Copy(src - copystart - 2);
|
689
|
+
offsetmap->Delete(2);
|
690
|
+
copystart = src;
|
691
|
+
}
|
692
|
+
dst[-1] = (unsigned char)Tbl[c + (nEntries * 1)];
|
693
|
+
total_changed++;
|
694
|
+
goto Do_state_table;
|
695
|
+
case kExitReplace32: // del 3, add 2 bytes to change
|
696
|
+
dst--;
|
697
|
+
if (offsetmap != NULL) {
|
698
|
+
offsetmap->Copy(src - copystart - 1);
|
699
|
+
offsetmap->Delete(1);
|
700
|
+
copystart = src;
|
701
|
+
}
|
702
|
+
dst[-2] = (unsigned char)Tbl[c + (nEntries * 2)];
|
703
|
+
dst[-1] = (unsigned char)Tbl[c + (nEntries * 1)];
|
704
|
+
total_changed++;
|
705
|
+
goto Do_state_table;
|
706
|
+
case kExitReplace21: // del 2, add 1 bytes to change
|
707
|
+
dst--;
|
708
|
+
if (offsetmap != NULL) {
|
709
|
+
offsetmap->Copy(src - copystart - 1);
|
710
|
+
offsetmap->Delete(1);
|
711
|
+
copystart = src;
|
712
|
+
}
|
713
|
+
dst[-1] = (unsigned char)Tbl[c + (nEntries * 1)];
|
714
|
+
total_changed++;
|
715
|
+
goto Do_state_table;
|
716
|
+
case kExitReplace3: // update 3 bytes to change
|
717
|
+
dst[-3] = (unsigned char)Tbl[c + (nEntries * 3)];
|
718
|
+
// Fall into next case
|
719
|
+
case kExitReplace2: // update 2 bytes to change
|
720
|
+
dst[-2] = (unsigned char)Tbl[c + (nEntries * 2)];
|
721
|
+
// Fall into next case
|
722
|
+
case kExitReplace1: // update 1 byte to change
|
723
|
+
dst[-1] = (unsigned char)Tbl[c + (nEntries * 1)];
|
724
|
+
total_changed++;
|
725
|
+
goto Do_state_table;
|
726
|
+
case kExitReplace1S0: // update 1 byte to change, 256-entry state
|
727
|
+
dst[-1] = (unsigned char)Tbl[c + (256 * 1)];
|
728
|
+
total_changed++;
|
729
|
+
goto Do_state_table;
|
730
|
+
// These can make the output string longer than the input
|
731
|
+
case kExitReplaceOffset2:
|
732
|
+
if ((nEntries != 256) && InStateZero(st, Tbl)) {
|
733
|
+
// For space-optimized table, we need multiples of 256 bytes
|
734
|
+
// in state0 and multiples of nEntries in other states
|
735
|
+
offset += ((unsigned char)Tbl[c + (256 * 2)] << 8);
|
736
|
+
} else {
|
737
|
+
offset += ((unsigned char)Tbl[c + (nEntries * 2)] << 8);
|
738
|
+
}
|
739
|
+
// Fall into next case
|
740
|
+
case kExitSpecial: // Apply special fixups [read: hacks]
|
741
|
+
case kExitReplaceOffset1:
|
742
|
+
if ((nEntries != 256) && InStateZero(st, Tbl)) {
|
743
|
+
// For space-optimized table, we need multiples of 256 bytes
|
744
|
+
// in state0 and multiples of nEntries in other states
|
745
|
+
offset += (unsigned char)Tbl[c + (256 * 1)];
|
746
|
+
} else {
|
747
|
+
offset += (unsigned char)Tbl[c + (nEntries * 1)];
|
748
|
+
}
|
749
|
+
{
|
750
|
+
const RemapEntry* re = &st->remap_base[offset];
|
751
|
+
int del_len = re->delete_bytes & ~kReplaceAndResumeFlag;
|
752
|
+
int add_len = re->add_bytes & ~kHtmlPlaintextFlag;
|
753
|
+
|
754
|
+
// Special-case non-HTML replacement of five sensitive entities
|
755
|
+
// " & ' < >
|
756
|
+
// 0022 0026 0027 003c 003e
|
757
|
+
// A replacement creating one of these is expressed as a pair of
|
758
|
+
// entries, one for HTML output and one for plaintext output.
|
759
|
+
// The first of the pair has the high bit of add_bytes set.
|
760
|
+
if (re->add_bytes & kHtmlPlaintextFlag) {
|
761
|
+
// Use this entry for plain text
|
762
|
+
if (!is_plain_text) {
|
763
|
+
// Use very next entry for HTML text (same back/delete length)
|
764
|
+
re = &st->remap_base[offset + 1];
|
765
|
+
add_len = re->add_bytes & ~kHtmlPlaintextFlag;
|
766
|
+
}
|
767
|
+
}
|
768
|
+
|
769
|
+
int string_offset = re->bytes_offset;
|
770
|
+
// After the replacement, need (dstlimit - newdst) >= (srclimit - src)
|
771
|
+
uint8* newdst = dst - del_len + add_len;
|
772
|
+
if ((dstlimit - newdst) < (srclimit - src)) {
|
773
|
+
// Won't fit; don't do the replacement. Caller may realloc and retry
|
774
|
+
e = kExitDstSpaceFull;
|
775
|
+
break; // exit, backing up over this char for later retry
|
776
|
+
}
|
777
|
+
dst -= del_len;
|
778
|
+
memcpy(dst, &st->remap_string[string_offset], add_len);
|
779
|
+
dst += add_len;
|
780
|
+
total_changed++;
|
781
|
+
if (offsetmap != NULL) {
|
782
|
+
if (add_len > del_len) {
|
783
|
+
offsetmap->Copy(src - copystart);
|
784
|
+
offsetmap->Insert(add_len - del_len);
|
785
|
+
copystart = src;
|
786
|
+
} else if (add_len < del_len) {
|
787
|
+
offsetmap->Copy(src - copystart + add_len - del_len);
|
788
|
+
offsetmap->Delete(del_len - add_len);
|
789
|
+
copystart = src;
|
790
|
+
}
|
791
|
+
}
|
792
|
+
if (re->delete_bytes & kReplaceAndResumeFlag) {
|
793
|
+
// There is a non-zero target state at the end of the
|
794
|
+
// replacement string
|
795
|
+
e = st->remap_string[string_offset + add_len];
|
796
|
+
Tbl = &Tbl_0[e << eshift];
|
797
|
+
goto Do_state_table_newe;
|
798
|
+
}
|
799
|
+
}
|
800
|
+
if (e == kExitRejectAlt) {break;}
|
801
|
+
if (e != kExitSpecial) {goto Do_state_table;}
|
802
|
+
|
803
|
+
// case kExitSpecial: // Apply special fixups [read: hacks]
|
804
|
+
// In this routine, do either UTF8CharToLower()
|
805
|
+
// fullwidth/halfwidth mapping or
|
806
|
+
// voiced mapping or
|
807
|
+
// semi-voiced mapping
|
808
|
+
|
809
|
+
// First, do EXIT_REPLACE_OFFSET1 action (above)
|
810
|
+
// Second: do additional code fixup
|
811
|
+
{
|
812
|
+
int srcdel = DoSpecialFixup(c, &src, srclimit, &dst, dstlimit);
|
813
|
+
if (offsetmap != NULL) {
|
814
|
+
if (srcdel != 0) {
|
815
|
+
offsetmap->Copy(src - copystart - srcdel);
|
816
|
+
offsetmap->Delete(srcdel);
|
817
|
+
copystart = src;
|
818
|
+
}
|
819
|
+
}
|
820
|
+
}
|
821
|
+
goto Do_state_table;
|
822
|
+
|
823
|
+
case kExitIllegalStructure: // structurally illegal byte; quit
|
824
|
+
case kExitReject: // NUL or illegal code encountered; quit
|
825
|
+
case kExitRejectAlt: // Apply replacement, then exit
|
826
|
+
default: // and all other exits
|
827
|
+
break;
|
828
|
+
} // End switch (e)
|
829
|
+
|
830
|
+
// Exit possibilities:
|
831
|
+
// Some other exit code, state0, back up one byte exactly
|
832
|
+
// Some other exit code, !state0, back up over last char
|
833
|
+
|
834
|
+
// Back up over exactly one byte of rejected/illegal UTF-8 character
|
835
|
+
src--;
|
836
|
+
dst--;
|
837
|
+
// Back up more if needed
|
838
|
+
if (!InStateZero(st, Tbl)) {
|
839
|
+
do {src--;dst--;} while ((src > isrc) && ((src[0] & 0xc0) == 0x80));
|
840
|
+
}
|
841
|
+
} else if (!InStateZero(st, Tbl)) {
|
842
|
+
// src >= srclimit, !state0
|
843
|
+
// Back up over truncated UTF-8 character
|
844
|
+
e = kExitIllegalStructure;
|
845
|
+
do {src--; dst--;} while ((src > isrc) && ((src[0] & 0xc0) == 0x80));
|
846
|
+
} else {
|
847
|
+
// src >= srclimit, state0
|
848
|
+
// Normal termination, source fully consumed
|
849
|
+
e = kExitOK;
|
850
|
+
}
|
851
|
+
|
852
|
+
if (offsetmap != NULL) {
|
853
|
+
if (src > copystart) {
|
854
|
+
offsetmap->Copy(src - copystart);
|
855
|
+
copystart = src;
|
856
|
+
}
|
857
|
+
}
|
858
|
+
|
859
|
+
// Possible return values here:
|
860
|
+
// kExitDstSpaceFull caller may realloc and retry from middle
|
861
|
+
// kExitIllegalStructure caller my overwrite/truncate
|
862
|
+
// kExitOK all done and happy
|
863
|
+
// kExitReject caller may overwrite/truncate
|
864
|
+
// kExitDoAgain LOOP NOT DONE; caller must retry from middle
|
865
|
+
// (may do fast ASCII loop first)
|
866
|
+
// kExitPlaceholder -unused-
|
867
|
+
// kExitNone -unused-
|
868
|
+
*bytes_consumed = src - isrc;
|
869
|
+
*bytes_filled = dst - odst;
|
870
|
+
*chars_changed = total_changed;
|
871
|
+
return e;
|
872
|
+
}
|
873
|
+
|
874
|
+
// TwoByte versions are needed for tables > 240 states, such
|
875
|
+
// as the table for full Unicode 4.1 canonical + compatibility mapping
|
876
|
+
|
877
|
+
// Scan a UTF-8 stringpiece based on state table with two-byte entries,
|
878
|
+
// copying to output stringpiece
|
879
|
+
// and doing text replacements.
|
880
|
+
// DO NOT CALL DIRECTLY. Use UTF8GenericReplace() below
|
881
|
+
// Needs caller to loop on kExitDoAgain
|
882
|
+
static int UTF8GenericReplaceInternalTwoByte(const UTF8ReplaceObj_2* st,
|
883
|
+
const StringPiece& istr,
|
884
|
+
StringPiece& ostr,
|
885
|
+
bool is_plain_text,
|
886
|
+
int* bytes_consumed,
|
887
|
+
int* bytes_filled,
|
888
|
+
int* chars_changed,
|
889
|
+
OffsetMap* offsetmap) {
|
890
|
+
int eshift = st->entry_shift;
|
891
|
+
int nEntries = (1 << eshift); // 64 or 256 entries per state
|
892
|
+
const uint8* isrc = reinterpret_cast<const uint8*>(istr.data());
|
893
|
+
const int ilen = istr.length();
|
894
|
+
const uint8* copystart = isrc;
|
895
|
+
const uint8* src = isrc;
|
896
|
+
const uint8* srclimit = src + ilen;
|
897
|
+
*bytes_consumed = 0;
|
898
|
+
*bytes_filled = 0;
|
899
|
+
*chars_changed = 0;
|
900
|
+
|
901
|
+
const uint8* odst = reinterpret_cast<const uint8*>(ostr.data());
|
902
|
+
const int olen = ostr.length();
|
903
|
+
uint8* dst = const_cast<uint8*>(odst);
|
904
|
+
uint8* dstlimit = dst + olen;
|
905
|
+
|
906
|
+
*chars_changed = 0;
|
907
|
+
|
908
|
+
int total_changed = 0;
|
909
|
+
|
910
|
+
// Invariant condition during replacements:
|
911
|
+
// remaining dst size >= remaining src size
|
912
|
+
if ((dstlimit - dst) < (srclimit - src)) {
|
913
|
+
if (offsetmap != NULL) {
|
914
|
+
offsetmap->Copy(src - copystart);
|
915
|
+
copystart = src;
|
916
|
+
}
|
917
|
+
return kExitDstSpaceFull_2;
|
918
|
+
}
|
919
|
+
const unsigned short* Tbl_0 = &st->state_table[st->state0];
|
920
|
+
|
921
|
+
Do_state_table_2:
|
922
|
+
// Do state-table scan, copying as we go
|
923
|
+
const unsigned short* Tbl = Tbl_0;
|
924
|
+
int e = 0;
|
925
|
+
uint8 c = 0;
|
926
|
+
|
927
|
+
Do_state_table_newe_2:
|
928
|
+
|
929
|
+
//----------------------------
|
930
|
+
while (src < srclimit) {
|
931
|
+
c = *src;
|
932
|
+
e = Tbl[c];
|
933
|
+
*dst = c;
|
934
|
+
src++;
|
935
|
+
dst++;
|
936
|
+
if (e >= kExitIllegalStructure_2) {break;}
|
937
|
+
Tbl = &Tbl_0[e << eshift];
|
938
|
+
}
|
939
|
+
//----------------------------
|
940
|
+
|
941
|
+
// Exit possibilities:
|
942
|
+
// Replacement code, do the replacement and loop
|
943
|
+
// Some other exit code, state0, back up one byte exactly
|
944
|
+
// Some other exit code, !state0, back up over last char
|
945
|
+
// source consumed, state0, exit OK
|
946
|
+
// source consumed, !state0, back up over partial char
|
947
|
+
// For illegal byte in state0, avoid backup up over PREVIOUS char
|
948
|
+
// For truncated last char, back up to beginning of it
|
949
|
+
|
950
|
+
if (e >= kExitIllegalStructure_2) {
|
951
|
+
// Switch on exit code; most loop back to top
|
952
|
+
int offset = 0;
|
953
|
+
switch (e) {
|
954
|
+
// These all make the output string the same size or shorter
|
955
|
+
// No checking needed
|
956
|
+
case kExitReplace31_2: // del 2, add 1 bytes to change
|
957
|
+
dst -= 2;
|
958
|
+
if (offsetmap != NULL) {
|
959
|
+
offsetmap->Copy(src - copystart - 2);
|
960
|
+
offsetmap->Delete(2);
|
961
|
+
copystart = src;
|
962
|
+
}
|
963
|
+
dst[-1] = (unsigned char)(Tbl[c + (nEntries * 1)] & 0xff);
|
964
|
+
total_changed++;
|
965
|
+
goto Do_state_table_2;
|
966
|
+
case kExitReplace32_2: // del 3, add 2 bytes to change
|
967
|
+
dst--;
|
968
|
+
if (offsetmap != NULL) {
|
969
|
+
offsetmap->Copy(src - copystart - 1);
|
970
|
+
offsetmap->Delete(1);
|
971
|
+
copystart = src;
|
972
|
+
}
|
973
|
+
dst[-2] = (unsigned char)(Tbl[c + (nEntries * 1)] >> 8 & 0xff);
|
974
|
+
dst[-1] = (unsigned char)(Tbl[c + (nEntries * 1)] & 0xff);
|
975
|
+
total_changed++;
|
976
|
+
goto Do_state_table_2;
|
977
|
+
case kExitReplace21_2: // del 2, add 1 bytes to change
|
978
|
+
dst--;
|
979
|
+
if (offsetmap != NULL) {
|
980
|
+
offsetmap->Copy(src - copystart - 1);
|
981
|
+
offsetmap->Delete(1);
|
982
|
+
copystart = src;
|
983
|
+
}
|
984
|
+
dst[-1] = (unsigned char)(Tbl[c + (nEntries * 1)] & 0xff);
|
985
|
+
total_changed++;
|
986
|
+
goto Do_state_table_2;
|
987
|
+
case kExitReplace3_2: // update 3 bytes to change
|
988
|
+
dst[-3] = (unsigned char)(Tbl[c + (nEntries * 2)] & 0xff);
|
989
|
+
// Fall into next case
|
990
|
+
case kExitReplace2_2: // update 2 bytes to change
|
991
|
+
dst[-2] = (unsigned char)(Tbl[c + (nEntries * 1)] >> 8 & 0xff);
|
992
|
+
// Fall into next case
|
993
|
+
case kExitReplace1_2: // update 1 byte to change
|
994
|
+
dst[-1] = (unsigned char)(Tbl[c + (nEntries * 1)] & 0xff);
|
995
|
+
total_changed++;
|
996
|
+
goto Do_state_table_2;
|
997
|
+
case kExitReplace1S0_2: // update 1 byte to change, 256-entry state
|
998
|
+
dst[-1] = (unsigned char)(Tbl[c + (256 * 1)] & 0xff);
|
999
|
+
total_changed++;
|
1000
|
+
goto Do_state_table_2;
|
1001
|
+
// These can make the output string longer than the input
|
1002
|
+
case kExitReplaceOffset2_2:
|
1003
|
+
if ((nEntries != 256) && InStateZero_2(st, Tbl)) {
|
1004
|
+
// For space-optimized table, we need multiples of 256 bytes
|
1005
|
+
// in state0 and multiples of nEntries in other states
|
1006
|
+
offset += ((unsigned char)(Tbl[c + (256 * 1)] >> 8 & 0xff) << 8);
|
1007
|
+
} else {
|
1008
|
+
offset += ((unsigned char)(Tbl[c + (nEntries * 1)] >> 8 & 0xff) << 8);
|
1009
|
+
}
|
1010
|
+
// Fall into next case
|
1011
|
+
case kExitReplaceOffset1_2:
|
1012
|
+
if ((nEntries != 256) && InStateZero_2(st, Tbl)) {
|
1013
|
+
// For space-optimized table, we need multiples of 256 bytes
|
1014
|
+
// in state0 and multiples of nEntries in other states
|
1015
|
+
offset += (unsigned char)(Tbl[c + (256 * 1)] & 0xff);
|
1016
|
+
} else {
|
1017
|
+
offset += (unsigned char)(Tbl[c + (nEntries * 1)] & 0xff);
|
1018
|
+
}
|
1019
|
+
{
|
1020
|
+
const RemapEntry* re = &st->remap_base[offset];
|
1021
|
+
int del_len = re->delete_bytes & ~kReplaceAndResumeFlag;
|
1022
|
+
int add_len = re->add_bytes & ~kHtmlPlaintextFlag;
|
1023
|
+
// Special-case non-HTML replacement of five sensitive entities
|
1024
|
+
// " & ' < >
|
1025
|
+
// 0022 0026 0027 003c 003e
|
1026
|
+
// A replacement creating one of these is expressed as a pair of
|
1027
|
+
// entries, one for HTML output and one for plaintext output.
|
1028
|
+
// The first of the pair has the high bit of add_bytes set.
|
1029
|
+
if (re->add_bytes & kHtmlPlaintextFlag) {
|
1030
|
+
// Use this entry for plain text
|
1031
|
+
if (!is_plain_text) {
|
1032
|
+
// Use very next entry for HTML text (same back/delete length)
|
1033
|
+
re = &st->remap_base[offset + 1];
|
1034
|
+
add_len = re->add_bytes & ~kHtmlPlaintextFlag;
|
1035
|
+
}
|
1036
|
+
}
|
1037
|
+
|
1038
|
+
// After the replacement, need (dstlimit - dst) >= (srclimit - src)
|
1039
|
+
int string_offset = re->bytes_offset;
|
1040
|
+
// After the replacement, need (dstlimit - newdst) >= (srclimit - src)
|
1041
|
+
uint8* newdst = dst - del_len + add_len;
|
1042
|
+
if ((dstlimit - newdst) < (srclimit - src)) {
|
1043
|
+
// Won't fit; don't do the replacement. Caller may realloc and retry
|
1044
|
+
e = kExitDstSpaceFull_2;
|
1045
|
+
break; // exit, backing up over this char for later retry
|
1046
|
+
}
|
1047
|
+
dst -= del_len;
|
1048
|
+
memcpy(dst, &st->remap_string[string_offset], add_len);
|
1049
|
+
dst += add_len;
|
1050
|
+
if (offsetmap != NULL) {
|
1051
|
+
if (add_len > del_len) {
|
1052
|
+
offsetmap->Copy(src - copystart);
|
1053
|
+
offsetmap->Insert(add_len - del_len);
|
1054
|
+
copystart = src;
|
1055
|
+
} else if (add_len < del_len) {
|
1056
|
+
offsetmap->Copy(src - copystart + add_len - del_len);
|
1057
|
+
offsetmap->Delete(del_len - add_len);
|
1058
|
+
copystart = src;
|
1059
|
+
}
|
1060
|
+
}
|
1061
|
+
if (re->delete_bytes & kReplaceAndResumeFlag) {
|
1062
|
+
// There is a two-byte non-zero target state at the end of the
|
1063
|
+
// replacement string
|
1064
|
+
uint8 c1 = st->remap_string[string_offset + add_len];
|
1065
|
+
uint8 c2 = st->remap_string[string_offset + add_len + 1];
|
1066
|
+
e = (c1 << 8) | c2;
|
1067
|
+
Tbl = &Tbl_0[e << eshift];
|
1068
|
+
total_changed++;
|
1069
|
+
goto Do_state_table_newe_2;
|
1070
|
+
}
|
1071
|
+
}
|
1072
|
+
total_changed++;
|
1073
|
+
if (e == kExitRejectAlt_2) {break;}
|
1074
|
+
goto Do_state_table_2;
|
1075
|
+
|
1076
|
+
case kExitSpecial_2: // NO special fixups [read: hacks]
|
1077
|
+
case kExitIllegalStructure_2: // structurally illegal byte; quit
|
1078
|
+
case kExitReject_2: // NUL or illegal code encountered; quit
|
1079
|
+
// and all other exits
|
1080
|
+
default:
|
1081
|
+
break;
|
1082
|
+
} // End switch (e)
|
1083
|
+
|
1084
|
+
// Exit possibilities:
|
1085
|
+
// Some other exit code, state0, back up one byte exactly
|
1086
|
+
// Some other exit code, !state0, back up over last char
|
1087
|
+
|
1088
|
+
// Back up over exactly one byte of rejected/illegal UTF-8 character
|
1089
|
+
src--;
|
1090
|
+
dst--;
|
1091
|
+
// Back up more if needed
|
1092
|
+
if (!InStateZero_2(st, Tbl)) {
|
1093
|
+
do {src--;dst--;} while ((src > isrc) && ((src[0] & 0xc0) == 0x80));
|
1094
|
+
}
|
1095
|
+
} else if (!InStateZero_2(st, Tbl)) {
|
1096
|
+
// src >= srclimit, !state0
|
1097
|
+
// Back up over truncated UTF-8 character
|
1098
|
+
e = kExitIllegalStructure_2;
|
1099
|
+
|
1100
|
+
do {src--; dst--;} while ((src > isrc) && ((src[0] & 0xc0) == 0x80));
|
1101
|
+
} else {
|
1102
|
+
// src >= srclimit, state0
|
1103
|
+
// Normal termination, source fully consumed
|
1104
|
+
e = kExitOK_2;
|
1105
|
+
}
|
1106
|
+
|
1107
|
+
if (offsetmap != NULL) {
|
1108
|
+
if (src > copystart) {
|
1109
|
+
offsetmap->Copy(src - copystart);
|
1110
|
+
copystart = src;
|
1111
|
+
}
|
1112
|
+
}
|
1113
|
+
|
1114
|
+
|
1115
|
+
// Possible return values here:
|
1116
|
+
// kExitDstSpaceFull_2 caller may realloc and retry from middle
|
1117
|
+
// kExitIllegalStructure_2 caller my overwrite/truncate
|
1118
|
+
// kExitOK_2 all done and happy
|
1119
|
+
// kExitReject_2 caller may overwrite/truncate
|
1120
|
+
// kExitDoAgain_2 LOOP NOT DONE; caller must retry from middle
|
1121
|
+
// (may do fast ASCII loop first)
|
1122
|
+
// kExitPlaceholder_2 -unused-
|
1123
|
+
// kExitNone_2 -unused-
|
1124
|
+
*bytes_consumed = src - isrc;
|
1125
|
+
*bytes_filled = dst - odst;
|
1126
|
+
*chars_changed = total_changed;
|
1127
|
+
return e;
|
1128
|
+
}
|
1129
|
+
|
1130
|
+
|
1131
|
+
// Scan a UTF-8 stringpiece based on state table, copying to output stringpiece
|
1132
|
+
// and doing text replacements.
|
1133
|
+
// Also writes an optional OffsetMap. Pass NULL to skip writing one.
|
1134
|
+
// Always scan complete UTF-8 characters
|
1135
|
+
// Set number of bytes consumed from input, number filled to output.
|
1136
|
+
// Return reason for exiting
|
1137
|
+
int UTF8GenericReplace(const UTF8ReplaceObj* st,
|
1138
|
+
const StringPiece& istr,
|
1139
|
+
StringPiece& ostr,
|
1140
|
+
bool is_plain_text,
|
1141
|
+
int* bytes_consumed,
|
1142
|
+
int* bytes_filled,
|
1143
|
+
int* chars_changed,
|
1144
|
+
OffsetMap* offsetmap) {
|
1145
|
+
StringPiece local_istr(istr.data(), istr.length());
|
1146
|
+
StringPiece local_ostr(ostr.data(), ostr.length());
|
1147
|
+
int total_consumed = 0;
|
1148
|
+
int total_filled = 0;
|
1149
|
+
int total_changed = 0;
|
1150
|
+
int local_bytes_consumed, local_bytes_filled, local_chars_changed;
|
1151
|
+
int e;
|
1152
|
+
do {
|
1153
|
+
e = UTF8GenericReplaceInternal(st,
|
1154
|
+
local_istr, local_ostr, is_plain_text,
|
1155
|
+
&local_bytes_consumed, &local_bytes_filled,
|
1156
|
+
&local_chars_changed,
|
1157
|
+
offsetmap);
|
1158
|
+
local_istr.remove_prefix(local_bytes_consumed);
|
1159
|
+
local_ostr.remove_prefix(local_bytes_filled);
|
1160
|
+
total_consumed += local_bytes_consumed;
|
1161
|
+
total_filled += local_bytes_filled;
|
1162
|
+
total_changed += local_chars_changed;
|
1163
|
+
} while ( e == kExitDoAgain );
|
1164
|
+
*bytes_consumed = total_consumed;
|
1165
|
+
*bytes_filled = total_filled;
|
1166
|
+
*chars_changed = total_changed;
|
1167
|
+
return e;
|
1168
|
+
}
|
1169
|
+
|
1170
|
+
// Older version without offsetmap
|
1171
|
+
int UTF8GenericReplace(const UTF8ReplaceObj* st,
|
1172
|
+
const StringPiece& istr,
|
1173
|
+
StringPiece& ostr,
|
1174
|
+
bool is_plain_text,
|
1175
|
+
int* bytes_consumed,
|
1176
|
+
int* bytes_filled,
|
1177
|
+
int* chars_changed) {
|
1178
|
+
return UTF8GenericReplace(st,
|
1179
|
+
istr,
|
1180
|
+
ostr,
|
1181
|
+
is_plain_text,
|
1182
|
+
bytes_consumed,
|
1183
|
+
bytes_filled,
|
1184
|
+
chars_changed,
|
1185
|
+
NULL);
|
1186
|
+
}
|
1187
|
+
|
1188
|
+
// Older version without is_plain_text or offsetmap
|
1189
|
+
int UTF8GenericReplace(const UTF8ReplaceObj* st,
|
1190
|
+
const StringPiece& istr,
|
1191
|
+
StringPiece& ostr,
|
1192
|
+
int* bytes_consumed,
|
1193
|
+
int* bytes_filled,
|
1194
|
+
int* chars_changed) {
|
1195
|
+
bool is_plain_text = false;
|
1196
|
+
return UTF8GenericReplace(st,
|
1197
|
+
istr,
|
1198
|
+
ostr,
|
1199
|
+
is_plain_text,
|
1200
|
+
bytes_consumed,
|
1201
|
+
bytes_filled,
|
1202
|
+
chars_changed,
|
1203
|
+
NULL);
|
1204
|
+
}
|
1205
|
+
|
1206
|
+
// Scan a UTF-8 stringpiece based on state table with two-byte entries,
|
1207
|
+
// copying to output stringpiece
|
1208
|
+
// and doing text replacements.
|
1209
|
+
// Also writes an optional OffsetMap. Pass NULL to skip writing one.
|
1210
|
+
// Always scan complete UTF-8 characters
|
1211
|
+
// Set number of bytes consumed from input, number filled to output.
|
1212
|
+
// Return reason for exiting
|
1213
|
+
int UTF8GenericReplaceTwoByte(const UTF8ReplaceObj_2* st,
|
1214
|
+
const StringPiece& istr,
|
1215
|
+
StringPiece& ostr,
|
1216
|
+
bool is_plain_text,
|
1217
|
+
int* bytes_consumed,
|
1218
|
+
int* bytes_filled,
|
1219
|
+
int* chars_changed,
|
1220
|
+
OffsetMap* offsetmap) {
|
1221
|
+
StringPiece local_istr(istr.data(), istr.length());
|
1222
|
+
StringPiece local_ostr(ostr.data(), ostr.length());
|
1223
|
+
int total_consumed = 0;
|
1224
|
+
int total_filled = 0;
|
1225
|
+
int total_changed = 0;
|
1226
|
+
int local_bytes_consumed, local_bytes_filled, local_chars_changed;
|
1227
|
+
int e;
|
1228
|
+
do {
|
1229
|
+
e = UTF8GenericReplaceInternalTwoByte(st,
|
1230
|
+
local_istr, local_ostr, is_plain_text,
|
1231
|
+
&local_bytes_consumed,
|
1232
|
+
&local_bytes_filled,
|
1233
|
+
&local_chars_changed,
|
1234
|
+
offsetmap);
|
1235
|
+
local_istr.remove_prefix(local_bytes_consumed);
|
1236
|
+
local_ostr.remove_prefix(local_bytes_filled);
|
1237
|
+
total_consumed += local_bytes_consumed;
|
1238
|
+
total_filled += local_bytes_filled;
|
1239
|
+
total_changed += local_chars_changed;
|
1240
|
+
} while ( e == kExitDoAgain_2 );
|
1241
|
+
*bytes_consumed = total_consumed;
|
1242
|
+
*bytes_filled = total_filled;
|
1243
|
+
*chars_changed = total_changed;
|
1244
|
+
|
1245
|
+
return e - kExitOK_2 + kExitOK;
|
1246
|
+
}
|
1247
|
+
|
1248
|
+
// Older version without offsetmap
|
1249
|
+
int UTF8GenericReplaceTwoByte(const UTF8ReplaceObj_2* st,
|
1250
|
+
const StringPiece& istr,
|
1251
|
+
StringPiece& ostr,
|
1252
|
+
bool is_plain_text,
|
1253
|
+
int* bytes_consumed,
|
1254
|
+
int* bytes_filled,
|
1255
|
+
int* chars_changed) {
|
1256
|
+
return UTF8GenericReplaceTwoByte(st,
|
1257
|
+
istr,
|
1258
|
+
ostr,
|
1259
|
+
is_plain_text,
|
1260
|
+
bytes_consumed,
|
1261
|
+
bytes_filled,
|
1262
|
+
chars_changed,
|
1263
|
+
NULL);
|
1264
|
+
}
|
1265
|
+
|
1266
|
+
// Older version without is_plain_text or offsetmap
|
1267
|
+
int UTF8GenericReplaceTwoByte(const UTF8ReplaceObj_2* st,
|
1268
|
+
const StringPiece& istr,
|
1269
|
+
StringPiece& ostr,
|
1270
|
+
int* bytes_consumed,
|
1271
|
+
int* bytes_filled,
|
1272
|
+
int* chars_changed) {
|
1273
|
+
bool is_plain_text = false;
|
1274
|
+
return UTF8GenericReplaceTwoByte(st,
|
1275
|
+
istr,
|
1276
|
+
ostr,
|
1277
|
+
is_plain_text,
|
1278
|
+
bytes_consumed,
|
1279
|
+
bytes_filled,
|
1280
|
+
chars_changed,
|
1281
|
+
NULL);
|
1282
|
+
}
|
1283
|
+
|
1284
|
+
|
1285
|
+
|
1286
|
+
// Adjust a stringpiece to encompass complete UTF-8 characters.
|
1287
|
+
// The data pointer will be increased by 0..3 bytes to get to a character
|
1288
|
+
// boundary, and the length will then be decreased by 0..3 bytes
|
1289
|
+
// to encompass the last complete character.
|
1290
|
+
void UTF8TrimToChars(StringPiece* istr) {
|
1291
|
+
const char* src = istr->data();
|
1292
|
+
int len = istr->length();
|
1293
|
+
// Exit if empty string
|
1294
|
+
if (len == 0) {
|
1295
|
+
return;
|
1296
|
+
}
|
1297
|
+
|
1298
|
+
// Exit on simple, common case
|
1299
|
+
if ( ((src[0] & 0xc0) != 0x80) &&
|
1300
|
+
(static_cast<signed char>(src[len - 1]) >= 0) ) {
|
1301
|
+
// First byte is not a continuation and last byte is 7-bit ASCII -- done
|
1302
|
+
return;
|
1303
|
+
}
|
1304
|
+
|
1305
|
+
// Adjust the back end, len > 0
|
1306
|
+
const char* srclimit = src + len;
|
1307
|
+
// Backscan over any ending continuation bytes to find last char start
|
1308
|
+
const char* s = srclimit - 1; // Last byte of the string
|
1309
|
+
while ((src <= s) && ((*s & 0xc0) == 0x80)) {
|
1310
|
+
s--;
|
1311
|
+
}
|
1312
|
+
// Include entire last char if it fits
|
1313
|
+
if (src <= s) {
|
1314
|
+
int last_char_len = UTF8OneCharLen(s);
|
1315
|
+
if (s + last_char_len <= srclimit) {
|
1316
|
+
// Last char fits, so include it, else exclude it
|
1317
|
+
s += last_char_len;
|
1318
|
+
}
|
1319
|
+
}
|
1320
|
+
if (s != srclimit) {
|
1321
|
+
// s is one byte beyond the last full character, if any
|
1322
|
+
istr->remove_suffix(srclimit - s);
|
1323
|
+
// Exit if now empty string
|
1324
|
+
if (istr->length() == 0) {
|
1325
|
+
return;
|
1326
|
+
}
|
1327
|
+
}
|
1328
|
+
|
1329
|
+
// Adjust the front end, len > 0
|
1330
|
+
len = istr->length();
|
1331
|
+
srclimit = src + len;
|
1332
|
+
s = src; // First byte of the string
|
1333
|
+
// Scan over any beginning continuation bytes to find first char start
|
1334
|
+
while ((s < srclimit) && ((*s & 0xc0) == 0x80)) {
|
1335
|
+
s++;
|
1336
|
+
}
|
1337
|
+
if (s != src) {
|
1338
|
+
// s is at the first full character, if any
|
1339
|
+
istr->remove_prefix(s - src);
|
1340
|
+
}
|
1341
|
+
}
|
1342
|
+
|
1343
|
+
} // End namespace CLD2
|
1344
|
+
} // End namespace chrome_lang_id
|