Repository navigation
Expand file tree
/
Copy pathcolprobe.cpp
More file actions
1732 lines (1556 loc) · 49.9 KB
/
Copy pathcolprobe.cpp
File metadata and controls
1732 lines (1556 loc) · 49.9 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
/*
*******************************************************************************
*
* Copyright (C) 2003, International Business Machines
* Corporation and others. All Rights Reserved.
*
*******************************************************************************
*
* File colprobe.cpp
*
* Modification History:
*
* Date Name Description
* 03/18/2003 weiv Creation.
*******************************************************************************
*/
#include "uoptions.h"
#include "unicode/ucol.h"
#include "unicode/ucoleitr.h"
#include "unicode/ures.h"
#include "unicode/uniset.h"
#include "unicode/usetiter.h"
#include "unicode/ustring.h"
#include "unicode/uchar.h"
#include "unicode/uscript.h"
#include "uprops.h"
#include "hash.h"
#include "ucol_imp.h"
#include "unicode/ustdio.h"
#include "unicode/utrans.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <io.h>
#include <fcntl.h>
#include "colprobe.h"
#ifdef WIN32
#include <windows.h>
#else
//
// Stubs for Windows API functions when building on UNIXes.
//
typedef int DWORD;
inline int CompareStringW(DWORD, DWORD, UChar *, int, UChar *, int) {return 0;};
#include <sys/time.h>
unsigned long timeGetTime() {
struct timeval t;
gettimeofday(&t, 0);
unsigned long val = t.tv_sec * 1000; // Let it overflow. Who cares.
val += t.tv_usec / 1000;
return val;
};
inline int LCMapStringW(DWORD, DWORD, UChar *, int, UChar *, int) {return 0;};
const int LCMAP_SORTKEY = 0;
#define MAKELCID(a,b) 0
const int SORT_DEFAULT = 0;
#endif
#include "line.h"
static UBool gVerbose = FALSE;
static UBool gDebug = FALSE;
static UBool gQuiet = FALSE;
static UBool gExemplar = FALSE;
DWORD gWinLCID;
int gCount;
Line **gICULines;
UCollator *gCol;
UCollator *gUCA;
Line source;
Line target;
Line *gSource = &source;
Line *gTarget = ⌖
Hashtable gElements(FALSE);
Hashtable gExpansions(FALSE);
CompareFn gComparer;
const UChar separatorChar = 0x0030;
UFILE *out = NULL;
UFILE *err = NULL;
UFILE *log = NULL;
const char *progName = "colprobe";
const char *gLocale = NULL;
//char platform[256];
int32_t platformIndex = -1;
int32_t gPlatformNo = 0;
int32_t gPlatformIndexes[10];
int32_t gLocaleNo = 0;
const char* gLocales[100];
UBool gRulesStdin = FALSE;
enum {
HELP1,
HELP2,
VERBOSE,
QUIET,
VERSION,
ICUDATADIR,
COPYRIGHT,
LOCALE,
PLATFORM,
DEBUG,
EXEMPLAR,
RULESSTDIN
};
UOption options[]={
/*0*/ UOPTION_HELP_H,
/*1*/ UOPTION_HELP_QUESTION_MARK,
/*2*/ UOPTION_VERBOSE,
/*3*/ UOPTION_QUIET,
/*4*/ UOPTION_VERSION,
/*5*/ UOPTION_ICUDATADIR,
/*6*/ UOPTION_COPYRIGHT,
/*7*/ UOPTION_DEF("locale", 'l', UOPT_REQUIRES_ARG),
/*8*/ UOPTION_DEF("platform", 'p', UOPT_REQUIRES_ARG),
/*9*/ UOPTION_DEF("debug", 'D', UOPT_NO_ARG),
/*10*/ UOPTION_DEF("exemplar", 'E', UOPT_NO_ARG),
/*11*/ UOPTION_DEF("rulesstdin", 'R', UOPT_NO_ARG)
};
int Winstrcmp(const void *a, const void *b) {
gCount++;
int t;
t = CompareStringW(gWinLCID, 0,
(*(Line **)a)->name, (*(Line **)a)->len,
(*(Line **)b)->name, (*(Line **)b)->len);
return t-2;
}
int ICUstrcmp(const void *a, const void *b) {
gCount++;
UCollationResult t;
t = ucol_strcoll(gCol,
(*(Line **)a)->name, (*(Line **)a)->len,
(*(Line **)b)->name, (*(Line **)b)->len);
if (t == UCOL_LESS) return -1;
if (t == UCOL_GREATER) return +1;
return 0;
}
struct {
const char* name;
CompareFn comparer;
} platforms[] = {
{ "icu", ICUstrcmp },
{ "win", Winstrcmp}
};
void deleteLineElement(void *line) {
delete((Line *)line);
}
void stringToLower(char *string) {
uint32_t i = 0;
for(i = 0; i < strlen(string); i++) {
string[i] = tolower(string[i]);
}
}
void usage(const char *name) {
u_fprintf(out, "Usage: %s --locale loc_name --platform platform\n", name);
}
void listKnownPlatforms() {
int32_t i = 0;
u_fprintf(err, "Known platforms:\n");
for(i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) {
u_fprintf(err, "\t%s\n", platforms[i]);
}
}
void addPlatform(const char *platform) {
int32_t i;
//stringToLower(platform);
int32_t oldPlatformNo = gPlatformNo;
for(i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) {
if(strcmp(platform, platforms[i].name) == 0) {
gPlatformIndexes[gPlatformNo++] = i;
}
}
if(gPlatformNo == oldPlatformNo) {
u_fprintf(err, "Unknown platform %s\n", platform);
listKnownPlatforms();
}
}
void processArgs(int argc, char* argv[], UErrorCode &status)
{
int32_t i = 0;
U_MAIN_INIT_ARGS(argc, argv);
argc = u_parseArgs(argc, argv, (int32_t)(sizeof(options)/sizeof(options[0])), options);
if(argc < 0) {
u_fprintf(err, "Unknown option: %s\n", argv[-argc]);
usage(progName);
return;
}
if(options[0].doesOccur || options[1].doesOccur) {
usage(progName);
return;
}
if(options[VERBOSE].doesOccur) {
gVerbose = TRUE;
}
if(options[DEBUG].doesOccur) {
gDebug = TRUE;
gVerbose = TRUE;
}
if(options[EXEMPLAR].doesOccur) {
gExemplar = TRUE;
}
if(options[QUIET].doesOccur) {
gQuiet = TRUE;
}
/*
for(i = 8; i < 9; i++) {
if(!options[i].doesOccur) {
u_fprintf(err, "Option %s is required!\n", options[i].longName);
usage(progName);
status = U_ILLEGAL_ARGUMENT_ERROR;
}
if(options[i].value == NULL) {
u_fprintf(err, "Option %s needs an argument!\n", options[i].longName);
usage(progName);
status = U_ILLEGAL_ARGUMENT_ERROR;
}
}
*/
// ASCII based options specified on the command line
// this is for testing purposes, will allow to load
// up ICU rules and then poke through them.
// In that case, we test only ICU and don't need
// a locale.
if(options[RULESSTDIN].doesOccur) {
gRulesStdin = TRUE;
addPlatform("icu");
return;
}
if(options[LOCALE].doesOccur) {
gLocale = options[LOCALE].value;
} else {
for(i = 1; i < argc; i++) {
gLocales[gLocaleNo++] = argv[i];
}
}
if(options[PLATFORM].doesOccur) {
//strcpy(platform, options[PLATFORM].value);
//addPlatform("icu");
addPlatform(options[PLATFORM].value);
} else { // there is a list of platforms
u_fprintf(err, "Option %s is required!\n", options[i].longName);
usage(progName);
status = U_ILLEGAL_ARGUMENT_ERROR;
}
//
// Set up a Windows LCID
//
gWinLCID = uloc_getLCID(gLocale);
/*
if (gLocale != 0) {
gWinLCID = MAKELCID(gLocale, SORT_DEFAULT);
}
else {
gWinLCID = uloc_getLCID(gLocale);
}
*/
}
void printRules(const UChar *name, int32_t len, UFILE *file) {
// very rudimentary pretty rules print
int32_t i = 0;
UChar toPrint[16384];
int32_t toPrintIndex = 0;
for(i = 0; i < len; i++) {
if(name[i] == 0x0026) {
if(toPrintIndex) {
toPrint[toPrintIndex] = 0;
u_fprintf(file, "%U\n", toPrint);
toPrintIndex = 0;
toPrint[toPrintIndex++] = name[i];
} else {
toPrint[toPrintIndex++] = name[i];
}
} else {
toPrint[toPrintIndex++] = name[i];
}
}
if(toPrintIndex) {
toPrint[toPrintIndex] = 0;
u_fprintf(file, "%U\n", toPrint);
toPrintIndex = 0;
}
}
void escapeString(const UChar *name, int32_t len, UFILE *file) {
u_fprintf(file, "%U", name);
/*
int32_t j = 0;
for(j = 0; j < len; j++) {
if(name[j] >= 0x20 && name[j] < 0x80) {
u_fprintf(file, "%c", name[j]);
} else {
u_fprintf(file, "\\u%04X", name[j]);
}
}
*/
}
void escapeALine(Line *line, UFILE *file) {
escapeString(line->name, line->len, file);
}
void escapeExpansion(Line *line, UFILE *file) {
escapeString(line->expansionString, line->expLen, file);
}
void showNames(Line *line, UFILE *file) {
UErrorCode status = U_ZERO_ERROR;
int32_t j = 0;
char charName[256];
for(j = 0; j < line->len; j++) {
u_charName(line->name[j], U_EXTENDED_CHAR_NAME, charName, 256, &status);
u_fprintf(file, "%s ", charName);
}
}
void setArray(Line **array, Line *contents, int32_t size) {
int32_t i = 0;
for(i = 0; i < size; i++) {
array[i] = contents+i;
}
}
// set an array from a Hashtable
int32_t
setArray(Line **array, Hashtable *table = &gElements) {
int32_t size = table->count();
int32_t hashIndex = -1;
const UHashElement *hashElement = NULL;
int32_t count = 0;
while((hashElement = table->nextElement(hashIndex)) != NULL) {
array[count++] = (Line *)hashElement->value.pointer;
}
return size;
}
UBool trySwamped(Line **smaller, Line **greater, UChar chars[2], CompareFn comparer) {
u_strcpy(gSource->name, (*smaller)->name);
gSource->name[(*smaller)->len] = separatorChar;
gSource->name[(*smaller)->len+1] = chars[0];
gSource->name[(*smaller)->len+2] = 0;
gSource->len = (*smaller)->len+2;
u_strcpy(gTarget->name, (*greater)->name);
gTarget->name[(*greater)->len] = separatorChar;
gTarget->name[(*greater)->len+1] = chars[1];
gTarget->name[(*greater)->len+2] = 0;
gTarget->len = (*greater)->len+2;
if(comparer(&gSource, &gTarget) > 0) {
return TRUE;
} else {
return FALSE;
}
}
UBool trySwamps(Line **smaller, Line **greater, UChar chars[2], CompareFn comparer) {
gSource->name[0] = chars[0];
gSource->name[1] = separatorChar;
u_strcpy(gSource->name+2, (*smaller)->name);
gSource->len = (*smaller)->len+2;
gTarget->name[0] = chars[1];
gTarget->name[1] = separatorChar;
u_strcpy(gTarget->name+2, (*greater)->name);
gTarget->len = (*greater)->len+2;
if(comparer(&gSource, &gTarget) < 0) {
return TRUE;
} else {
return FALSE;
}
}
UColAttributeValue
probeStrength(Line** prevLine, Line **currLine, CompareFn comparer) {
// Primary swamps secondary
// have pairs where [0] 2> [1]
UChar primSwamps[][2] = {
{ 0x00E0, 0x0061 },
{ 0x0450, 0x0435 },
{ 0x31a3, 0x310d }
};
// Secondary swamps tertiary
// have pairs where [0] 3> [1]
UChar secSwamps[][2] = {
{ 0x0053, 0x0073 },
{ 0x0415, 0x0435 },
{ 0x31b6, 0x310e }
};
// Secondary is swamped by primary
// have pairs where [0] 1> [1]
UChar secSwamped[][2] = {
{ 0x0062, 0x0061 },
{ 0x0436, 0x0454 },
{ 0x310e, 0x310d }
};
// Tertiary is swamped by secondary
// have pairs where [0] 2> [1]
UChar terSwamped[][2] = {
{ 0x00E0, 0x0061 },
{ 0x0450, 0x0435 },
{ 0x31a3, 0x310d }
};
int32_t i = 0;
// Tertiary swamps equal?
int result = 0;
// Choose the pair
i = 0;
/*
if((*prevLine)->name[0] > 0xFF && (*currLine)->name[0] > 0xFF) {
i = 0;
} else if((*prevLine)->name[0] < 0x0400 && (*currLine)->name[0] < 0x0400) {
i = 1;
} else {
i = 2;
}
*/
// are they equal?
if((result = comparer(prevLine, currLine)) == 0) {
return UCOL_IDENTICAL;
} else if(result > 0) {
//fprintf(stderr, "lines should be ordered!");
return UCOL_OFF;
} else if(trySwamps(prevLine, currLine, primSwamps[i], comparer)) {
return UCOL_PRIMARY;
} else if(trySwamps(prevLine, currLine, secSwamps[i], comparer)) {
return UCOL_SECONDARY;
} else if(trySwamped(prevLine, currLine, terSwamped[i], comparer)) {
// is there a tertiary difference
return UCOL_TERTIARY;
} else {
//fprintf(stderr, "Unknown strength!\n");
return UCOL_ON;
}
}
// This function tries to probe the set of lines
// (already sorted by qsort) and deduct the strengths
void
analyzeStrength(Line **lines, int32_t size, CompareFn comparer) {
int32_t i = 0;
for(i = 1; i < size; i++) {
Line **prevLine = lines+i-1;
Line **currLine = lines+i;
(*currLine)->strength = probeStrength(prevLine, currLine, comparer);
(*currLine)->sortedIndex = i;
(*currLine)->previous = *prevLine;
(*prevLine)->next = *currLine;
}
}
void printStrength(UColAttributeValue strength, UFILE *file) {
u_fprintf(file, " ");
switch(strength) {
case UCOL_IDENTICAL:
u_fprintf(file, "=");
break;
case UCOL_TERTIARY:
//u_fprintf(file, "<3");
u_fprintf(file, "<<<");
break;
case UCOL_SECONDARY:
//u_fprintf(file, "<2");
u_fprintf(file, "<<");
break;
case UCOL_PRIMARY:
//u_fprintf(file, "<1");
u_fprintf(file, "<");
break;
case UCOL_OFF:
u_fprintf(file, ">?");
default:
u_fprintf(file, "?!");
break;
}
u_fprintf(file, " ");
}
void printStrength(Line *line, UFILE *file) {
printStrength(line->strength, file);
}
void printLine(Line *line, UFILE *file) {
escapeALine(line, file);
if(line->isExpansion) {
u_fprintf(file, "/");
escapeExpansion(line, file);
}
}
void printOrdering(Line **lines, int32_t size, UFILE *file, UBool useLinks = FALSE) {
int32_t i = 0;
//printLine(*lines);
//escapeALine(*lines); // Print first line
Line *line = NULL;
Line *previous = *lines;
if(previous->isReset) {
u_fprintf(file, "\n& ");
escapeALine(previous, file);
} else if(!previous->isRemoved) {
printLine(previous, file);
}
i = 1;
while(i < size && previous->next) {
if(useLinks) {
line = previous->next;
} else {
line = *(lines+i);
}
if(line->isReset) {
u_fprintf(file, "\n& ");
escapeALine(line, file);
} else if(!line->isRemoved) {
if(file == out) {
u_fprintf(file, "\n");
}
if(i > 0) {
printStrength(line, file);
}
printLine(line, file);
//escapeALine(line, file);
}
previous = line;
i++;
}
u_fprintf(file, "\n");
}
void setIndexes(Line **lines, int32_t size) {
int32_t i = 0;
(*lines)->sortedIndex = 0;
for(i = 1; i < size; i++) {
Line *line = *(lines+i);
Line *prev = *(lines+i-1);
line->previous = prev;
prev->next = line;
line->sortedIndex = i;
}
}
// this seems to be a dead end
void
noteExpansion(Line **gLines, Line *line, int32_t size, CompareFn comparer) {
UErrorCode status = U_ZERO_ERROR;
UnicodeString key(line->name, line->len);
//Line *toInsert = (Line *)gElements.get(key);
Line *toInsert = (Line *)gExpansions.get(key);
if(toInsert != NULL) {
toInsert->isExpansion = TRUE;
u_strcpy(toInsert->expansionString, line->expansionString);
toInsert->expLen = line->expLen;
toInsert->previous->next = toInsert->next;
toInsert->next->previous = toInsert->previous;
gElements.remove(key);
} else {
toInsert = new Line(*line);
toInsert->isExpansion = TRUE;
gElements.put(UnicodeString(toInsert->name, toInsert->len), toInsert, status);
}
int32_t i = 0;
Line testLine;
Line *l = &testLine;
for(i = 0; i < size; i++) {
u_strcpy(testLine.name, (*(gLines+i))->name);
u_strcat(testLine.name, line->expansionString);
testLine.len = (*(gLines+i))->len + line->expLen;
if(comparer(&l, &line) > 0) {
toInsert->previous = *(gLines+i-1);
toInsert->next = *(gLines+i);
toInsert->previous->next = toInsert;
toInsert->next->previous = toInsert;
break;
}
}
if(gVerbose) {
u_fprintf(log, "Adding expansion\n");
escapeALine(line, log);
u_fprintf(log, "/");
escapeExpansion(line, log);
u_fprintf(log, " ");
}
}
void
positionExpansions(Line **gLines, int32_t size, CompareFn comparer) {
int result = 0;
Line *line = NULL;
Line *toMove = NULL;
int32_t i = 0, j = 0;
Line **sortedExpansions = new Line*[gExpansions.count()];
int32_t sortedExpansionsSize = setArray(sortedExpansions, &gExpansions);
qsort(sortedExpansions, sortedExpansionsSize, sizeof(Line *), comparer);
// Make a list of things in the vincinity of expansion candidate
for(j = 0; j < sortedExpansionsSize; j++) {
line = *(sortedExpansions+j);
UnicodeString key(line->name, line->len);
toMove = (Line *)gElements.get(key);
int32_t i = 0;
Line testLine, prevTestLine;
Line *l = &testLine;
Line *prevL = &prevTestLine;
// This can be further optimized, since we now know that we have a
// sorted list of expansions, so current can start from toMove, since all
// the elements before it are already smaller. In the beggining it needs to
// be on gLines, though.
Line *current = *gLines;
while(current) {
if(current == toMove) {
// we are wading through a sorted list
// if we found ourselves, it means that we
// are already in a right place, so no moving
// is needed, but we need to make sure we have
// the right strength.
toMove->strength = probeStrength(&prevL, &toMove, comparer);
if(0) {
u_fprintf(log, "Positioned expansion without moving ");
printLine(toMove, log);
u_fprintf(log, " new ordering: \n");
printOrdering(gLines, size, log, TRUE);
}
break;
} else {
u_strcpy(testLine.name, current->name);
if(!current->isExpansion) {
u_strcat(testLine.name, line->expansionString);
testLine.len = current->len + line->expLen;
} else {
testLine.len = current->len;
}
if(comparer(&l, &line) > 0) {
// remove from chain
if(toMove->next) {
toMove->next->strength = probeStrength(&(toMove->previous), &(toMove->next), comparer);
toMove->next->previous = toMove->previous;
}
if(toMove->previous) {
toMove->previous->next = toMove->next;
}
// insert
toMove->previous = current->previous;
toMove->next = current;
if(current->previous) {
current->previous->next = toMove;
}
current->previous = toMove;
toMove->strength = probeStrength(&prevL, &toMove, comparer);
toMove->next->strength = probeStrength(&toMove, &l, comparer);
if(0) {
u_fprintf(log, "Positioned expansion ");
printLine(toMove, log);
u_fprintf(log, " new ordering: \n");
printOrdering(gLines, size, log, TRUE);
}
if(toMove->strength == UCOL_IDENTICAL) {
// check for craziness such as s = ss/s
// such line would consist of previous (or next) concatenated with the expansion value
// make a test
UChar fullString[256];
u_strcpy(fullString, toMove->previous->name);
u_strcat(fullString, toMove->expansionString);
if(u_strcmp(fullString, toMove->name) == 0) {
toMove->previous->next = toMove->next;
toMove->next->previous = toMove->previous;
toMove->isRemoved = TRUE;
u_fprintf(log, "Removed: ");
printLine(toMove, log);
u_fprintf(log, "\n");
}
} else if(toMove->next->strength == UCOL_IDENTICAL) {
UChar fullString[256];
u_strcpy(fullString, toMove->next->name);
u_strcat(fullString, toMove->expansionString);
if(u_strcmp(fullString, toMove->name) == 0) {
toMove->next->strength = toMove->strength;
toMove->previous->next = toMove->next;
toMove->next->previous = toMove->previous;
toMove->isRemoved = TRUE;
u_fprintf(log, "Removed because of back: ");
printLine(toMove, log);
u_fprintf(log, "\n");
}
}
break;
}
prevTestLine = testLine;
}
current = current->next;
}
}
delete[] sortedExpansions;
}
void
noteExpansion(Line *line) {
UErrorCode status = U_ZERO_ERROR;
UnicodeString key(line->name, line->len);
Line *el = (Line *)gElements.get(key);
if(el != NULL) {
el->isExpansion = TRUE;
u_strcpy(el->expansionString, line->expansionString);
el->expLen = line->expLen;
} else {
Line *toInsert = new Line(*line);
toInsert->isExpansion = TRUE;
gElements.put(UnicodeString(line->name, line->len), toInsert, status);
}
Line *el2 = (Line *)gExpansions.get(key);
el2->isExpansion = TRUE;
u_strcpy(el2->expansionString, line->expansionString);
el2->expLen = line->expLen;
if(gDebug) {
u_fprintf(log, "Adding expansion\n");
printLine(line, log);
u_fprintf(log, "\n");
}
}
void
noteContraction(Line *line) {
UErrorCode status = U_ZERO_ERROR;
Line *toInsert = new Line(*line);
toInsert->isContraction = TRUE;
gElements.put(UnicodeString(line->name, line->len), toInsert, status);
if(gVerbose) {
u_fprintf(log, "Adding contraction\n");
escapeALine(line, log);
u_fprintf(log, " ");
}
}
void
noteElement(Line *line) {
UErrorCode status = U_ZERO_ERROR;
Line *toInsert = new Line(*line);
gElements.put(UnicodeString(line->name, line->len), toInsert, status);
if(0) { //if(gDebug)
escapeALine(line, log);
u_fprintf(log, " ");
}
}
// This function checks if a combination of characters has changed place with the
// adjacent elements. If so, these are most probably contractions.
// However, it still needs to be checked if these contractions are fake - the
// test is simple - if xy is suspected contraction, if we get that x/y is expansion, then
// xy is a fake contraction.
int32_t
analyzeContractions(Line** lines, int32_t size, CompareFn comparer) {
int32_t i = 0, j = 0;
int32_t outOfOrder = 0;
UColAttributeValue strength = UCOL_OFF;
UColAttributeValue currStrength = UCOL_OFF;
Line **prevLine = lines;
Line **currLine = NULL;
Line **backupLine = NULL;
UBool prevIsContraction = FALSE, currIsContraction = FALSE;
// Problem here is detecting a contraction that is at the very end of the sorted list
for(i = 1; i < size; i++) {
currLine = lines+i;
strength = probeStrength(prevLine, currLine, comparer);
if(strength == UCOL_OFF || strength != (*currLine)->strength) {
prevIsContraction = FALSE;
currIsContraction = FALSE;
if(!outOfOrder) {
if(gVerbose) {
u_fprintf(log, "Possible contractions: ");
}
}
// now we have two elements that are different. The question is,
// which one of them is the contraction - which one has moved.
// Could be the previous, but could also be the current.
outOfOrder++;
// First, lets check whether the previous has jumped back
j = i+1;
// skip all the nexts that have smaller strength, they don't have an effect
while(j < size && (*(lines+j))->strength > (*currLine)->strength) {
j++;
}
// check if there are other elements of same or greater strength
while(j < size &&
(strength = probeStrength(prevLine, (backupLine = lines+j), comparer)) == UCOL_OFF) {
j++;
// if we skipped more than one, it might be in fact a contraction
prevIsContraction = TRUE;
}
if(prevIsContraction) {
noteContraction(*prevLine);
j = i-2;
// add all the previous elements with smaller strength, since they also
// will jump over and are contractions
while(j >= 0 && (*(lines+j+1))->strength > (*currLine)->strength) {
strength = probeStrength(lines+j, currLine, comparer);
if(strength == UCOL_OFF) {
noteContraction(*(lines+j));
}
j--;
}
}
// now we check if the current element is jumping forward,
// the dance steps are analogous to above.
j = i - 2;
while(j >= 0 && (*(lines+j+1))->strength > (*currLine)->strength) {
j--;
}
while(j >= 0 &&
(strength = probeStrength((backupLine = lines+j), currLine, comparer)) == UCOL_OFF) {
j--;
currIsContraction = TRUE;
}
if(currIsContraction) {
if(gVerbose) {
escapeALine(*currLine, log);
u_fprintf(log, " ");
}
j = i+1;
while(j < size && (*(lines+j))->strength > (*currLine)->strength) {
strength = probeStrength(prevLine, lines+j, comparer);
if(strength == UCOL_OFF) {
noteContraction(*(lines+j));
}
j++;
}
}
// Not sure about either. List both and then check
if(!(prevIsContraction || currIsContraction)) {
noteContraction(*prevLine);
noteContraction(*currLine);
}
}
prevLine = currLine;
}
if(outOfOrder) {
if(gVerbose) {
u_fprintf(log, "\n");
}
}
return outOfOrder;
}
int32_t
detectContractions(Line **gLines, Line *lines, int32_t size, CompareFn comparer) {
int32_t i = 0, j = 0;
int32_t noContractions = 0;
// Create and compare doubles:
Line *backupLines = new Line[size];
Line::copyArray(backupLines, lines, size);
// detect contractions
Line **gLinesBackup = NULL; //new Line*[size];
for(i = 0; i < size; i++) {
// preserve index and previous
Line::copyArray(lines, backupLines, size);
for(j = 0; j < size; j++) {
u_strcpy(lines[j].name, backupLines[i].name);
u_strcat(lines[j].name, backupLines[j].name);
lines[j].len = backupLines[i].len+backupLines[j].len;
}
if((noContractions += analyzeContractions(gLines, size, comparer)) && gDebug) {
if(gLinesBackup == NULL) {
gLinesBackup = new Line*[size];
}
// Show the sorted doubles, for debugging
setArray(gLinesBackup, lines, size);
qsort(gLinesBackup, size, sizeof(Line *), comparer);
//setIndexes(gLinesBackup, size);
analyzeStrength(gLinesBackup, size, comparer);
printOrdering(gLinesBackup, size, log);
}
if(!gQuiet) {
u_fprintf(log, ".");
}
}
if(!gQuiet) {
u_fprintf(log, "\n");
}
delete[] backupLines;
if(gLinesBackup) {
delete[] gLinesBackup;
}
return noContractions;
}
// gLines in this function is an array of sorted pointers.
// Contractions are already included.
int32_t
detectExpansions(Line **gLines, int32_t size, CompareFn comparer) {
UErrorCode status = U_ZERO_ERROR;
// detect expansions
UColAttributeValue startStrength = UCOL_OFF, endStrength = UCOL_OFF,
strength = UCOL_OFF, previousStrength = UCOL_OFF;
Line start, end, src;
Line *startP = &start, *endP = &end, *srcP = &src;
Line *current = NULL;
memset(startP, 0, sizeof(Line));
memset(endP, 0, sizeof(Line));
memset(srcP, 0, sizeof(Line));
int32_t srcLen;
int32_t i = 0, j = 0, k = 0;
for(i = 0; i < size; i++) {
u_strcpy(start.name, (*(gLines+i))->name);
u_strcpy(end.name, (*(gLines+i))->name);
srcLen = (*(gLines+i))->len;
u_strcpy(start.name+srcLen, (*(gLines))->name);
start.len = srcLen + (*(gLines))->len;
u_strcpy(end.name+srcLen, (*(gLines+size-1))->name);
end.len = srcLen + (*(gLines+size-1))->len;
for(k = 0; k < size; k++) { // k is index of a thing that is not doubled
current = *(gLines+k);
// see if we have moved to front
// has it moved to the very beggining
if((startStrength = probeStrength((gLines+k), &startP, comparer)) != UCOL_OFF) {
continue; // this one is in the front
}
// has it moved to the very end?
if((endStrength = probeStrength(&endP, (gLines+k), comparer)) != UCOL_OFF) {
continue; // this one is in the back
}
// Potential Expansion
if(gDebug) { //gVerbose
u_fprintf(log, "Possible expansion: ");
escapeALine(*(gLines+k), log);
u_fprintf(log, " ");
}
// Now we have to make sure that this is really an expansion
// First, we have to find it
u_strcpy(src.name, (*(gLines+i))->name);
for(j = 0; j < size; j++) {
u_strcpy(src.name+srcLen, (*(gLines+j))->name);
src.len = srcLen + (*(gLines+j))->len;
if((strength = probeStrength(&srcP, (gLines+k), comparer)) == UCOL_OFF) {
strength = probeStrength((gLines+k), &srcP, comparer);
// we found it *(gLines+j-1) is the element that is interesting
// since gLines+j-1 < gLines+k < gLines+j
if(gDebug) { //gVerbose
u_fprintf(log, "i = %i, k = %i, j = %i ", i, k, j);
escapeALine(*(gLines+i), log);
escapeALine(*(gLines+j-1), log);
printStrength(previousStrength, log);
escapeALine(current, log);
printStrength(strength, log);
escapeALine(*(gLines+i), log);
escapeALine(*(gLines+j), log);
u_fprintf(log, "\n");