-
-
Notifications
You must be signed in to change notification settings - Fork 714
/
utext.cpp
2880 lines (2494 loc) · 96.9 KB
/
utext.cpp
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
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/*
*******************************************************************************
*
* Copyright (C) 2005-2016, International Business Machines
* Corporation and others. All Rights Reserved.
*
*******************************************************************************
* file name: utext.cpp
* encoding: UTF-8
* tab size: 8 (not used)
* indentation:4
*
* created on: 2005apr12
* created by: Markus W. Scherer
*/
#include <cstddef>
#include "unicode/utypes.h"
#include "unicode/ustring.h"
#include "unicode/unistr.h"
#include "unicode/chariter.h"
#include "unicode/utext.h"
#include "unicode/utf.h"
#include "unicode/utf8.h"
#include "unicode/utf16.h"
#include "ustr_imp.h"
#include "cmemory.h"
#include "cstring.h"
#include "uassert.h"
#include "putilimp.h"
U_NAMESPACE_USE
#define I32_FLAG(bitIndex) ((int32_t)1<<(bitIndex))
static UBool
utext_access(UText *ut, int64_t index, UBool forward) {
return ut->pFuncs->access(ut, index, forward);
}
U_CAPI UBool U_EXPORT2
utext_moveIndex32(UText *ut, int32_t delta) {
UChar32 c;
if (delta > 0) {
do {
if(ut->chunkOffset>=ut->chunkLength && !utext_access(ut, ut->chunkNativeLimit, true)) {
return false;
}
c = ut->chunkContents[ut->chunkOffset];
if (U16_IS_SURROGATE(c)) {
c = utext_next32(ut);
if (c == U_SENTINEL) {
return false;
}
} else {
ut->chunkOffset++;
}
} while(--delta>0);
} else if (delta<0) {
do {
if(ut->chunkOffset<=0 && !utext_access(ut, ut->chunkNativeStart, false)) {
return false;
}
c = ut->chunkContents[ut->chunkOffset-1];
if (U16_IS_SURROGATE(c)) {
c = utext_previous32(ut);
if (c == U_SENTINEL) {
return false;
}
} else {
ut->chunkOffset--;
}
} while(++delta<0);
}
return true;
}
U_CAPI int64_t U_EXPORT2
utext_nativeLength(UText *ut) {
return ut->pFuncs->nativeLength(ut);
}
U_CAPI UBool U_EXPORT2
utext_isLengthExpensive(const UText *ut) {
UBool r = (ut->providerProperties & I32_FLAG(UTEXT_PROVIDER_LENGTH_IS_EXPENSIVE)) != 0;
return r;
}
U_CAPI int64_t U_EXPORT2
utext_getNativeIndex(const UText *ut) {
if(ut->chunkOffset <= ut->nativeIndexingLimit) {
return ut->chunkNativeStart+ut->chunkOffset;
} else {
return ut->pFuncs->mapOffsetToNative(ut);
}
}
U_CAPI void U_EXPORT2
utext_setNativeIndex(UText *ut, int64_t index) {
if(index<ut->chunkNativeStart || index>=ut->chunkNativeLimit) {
// The desired position is outside of the current chunk.
// Access the new position. Assume a forward iteration from here,
// which will also be optimimum for a single random access.
// Reverse iterations may suffer slightly.
ut->pFuncs->access(ut, index, true);
} else if((int32_t)(index - ut->chunkNativeStart) <= ut->nativeIndexingLimit) {
// utf-16 indexing.
ut->chunkOffset=(int32_t)(index-ut->chunkNativeStart);
} else {
ut->chunkOffset=ut->pFuncs->mapNativeIndexToUTF16(ut, index);
}
// The convention is that the index must always be on a code point boundary.
// Adjust the index position if it is in the middle of a surrogate pair.
if (ut->chunkOffset<ut->chunkLength) {
char16_t c= ut->chunkContents[ut->chunkOffset];
if (U16_IS_TRAIL(c)) {
if (ut->chunkOffset==0) {
ut->pFuncs->access(ut, ut->chunkNativeStart, false);
}
if (ut->chunkOffset>0) {
char16_t lead = ut->chunkContents[ut->chunkOffset-1];
if (U16_IS_LEAD(lead)) {
ut->chunkOffset--;
}
}
}
}
}
U_CAPI int64_t U_EXPORT2
utext_getPreviousNativeIndex(UText *ut) {
//
// Fast-path the common case.
// Common means current position is not at the beginning of a chunk
// and the preceding character is not supplementary.
//
int32_t i = ut->chunkOffset - 1;
int64_t result;
if (i >= 0) {
char16_t c = ut->chunkContents[i];
if (U16_IS_TRAIL(c) == false) {
if (i <= ut->nativeIndexingLimit) {
result = ut->chunkNativeStart + i;
} else {
ut->chunkOffset = i;
result = ut->pFuncs->mapOffsetToNative(ut);
ut->chunkOffset++;
}
return result;
}
}
// If at the start of text, simply return 0.
if (ut->chunkOffset==0 && ut->chunkNativeStart==0) {
return 0;
}
// Harder, less common cases. We are at a chunk boundary, or on a surrogate.
// Keep it simple, use other functions to handle the edges.
//
utext_previous32(ut);
result = UTEXT_GETNATIVEINDEX(ut);
utext_next32(ut);
return result;
}
//
// utext_current32. Get the UChar32 at the current position.
// UText iteration position is always on a code point boundary,
// never on the trail half of a surrogate pair.
//
U_CAPI UChar32 U_EXPORT2
utext_current32(UText *ut) {
UChar32 c;
if (ut->chunkOffset==ut->chunkLength) {
// Current position is just off the end of the chunk.
if (ut->pFuncs->access(ut, ut->chunkNativeLimit, true) == false) {
// Off the end of the text.
return U_SENTINEL;
}
}
c = ut->chunkContents[ut->chunkOffset];
if (U16_IS_LEAD(c) == false) {
// Normal, non-supplementary case.
return c;
}
//
// Possible supplementary char.
//
UChar32 trail = 0;
UChar32 supplementaryC = c;
if ((ut->chunkOffset+1) < ut->chunkLength) {
// The trail surrogate is in the same chunk.
trail = ut->chunkContents[ut->chunkOffset+1];
} else {
// The trail surrogate is in a different chunk.
// Because we must maintain the iteration position, we need to switch forward
// into the new chunk, get the trail surrogate, then revert the chunk back to the
// original one.
// An edge case to be careful of: the entire text may end with an unpaired
// leading surrogate. The attempt to access the trail will fail, but
// the original position before the unpaired lead still needs to be restored.
int64_t nativePosition = ut->chunkNativeLimit;
if (ut->pFuncs->access(ut, nativePosition, true)) {
trail = ut->chunkContents[ut->chunkOffset];
}
UBool r = ut->pFuncs->access(ut, nativePosition, false); // reverse iteration flag loads preceding chunk
U_ASSERT(r);
// Here we need to restore chunkOffset since the access functions were called with
// chunkNativeLimit but that is not where we were (we were 1 code unit before the
// limit). Restoring was originally added in ICU-4669 but did not support access
// functions that changed the chunk size, the following does.
ut->chunkOffset = ut->chunkLength - 1;
if(!r) {
return U_SENTINEL;
}
}
if (U16_IS_TRAIL(trail)) {
supplementaryC = U16_GET_SUPPLEMENTARY(c, trail);
}
return supplementaryC;
}
U_CAPI UChar32 U_EXPORT2
utext_char32At(UText *ut, int64_t nativeIndex) {
UChar32 c = U_SENTINEL;
// Fast path the common case.
if (nativeIndex>=ut->chunkNativeStart && nativeIndex < ut->chunkNativeStart + ut->nativeIndexingLimit) {
ut->chunkOffset = (int32_t)(nativeIndex - ut->chunkNativeStart);
c = ut->chunkContents[ut->chunkOffset];
if (U16_IS_SURROGATE(c) == false) {
return c;
}
}
utext_setNativeIndex(ut, nativeIndex);
if (nativeIndex>=ut->chunkNativeStart && ut->chunkOffset<ut->chunkLength) {
c = ut->chunkContents[ut->chunkOffset];
if (U16_IS_SURROGATE(c)) {
// For surrogates, let current32() deal with the complications
// of supplementaries that may span chunk boundaries.
c = utext_current32(ut);
}
}
return c;
}
U_CAPI UChar32 U_EXPORT2
utext_next32(UText *ut) {
UChar32 c;
if (ut->chunkOffset >= ut->chunkLength) {
if (ut->pFuncs->access(ut, ut->chunkNativeLimit, true) == false) {
return U_SENTINEL;
}
}
c = ut->chunkContents[ut->chunkOffset++];
if (U16_IS_LEAD(c) == false) {
// Normal case, not supplementary.
// (A trail surrogate seen here is just returned as is, as a surrogate value.
// It cannot be part of a pair.)
return c;
}
if (ut->chunkOffset >= ut->chunkLength) {
if (ut->pFuncs->access(ut, ut->chunkNativeLimit, true) == false) {
// c is an unpaired lead surrogate at the end of the text.
// return it as it is.
return c;
}
}
UChar32 trail = ut->chunkContents[ut->chunkOffset];
if (U16_IS_TRAIL(trail) == false) {
// c was an unpaired lead surrogate, not at the end of the text.
// return it as it is (unpaired). Iteration position is on the
// following character, possibly in the next chunk, where the
// trail surrogate would have been if it had existed.
return c;
}
UChar32 supplementary = U16_GET_SUPPLEMENTARY(c, trail);
ut->chunkOffset++; // move iteration position over the trail surrogate.
return supplementary;
}
U_CAPI UChar32 U_EXPORT2
utext_previous32(UText *ut) {
UChar32 c;
if (ut->chunkOffset <= 0) {
if (ut->pFuncs->access(ut, ut->chunkNativeStart, false) == false) {
return U_SENTINEL;
}
}
ut->chunkOffset--;
c = ut->chunkContents[ut->chunkOffset];
if (U16_IS_TRAIL(c) == false) {
// Normal case, not supplementary.
// (A lead surrogate seen here is just returned as is, as a surrogate value.
// It cannot be part of a pair.)
return c;
}
if (ut->chunkOffset <= 0) {
if (ut->pFuncs->access(ut, ut->chunkNativeStart, false) == false) {
// c is an unpaired trail surrogate at the start of the text.
// return it as it is.
return c;
}
}
UChar32 lead = ut->chunkContents[ut->chunkOffset-1];
if (U16_IS_LEAD(lead) == false) {
// c was an unpaired trail surrogate, not at the end of the text.
// return it as it is (unpaired). Iteration position is at c
return c;
}
UChar32 supplementary = U16_GET_SUPPLEMENTARY(lead, c);
ut->chunkOffset--; // move iteration position over the lead surrogate.
return supplementary;
}
U_CAPI UChar32 U_EXPORT2
utext_next32From(UText *ut, int64_t index) {
UChar32 c = U_SENTINEL;
if(index<ut->chunkNativeStart || index>=ut->chunkNativeLimit) {
// Desired position is outside of the current chunk.
if(!ut->pFuncs->access(ut, index, true)) {
// no chunk available here
return U_SENTINEL;
}
} else if (index - ut->chunkNativeStart <= (int64_t)ut->nativeIndexingLimit) {
// Desired position is in chunk, with direct 1:1 native to UTF16 indexing
ut->chunkOffset = (int32_t)(index - ut->chunkNativeStart);
} else {
// Desired position is in chunk, with non-UTF16 indexing.
ut->chunkOffset = ut->pFuncs->mapNativeIndexToUTF16(ut, index);
}
c = ut->chunkContents[ut->chunkOffset++];
if (U16_IS_SURROGATE(c)) {
// Surrogates. Many edge cases. Use other functions that already
// deal with the problems.
utext_setNativeIndex(ut, index);
c = utext_next32(ut);
}
return c;
}
U_CAPI UChar32 U_EXPORT2
utext_previous32From(UText *ut, int64_t index) {
//
// Return the character preceding the specified index.
// Leave the iteration position at the start of the character that was returned.
//
UChar32 cPrev; // The character preceding cCurr, which is what we will return.
// Address the chunk containing the position preceding the incoming index
// A tricky edge case:
// We try to test the requested native index against the chunkNativeStart to determine
// whether the character preceding the one at the index is in the current chunk.
// BUT, this test can fail with UTF-8 (or any other multibyte encoding), when the
// requested index is on something other than the first position of the first char.
//
if(index<=ut->chunkNativeStart || index>ut->chunkNativeLimit) {
// Requested native index is outside of the current chunk.
if(!ut->pFuncs->access(ut, index, false)) {
// no chunk available here
return U_SENTINEL;
}
} else if(index - ut->chunkNativeStart <= (int64_t)ut->nativeIndexingLimit) {
// Direct UTF-16 indexing.
ut->chunkOffset = (int32_t)(index - ut->chunkNativeStart);
} else {
ut->chunkOffset=ut->pFuncs->mapNativeIndexToUTF16(ut, index);
if (ut->chunkOffset==0 && !ut->pFuncs->access(ut, index, false)) {
// no chunk available here
return U_SENTINEL;
}
}
//
// Simple case with no surrogates.
//
ut->chunkOffset--;
cPrev = ut->chunkContents[ut->chunkOffset];
if (U16_IS_SURROGATE(cPrev)) {
// Possible supplementary. Many edge cases.
// Let other functions do the heavy lifting.
utext_setNativeIndex(ut, index);
cPrev = utext_previous32(ut);
}
return cPrev;
}
U_CAPI int32_t U_EXPORT2
utext_extract(UText *ut,
int64_t start, int64_t limit,
char16_t *dest, int32_t destCapacity,
UErrorCode *status) {
return ut->pFuncs->extract(ut, start, limit, dest, destCapacity, status);
}
U_CAPI UBool U_EXPORT2
utext_equals(const UText *a, const UText *b) {
if (a==nullptr || b==nullptr ||
a->magic != UTEXT_MAGIC ||
b->magic != UTEXT_MAGIC) {
// Null or invalid arguments don't compare equal to anything.
return false;
}
if (a->pFuncs != b->pFuncs) {
// Different types of text providers.
return false;
}
if (a->context != b->context) {
// Different sources (different strings)
return false;
}
if (utext_getNativeIndex(a) != utext_getNativeIndex(b)) {
// Different current position in the string.
return false;
}
return true;
}
U_CAPI UBool U_EXPORT2
utext_isWritable(const UText *ut)
{
UBool b = (ut->providerProperties & I32_FLAG(UTEXT_PROVIDER_WRITABLE)) != 0;
return b;
}
U_CAPI void U_EXPORT2
utext_freeze(UText *ut) {
// Zero out the WRITABLE flag.
ut->providerProperties &= ~(I32_FLAG(UTEXT_PROVIDER_WRITABLE));
}
U_CAPI UBool U_EXPORT2
utext_hasMetaData(const UText *ut)
{
UBool b = (ut->providerProperties & I32_FLAG(UTEXT_PROVIDER_HAS_META_DATA)) != 0;
return b;
}
U_CAPI int32_t U_EXPORT2
utext_replace(UText *ut,
int64_t nativeStart, int64_t nativeLimit,
const char16_t *replacementText, int32_t replacementLength,
UErrorCode *status)
{
if (U_FAILURE(*status)) {
return 0;
}
if ((ut->providerProperties & I32_FLAG(UTEXT_PROVIDER_WRITABLE)) == 0) {
*status = U_NO_WRITE_PERMISSION;
return 0;
}
int32_t i = ut->pFuncs->replace(ut, nativeStart, nativeLimit, replacementText, replacementLength, status);
return i;
}
U_CAPI void U_EXPORT2
utext_copy(UText *ut,
int64_t nativeStart, int64_t nativeLimit,
int64_t destIndex,
UBool move,
UErrorCode *status)
{
if (U_FAILURE(*status)) {
return;
}
if ((ut->providerProperties & I32_FLAG(UTEXT_PROVIDER_WRITABLE)) == 0) {
*status = U_NO_WRITE_PERMISSION;
return;
}
ut->pFuncs->copy(ut, nativeStart, nativeLimit, destIndex, move, status);
}
U_CAPI UText * U_EXPORT2
utext_clone(UText *dest, const UText *src, UBool deep, UBool readOnly, UErrorCode *status) {
if (U_FAILURE(*status)) {
return dest;
}
UText *result = src->pFuncs->clone(dest, src, deep, status);
if (U_FAILURE(*status)) {
return result;
}
if (result == nullptr) {
*status = U_MEMORY_ALLOCATION_ERROR;
return result;
}
if (readOnly) {
utext_freeze(result);
}
return result;
}
//------------------------------------------------------------------------------
//
// UText common functions implementation
//
//------------------------------------------------------------------------------
//
// UText.flags bit definitions
//
enum {
UTEXT_HEAP_ALLOCATED = 1, // 1 if ICU has allocated this UText struct on the heap.
// 0 if caller provided storage for the UText.
UTEXT_EXTRA_HEAP_ALLOCATED = 2, // 1 if ICU has allocated extra storage as a separate
// heap block.
// 0 if there is no separate allocation. Either no extra
// storage was requested, or it is appended to the end
// of the main UText storage.
UTEXT_OPEN = 4 // 1 if this UText is currently open
// 0 if this UText is not open.
};
//
// Extended form of a UText. The purpose is to aid in computing the total size required
// when a provider asks for a UText to be allocated with extra storage.
struct ExtendedUText {
UText ut;
std::max_align_t extension;
};
static const UText emptyText = UTEXT_INITIALIZER;
U_CAPI UText * U_EXPORT2
utext_setup(UText *ut, int32_t extraSpace, UErrorCode *status) {
if (U_FAILURE(*status)) {
return ut;
}
if (ut == nullptr) {
// We need to heap-allocate storage for the new UText
int32_t spaceRequired = sizeof(UText);
if (extraSpace > 0) {
spaceRequired = sizeof(ExtendedUText) + extraSpace - sizeof(std::max_align_t);
}
ut = (UText *)uprv_malloc(spaceRequired);
if (ut == nullptr) {
*status = U_MEMORY_ALLOCATION_ERROR;
return nullptr;
} else {
*ut = emptyText;
ut->flags |= UTEXT_HEAP_ALLOCATED;
if (spaceRequired>0) {
ut->extraSize = extraSpace;
ut->pExtra = &((ExtendedUText *)ut)->extension;
}
}
} else {
// We have been supplied with an already existing UText.
// Verify that it really appears to be a UText.
if (ut->magic != UTEXT_MAGIC) {
*status = U_ILLEGAL_ARGUMENT_ERROR;
return ut;
}
// If the ut is already open and there's a provider supplied close
// function, call it.
if ((ut->flags & UTEXT_OPEN) && ut->pFuncs->close != nullptr) {
ut->pFuncs->close(ut);
}
ut->flags &= ~UTEXT_OPEN;
// If extra space was requested by our caller, check whether
// sufficient already exists, and allocate new if needed.
if (extraSpace > ut->extraSize) {
// Need more space. If there is existing separately allocated space,
// delete it first, then allocate new space.
if (ut->flags & UTEXT_EXTRA_HEAP_ALLOCATED) {
uprv_free(ut->pExtra);
ut->extraSize = 0;
}
ut->pExtra = uprv_malloc(extraSpace);
if (ut->pExtra == nullptr) {
*status = U_MEMORY_ALLOCATION_ERROR;
} else {
ut->extraSize = extraSpace;
ut->flags |= UTEXT_EXTRA_HEAP_ALLOCATED;
}
}
}
if (U_SUCCESS(*status)) {
ut->flags |= UTEXT_OPEN;
// Initialize all remaining fields of the UText.
//
ut->context = nullptr;
ut->chunkContents = nullptr;
ut->p = nullptr;
ut->q = nullptr;
ut->r = nullptr;
ut->a = 0;
ut->b = 0;
ut->c = 0;
ut->chunkOffset = 0;
ut->chunkLength = 0;
ut->chunkNativeStart = 0;
ut->chunkNativeLimit = 0;
ut->nativeIndexingLimit = 0;
ut->providerProperties = 0;
ut->privA = 0;
ut->privB = 0;
ut->privC = 0;
ut->privP = nullptr;
if (ut->pExtra!=nullptr && ut->extraSize>0)
uprv_memset(ut->pExtra, 0, ut->extraSize);
}
return ut;
}
U_CAPI UText * U_EXPORT2
utext_close(UText *ut) {
if (ut==nullptr ||
ut->magic != UTEXT_MAGIC ||
(ut->flags & UTEXT_OPEN) == 0)
{
// The supplied ut is not an open UText.
// Do nothing.
return ut;
}
// If the provider gave us a close function, call it now.
// This will clean up anything allocated specifically by the provider.
if (ut->pFuncs->close != nullptr) {
ut->pFuncs->close(ut);
}
ut->flags &= ~UTEXT_OPEN;
// If we (the framework) allocated the UText or subsidiary storage,
// delete it.
if (ut->flags & UTEXT_EXTRA_HEAP_ALLOCATED) {
uprv_free(ut->pExtra);
ut->pExtra = nullptr;
ut->flags &= ~UTEXT_EXTRA_HEAP_ALLOCATED;
ut->extraSize = 0;
}
// Zero out function table of the closed UText. This is a defensive move,
// intended to cause applications that inadvertently use a closed
// utext to crash with null pointer errors.
ut->pFuncs = nullptr;
if (ut->flags & UTEXT_HEAP_ALLOCATED) {
// This UText was allocated by UText setup. We need to free it.
// Clear magic, so we can detect if the user messes up and immediately
// tries to reopen another UText using the deleted storage.
ut->magic = 0;
uprv_free(ut);
ut = nullptr;
}
return ut;
}
//
// invalidateChunk Reset a chunk to have no contents, so that the next call
// to access will cause new data to load.
// This is needed when copy/move/replace operate directly on the
// backing text, potentially putting it out of sync with the
// contents in the chunk.
//
static void
invalidateChunk(UText *ut) {
ut->chunkLength = 0;
ut->chunkNativeLimit = 0;
ut->chunkNativeStart = 0;
ut->chunkOffset = 0;
ut->nativeIndexingLimit = 0;
}
//
// pinIndex Do range pinning on a native index parameter.
// 64 bit pinning is done in place.
// 32 bit truncated result is returned as a convenience for
// use in providers that don't need 64 bits.
static int32_t
pinIndex(int64_t &index, int64_t limit) {
if (index<0) {
index = 0;
} else if (index > limit) {
index = limit;
}
return (int32_t)index;
}
U_CDECL_BEGIN
//
// Pointer relocation function,
// a utility used by shallow clone.
// Adjust a pointer that refers to something within one UText (the source)
// to refer to the same relative offset within a another UText (the target)
//
static void adjustPointer(UText *dest, const void **destPtr, const UText *src) {
// convert all pointers to (char *) so that byte address arithmetic will work.
char *dptr = (char *)*destPtr;
char *dUText = (char *)dest;
char *sUText = (char *)src;
if (dptr >= (char *)src->pExtra && dptr < ((char*)src->pExtra)+src->extraSize) {
// target ptr was to something within the src UText's pExtra storage.
// relocate it into the target UText's pExtra region.
*destPtr = ((char *)dest->pExtra) + (dptr - (char *)src->pExtra);
} else if (dptr>=sUText && dptr < sUText+src->sizeOfStruct) {
// target ptr was pointing to somewhere within the source UText itself.
// Move it to the same offset within the target UText.
*destPtr = dUText + (dptr-sUText);
}
}
//
// Clone. This is a generic copy-the-utext-by-value clone function that can be
// used as-is with some utext types, and as a helper by other clones.
//
static UText * U_CALLCONV
shallowTextClone(UText * dest, const UText * src, UErrorCode * status) {
if (U_FAILURE(*status)) {
return nullptr;
}
int32_t srcExtraSize = src->extraSize;
//
// Use the generic text_setup to allocate storage if required.
//
dest = utext_setup(dest, srcExtraSize, status);
if (U_FAILURE(*status)) {
return dest;
}
//
// flags (how the UText was allocated) and the pointer to the
// extra storage must retain the values in the cloned utext that
// were set up by utext_setup. Save them separately before
// copying the whole struct.
//
void *destExtra = dest->pExtra;
int32_t flags = dest->flags;
//
// Copy the whole UText struct by value.
// Any "Extra" storage is copied also.
//
int sizeToCopy = src->sizeOfStruct;
if (sizeToCopy > dest->sizeOfStruct) {
sizeToCopy = dest->sizeOfStruct;
}
uprv_memcpy(dest, src, sizeToCopy);
dest->pExtra = destExtra;
dest->flags = flags;
if (srcExtraSize > 0) {
uprv_memcpy(dest->pExtra, src->pExtra, srcExtraSize);
}
//
// Relocate any pointers in the target that refer to the UText itself
// to point to the cloned copy rather than the original source.
//
adjustPointer(dest, &dest->context, src);
adjustPointer(dest, &dest->p, src);
adjustPointer(dest, &dest->q, src);
adjustPointer(dest, &dest->r, src);
adjustPointer(dest, (const void **)&dest->chunkContents, src);
// The newly shallow-cloned UText does _not_ own the underlying storage for the text.
// (The source for the clone may or may not have owned the text.)
dest->providerProperties &= ~I32_FLAG(UTEXT_PROVIDER_OWNS_TEXT);
return dest;
}
U_CDECL_END
//------------------------------------------------------------------------------
//
// UText implementation for UTF-8 char * strings (read-only)
// Limitation: string length must be <= 0x7fffffff in length.
// (length must for in an int32_t variable)
//
// Use of UText data members:
// context pointer to UTF-8 string
// utext.b is the input string length (bytes).
// utext.c Length scanned so far in string
// (for optimizing finding length of zero terminated strings.)
// utext.p pointer to the current buffer
// utext.q pointer to the other buffer.
//
//------------------------------------------------------------------------------
// Chunk size.
// Must be less than 85 (256/3), because of byte mapping from char16_t indexes to native indexes.
// Worst case is three native bytes to one char16_t. (Supplemenaries are 4 native bytes
// to two UChars.)
// The longest illegal byte sequence treated as a single error (and converted to U+FFFD)
// is a three-byte sequence (truncated four-byte sequence).
//
enum { UTF8_TEXT_CHUNK_SIZE=32 };
//
// UTF8Buf Two of these structs will be set up in the UText's extra allocated space.
// Each contains the char16_t chunk buffer, the to and from native maps, and
// header info.
//
// because backwards iteration fills the buffers starting at the end and
// working towards the front, the filled part of the buffers may not begin
// at the start of the available storage for the buffers.
//
// Buffer size is one bigger than the specified UTF8_TEXT_CHUNK_SIZE to allow for
// the last character added being a supplementary, and thus requiring a surrogate
// pair. Doing this is simpler than checking for the edge case.
//
struct UTF8Buf {
int32_t bufNativeStart; // Native index of first char in char16_t buf
int32_t bufNativeLimit; // Native index following last char in buf.
int32_t bufStartIdx; // First filled position in buf.
int32_t bufLimitIdx; // Limit of filled range in buf.
int32_t bufNILimit; // Limit of native indexing part of buf
int32_t toUCharsMapStart; // Native index corresponding to
// mapToUChars[0].
// Set to bufNativeStart when filling forwards.
// Set to computed value when filling backwards.
char16_t buf[UTF8_TEXT_CHUNK_SIZE+4]; // The char16_t buffer. Requires one extra position beyond the
// the chunk size, to allow for surrogate at the end.
// Length must be identical to mapToNative array, below,
// because of the way indexing works when the array is
// filled backwards during a reverse iteration. Thus,
// the additional extra size.
uint8_t mapToNative[UTF8_TEXT_CHUNK_SIZE+4]; // map char16_t index in buf to
// native offset from bufNativeStart.
// Requires two extra slots,
// one for a supplementary starting in the last normal position,
// and one for an entry for the buffer limit position.
uint8_t mapToUChars[UTF8_TEXT_CHUNK_SIZE*3+6]; // Map native offset from bufNativeStart to
// corresponding offset in filled part of buf.
int32_t align;
};
U_CDECL_BEGIN
//
// utf8TextLength
//
// Get the length of the string. If we don't already know it,
// we'll need to scan for the trailing nul.
//
static int64_t U_CALLCONV
utf8TextLength(UText *ut) {
if (ut->b < 0) {
// Zero terminated string, and we haven't scanned to the end yet.
// Scan it now.
const char *r = (const char *)ut->context + ut->c;
while (*r != 0) {
r++;
}
if ((r - (const char *)ut->context) < 0x7fffffff) {
ut->b = (int32_t)(r - (const char *)ut->context);
} else {
// Actual string was bigger (more than 2 gig) than we
// can handle. Clip it to 2 GB.
ut->b = 0x7fffffff;
}
ut->providerProperties &= ~I32_FLAG(UTEXT_PROVIDER_LENGTH_IS_EXPENSIVE);
}
return ut->b;
}
static UBool U_CALLCONV
utf8TextAccess(UText *ut, int64_t index, UBool forward) {
//
// Apologies to those who are allergic to goto statements.
// Consider each goto to a labelled block to be the equivalent of
// call the named block as if it were a function();
// return;
//
const uint8_t *s8=(const uint8_t *)ut->context;
UTF8Buf *u8b = nullptr;
int32_t length = ut->b; // Length of original utf-8
int32_t ix= (int32_t)index; // Requested index, trimmed to 32 bits.
int32_t mapIndex = 0;
if (index<0) {
ix=0;
} else if (index > 0x7fffffff) {
// Strings with 64 bit lengths not supported by this UTF-8 provider.
ix = 0x7fffffff;
}
// Pin requested index to the string length.
if (ix>length) {
if (length>=0) {
ix=length;
} else if (ix>=ut->c) {
// Zero terminated string, and requested index is beyond
// the region that has already been scanned.
// Scan up to either the end of the string or to the
// requested position, whichever comes first.
while (ut->c<ix && s8[ut->c]!=0) {
ut->c++;
}
// TODO: support for null terminated string length > 32 bits.
if (s8[ut->c] == 0) {
// We just found the actual length of the string.
// Trim the requested index back to that.
ix = ut->c;
ut->b = ut->c;
length = ut->c;
ut->providerProperties &= ~I32_FLAG(UTEXT_PROVIDER_LENGTH_IS_EXPENSIVE);
}
}
}
//
// Dispatch to the appropriate action for a forward iteration request.
//
if (forward) {
if (ix==ut->chunkNativeLimit) {
// Check for normal sequential iteration cases first.
if (ix==length) {
// Just reached end of string
// Don't swap buffers, but do set the
// current buffer position.
ut->chunkOffset = ut->chunkLength;
return false;
} else {
// End of current buffer.
// check whether other buffer already has what we need.
UTF8Buf *altB = (UTF8Buf *)ut->q;
if (ix>=altB->bufNativeStart && ix<altB->bufNativeLimit) {
goto swapBuffers;
}