summaryrefslogtreecommitdiffstats
path: root/toolkit/components/translation/cld2/internal/utf8statetable.cc
blob: 8c97123d8c0717c2070d93a1fb8d9413e77bd091 (plain)
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
// Copyright 2013 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//
// State Table follower for scanning UTF-8 strings without converting to
// 32- or 16-bit Unicode values.
//

#ifdef COMPILER_MSVC
// MSVC warns: warning C4309: 'initializing' : truncation of constant value
// But the value is in fact not truncated.  0xFF still comes out 0xFF at
// runtime.
#pragma warning ( disable : 4309 )
#endif

#include "utf8statetable.h"

#include <stdint.h>                     // for uintptr_t
#include <string.h>                     // for NULL, memcpy, memmove

#include "integral_types.h"        // for uint8, uint32, int8
#include "stringpiece.h"
#include "offsetmap.h"


namespace CLD2 {

static const int kReplaceAndResumeFlag = 0x80; // Bit in del byte to distinguish
                                               // optional next-state field
                                               // after replacement text
static const int kHtmlPlaintextFlag = 0x80;    // Bit in add byte to distinguish
                                               // HTML replacement vs. plaintext


/**
 * This code implements a little interpreter for UTF8 state
 * tables. There are three kinds of quite-similar state tables,
 * property, scanning, and replacement. Each state in one of
 * these tables consists of an array of 256 or 64 one-byte
 * entries. The state is subscripted by an incoming source byte,
 * and the entry either specifies the next state or specifies an
 * action. Space-optimized tables have full 256-entry states for
 * the first byte of a UTF-8 character, but only 64-entry states
 * for continuation bytes. Space-optimized tables may only be
 * used with source input that has been checked to be
 * structurally- (or stronger interchange-) valid.
 *
 * A property state table has an unsigned one-byte property for
 * each possible UTF-8 character. One-byte character properties
 * are in the state[0] array, while for other lengths the
 * state[0] array gives the next state, which contains the
 * property value for two-byte characters or yet another state
 * for longer ones. The code simply loads the right number of
 * next-state values, then returns the final byte as property
 * value. There are no actions specified in property tables.
 * States are typically shared for multi-byte UTF-8 characters
 * that all have the same property value.
 *
 * A scanning state table has entries that are either a
 * next-state specifier for bytes that are accepted by the
 * scanner, or an exit action for the last byte of each
 * character that is rejected by the scanner.
 *
 * Scanning long strings involves a tight loop that picks up one
 * byte at a time and follows next-state value back to state[0]
 * for each accepted UTF-8 character. Scanning stops at the end
 * of the string or at the first character encountered that has
 * an exit action such as "reject". Timing information is given
 * below.
 *
 * Since so much of Google's text is 7-bit-ASCII values
 * (approximately 94% of the bytes of web documents), the
 * scanning interpreter has two speed optimizations. One checks
 * 8 bytes at a time to see if they are all in the range lo..hi,
 * as specified in constants in the overall statetable object.
 * The check involves ORing together four 4-byte values that
 * overflow into the high bit of some byte when a byte is out of
 * range. For seven-bit-ASCII, lo is 0x20 and hi is 0x7E. This
 * loop is about 8x faster than the one-byte-at-a-time loop.
 *
 * If checking for exit bytes in the 0x00-0x1F and 7F range is
 * unneeded, an even faster loop just looks at the high bits of
 * 8 bytes at once, and is about 1.33x faster than the lo..hi
 * loop.
 *
 * Exit from the scanning routines backs up to the first byte of
 * the rejected character, so the text spanned is always a
 * complete number of UTF-8 characters. The normal scanning exit
 * is at the first rejected character, or at the end of the
 * input text. Scanning also exits on any detected ill-formed
 * character or at a special do-again action built into some
 * exit-optimized tables. The do-again action gets back to the
 * top of the scanning loop to retry eight-byte ASCII scans. It
 * is typically put into state tables after four seven-bit-ASCII
 * characters in a row are seen, to allow restarting the fast
 * scan after some slower processing of multi-byte characters.
 *
 * A replacement state table is similar to a scanning state
 * table but has more extensive actions. The default
 * byte-at-a-time loop copies one byte from source to
 * destination and goes to the next state. The replacement
 * actions overwrite 1-3 bytes of the destination with different
 * bytes, possibly shortening the output by 1 or 2 bytes. The
 * replacement bytes come from within the state table, from
 * dummy states inserted just after any state that contains a
 * replacement action. This gives a quick address calculation for
 * the replacement byte(s) and gives some cache locality.
 *
 * Additional replacement actions use one or two bytes from
 * within dummy states to index a side table of more-extensive
 * replacements. The side table specifies a length of 0..15
 * destination bytes to overwrite and a length of 0..127 bytes
 * to overwrite them with, plus the actual replacement bytes.
 *
 * This side table uses one extra bit to specify a pair of
 * replacements, the first to be used in an HTML context and the
 * second to be used in a plaintext context. This allows
 * replacements that are spelled with "&lt;" in the former
 * context and "<" in the latter.
 *
 * The side table also uses an extra bit to specify a non-zero
 * next state after a replacement. This allows a combination
 * replacement and state change, used to implement a limited
 * version of the Boyer-Moore algorithm for multi-character
 * replacement without backtracking. This is useful when there
 * are overlapping replacements, such as ch => x and also c =>
 * y, the latter to be used only if the character after c is not
 * h. in this case, the state[0] table's entry for c would
 * change c to y and also have a next-state of say n, and the
 * state[n] entry for h would specify a replacement of the two
 * bytes yh by x. No backtracking is needed.
 *
 * A replacement table may also include the exit actions of a
 * scanning state table, so some character sequences can
 * terminate early.
 *
 * During replacement, an optional data structure called an
 * offset map can be updated to reflect each change in length
 * between source and destination. This offset map can later be
 * used to map destination-string offsets to corresponding
 * source-string offsets or vice versa.
 *
 * The routines below also have variants in which state-table
 * entries are all two bytes instead of one byte. This allows
 * tables with more than 240 total states, but takes up twice as
 * much space per state.
 *
**/

// Return true if current Tbl pointer is within state0 range
// Note that unsigned compare checks both ends of range simultaneously
static inline bool InStateZero(const UTF8ScanObj* st, const uint8* Tbl) {
  const uint8* Tbl0 = &st->state_table[st->state0];
  return (static_cast<uint32>(Tbl - Tbl0) < st->state0_size);
}

static inline bool InStateZero_2(const UTF8ReplaceObj_2* st,
                                 const unsigned short int* Tbl) {
  const unsigned short int* Tbl0 =  &st->state_table[st->state0];
  // Word difference, not byte difference
  return (static_cast<uint32>(Tbl - Tbl0) < st->state0_size);
}

// UTF8PropObj, UTF8ScanObj, UTF8ReplaceObj are all typedefs of
// UTF8MachineObj.

static bool IsPropObj(const UTF8StateMachineObj& obj) {
  return obj.fast_state == NULL
      && obj.max_expand == 0;
}

static bool IsPropObj_2(const UTF8StateMachineObj_2& obj) {
  return obj.fast_state == NULL
      && obj.max_expand == 0;
}

static bool IsScanObj(const UTF8StateMachineObj& obj) {
  return obj.fast_state != NULL
      && obj.max_expand == 0;
}

static bool IsReplaceObj(const UTF8StateMachineObj& obj) {
  // Normally, obj.fast_state != NULL, but the handwritten tables
  // in utf8statetable_unittest don't handle fast_states.
  return obj.max_expand > 0;
}

static bool IsReplaceObj_2(const UTF8StateMachineObj_2& obj) {
  return obj.max_expand > 0;
}

// Look up property of one UTF-8 character and advance over it
// Return 0 if input length is zero
// Return 0 and advance one byte if input is ill-formed
uint8 UTF8GenericProperty(const UTF8PropObj* st,
                          const uint8** src,
                          int* srclen) {
  if (*srclen <= 0) {
    return 0;
  }

  const uint8* lsrc = *src;
  const uint8* Tbl_0 = &st->state_table[st->state0];
  const uint8* Tbl = Tbl_0;
  int e;
  int eshift = st->entry_shift;

  // Short series of tests faster than switch, optimizes 7-bit ASCII
  unsigned char c = lsrc[0];
  if (static_cast<signed char>(c) >= 0) {           // one byte
    e = Tbl[c];
    *src += 1;
    *srclen -= 1;
  } else if (((c & 0xe0) == 0xc0) && (*srclen >= 2)) {     // two bytes
    e = Tbl[c];
    Tbl = &Tbl_0[e << eshift];
    e = Tbl[lsrc[1]];
    *src += 2;
    *srclen -= 2;
  } else if (((c & 0xf0) == 0xe0) && (*srclen >= 3)) {     // three bytes
    e = Tbl[c];
    Tbl = &Tbl_0[e << eshift];
    e = Tbl[lsrc[1]];
    Tbl = &Tbl_0[e << eshift];
    e = Tbl[lsrc[2]];
    *src += 3;
    *srclen -= 3;
  }else if (((c & 0xf8) == 0xf0) && (*srclen >= 4)) {     // four bytes
    e = Tbl[c];
    Tbl = &Tbl_0[e << eshift];
    e = Tbl[lsrc[1]];
    Tbl = &Tbl_0[e << eshift];
    e = Tbl[lsrc[2]];
    Tbl = &Tbl_0[e << eshift];
    e = Tbl[lsrc[3]];
    *src += 4;
    *srclen -= 4;
  } else {                                                // Ill-formed
    e = 0;
    *src += 1;
    *srclen -= 1;
  }
  return e;
}

bool UTF8HasGenericProperty(const UTF8PropObj& st, const char* src) {
  const uint8* lsrc = reinterpret_cast<const uint8*>(src);
  const uint8* Tbl_0 = &st.state_table[st.state0];
  const uint8* Tbl = Tbl_0;
  int e;
  int eshift = st.entry_shift;

  // Short series of tests faster than switch, optimizes 7-bit ASCII
  unsigned char c = lsrc[0];
  if (static_cast<signed char>(c) >= 0) {           // one byte
    e = Tbl[c];
  } else if ((c & 0xe0) == 0xc0) {     // two bytes
    e = Tbl[c];
    Tbl = &Tbl_0[e << eshift];
    e = Tbl[lsrc[1]];
  } else if ((c & 0xf0) == 0xe0) {     // three bytes
    e = Tbl[c];
    Tbl = &Tbl_0[e << eshift];
    e = Tbl[lsrc[1]];
    Tbl = &Tbl_0[e << eshift];
    e = Tbl[lsrc[2]];
  } else {                             // four bytes
    e = Tbl[c];
    Tbl = &Tbl_0[e << eshift];
    e = Tbl[lsrc[1]];
    Tbl = &Tbl_0[e << eshift];
    e = Tbl[lsrc[2]];
    Tbl = &Tbl_0[e << eshift];
    e = Tbl[lsrc[3]];
  }
  return e;
}


// BigOneByte versions are needed for tables > 240 states, but most
// won't need the TwoByte versions.
// Internally, to next-to-last offset is multiplied by 16 and the last
// offset is relative instead of absolute.
// Look up property of one UTF-8 character and advance over it
// Return 0 if input length is zero
// Return 0 and advance one byte if input is ill-formed
uint8 UTF8GenericPropertyBigOneByte(const UTF8PropObj* st,
                          const uint8** src,
                          int* srclen) {
  if (*srclen <= 0) {
    return 0;
  }

  const uint8* lsrc = *src;
  const uint8* Tbl_0 = &st->state_table[st->state0];
  const uint8* Tbl = Tbl_0;
  int e;
  int eshift = st->entry_shift;

  // Short series of tests faster than switch, optimizes 7-bit ASCII
  unsigned char c = lsrc[0];
  if (static_cast<signed char>(c) >= 0) {           // one byte
    e = Tbl[c];
    *src += 1;
    *srclen -= 1;
  } else if (((c & 0xe0) == 0xc0) && (*srclen >= 2)) {     // two bytes
    e = Tbl[c];
    Tbl = &Tbl_0[e << eshift];
    e = Tbl[lsrc[1]];
    *src += 2;
    *srclen -= 2;
  } else if (((c & 0xf0) == 0xe0) && (*srclen >= 3)) {     // three bytes
    e = Tbl[c];
    Tbl = &Tbl_0[e << (eshift + 4)];  // 16x the range
    e = (reinterpret_cast<const int8*>(Tbl))[lsrc[1]];
    Tbl = &Tbl[e << eshift];          // Relative +/-
    e = Tbl[lsrc[2]];
    *src += 3;
    *srclen -= 3;
  }else if (((c & 0xf8) == 0xf0) && (*srclen >= 4)) {     // four bytes
    e = Tbl[c];
    Tbl = &Tbl_0[e << eshift];
    e = Tbl[lsrc[1]];
    Tbl = &Tbl_0[e << (eshift + 4)];  // 16x the range
    e = (reinterpret_cast<const int8*>(Tbl))[lsrc[2]];
    Tbl = &Tbl[e << eshift];          // Relative +/-
    e = Tbl[lsrc[3]];
    *src += 4;
    *srclen -= 4;
  } else {                                                // Ill-formed
    e = 0;
    *src += 1;
    *srclen -= 1;
  }
  return e;
}

// BigOneByte versions are needed for tables > 240 states, but most
// won't need the TwoByte versions.
bool UTF8HasGenericPropertyBigOneByte(const UTF8PropObj& st, const char* src) {
  const uint8* lsrc = reinterpret_cast<const uint8*>(src);
  const uint8* Tbl_0 = &st.state_table[st.state0];
  const uint8* Tbl = Tbl_0;
  int e;
  int eshift = st.entry_shift;

  // Short series of tests faster than switch, optimizes 7-bit ASCII
  unsigned char c = lsrc[0];
  if (static_cast<signed char>(c) >= 0) {           // one byte
    e = Tbl[c];
  } else if ((c & 0xe0) == 0xc0) {    // two bytes
    e = Tbl[c];
    Tbl = &Tbl_0[e << eshift];
    e = Tbl[lsrc[1]];
  } else if ((c & 0xf0) == 0xe0) {    // three bytes
    e = Tbl[c];
    Tbl = &Tbl_0[e << (eshift + 4)];  // 16x the range
    e = (reinterpret_cast<const int8*>(Tbl))[lsrc[1]];
    Tbl = &Tbl[e << eshift];          // Relative +/-
    e = Tbl[lsrc[2]];
  } else {                            // four bytes
    e = Tbl[c];
    Tbl = &Tbl_0[e << eshift];
    e = Tbl[lsrc[1]];
    Tbl = &Tbl_0[e << (eshift + 4)];  // 16x the range
    e = (reinterpret_cast<const int8*>(Tbl))[lsrc[2]];
    Tbl = &Tbl[e << eshift];          // Relative +/-
    e = Tbl[lsrc[3]];
  }
  return e;
}


// TwoByte versions are needed for tables > 240 states
// Look up property of one UTF-8 character and advance over it
// Return 0 if input length is zero
// Return 0 and advance one byte if input is ill-formed
uint8 UTF8GenericPropertyTwoByte(const UTF8PropObj_2* st,
                          const uint8** src,
                          int* srclen) {
  if (*srclen <= 0) {
    return 0;
  }

  const uint8* lsrc = *src;
  const unsigned short* Tbl_0 = &st->state_table[st->state0];
  const unsigned short* Tbl = Tbl_0;
  int e;
  int eshift = st->entry_shift;

  // Short series of tests faster than switch, optimizes 7-bit ASCII
  unsigned char c = lsrc[0];
  if (static_cast<signed char>(c) >= 0) {           // one byte
    e = Tbl[c];
    *src += 1;
    *srclen -= 1;
  } else if (((c & 0xe0) == 0xc0) && (*srclen >= 2)) {     // two bytes
    e = Tbl[c];
    Tbl = &Tbl_0[e << eshift];
    e = Tbl[lsrc[1]];
    *src += 2;
    *srclen -= 2;
  } else if (((c & 0xf0) == 0xe0) && (*srclen >= 3)) {     // three bytes
    e = Tbl[c];
    Tbl = &Tbl_0[e << eshift];
    e = Tbl[lsrc[1]];
    Tbl = &Tbl_0[e << eshift];
    e = Tbl[lsrc[2]];
    *src += 3;
    *srclen -= 3;
  }else if (((c & 0xf8) == 0xf0) && (*srclen >= 4)) {     // four bytes
    e = Tbl[c];
    Tbl = &Tbl_0[e << eshift];
    e = Tbl[lsrc[1]];
    Tbl = &Tbl_0[e << eshift];
    e = Tbl[lsrc[2]];
    Tbl = &Tbl_0[e << eshift];
    e = Tbl[lsrc[3]];
    *src += 4;
    *srclen -= 4;
  } else {                                                // Ill-formed
    e = 0;
    *src += 1;
    *srclen -= 1;
  }
  return e;
}

// TwoByte versions are needed for tables > 240 states
bool UTF8HasGenericPropertyTwoByte(const UTF8PropObj_2& st, const char* src) {
  const uint8* lsrc = reinterpret_cast<const uint8*>(src);
  const unsigned short* Tbl_0 = &st.state_table[st.state0];
  const unsigned short* Tbl = Tbl_0;
  int e;
  int eshift = st.entry_shift;

  // Short series of tests faster than switch, optimizes 7-bit ASCII
  unsigned char c = lsrc[0];
  if (static_cast<signed char>(c) >= 0) {           // one byte
    e = Tbl[c];
  } else if ((c & 0xe0) == 0xc0) {     // two bytes
    e = Tbl[c];
    Tbl = &Tbl_0[e << eshift];
    e = Tbl[lsrc[1]];
  } else if ((c & 0xf0) == 0xe0) {     // three bytes
    e = Tbl[c];
    Tbl = &Tbl_0[e << eshift];
    e = Tbl[lsrc[1]];
    Tbl = &Tbl_0[e << eshift];
    e = Tbl[lsrc[2]];
  } else {                             // four bytes
    e = Tbl[c];
    Tbl = &Tbl_0[e << eshift];
    e = Tbl[lsrc[1]];
    Tbl = &Tbl_0[e << eshift];
    e = Tbl[lsrc[2]];
    Tbl = &Tbl_0[e << eshift];
    e = Tbl[lsrc[3]];
  }
  return e;
}


// Approximate speeds on 2.8 GHz Pentium 4:
//   GenericScan 1-byte loop           300 MB/sec *
//   GenericScan 4-byte loop          1200 MB/sec
//   GenericScan 8-byte loop          2400 MB/sec *
//   GenericScanFastAscii 4-byte loop 3000 MB/sec
//   GenericScanFastAscii 8-byte loop 3200 MB/sec *
//
// * Implemented below. FastAscii loop is memory-bandwidth constrained.

// Scan a UTF-8 stringpiece based on state table.
// Always scan complete UTF-8 characters
// Set number of bytes scanned. Return reason for exiting
int UTF8GenericScan(const UTF8ScanObj* st,
                    const StringPiece& str,
                    int* bytes_consumed) {
  int eshift = st->entry_shift;       // 6 (space optimized) or 8
  // int nEntries = (1 << eshift);       // 64 or 256 entries per state

  const uint8* isrc =
    reinterpret_cast<const uint8*>(str.data());
  const uint8* src = isrc;
  const int len = str.length();
  const uint8* srclimit = isrc + len;
  const uint8* srclimit8 = srclimit - 7;
  *bytes_consumed = 0;
  if (len == 0) return kExitOK;

  const uint8* Tbl_0 = &st->state_table[st->state0];

DoAgain:
  // Do state-table scan
  int e = 0;
  uint8 c;

  // Do fast for groups of 8 identity bytes.
  // This covers a lot of 7-bit ASCII ~8x faster than the 1-byte loop,
  // including slowing slightly on cr/lf/ht
  //----------------------------
  const uint8* Tbl2 = &st->fast_state[0];
  uint32 losub = st->losub;
  uint32 hiadd = st->hiadd;
  while (src < srclimit8) {
    uint32 s0123 = (reinterpret_cast<const uint32 *>(src))[0];
    uint32 s4567 = (reinterpret_cast<const uint32 *>(src))[1];
    src += 8;
    // This is a fast range check for all bytes in [lowsub..0x80-hiadd)
    uint32 temp = (s0123 - losub) | (s0123 + hiadd) |
                  (s4567 - losub) | (s4567 + hiadd);
    if ((temp & 0x80808080) != 0) {
      // We typically end up here on cr/lf/ht; src was incremented
      int e0123 = (Tbl2[src[-8]] | Tbl2[src[-7]]) |
                  (Tbl2[src[-6]] | Tbl2[src[-5]]);
      if (e0123 != 0) {src -= 8; break;}    // Exit on Non-interchange
      e0123 = (Tbl2[src[-4]] | Tbl2[src[-3]]) |
              (Tbl2[src[-2]] | Tbl2[src[-1]]);
      if (e0123 != 0) {src -= 4; break;}    // Exit on Non-interchange
      // Else OK, go around again
    }
  }
  //----------------------------

  // Byte-at-a-time scan
  //----------------------------
  const uint8* Tbl = Tbl_0;
  while (src < srclimit) {
    c = *src;
    e = Tbl[c];
    src++;
    if (e >= kExitIllegalStructure) {break;}
    Tbl = &Tbl_0[e << eshift];
  }
  //----------------------------


  // Exit possibilities:
  //  Some exit code, !state0, back up over last char
  //  Some exit code, state0, back up one byte exactly
  //  source consumed, !state0, back up over partial char
  //  source consumed, state0, exit OK
  // For illegal byte in state0, avoid backup up over PREVIOUS char
  // For truncated last char, back up to beginning of it

  if (e >= kExitIllegalStructure) {
    // Back up over exactly one byte of rejected/illegal UTF-8 character
    src--;
    // Back up more if needed
    if (!InStateZero(st, Tbl)) {
      do {src--;} while ((src > isrc) && ((src[0] & 0xc0) == 0x80));
    }
  } else if (!InStateZero(st, Tbl)) {
    // Back up over truncated UTF-8 character
    e = kExitIllegalStructure;
    do {src--;} while ((src > isrc) && ((src[0] & 0xc0) == 0x80));
  } else {
    // Normal termination, source fully consumed
    e = kExitOK;
  }

  if (e == kExitDoAgain) {
    // Loop back up to the fast scan
    goto DoAgain;
  }

  *bytes_consumed = src - isrc;
  return e;
}

// Scan a UTF-8 stringpiece based on state table.
// Always scan complete UTF-8 characters
// Set number of bytes scanned. Return reason for exiting
// OPTIMIZED for case of 7-bit ASCII 0000..007f all valid
int UTF8GenericScanFastAscii(const UTF8ScanObj* st,
                    const StringPiece& str,
                    int* bytes_consumed) {
  const uint8* isrc =
    reinterpret_cast<const uint8*>(str.data());
  const uint8* src = isrc;
  const int len = str.length();
  const uint8* srclimit = isrc + len;
  const uint8* srclimit8 = srclimit - 7;
  *bytes_consumed = 0;
  if (len == 0) return kExitOK;

  int n;
  int rest_consumed;
  int exit_reason;
  do {
    // Skip 8 bytes of ASCII at a whack; no endianness issue
    while ((src < srclimit8) &&
           (((reinterpret_cast<const uint32*>(src)[0] |
              reinterpret_cast<const uint32*>(src)[1]) & 0x80808080) == 0)) {
      src += 8;
    }
    // Run state table on the rest
    n = src - isrc;
    StringPiece str2(str.data() + n, str.length() - n);
    exit_reason = UTF8GenericScan(st, str2, &rest_consumed);
    src += rest_consumed;
  } while ( exit_reason == kExitDoAgain );

  *bytes_consumed = src - isrc;
  return exit_reason;
}

// Hack to change halfwidth katakana to match an old UTF8CharToLower()

// Return number of src bytes skipped
static int DoSpecialFixup(const unsigned char c,
                    const unsigned char** srcp, const unsigned char* srclimit,
                    unsigned char** dstp, unsigned char* dstlimit) {
  return 0;
}


// Scan a UTF-8 stringpiece based on state table, copying to output stringpiece
//   and doing text replacements.
// DO NOT CALL DIRECTLY. Use UTF8GenericReplace() below
//   Needs caller to loop on kExitDoAgain
static int UTF8GenericReplaceInternal(const UTF8ReplaceObj* st,
                    const StringPiece& istr,
                    StringPiece& ostr,
                    bool is_plain_text,
                    int* bytes_consumed,
                    int* bytes_filled,
                    int* chars_changed,
                    OffsetMap* offsetmap) {
  int eshift = st->entry_shift;
  int nEntries = (1 << eshift);       // 64 or 256 entries per state
  const uint8* isrc = reinterpret_cast<const uint8*>(istr.data());
  const int ilen = istr.length();
  const uint8* copystart = isrc;
  const uint8* src = isrc;
  const uint8* srclimit = src + ilen;
  *bytes_consumed = 0;
  *bytes_filled = 0;
  *chars_changed = 0;

  const uint8* odst = reinterpret_cast<const uint8*>(ostr.data());
  const int olen = ostr.length();
  uint8* dst = const_cast<uint8*>(odst);
  uint8* dstlimit = dst + olen;

  int total_changed = 0;

  // Invariant condition during replacements:
  //  remaining dst size >= remaining src size
  if ((dstlimit - dst) < (srclimit - src)) {
    if (offsetmap != NULL) {
      offsetmap->Copy(src - copystart);
      copystart = src;
    }
    return kExitDstSpaceFull;
  }
  const uint8* Tbl_0 = &st->state_table[st->state0];

 Do_state_table:
  // Do state-table scan, copying as we go
  const uint8* Tbl = Tbl_0;
  int e = 0;
  uint8 c = 0;

 Do_state_table_newe:

  //----------------------------
  while (src < srclimit) {
    c = *src;
    e = Tbl[c];
    *dst = c;
    src++;
    dst++;
    if (e >= kExitIllegalStructure) {break;}
    Tbl = &Tbl_0[e << eshift];
  }
  //----------------------------

  // Exit possibilities:
  //  Replacement code, do the replacement and loop
  //  Some other exit code, state0, back up one byte exactly
  //  Some other exit code, !state0, back up over last char
  //  source consumed, state0, exit OK
  //  source consumed, !state0, back up over partial char
  // For illegal byte in state0, avoid backup up over PREVIOUS char
  // For truncated last char, back up to beginning of it

  if (e >= kExitIllegalStructure) {
    // Switch on exit code; most loop back to top
    int offset = 0;
    switch (e) {
    // These all make the output string the same size or shorter
    // No checking needed
    case kExitReplace31:    // del 2, add 1 bytes to change
      dst -= 2;
      if (offsetmap != NULL) {
        offsetmap->Copy(src - copystart - 2);
        offsetmap->Delete(2);
        copystart = src;
      }
      dst[-1] = (unsigned char)Tbl[c + (nEntries * 1)];
      total_changed++;
      goto Do_state_table;
    case kExitReplace32:    // del 3, add 2 bytes to change
      dst--;
      if (offsetmap != NULL) {
        offsetmap->Copy(src - copystart - 1);
        offsetmap->Delete(1);
        copystart = src;
      }
      dst[-2] = (unsigned char)Tbl[c + (nEntries * 2)];
      dst[-1] = (unsigned char)Tbl[c + (nEntries * 1)];
      total_changed++;
      goto Do_state_table;
    case kExitReplace21:    // del 2, add 1 bytes to change
      dst--;
      if (offsetmap != NULL) {
        offsetmap->Copy(src - copystart - 1);
        offsetmap->Delete(1);
        copystart = src;
      }
      dst[-1] = (unsigned char)Tbl[c + (nEntries * 1)];
      total_changed++;
      goto Do_state_table;
    case kExitReplace3:    // update 3 bytes to change
      dst[-3] = (unsigned char)Tbl[c + (nEntries * 3)];
      // Fall into next case
    case kExitReplace2:    // update 2 bytes to change
      dst[-2] = (unsigned char)Tbl[c + (nEntries * 2)];
      // Fall into next case
    case kExitReplace1:    // update 1 byte to change
      dst[-1] = (unsigned char)Tbl[c + (nEntries * 1)];
      total_changed++;
      goto Do_state_table;
    case kExitReplace1S0:     // update 1 byte to change, 256-entry state
      dst[-1] = (unsigned char)Tbl[c + (256 * 1)];
      total_changed++;
      goto Do_state_table;
    // These can make the output string longer than the input
    case kExitReplaceOffset2:
      if ((nEntries != 256) && InStateZero(st, Tbl)) {
        // For space-optimized table, we need multiples of 256 bytes
        // in state0 and multiples of nEntries in other states
        offset += ((unsigned char)Tbl[c + (256 * 2)] << 8);
      } else {
        offset += ((unsigned char)Tbl[c + (nEntries * 2)] << 8);
      }
      // Fall into next case
    case kExitSpecial:      // Apply special fixups [read: hacks]
    case kExitReplaceOffset1:
      if ((nEntries != 256) && InStateZero(st, Tbl)) {
        // For space-optimized table, we need multiples of 256 bytes
        // in state0 and multiples of nEntries in other states
        offset += (unsigned char)Tbl[c + (256 * 1)];
      } else {
        offset += (unsigned char)Tbl[c + (nEntries * 1)];
      }
      {
        const RemapEntry* re = &st->remap_base[offset];
        int del_len = re->delete_bytes & ~kReplaceAndResumeFlag;
        int add_len = re->add_bytes & ~kHtmlPlaintextFlag;

        // Special-case non-HTML replacement of five sensitive entities
        //   &quot; &amp; &apos; &lt; &gt;
        //   0022   0026  0027   003c 003e
        // A replacement creating one of these is expressed as a pair of
        // entries, one for HTML output and one for plaintext output.
        // The first of the pair has the high bit of add_bytes set.
        if (re->add_bytes & kHtmlPlaintextFlag) {
          // Use this entry for plain text
          if (!is_plain_text) {
            // Use very next entry for HTML text (same back/delete length)
            re = &st->remap_base[offset + 1];
            add_len = re->add_bytes & ~kHtmlPlaintextFlag;
          }
        }

        int string_offset = re->bytes_offset;
        // After the replacement, need (dstlimit - newdst) >= (srclimit - src)
        uint8* newdst = dst - del_len + add_len;
        if ((dstlimit - newdst) < (srclimit - src)) {
          // Won't fit; don't do the replacement. Caller may realloc and retry
          e = kExitDstSpaceFull;
          break;    // exit, backing up over this char for later retry
        }
        dst -= del_len;
        memcpy(dst, &st->remap_string[string_offset], add_len);
        dst += add_len;
        total_changed++;
        if (offsetmap != NULL) {
          if (add_len > del_len) {
            offsetmap->Copy(src - copystart);
            offsetmap->Insert(add_len - del_len);
            copystart = src;
          } else if (add_len < del_len) {
            offsetmap->Copy(src - copystart + add_len - del_len);
            offsetmap->Delete(del_len - add_len);
            copystart = src;
          }
        }
        if (re->delete_bytes & kReplaceAndResumeFlag) {
          // There is a non-zero  target state at the end of the
          // replacement string
          e = st->remap_string[string_offset + add_len];
          Tbl = &Tbl_0[e << eshift];
          goto Do_state_table_newe;
        }
      }
      if (e == kExitRejectAlt) {break;}
      if (e != kExitSpecial) {goto Do_state_table;}

    // case kExitSpecial:      // Apply special fixups [read: hacks]
      // In this routine, do either UTF8CharToLower()
      //   fullwidth/halfwidth mapping or
      //   voiced mapping or
      //   semi-voiced mapping

      // First, do EXIT_REPLACE_OFFSET1 action (above)
      // Second: do additional code fixup
      {
        int srcdel = DoSpecialFixup(c, &src, srclimit, &dst, dstlimit);
        if (offsetmap != NULL) {
          if (srcdel != 0) {
            offsetmap->Copy(src - copystart - srcdel);
            offsetmap->Delete(srcdel);
            copystart = src;
          }
        }
      }
      goto Do_state_table;

    case kExitIllegalStructure:   // structurally illegal byte; quit
    case kExitReject:             // NUL or illegal code encountered; quit
    case kExitRejectAlt:          // Apply replacement, then exit
    default:                      // and all other exits
      break;
    }   // End switch (e)

    // Exit possibilities:
    //  Some other exit code, state0, back up one byte exactly
    //  Some other exit code, !state0, back up over last char

    // Back up over exactly one byte of rejected/illegal UTF-8 character
    src--;
    dst--;
    // Back up more if needed
    if (!InStateZero(st, Tbl)) {
      do {src--;dst--;} while ((src > isrc) && ((src[0] & 0xc0) == 0x80));
    }
  } else if (!InStateZero(st, Tbl)) {
    // src >= srclimit, !state0
    // Back up over truncated UTF-8 character
    e = kExitIllegalStructure;
    do {src--; dst--;} while ((src > isrc) && ((src[0] & 0xc0) == 0x80));
  } else {
    // src >= srclimit, state0
    // Normal termination, source fully consumed
    e = kExitOK;
  }

  if (offsetmap != NULL) {
    if (src > copystart) {
      offsetmap->Copy(src - copystart);
      copystart = src;
    }
  }

  // Possible return values here:
  //  kExitDstSpaceFull         caller may realloc and retry from middle
  //  kExitIllegalStructure     caller my overwrite/truncate
  //  kExitOK                   all done and happy
  //  kExitReject               caller may overwrite/truncate
  //  kExitDoAgain              LOOP NOT DONE; caller must retry from middle
  //                            (may do fast ASCII loop first)
  //  kExitPlaceholder          -unused-
  //  kExitNone                 -unused-
  *bytes_consumed = src - isrc;
  *bytes_filled = dst - odst;
  *chars_changed = total_changed;
  return e;
}

// TwoByte versions are needed for tables > 240 states, such
// as the table for full Unicode 4.1 canonical + compatibility mapping

// Scan a UTF-8 stringpiece based on state table with two-byte entries,
//   copying to output stringpiece
//   and doing text replacements.
// DO NOT CALL DIRECTLY. Use UTF8GenericReplace() below
//   Needs caller to loop on kExitDoAgain
static int UTF8GenericReplaceInternalTwoByte(const UTF8ReplaceObj_2* st,
                    const StringPiece& istr,
                    StringPiece& ostr,
                    bool is_plain_text,
                    int* bytes_consumed,
                    int* bytes_filled,
                    int* chars_changed,
                    OffsetMap* offsetmap) {
  int eshift = st->entry_shift;
  int nEntries = (1 << eshift);       // 64 or 256 entries per state
  const uint8* isrc = reinterpret_cast<const uint8*>(istr.data());
  const int ilen = istr.length();
  const uint8* copystart = isrc;
  const uint8* src = isrc;
  const uint8* srclimit = src + ilen;
  *bytes_consumed = 0;
  *bytes_filled = 0;
  *chars_changed = 0;

  const uint8* odst = reinterpret_cast<const uint8*>(ostr.data());
  const int olen = ostr.length();
  uint8* dst = const_cast<uint8*>(odst);
  uint8* dstlimit = dst + olen;

  *chars_changed = 0;

  int total_changed = 0;

  int src_lll = srclimit - src;
  int dst_lll = dstlimit - dst;


  // Invariant condition during replacements:
  //  remaining dst size >= remaining src size
  if ((dstlimit - dst) < (srclimit - src)) {
    if (offsetmap != NULL) {
      offsetmap->Copy(src - copystart);
      copystart = src;
    }
    return kExitDstSpaceFull_2;
  }
  const unsigned short* Tbl_0 = &st->state_table[st->state0];

 Do_state_table_2:
  // Do state-table scan, copying as we go
  const unsigned short* Tbl = Tbl_0;
  int e = 0;
  uint8 c = 0;

 Do_state_table_newe_2:

  //----------------------------
  while (src < srclimit) {
    c = *src;
    e = Tbl[c];
    *dst = c;
    src++;
    dst++;
    if (e >= kExitIllegalStructure_2) {break;}
    Tbl = &Tbl_0[e << eshift];
  }
  //----------------------------
  src_lll = src - isrc;
  dst_lll = dst - odst;

  // Exit possibilities:
  //  Replacement code, do the replacement and loop
  //  Some other exit code, state0, back up one byte exactly
  //  Some other exit code, !state0, back up over last char
  //  source consumed, state0, exit OK
  //  source consumed, !state0, back up over partial char
  // For illegal byte in state0, avoid backup up over PREVIOUS char
  // For truncated last char, back up to beginning of it

  if (e >= kExitIllegalStructure_2) {
    // Switch on exit code; most loop back to top
    int offset = 0;
    switch (e) {
    // These all make the output string the same size or shorter
    // No checking needed
    case kExitReplace31_2:    // del 2, add 1 bytes to change
      dst -= 2;
      if (offsetmap != NULL) {
        offsetmap->Copy(src - copystart - 2);
        offsetmap->Delete(2);
        copystart = src;
      }
      dst[-1] = (unsigned char)(Tbl[c + (nEntries * 1)] & 0xff);
      total_changed++;
      goto Do_state_table_2;
    case kExitReplace32_2:    // del 3, add 2 bytes to change
      dst--;
      if (offsetmap != NULL) {
        offsetmap->Copy(src - copystart - 1);
        offsetmap->Delete(1);
        copystart = src;
      }
      dst[-2] = (unsigned char)(Tbl[c + (nEntries * 1)] >> 8 & 0xff);
      dst[-1] = (unsigned char)(Tbl[c + (nEntries * 1)] & 0xff);
      total_changed++;
      goto Do_state_table_2;
    case kExitReplace21_2:    // del 2, add 1 bytes to change
      dst--;
      if (offsetmap != NULL) {
        offsetmap->Copy(src - copystart - 1);
        offsetmap->Delete(1);
        copystart = src;
      }
      dst[-1] = (unsigned char)(Tbl[c + (nEntries * 1)] & 0xff);
      total_changed++;
      goto Do_state_table_2;
    case kExitReplace3_2:    // update 3 bytes to change
      dst[-3] = (unsigned char)(Tbl[c + (nEntries * 2)] & 0xff);
      // Fall into next case
    case kExitReplace2_2:    // update 2 bytes to change
      dst[-2] = (unsigned char)(Tbl[c + (nEntries * 1)] >> 8 & 0xff);
      // Fall into next case
    case kExitReplace1_2:    // update 1 byte to change
      dst[-1] = (unsigned char)(Tbl[c + (nEntries * 1)] & 0xff);
      total_changed++;
      goto Do_state_table_2;
    case kExitReplace1S0_2:     // update 1 byte to change, 256-entry state
      dst[-1] = (unsigned char)(Tbl[c + (256 * 1)] & 0xff);
      total_changed++;
      goto Do_state_table_2;
    // These can make the output string longer than the input
    case kExitReplaceOffset2_2:
      if ((nEntries != 256) && InStateZero_2(st, Tbl)) {
        // For space-optimized table, we need multiples of 256 bytes
        // in state0 and multiples of nEntries in other states
        offset += ((unsigned char)(Tbl[c + (256 * 1)] >> 8 & 0xff) << 8);
      } else {
        offset += ((unsigned char)(Tbl[c + (nEntries * 1)] >> 8 & 0xff) << 8);
      }
      // Fall into next case
    case kExitReplaceOffset1_2:
      if ((nEntries != 256) && InStateZero_2(st, Tbl)) {
        // For space-optimized table, we need multiples of 256 bytes
        // in state0 and multiples of nEntries in other states
        offset += (unsigned char)(Tbl[c + (256 * 1)] & 0xff);
      } else {
        offset += (unsigned char)(Tbl[c + (nEntries * 1)] & 0xff);
      }
      {
        const RemapEntry* re = &st->remap_base[offset];
        int del_len = re->delete_bytes & ~kReplaceAndResumeFlag;
        int add_len = re->add_bytes & ~kHtmlPlaintextFlag;
        // Special-case non-HTML replacement of five sensitive entities
        //   &quot; &amp; &apos; &lt; &gt;
        //   0022   0026  0027   003c 003e
        // A replacement creating one of these is expressed as a pair of
        // entries, one for HTML output and one for plaintext output.
        // The first of the pair has the high bit of add_bytes set.
        if (re->add_bytes & kHtmlPlaintextFlag) {
          // Use this entry for plain text
          if (!is_plain_text) {
            // Use very next entry for HTML text (same back/delete length)
            re = &st->remap_base[offset + 1];
            add_len = re->add_bytes & ~kHtmlPlaintextFlag;
          }
        }

        // After the replacement, need (dstlimit - dst) >= (srclimit - src)
        int string_offset = re->bytes_offset;
        // After the replacement, need (dstlimit - newdst) >= (srclimit - src)
        uint8* newdst = dst - del_len + add_len;
        if ((dstlimit - newdst) < (srclimit - src)) {
          // Won't fit; don't do the replacement. Caller may realloc and retry
          e = kExitDstSpaceFull_2;
          break;    // exit, backing up over this char for later retry
        }
        dst -= del_len;
        memcpy(dst, &st->remap_string[string_offset], add_len);
        dst += add_len;
        if (offsetmap != NULL) {
          if (add_len > del_len) {
            offsetmap->Copy(src - copystart);
            offsetmap->Insert(add_len - del_len);
            copystart = src;
          } else if (add_len < del_len) {
            offsetmap->Copy(src - copystart + add_len - del_len);
            offsetmap->Delete(del_len - add_len);
            copystart = src;
          }
        }
        if (re->delete_bytes & kReplaceAndResumeFlag) {
          // There is a two-byte non-zero target state at the end of the
          // replacement string
          uint8 c1 = st->remap_string[string_offset + add_len];
          uint8 c2 = st->remap_string[string_offset + add_len + 1];
          e = (c1 << 8) | c2;
          Tbl = &Tbl_0[e << eshift];
          total_changed++;
          goto Do_state_table_newe_2;
        }
      }
      total_changed++;
      if (e == kExitRejectAlt_2) {break;}
      goto Do_state_table_2;

    case kExitSpecial_2:           // NO special fixups [read: hacks]
    case kExitIllegalStructure_2:  // structurally illegal byte; quit
    case kExitReject_2:            // NUL or illegal code encountered; quit
                                   // and all other exits
    default:
      break;
    }   // End switch (e)

    // Exit possibilities:
    //  Some other exit code, state0, back up one byte exactly
    //  Some other exit code, !state0, back up over last char

    // Back up over exactly one byte of rejected/illegal UTF-8 character
    src--;
    dst--;
    // Back up more if needed
    if (!InStateZero_2(st, Tbl)) {
      do {src--;dst--;} while ((src > isrc) && ((src[0] & 0xc0) == 0x80));
    }
  } else if (!InStateZero_2(st, Tbl)) {
    // src >= srclimit, !state0
    // Back up over truncated UTF-8 character
    e = kExitIllegalStructure_2;

    do {src--; dst--;} while ((src > isrc) && ((src[0] & 0xc0) == 0x80));
  } else {
    // src >= srclimit, state0
    // Normal termination, source fully consumed
    e = kExitOK_2;
  }

  if (offsetmap != NULL) {
    if (src > copystart) {
      offsetmap->Copy(src - copystart);
      copystart = src;
    }
  }


  // Possible return values here:
  //  kExitDstSpaceFull_2         caller may realloc and retry from middle
  //  kExitIllegalStructure_2     caller my overwrite/truncate
  //  kExitOK_2                   all done and happy
  //  kExitReject_2               caller may overwrite/truncate
  //  kExitDoAgain_2              LOOP NOT DONE; caller must retry from middle
  //                            (may do fast ASCII loop first)
  //  kExitPlaceholder_2          -unused-
  //  kExitNone_2                 -unused-
  *bytes_consumed = src - isrc;
  *bytes_filled = dst - odst;
  *chars_changed = total_changed;
  return e;
}


// Scan a UTF-8 stringpiece based on state table, copying to output stringpiece
//   and doing text replacements.
// Also writes an optional OffsetMap. Pass NULL to skip writing one.
// Always scan complete UTF-8 characters
// Set number of bytes consumed from input, number filled to output.
// Return reason for exiting
int UTF8GenericReplace(const UTF8ReplaceObj* st,
                    const StringPiece& istr,
                    StringPiece& ostr,
                    bool is_plain_text,
                    int* bytes_consumed,
                    int* bytes_filled,
                    int* chars_changed,
                    OffsetMap* offsetmap) {
  StringPiece local_istr(istr.data(), istr.length());
  StringPiece local_ostr(ostr.data(), ostr.length());
  int total_consumed = 0;
  int total_filled = 0;
  int total_changed = 0;
  int local_bytes_consumed, local_bytes_filled, local_chars_changed;
  int e;
  do {
    e = UTF8GenericReplaceInternal(st,
                    local_istr, local_ostr, is_plain_text,
                    &local_bytes_consumed, &local_bytes_filled,
                    &local_chars_changed,
                    offsetmap);
    local_istr.remove_prefix(local_bytes_consumed);
    local_ostr.remove_prefix(local_bytes_filled);
    total_consumed += local_bytes_consumed;
    total_filled += local_bytes_filled;
    total_changed += local_chars_changed;
  } while ( e == kExitDoAgain );
  *bytes_consumed = total_consumed;
  *bytes_filled = total_filled;
  *chars_changed = total_changed;
  return e;
}

// Older version without offsetmap
int UTF8GenericReplace(const UTF8ReplaceObj* st,
                    const StringPiece& istr,
                    StringPiece& ostr,
                    bool is_plain_text,
                    int* bytes_consumed,
                    int* bytes_filled,
                    int* chars_changed) {
  return UTF8GenericReplace(st,
                    istr,
                    ostr,
                    is_plain_text,
                    bytes_consumed,
                    bytes_filled,
                    chars_changed,
                    NULL);
}

// Older version without is_plain_text or offsetmap
int UTF8GenericReplace(const UTF8ReplaceObj* st,
                    const StringPiece& istr,
                    StringPiece& ostr,
                    int* bytes_consumed,
                    int* bytes_filled,
                    int* chars_changed) {
  bool is_plain_text = false;
  return UTF8GenericReplace(st,
                    istr,
                    ostr,
                    is_plain_text,
                    bytes_consumed,
                    bytes_filled,
                    chars_changed,
                    NULL);
}

// Scan a UTF-8 stringpiece based on state table with two-byte entries,
//   copying to output stringpiece
//   and doing text replacements.
// Also writes an optional OffsetMap. Pass NULL to skip writing one.
// Always scan complete UTF-8 characters
// Set number of bytes consumed from input, number filled to output.
// Return reason for exiting
int UTF8GenericReplaceTwoByte(const UTF8ReplaceObj_2* st,
                    const StringPiece& istr,
                    StringPiece& ostr,
                    bool is_plain_text,
                    int* bytes_consumed,
                    int* bytes_filled,
                    int* chars_changed,
                    OffsetMap* offsetmap) {
  StringPiece local_istr(istr.data(), istr.length());
  StringPiece local_ostr(ostr.data(), ostr.length());
  int total_consumed = 0;
  int total_filled = 0;
  int total_changed = 0;
  int local_bytes_consumed, local_bytes_filled, local_chars_changed;
  int e;
  do {
    e = UTF8GenericReplaceInternalTwoByte(st,
                    local_istr, local_ostr, is_plain_text,
                    &local_bytes_consumed,
                    &local_bytes_filled,
                    &local_chars_changed,
                    offsetmap);
    local_istr.remove_prefix(local_bytes_consumed);
    local_ostr.remove_prefix(local_bytes_filled);
    total_consumed += local_bytes_consumed;
    total_filled += local_bytes_filled;
    total_changed += local_chars_changed;
  } while ( e == kExitDoAgain_2 );
  *bytes_consumed = total_consumed;
  *bytes_filled = total_filled;
  *chars_changed = total_changed;

  return e - kExitOK_2 + kExitOK;
}

// Older version without offsetmap
int UTF8GenericReplaceTwoByte(const UTF8ReplaceObj_2* st,
                    const StringPiece& istr,
                    StringPiece& ostr,
                    bool is_plain_text,
                    int* bytes_consumed,
                    int* bytes_filled,
                    int* chars_changed) {
  return UTF8GenericReplaceTwoByte(st,
                    istr,
                    ostr,
                    is_plain_text,
                    bytes_consumed,
                    bytes_filled,
                    chars_changed,
                    NULL);
}

// Older version without is_plain_text or offsetmap
int UTF8GenericReplaceTwoByte(const UTF8ReplaceObj_2* st,
                    const StringPiece& istr,
                    StringPiece& ostr,
                    int* bytes_consumed,
                    int* bytes_filled,
                    int* chars_changed) {
  bool is_plain_text = false;
  return UTF8GenericReplaceTwoByte(st,
                    istr,
                    ostr,
                    is_plain_text,
                    bytes_consumed,
                    bytes_filled,
                    chars_changed,
                    NULL);
}



// Adjust a stringpiece to encompass complete UTF-8 characters.
// The data pointer will be increased by 0..3 bytes to get to a character
// boundary, and the length will then be decreased by 0..3 bytes
// to encompass the last complete character.
void UTF8TrimToChars(StringPiece* istr) {
  const char* src = istr->data();
  int len = istr->length();
  // Exit if empty string
  if (len == 0) {
    return;
  }

  // Exit on simple, common case
  if ( ((src[0] & 0xc0) != 0x80) &&
       (static_cast<signed char>(src[len - 1]) >= 0) ) {
    // First byte is not a continuation and last byte is 7-bit ASCII -- done
    return;
  }

  // Adjust the back end, len > 0
  const char* srclimit = src + len;
  // Backscan over any ending continuation bytes to find last char start
  const char* s = srclimit - 1;         // Last byte of the string
  while ((src <= s) && ((*s & 0xc0) == 0x80)) {
    s--;
  }
  // Include entire last char if it fits
  if (src <= s) {
    int last_char_len = UTF8OneCharLen(s);
    if (s + last_char_len <= srclimit) {
      // Last char fits, so include it, else exclude it
      s += last_char_len;
    }
  }
  if (s != srclimit) {
    // s is one byte beyond the last full character, if any
    istr->remove_suffix(srclimit - s);
    // Exit if now empty string
    if (istr->length() == 0) {
      return;
    }
  }

  // Adjust the front end, len > 0
  len = istr->length();
  srclimit = src + len;
  s = src;                            // First byte of the string
  // Scan over any beginning continuation bytes to find first char start
  while ((s < srclimit) && ((*s & 0xc0) == 0x80)) {
    s++;
  }
  if (s != src) {
    // s is at the first full character, if any
    istr->remove_prefix(s - src);
  }
}

}       // End namespace CLD2