Repository navigation
Expand file tree
/
Copy pathsortedlines.cpp
More file actions
2069 lines (1932 loc) · 64.1 KB
/
Copy pathsortedlines.cpp
File metadata and controls
2069 lines (1932 loc) · 64.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
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
// © 2017 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
#include "sortedlines.h"
static int codePointCmp(const void *a, const void *b) {
return u_strcmp((*(Line **)a)->name, (*(Line **)b)->name);
}
SortedLines::SortedLines(const UnicodeSet &set, const UnicodeSet &excludeBounds, const StrengthProbe &probe,
UPrinter *logger, UPrinter *debug) :
toSort(NULL),
toSortCapacity(0),
lines(NULL),
size(0),
capacity(0),
repertoire(set),
excludeBounds(excludeBounds),
probe(probe),
first(NULL),
last(NULL),
logger(logger),
debug(debug),
contractionsTable(NULL),
duplicators(NULL),
maxExpansionPrefixSize(0),
wordSort(FALSE),
frenchSecondary(FALSE),
upperFirst(FALSE),
sortkeys(NULL),
sortkeyOffset(0)
{
memset(UB, 0, sizeof(UB));
int32_t i = 0;
for(i = 0; i < UCOL_OFF; i++) {
UB[i] = ∅
}
init();
}
SortedLines::~SortedLines()
{
delete[] lines;
if(sortkeys) {
delete[] sortkeys;
}
if(toSort) {
delete[] toSort;
}
if(contractionsTable) {
delete contractionsTable;
}
if(duplicators) {
delete duplicators;
}
}
void
SortedLines::getBounds(UErrorCode &status) {
// first sort through the set
debug->log(toString(), TRUE);
int32_t i = 0, j = 0;
UColAttributeValue strength = UCOL_OFF;
for(i = 0; i < size; i++) {
if(toSort[i]->strengthFromEmpty < strength) {
if(i && strength < UCOL_OFF) {
//u_strcpy(UB[strength], toSort[i-1]->name);
j = 1;
while(excludeBounds.contains(UnicodeString(toSort[i-j]->name, toSort[i-j]->len))) {
j++;
}
UB[strength] = toSort[i-j];
}
strength = toSort[i]->strengthFromEmpty;
if(strength == UCOL_PRIMARY) {
probe.SE = toSort[i]->name[0];
}
}
}
//u_strcpy(UB[strength], toSort[size-1]->name);
// a different solution for bounds: go from end and see if the guys on the top
// cause duplication for things
UChar dupch[] = { 0x0020, 0x0030, 0x0042, 0x0051, 0x0062, 0x0071, 0x0391, 0x0396, 0x03b1, 0x03b6 };
j = 1;
Line dup;
Line bound;
int32_t dups = 0;
while(j < size) {
dups = 0;
for(i = 0; i < sizeof(dupch)/sizeof(dupch[0]); i++) {
dup.setTo(dupch[i]);
dup.append(dupch[i]);
bound.setTo(dupch[i]);
bound.append(toSort[size-j]->name, toSort[size-j]->len);
if(probe.getStrength(dup, bound) >= UCOL_IDENTICAL) {
dups++;
}
}
if(dups == 0) {
break;
} else {
if(!duplicators) {
duplicators = new Hashtable();
}
duplicators->put(UnicodeString(toSort[size-j]->name, toSort[size-j]->len), &toSort[size-j], status);
debug->log(toSort[size-j]->toString());
debug->log(" is not good enough to be an upper bound\n");
j++;
}
}
if(j == size) {
debug->log("Oi! I'm hallucinating. Will use the first upper bound");
delete duplicators;
duplicators = NULL;
j = 1;
}
/*
j = 1;
while(excludeBounds.contains(UnicodeString(toSort[size-j]->name, toSort[size-j]->len))) {
j++;
}
*/
UB[strength] = toSort[size-j];
for(i = 0; i < UCOL_OFF; i++) {
if(UB[i]) {
//debug->log(UB[i], TRUE);
debug->log(UB[i]->toString(TRUE), TRUE);
}
}
}
// classifies repertoire according to the strength of their difference
// from the empty string
void
SortedLines::classifyRepertoire() {
UColAttributeValue strongestStrengthFromEmpty = UCOL_OFF;
int32_t lastChange = 0;
int32_t i = 0, j = 0;
while(i < size) // && probe.distanceFromEmptyString(*toSort[i]) > UCOL_PRIMARY)
{
toSort[i]->strengthFromEmpty = probe.distanceFromEmptyString(*toSort[i]);
if(toSort[i]->strengthFromEmpty < strongestStrengthFromEmpty) {
strongestStrengthFromEmpty = toSort[i]->strengthFromEmpty;
lastChange = i;
} else if (toSort[i]->strengthFromEmpty > strongestStrengthFromEmpty) {
// there is a problem in detection. Most probably a quaternary.
// why don't we try to interpolate
UColAttributeValue nextStrength = UCOL_OFF;
UColAttributeValue prevStrength = UCOL_OFF;
UColAttributeValue st = UCOL_OFF;
logger->log("Interpolating to get the distance from empty for Line ");
logger->log(toSort[i]->toString(TRUE), TRUE);
if(i) {
st = probe.getStrength(*toSort[i-1], *toSort[i]);
if(st == UCOL_OFF) {
logger->log("Cannot deduce distance from empty using previous element. Something is very wrong! Line:");
logger->log(toSort[i]->toString(TRUE), TRUE);
} else if(st == UCOL_IDENTICAL || st >= toSort[i-1]->strengthFromEmpty) {
prevStrength = toSort[i-1]->strengthFromEmpty;
} else if(st < toSort[i-1]->strengthFromEmpty) {
prevStrength = st;
}
toSort[i]->strengthFromEmpty = prevStrength;
}
if(i < size-2) {
toSort[i+1]->strengthFromEmpty = probe.distanceFromEmptyString(*toSort[i+1]);
st = probe.getStrength(*toSort[i+1], *toSort[i]);
if(st == UCOL_OFF) {
logger->log("Cannot deduce distance from empty using next element. Something is very wrong! Line:");
logger->log(toSort[i]->toString(TRUE), TRUE);
} else if(st == UCOL_IDENTICAL || st < toSort[i+1]->strengthFromEmpty) {
nextStrength = toSort[i+1]->strengthFromEmpty;
} else if(st >= toSort[i+1]->strengthFromEmpty) {
nextStrength = st;
}
if(i) {
if(prevStrength != nextStrength) {
logger->log("Inconsistent results from interpolation! Results will most likely be wrong\n");
}
}
toSort[i]->strengthFromEmpty = nextStrength;
}
/*
UColAttributeValue problemStrength = UCOL_PRIMARY;
for(j = lastChange; j < i ; j++) {
if(toSort[j]->strength > problemStrength) {
problemStrength = toSort[j]->strength;
}
}
for(j = lastChange; j < i ; j++) {
toSort[j]->strengthFromEmpty = problemStrength;
}
strongestStrengthFromEmpty = toSort[i]->strengthFromEmpty;
lastChange = i;
debug->log("Problem detected in distances from empty. Most probably word sort is on\n");
*/
wordSort = TRUE;
}
i++;
}
debug->log("Distances from empty string\n");
debug->log(toStringFromEmpty(), TRUE);
}
void
SortedLines::analyse(UErrorCode &status) {
frenchSecondary = probe.isFrenchSecondary(status);
if(U_FAILURE(status)) {
logger->log("Test for French secondary failed. Bailing out!\n");
return;
}
logger->log("French secondary value is %i\n", frenchSecondary, frenchSecondary);
upperFirst = probe.isUpperFirst(status);
if(U_FAILURE(status)) {
logger->log("Test for upper first failed. Bailing out!\n");
return;
}
logger->log("upper first value is %i\n", upperFirst, upperFirst);
sort(TRUE, TRUE);
classifyRepertoire();
getBounds(status);
//sort(TRUE, TRUE);
addContractionsToRepertoire(status);
//sort(TRUE, TRUE);
debug->log("\n*** Order after detecting contractions\n\n");
calculateSortKeys();
debug->log(toPrettyString(FALSE, TRUE), TRUE);
detectExpansions();
}
void SortedLines::init()
{
size = repertoire.size();
capacity = 5*size;
lines = new Line[capacity];
init(repertoire, lines);
}
void SortedLines::init(UnicodeSet &rep, Line *lin)
{
UnicodeSetIterator exemplarUSetIter(rep);
int32_t size = 0;
while(exemplarUSetIter.next()) {
Line *currLine = lin+size;
if(exemplarUSetIter.isString()) { // process a string
currLine->setTo(exemplarUSetIter.getString());
} else { // process code point
currLine->setTo(exemplarUSetIter.getCodepoint());
}
currLine->name[currLine->len] = 0; // zero terminate, for our evil ways
//currLine->index = size;
size++;
}
}
void
SortedLines::setSortingArray(Line **sortingArray, Line *elements, int32_t sizeToSort) {
int32_t i = 0;
for(i = 0; i < sizeToSort; i++) {
sortingArray[i] = &elements[i];
}
}
int32_t
SortedLines::setSortingArray(Line **sortingArray, Hashtable *table) {
int32_t size = table->count();
int32_t hashIndex = -1;
const UHashElement *hashElement = NULL;
int32_t count = 0;
while((hashElement = table->nextElement(hashIndex)) != NULL) {
sortingArray[count++] = (Line *)hashElement->value.pointer;
}
return size;
}
void
SortedLines::sort(Line **sortingArray, int32_t sizeToSort, UBool setStrengths, UBool link) {
int32_t i = 0;
int32_t equalStart = 0;
UColAttributeValue equalStrength = UCOL_OFF;
qsort(sortingArray, sizeToSort, sizeof(Line *), probe.comparer);
if(setStrengths) { // analyze strengths
for(i = 1; i < sizeToSort; i++) {
sortingArray[i]->strength = probe.getStrength(*sortingArray[i-1], *sortingArray[i]);
}
// for equal guys, do the code point ordering
i = 1;
while(i < sizeToSort)
{
if(sortingArray[i]->strength == UCOL_IDENTICAL) {
equalStart = i - 1;
equalStrength = sortingArray[equalStart]->strength;
sortingArray[equalStart]->strength = UCOL_IDENTICAL;
while(i < sizeToSort && sortingArray[i]->strength == UCOL_IDENTICAL) {
i++;
}
qsort(sortingArray+equalStart, i-equalStart, sizeof(Line *), codePointCmp);
sortingArray[equalStart]->strength = equalStrength;
} else {
i++;
}
}
}
if(link) { // do the linking
for(i = 0; i < sizeToSort - 1; i++) {
Line *curr = *(sortingArray+i);
curr->next = *(sortingArray+i+1);
(*(sortingArray+i+1))->previous = curr;
}
}
}
void
SortedLines::sort(UBool setStrengths, UBool link) {
if(toSortCapacity < size || !toSort) {
if(toSort) {
delete[] toSort;
}
toSort = new Line*[size*2];
toSortCapacity = size*2;
}
setSortingArray(toSort, lines, size);
sort(toSort, size, setStrengths, link);
first = last = NULL;
if(link) { // do the linking
first = *toSort;
last = *(toSort+size-1);
}
}
void
SortedLines::updateBounds(UnicodeSet &set) {
Line line;
UnicodeString s1;
UnicodeSetIterator it1(set);
while(it1.next()) {
if(!debug->isOn()) {
logger->log(".");
}
if(it1.isString()) { // process a string
s1.setTo(it1.getString());
} else { // process code point
s1.setTo(it1.getCodepoint());
}
//line.setTo(s1);
UColAttributeValue strength = probe.distanceFromEmptyString(s1);
if(probe.compare(UnicodeString(UB[strength]->name), s1) < 0) {
// TODO: leak here - fixit!
UB[strength] = new Line(s1);
//u_strcpy(UB[strength], s1.getTerminatedBuffer());
}
}
}
void SortedLines::addAll(Line* toAdd, int32_t toAddSize)
{
if(size+toAddSize > capacity) {
int32_t doGrowingBreakpoint = 0;
// we need to do growing here
}
int32_t i = 0;
for(i = 0; i < toAddSize; i++) {
lines[size+i] = toAdd[i];
}
size += toAddSize;
}
void SortedLines::setDistancesFromEmpty(Line* array, int32_t arraySize)
{
int32_t i = 0;
for(i = 0; i < arraySize; i++) {
array[i].strengthFromEmpty = probe.distanceFromEmptyString(array[i]);
}
}
// adds contractions in to repertoire
int32_t SortedLines::addContractionsToRepertoire(UErrorCode &status)
{
logger->log("\n*** Detecting contractions\n\n");
contractionsTable = new Hashtable();
int32_t noConts = 0;
int32_t allocateSize = 50*size;
// first check for simple contractions
Line* delta = new Line[allocateSize];
Line** deltaSorted = new Line*[allocateSize];
Line* lesserToAddTo = new Line[allocateSize];
Line* newDelta = new Line[allocateSize];
Line** newDeltaSorted = new Line*[allocateSize];
Line* deltaP = delta;
Line** deltaPP = deltaSorted;
Line* newDeltaP = newDelta;
int32_t deltaSize = 0, lesserToAddToSize = 0, newDeltaSize = 0;
logger->log("++ Contraction detection generation 0\n");
noConts = detectContractions(toSort, size, toSort, size,
delta, deltaSize, lesserToAddTo, lesserToAddToSize, 3*size, status);
setSortingArray(deltaSorted, delta, deltaSize);
sort(deltaSorted, deltaSize, TRUE);
setDistancesFromEmpty(delta, deltaSize);
int32_t deltaPSize = deltaSize;
//updateBounds(delta);
int32_t generation = 0;
// if we found any, we have to try multiple contractions
// However, we want to prevent the contractions explosion
// if the number of simple contractions is greater than the
// starting size, chances are that we either have an algorithmic
// contraction (like iteration marks on w2k) or something
// is seriosly wrong.
if(deltaPSize < size/2) {
while (deltaPSize && generation < 1) {
generation++;
logger->log("\n++ Contraction detection generation %i\n", generation, generation);
// find more, but avoid testing the combinations we already have
noConts += detectContractions(toSort, size, deltaPP, deltaPSize,
newDeltaP, newDeltaSize, lesserToAddTo, lesserToAddToSize, 3*size, status);
noConts += detectContractions(deltaPP, deltaPSize, toSort, size,
newDeltaP, newDeltaSize, lesserToAddTo, lesserToAddToSize, 3*size, status);
calculateSortKeys();
addAll(deltaP, deltaPSize);
setSortingArray(toSort, lines, size);
sort(TRUE, TRUE);
setSortingArray(newDeltaSorted, newDeltaP, newDeltaSize);
sort(newDeltaSorted, newDeltaSize, TRUE);
// if no new ones, bail
//if (newDeltaSize == 0) break;
deltaPSize = newDeltaSize;
newDeltaSize = 0;
if(deltaP == delta) {
deltaP = newDelta;
deltaPP = newDeltaSorted;
newDeltaP = delta;
} else {
deltaP = delta;
deltaPP = deltaSorted;
newDeltaP = newDelta;
}
setDistancesFromEmpty(deltaP, deltaPSize);
}
}
status = U_ZERO_ERROR;
// add stuff from the last batch
addAll(deltaP, deltaPSize);
// warning: we don't add the lesser ones in recursively, since they will
// infinitely loop
setDistancesFromEmpty(lesserToAddTo, lesserToAddToSize);
addAll(lesserToAddTo, lesserToAddToSize);
setSortingArray(toSort, lines, size);
sort(TRUE, TRUE);
delete[] deltaSorted;
delete[] delta;
delete[] lesserToAddTo;
delete[] newDeltaSorted;
delete[] newDelta;
return noConts;
}
int32_t SortedLines::detectContractions(Line **firstRep, int32_t firstSize,
Line **secondRep, int32_t secondSize,
Line *toAddTo, int32_t &toAddToSize,
Line *lesserToAddTo, int32_t &lesserToAddToSize,
int32_t capacity, UErrorCode &status)
{
int32_t noConts = 0;
int i = 0, j = 0, k = 0;
Line lower, upper, trial, toAdd, helper;
UChar32 firstStart, firstEnd, secondStart;
UChar NFCTrial[256];
int32_t NFCTrialLen = 0;
UBool thai;
i = -1;
while(i < firstSize-1 && U_SUCCESS(status)) {
i++;
if(!debug->isOn()) {
logger->log("\rTesting %05i/%05i. Found %05i conts.", i, firstSize, noConts);
}
U16_GET(firstRep[i]->name, 0, 0, firstRep[i]->len, firstStart);
if(uscript_getScript(firstStart, &status) == USCRIPT_HAN || firstRep[i]->strengthFromEmpty > UCOL_PRIMARY) //UCOL_TERTIARY)
{
continue;
}
lower = *firstRep[i];
for(j = 0; j < secondSize; j++) {
if(noConts == capacity) {
return noConts;
}
U16_GET(secondRep[j]->name, 0, 0, secondRep[j]->len, secondStart);
if(firstStart == 0x41 && secondStart == 0x308) {
int32_t putBreakPointHere = 0;
}
if(uscript_getScript(secondStart, &status) == USCRIPT_HAN) // || secondRep[j]->strengthFromEmpty > UCOL_TERTIARY)
{
continue;
}
if(duplicators && duplicators->get(UnicodeString(secondRep[j]->name, secondRep[j]->len)) != NULL) {
debug->log("Skipping duplicator ");
debug->log(secondRep[j]->toString(), TRUE);
continue;
}
if(firstRep[i]->name[0] == 0x61 && secondRep[j]->name[0] == 0x308) {
int32_t putBreakpointhere = 0;
}
upper.setToConcat(firstRep[i], UB[UCOL_PRIMARY]);
//upper.setToConcat(firstRep[i], UB[secondRep[j]->strengthFromEmpty]);
toAdd.setToConcat(firstRep[i], secondRep[j]);
U16_GET(firstRep[i]->name, 0, firstRep[i]->len-1, firstRep[i]->len, firstEnd);
if((thai = u_hasBinaryProperty(firstEnd, UCHAR_LOGICAL_ORDER_EXCEPTION))) {
// this means that the lower is single reordering character
// if we do the lower test without taking this into account,
// we'll comparing the secondRep directly to Thai. We add UB[UCOL_PRIMARY] to
// end of lower and in the middle of trial, so we will have
// lower = Thai + UB, trial Thai + UB + x, resolving to
// UB + Thai vs UB + Thai + x.
// for upper bound, we do the similar, so we have
// upper = Thai + UB + UB, trial = Thai + UB + x,
// resolving to UB + Thai + UB vs UB + Thai + x
if(secondRep[j]->firstCC) {
UChar32 UBChar;
U16_GET(UB[UCOL_SECONDARY]->name, 0, 0, UB[UCOL_SECONDARY]->len, UBChar);
if(secondRep[j]->firstCC > u_getCombiningClass(UBChar)) {
continue;
}
}
upper = *firstRep[i];
upper.append(*UB[UCOL_PRIMARY]);
//upper.append(*UB[secondRep[j]->strengthFromEmpty]);
upper.append(*UB[UCOL_PRIMARY]);
lower.append(*UB[UCOL_PRIMARY]);
trial = *firstRep[i];
trial.append(*UB[UCOL_PRIMARY]);
trial.append(*secondRep[j]);
} else if((firstRep[i]->lastCC > secondRep[j]->firstCC && secondRep[j]->firstCC && !frenchSecondary)
|| (firstRep[i]->firstCC < secondRep[j]->lastCC && firstRep[i]->firstCC && frenchSecondary)) {
// Skip because normalization will reorder
// there will be a chance to check this again, since if we
// try a+b, we will also try b+a
continue;
} else if(frenchSecondary && (firstRep[i]->strengthFromEmpty > UCOL_PRIMARY && secondRep[j]->strengthFromEmpty > UCOL_PRIMARY)) {
continue;
}else if(firstRep[i]->lastCC && secondRep[j]->firstCC && frenchSecondary) {
trial.setToConcat(secondRep[j], firstRep[i]);
} else {
trial.setToConcat(firstRep[i], secondRep[j]);
}
// Now let's check the trial. The problem is that when you combine characters,
// you can end up with concatenation that is unknown for the examined API.
NFCTrialLen = unorm_normalize(trial.name, trial.len, UNORM_NFC, 0, NFCTrial, 256, &status);
if((u_strcmp(trial.name, NFCTrial) == 0) || u_strFindLast(NFCTrial, NFCTrialLen, secondRep[j]->name, secondRep[j]->len)) {
if(secondRep[j]->strengthFromEmpty > UCOL_TERTIARY) {
continue;
}
}
UChar32 c;
U16_GET(NFCTrial, 0, 0, NFCTrialLen, c);
helper.setTo(c);
if(probe.distanceFromEmptyString(helper) > UCOL_TERTIARY) {
continue;
}
if(NFCTrialLen > 1) {
U16_GET(NFCTrial, 0, NFCTrialLen-1, NFCTrialLen, c);
helper.setTo(c);
if(probe.distanceFromEmptyString(helper) > UCOL_TERTIARY) {
continue;
}
}
if (probe.compare(lower, trial) >= 0) { // if lower is bigger than trial
// this might be ok, but I'm having doubts. Here is an additional check:
if(firstRep[i]->len == 1 || secondRep[j]->strengthFromEmpty == UCOL_PRIMARY) {
// I'm basically saying that I'll add this kind of contraction for cases where I combine
// one letter with an accent OR when I'm combining more than one symbol with a letter.
noteContraction("L", lesserToAddTo, lesserToAddToSize, firstRep[i], secondRep[j], noConts, status);
}
}
else if (probe.compare(trial, upper) > 0) { // trial is bigger than upper??
noteContraction("U", toAddTo, toAddToSize, firstRep[i], secondRep[j], noConts, status);
}
#if 0
else if(firstRep[i]->strengthFromEmpty == UCOL_PRIMARY)
{
Line expansionLine;
if(getExpansionLine(trial, *firstRep[i], *secondRep[j], expansionLine) &&
expansionLine.len && !(expansionLine == *secondRep[j])) {
noteContraction("D", toAddTo, toAddToSize, firstRep[i], secondRep[j], noConts, status);
}
}
#endif
else if(firstRep[i]->strengthFromEmpty == UCOL_PRIMARY && probe.getStrength(lower, trial) < secondRep[j]->strengthFromEmpty) {
noteContraction("D1", toAddTo, toAddToSize, firstRep[i], secondRep[j], noConts, status);
}
else if (firstRep[i]->strengthFromEmpty == UCOL_PRIMARY && secondRep[j]->strengthFromEmpty == UCOL_PRIMARY)
{
// I have added an additional check. The checks versus upper and lower bound should be sufficient
// when the right side is a combining mark. There might be a reordering of combining marks, but
// that should be already visible in their order.
// compare the sequence
// Y- <? Y <? Y+
// and
// XY- <? XY <? XY+
Line xym, xyp, xy;
UBool xymIsContraction = FALSE, toAddIsContraction = FALSE;
if(j) {
if(((!secondRep[j-1]->firstCC || firstRep[i]->lastCC < secondRep[j-1]->firstCC) && !frenchSecondary)
||((!firstRep[i]->firstCC || firstRep[i]->firstCC > secondRep[j-1]->lastCC) && frenchSecondary)) {
xym.setToConcat(firstRep[i], secondRep[j-1]);
toAdd.strength = probe.getStrength(xym, toAdd);
if(secondRep[j]->strength != toAdd.strength) {
// there is possibility that either xym or xy are contractions
// There are two situations:
// xym > xy or xym <n xy and ym <k y but n != k
// if they are reordered, we are going to see if each of them
// is further reordered
if(toAdd.strength == UCOL_OFF) {
// check whether toAdd shifted more down
k = j - 2;
while(k>=0 && secondRep[k]->strength > secondRep[j]->strength) {
k--;
}
while(!toAddIsContraction && k>=0) {
xyp.setToConcat(firstRep[i], secondRep[k]);
if(contractionsTable->get(UnicodeString(xyp.name, xyp.len)) != NULL) {
k--;
continue;
}
if(probe.compare(xyp, xym) >= 0) {
// xyp looks like a contraction
noteContraction("!1", toAddTo, toAddToSize, firstRep[i], secondRep[j], noConts, status);
toAddIsContraction = TRUE;
} else {
break;
}
}
// first let's see if xym has moved beyond
if(contractionsTable->get(UnicodeString(xym.name, xym.len)) == NULL) {
k = j+1;
// ignore weaker strengths
while(k < secondSize && secondRep[k]->strength > secondRep[j]->strength) {
k++;
}
// check if we skipped the following guy
if(k < secondSize) {
xyp.setToConcat(firstRep[i], secondRep[k]);
if(probe.compare(xyp, xym) <= 0) {
// xyp looks like a contraction
noteContraction("!2", toAddTo, toAddToSize, firstRep[i], secondRep[j-1], noConts, status);
xymIsContraction = TRUE;
}
}
} else {
xymIsContraction = TRUE;
}
// if they have reordered, but none has moved, then we add them both
// and hope for the best
if(!xymIsContraction && !toAddIsContraction) {
// it is possible that there is an NFC version version of one of the
// strings. If we have XY > XZ, but NFC(XZ) = W and X < W, we might have
// have a false contraction.
trial.len = unorm_normalize(toAdd.name, toAdd.len, UNORM_NFC, 0, trial.name, 25, &status);
//UColAttributeValue strength = probe.getStrength(*firstRep[i], trial);
if(trial == toAdd) {
noteContraction("!3", toAddTo, toAddToSize, firstRep[i], secondRep[j-1], noConts, status);
noteContraction("!3", toAddTo, toAddToSize, firstRep[i], secondRep[j], noConts, status);
} else {
noteContraction("!4", toAddTo, toAddToSize, firstRep[i], secondRep[j], noConts, status);
}
}
} else { // only the strength has changed
// check whether the previous is contraction and if not, add the current
if(contractionsTable->get(UnicodeString(xym.name, xym.len)) == NULL) {
noteContraction("!5", toAddTo, toAddToSize, firstRep[i], secondRep[j], noConts, status);
}
}
}
}
}
}
if(thai) { // restore lower
lower = *firstRep[i];
}
}
}
return noConts;
}
void
SortedLines::noteContraction(const char* msg, Line *toAddTo, int32_t &toAddToSize, Line *left, Line *right, int32_t &noConts, UErrorCode &status)
{
Line toAdd;
toAdd.setToConcat(left, right);
toAdd.left = left;
toAdd.right = right;
// if we're adding an accent to an existing contraction, we want to check
#if 0
Line test, trial1, trial2;
if(right->strengthFromEmpty > UCOL_PRIMARY) {
if(left->right && left->right->previous && left->right->next) {
test.setToConcat(left->left, left->right->previous);
trial1.setToConcat(&test, right);
test.setToConcat(left->left, left->right->next);
trial2.setToConcat(&test, right);
if(probe.compare(trial1, toAdd) < 0 && probe.compare(toAdd, trial2) < 0) {
// this means that the contraction has been broken by the newly added accent
// so while 'ch' is contraction, 'ch'+dot_above sorts between 'cg'+dot_above and 'ci'+dot_above
debug->log("Con -");
debug->log(msg);
debug->log(toAdd.toString(FALSE), TRUE);
return;
}
} else {
if(right->previous && right->next) {
trial1.setToConcat(left, right->previous);
trial2.setToConcat(left, right->next);
if(probe.compare(trial1, toAdd) < 0 && probe.compare(toAdd, trial2) < 0) {
// this means that the contraction has been broken by the newly added accent
// so while 'ch' is contraction, 'ch'+dot_above sorts between 'cg'+dot_above and 'ci'+dot_above
debug->log("Con -");
debug->log(msg);
debug->log(toAdd.toString(FALSE), TRUE);
return;
}
}
if(left->previous && left->next) {
trial1.setToConcat(left->previous, right);
trial2.setToConcat(left->next, right);
if(probe.compare(trial1, toAdd) < 0 && probe.compare(toAdd, trial2) < 0) {
// this means that the contraction has been broken by the newly added accent
// so while 'ch' is contraction, 'ch'+dot_above sorts between 'cg'+dot_above and 'ci'+dot_above
debug->log("Con -");
debug->log(msg);
debug->log(toAdd.toString(FALSE), TRUE);
return;
}
}
}
}
if(right->right && right->right->strengthFromEmpty > UCOL_PRIMARY && right->left->previous && right->left->next) { // maybe we already had a contraction with an accent
test.setToConcat(right->left->previous, right->right);
trial1.setToConcat(left, &test);
test.setToConcat(right->left->next, right->right);
trial2.setToConcat(left, &test);
if(probe.compare(trial1, toAdd) < 0 && probe.compare(toAdd, trial2) < 0) {
// this means that the contraction has been broken by the newly added accent
// so while 'ch' is contraction, 'ch'+dot_above sorts between 'cg'+dot_above and 'ci'+dot_above
debug->log("Con -");
debug->log(msg);
debug->log(toAdd.toString(FALSE), TRUE);
return;
}
}
#endif
if(contractionsTable->get(UnicodeString(toAdd.name, toAdd.len)) == NULL) {
if(probe.distanceFromEmptyString(toAdd) <= UCOL_TERTIARY) {
toAddTo[toAddToSize++] = toAdd;
contractionsTable->put(UnicodeString(toAdd.name, toAdd.len), &toAdd, status);
noConts++;
debug->log(msg);
debug->log(" Con + ");
debug->log(toAdd.toString(FALSE), TRUE);
if(!left->sortKey) {
calculateSortKey(*left);
}
debug->log(left->dumpSortkey());
debug->log(" + ");
if(!right->sortKey) {
calculateSortKey(*right);
}
debug->log(right->dumpSortkey());
debug->log(" = ");
calculateSortKey(toAdd);
debug->log(toAdd.dumpSortkey(), TRUE);
if(noConts > size/2) {
status = U_BUFFER_OVERFLOW_ERROR;
}
}
}
}
UBool
SortedLines::getExpansionLine(const Line &expansion, const Line &previous, const Line &exp, Line &expansionLine)
{
int expIndexSize = 0;
UColAttributeValue expStrength = UCOL_OFF;
int32_t comparisonResult = 0;
int32_t i = 0, k = 0, prevK = 0;
Line trial;
UBool sequenceCompleted = FALSE;
int32_t expIndexes[256];
int32_t expIndexesSize = 0;
if(!sequenceCompleted) {
expIndexSize = 0;
expansionLine.clear();
// we will start from strength between the expansion
// and the target (toSort[i] and toSort[j]. First we
// will add as many primaries as possible. Then we will
// try to add secondary pieces and then tertiary.
// found an expansion - what is the expanding sequence?
expStrength = UCOL_PRIMARY;
while(!sequenceCompleted) {
k = 0;
prevK = 0;
while(k < size) {
if(expansionLine.len > 15) {
sequenceCompleted = TRUE;
break;
}
while(k < size && toSort[k]->strength != UCOL_PRIMARY)
{
k++;
}
// nothing found
if(k == size) {
break;
}
// we need to skip over reordering things. If they were worthy, they would
// have been detected in the previous iteration.
//if(expansionLine.lastCC && toSort[k]->firstCC && expansionLine.lastCC > toSort[k]->firstCC) {
//k++;
//continue;
//}
trial = previous;
trial.append(expansionLine);
trial.append(*toSort[k]);
if(toSort[k]->name[0] == 0x0067) {
int32_t putBreakPointHere = 0;
}
comparisonResult = probe.compare(trial, expansion);
if(comparisonResult == 0) {
expansionLine = *toSort[k];
return TRUE;
} else if (comparisonResult > 0) {
if(prevK) {
if(exp == *toSort[prevK]) {
expansionLine = exp;
return TRUE;
}
i = prevK;
while(i < k-1) {
i++;
if(toSort[i]->strength > exp.strength) {
continue;
}
trial = previous;
trial.append(expansionLine);
trial.append(*toSort[i]);
if(probe.compare(trial, expansion) > 0) {
break;
}
}
// we got into situation where we have ch > ch+dot-below
// however, ch is a contraction and therefore we cannot use
// it properly. If we have hit on a contraction, we'll just try
// to continue. Probably need more logic here.
if(contractionsTable->get(UnicodeString(trial.name, trial.len)) == NULL) {
expansionLine.append(*toSort[i-1]);
expIndexes[expIndexSize++] = i-1;
break;
} else {
int32_t putBreakPointHere = 0;
}
} else {
sequenceCompleted = TRUE;
break;
}
//break;
}
prevK = k;
k++;
}
if(!prevK || k == size) {
break;
}
}
}
return expIndexSize > 0;
}
int32_t
SortedLines::gooseUp(int32_t resetIndex, int32_t expansionIndex, Line &expLine, int32_t *expIndexes, int32_t &expIndexSize, UColAttributeValue strength)
{
int32_t i = expansionIndex, k = resetIndex+1, n = 0, m = 0, start = 0;
UBool haveChanges = FALSE;
Line trial, prefix, suffix;
// we will first try goosing up the reset index
//while(toSort[k]->strength >= strength)
for( ; toSort[k]->strength == strength; k++)
{
//if(toSort[k]->strength > strength) {
//continue;
//}
trial.setToConcat(toSort[k], &expLine);
if(probe.compare(trial, *toSort[i]) > 0) {
break;
}
}
resetIndex = k-1;
// goose up individual characters
prefix = *toSort[resetIndex];
for(n = 0; n < expIndexSize; n++) {
suffix.clear();
for(m = n+1; m < expIndexSize; m++) {
suffix.append(*toSort[expIndexes[m]]);
}
k = expIndexes[n]+1;
//while(toSort[k]->strength >= strength)
for( ; toSort[k]->strength == strength; k++)
{
//if(toSort[k]->strength > strength) {
//continue;
//}
trial.setToConcat(&prefix, toSort[k]);
trial.append(suffix);
if(probe.compare(trial, *toSort[i]) > 0) {
break;
}
}
if(k > expIndexes[n]+1) {
haveChanges = TRUE;
expIndexes[n] = k-1;
}
prefix.append(*toSort[expIndexes[n]]);
}
// try inserting ingorables
UColAttributeValue lastStr = UCOL_OFF;
k = 0;
while(toSort[k]->strengthFromEmpty > strength) {
k++;
}
if(toSort[k]->strengthFromEmpty == strength) {
start = k;
prefix = *toSort[resetIndex];
n = 0;
while(n <= expIndexSize) {
suffix.clear();
for(m = n; m < expIndexSize; m++) {
suffix.append(*toSort[expIndexes[m]]);
}
k = start;
while(toSort[k]->strengthFromEmpty == strength) {
trial.setToConcat(&prefix, toSort[k]);
trial.append(suffix);
lastStr = probe.getStrength(trial, *toSort[i]);
if(lastStr == UCOL_OFF) { // shot over - we won't find anything here
break;
} else if(lastStr > strength) {
for(m = expIndexSize; m > n; m--) {
expIndexes[m] = expIndexes[m-1];
}
expIndexes[n] = k;
expIndexSize++;
haveChanges = TRUE;
break;
}
#if 0
if(probe.compare(trial, *toSort[i]) > 0) {
// if the first one skips, that means that
// this position doesn't work
if(k > start) {
// insert an ignorable on position n
for(m = expIndexSize; m > n; m--) {
expIndexes[m] = expIndexes[m-1];
}
expIndexes[n] = k-1;
expIndexSize++;
haveChanges = TRUE;