summaryrefslogtreecommitdiff
path: root/Build/source/libs/icu/icu-4.6/i18n/ucol_cnt.cpp
blob: 7c274b763e9ce3d31d9142bc59ee056922d4d05b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
/*
 *******************************************************************************
 *
 *   Copyright (C) 2001-2008, International Business Machines
 *   Corporation and others.  All Rights Reserved.
 *
 *******************************************************************************
 *   file name:  ucol_cnt.cpp
 *   encoding:   US-ASCII
 *   tab size:   8 (not used)
 *   indentation:4
 *
 *   created 02/22/2001
 *   created by: Vladimir Weinstein
 *
 * This module maintains a contraction table structure in expanded form
 * and provides means to flatten this structure
 * 
 */

#include "unicode/utypes.h"

#if !UCONFIG_NO_COLLATION

#include "unicode/uchar.h"
#include "ucol_cnt.h"
#include "cmemory.h"

static void uprv_growTable(ContractionTable *tbl, UErrorCode *status) {
    if(tbl->position == tbl->size) {
        uint32_t *newData = (uint32_t *)uprv_realloc(tbl->CEs, 2*tbl->size*sizeof(uint32_t));
        if(newData == NULL) {
            *status = U_MEMORY_ALLOCATION_ERROR;
            return;
        }
        UChar *newCPs = (UChar *)uprv_realloc(tbl->codePoints, 2*tbl->size*sizeof(UChar));
        if(newCPs == NULL) {
            uprv_free(newData);
            *status = U_MEMORY_ALLOCATION_ERROR;
            return;
        }
        tbl->CEs = newData;
        tbl->codePoints = newCPs;
        tbl->size *= 2;
    }
}

U_CAPI CntTable*  U_EXPORT2
/*uprv_cnttab_open(CompactEIntArray *mapping, UErrorCode *status) {*/
uprv_cnttab_open(UNewTrie *mapping, UErrorCode *status) {
    if(U_FAILURE(*status)) {
        return 0;
    }
    CntTable *tbl = (CntTable *)uprv_malloc(sizeof(CntTable));
    if(tbl == NULL) {
        *status = U_MEMORY_ALLOCATION_ERROR;
        return NULL;
    }
    tbl->mapping = mapping;
    tbl->elements = (ContractionTable **)uprv_malloc(INIT_EXP_TABLE_SIZE*sizeof(ContractionTable *));
    if(tbl->elements == NULL) {
        *status = U_MEMORY_ALLOCATION_ERROR;
        uprv_free(tbl);
        return NULL;
    }
    tbl->capacity = INIT_EXP_TABLE_SIZE;
    uprv_memset(tbl->elements, 0, INIT_EXP_TABLE_SIZE*sizeof(ContractionTable *));
    tbl->size = 0;
    tbl->position = 0;
    tbl->CEs = NULL;
    tbl->codePoints = NULL;
    tbl->offsets = NULL;
    tbl->currentTag = NOT_FOUND_TAG;
    return tbl;
}

static ContractionTable *addATableElement(CntTable *table, uint32_t *key, UErrorCode *status) {
    ContractionTable *el = (ContractionTable *)uprv_malloc(sizeof(ContractionTable));
    if(el == NULL) {
        goto outOfMemory;
    }
    el->CEs = (uint32_t *)uprv_malloc(INIT_EXP_TABLE_SIZE*sizeof(uint32_t));
    if(el->CEs == NULL) {
        goto outOfMemory;
    }

    el->codePoints = (UChar *)uprv_malloc(INIT_EXP_TABLE_SIZE*sizeof(UChar));
    if(el->codePoints == NULL) {
        uprv_free(el->CEs);
        goto outOfMemory;
    }

    el->position = 0;
    el->size = INIT_EXP_TABLE_SIZE;
    uprv_memset(el->CEs, 0, INIT_EXP_TABLE_SIZE*sizeof(uint32_t));
    uprv_memset(el->codePoints, 0, INIT_EXP_TABLE_SIZE*sizeof(UChar));

    table->elements[table->size] = el;

    //uhash_put(table->elements, (void *)table->size, el, status);

    *key = table->size++;

    if(table->size == table->capacity) {
        ContractionTable **newElements = (ContractionTable **)uprv_malloc(table->capacity*2*sizeof(ContractionTable *));
        // do realloc
        /*        table->elements = (ContractionTable **)realloc(table->elements, table->capacity*2*sizeof(ContractionTable *));*/
        if(newElements == NULL) {
            uprv_free(el->codePoints);
            uprv_free(el->CEs);
            goto outOfMemory;
        }
        ContractionTable **oldElements = table->elements;
        uprv_memcpy(newElements, oldElements, table->capacity*sizeof(ContractionTable *));
        uprv_memset(newElements+table->capacity, 0, table->capacity*sizeof(ContractionTable *));
        table->capacity *= 2;
        table->elements = newElements;
        uprv_free(oldElements);
    }

    return el;

outOfMemory:
    *status = U_MEMORY_ALLOCATION_ERROR;
    if (el) uprv_free(el);
    return NULL;
}

U_CAPI int32_t  U_EXPORT2
uprv_cnttab_constructTable(CntTable *table, uint32_t mainOffset, UErrorCode *status) {
    int32_t i = 0, j = 0;
    if(U_FAILURE(*status) || table->size == 0) {
        return 0;
    }

    table->position = 0;

    if(table->offsets != NULL) {
        uprv_free(table->offsets);
    }
    table->offsets = (int32_t *)uprv_malloc(table->size*sizeof(int32_t));
    if(table->offsets == NULL) {
        *status = U_MEMORY_ALLOCATION_ERROR;
        return 0;
    }


    /* See how much memory we need */
    for(i = 0; i<table->size; i++) {
        table->offsets[i] = table->position+mainOffset;
        table->position += table->elements[i]->position;
    }

    /* Allocate it */
    if(table->CEs != NULL) {
        uprv_free(table->CEs);
    }
    table->CEs = (uint32_t *)uprv_malloc(table->position*sizeof(uint32_t));
    if(table->CEs == NULL) {
        *status = U_MEMORY_ALLOCATION_ERROR;
        uprv_free(table->offsets);
        table->offsets = NULL;
        return 0;
    }
    uprv_memset(table->CEs, '?', table->position*sizeof(uint32_t));

    if(table->codePoints != NULL) {
        uprv_free(table->codePoints);
    }
    table->codePoints = (UChar *)uprv_malloc(table->position*sizeof(UChar));
    if(table->codePoints == NULL) {
        *status = U_MEMORY_ALLOCATION_ERROR;
        uprv_free(table->offsets);
        table->offsets = NULL;
        uprv_free(table->CEs);
        table->CEs = NULL;
        return 0;
    }
    uprv_memset(table->codePoints, '?', table->position*sizeof(UChar));

    /* Now stuff the things in*/

    UChar *cpPointer = table->codePoints;
    uint32_t *CEPointer = table->CEs;
    for(i = 0; i<table->size; i++) {
        int32_t size = table->elements[i]->position;
        uint8_t ccMax = 0, ccMin = 255, cc = 0;
        for(j = 1; j<size; j++) {
            cc = u_getCombiningClass(table->elements[i]->codePoints[j]);
            if(cc>ccMax) {
                ccMax = cc;
            }
            if(cc<ccMin) {
                ccMin = cc;
            }
            *(cpPointer+j) = table->elements[i]->codePoints[j];
        }
        *cpPointer = ((ccMin==ccMax)?1:0 << 8) | ccMax;

        uprv_memcpy(CEPointer, table->elements[i]->CEs, size*sizeof(uint32_t));
        for(j = 0; j<size; j++) {
            if(isCntTableElement(*(CEPointer+j))) {
                *(CEPointer+j) = constructContractCE(getCETag(*(CEPointer+j)), table->offsets[getContractOffset(*(CEPointer+j))]);
            }
        }
        cpPointer += size;
        CEPointer += size;
    }

    // TODO: this one apparently updates the contraction CEs to point to a real address (relative to the 
    // start of the flat file). However, what is done below is just wrong and it affects building of 
    // tailorings that have constructions in a bad way. At least, one should enumerate the trie. Also,
    // keeping a list of code points that are contractions might be smart, although I'm not sure if it's
    // feasible.
    uint32_t CE;
    for(i = 0; i<=0x10FFFF; i++) {
        /*CE = ucmpe32_get(table->mapping, i);*/
        CE = utrie_get32(table->mapping, i, NULL);
        if(isCntTableElement(CE)) {
            CE = constructContractCE(getCETag(CE), table->offsets[getContractOffset(CE)]);
            /*ucmpe32_set(table->mapping, i, CE);*/
            utrie_set32(table->mapping, i, CE);
        }
    }


    return table->position;
}

static ContractionTable *uprv_cnttab_cloneContraction(ContractionTable *t, UErrorCode *status) {
    ContractionTable *r = (ContractionTable *)uprv_malloc(sizeof(ContractionTable));
    if(r == NULL) {
        goto outOfMemory;
    }

    r->position = t->position;
    r->size = t->size;

    r->codePoints = (UChar *)uprv_malloc(sizeof(UChar)*t->size);
    if(r->codePoints == NULL) {
        goto outOfMemory;
    }
    r->CEs = (uint32_t *)uprv_malloc(sizeof(uint32_t)*t->size);
    if(r->CEs == NULL) {
        uprv_free(r->codePoints);
        goto outOfMemory;
    }
    uprv_memcpy(r->codePoints, t->codePoints, sizeof(UChar)*t->size);
    uprv_memcpy(r->CEs, t->CEs, sizeof(uint32_t)*t->size);

    return r;

outOfMemory:
    *status = U_MEMORY_ALLOCATION_ERROR;
    if (r) uprv_free(r);
    return NULL;
}

U_CAPI CntTable* U_EXPORT2
uprv_cnttab_clone(CntTable *t, UErrorCode *status) {
    if(U_FAILURE(*status)) {
        return NULL;
    }
    int32_t i = 0;
    CntTable *r = (CntTable *)uprv_malloc(sizeof(CntTable));
    /* test for NULL */
    if (r == NULL) {
        goto outOfMemory;
    }
    r->position = t->position;
    r->size = t->size;
    r->capacity = t->capacity;

    r->mapping = t->mapping;

    r->elements = (ContractionTable **)uprv_malloc(t->capacity*sizeof(ContractionTable *));
    /* test for NULL */
    if (r->elements == NULL) {
        goto outOfMemory;
    }
    //uprv_memcpy(r->elements, t->elements, t->capacity*sizeof(ContractionTable *));

    for(i = 0; i<t->size; i++) {
        r->elements[i] = uprv_cnttab_cloneContraction(t->elements[i], status);
    }

    if(t->CEs != NULL) {
        r->CEs = (uint32_t *)uprv_malloc(t->position*sizeof(uint32_t));
        /* test for NULL */
        if (r->CEs == NULL) {
            uprv_free(r->elements);
            goto outOfMemory;
        }
        uprv_memcpy(r->CEs, t->CEs, t->position*sizeof(uint32_t));
    } else {
        r->CEs = NULL;
    }

    if(t->codePoints != NULL) {
        r->codePoints = (UChar *)uprv_malloc(t->position*sizeof(UChar));
        /* test for NULL */
        if (r->codePoints == NULL) {
            uprv_free(r->CEs);
            uprv_free(r->elements);
            goto outOfMemory;
        }
        uprv_memcpy(r->codePoints, t->codePoints, t->position*sizeof(UChar));
    } else {
        r->codePoints = NULL;
    }

    if(t->offsets != NULL) {
        r->offsets = (int32_t *)uprv_malloc(t->size*sizeof(int32_t));
        /* test for NULL */
        if (r->offsets == NULL) {
            uprv_free(r->codePoints);
            uprv_free(r->CEs);
            uprv_free(r->elements);
            goto outOfMemory;
        }
        uprv_memcpy(r->offsets, t->offsets, t->size*sizeof(int32_t));
    } else {
        r->offsets = NULL;
    }

    return r;

outOfMemory:
    *status = U_MEMORY_ALLOCATION_ERROR;
    if (r) uprv_free(r);
    return NULL;
}

U_CAPI void  U_EXPORT2
uprv_cnttab_close(CntTable *table) {
    int32_t i = 0;
    for(i = 0; i<table->size; i++) {
        uprv_free(table->elements[i]->CEs);
        uprv_free(table->elements[i]->codePoints);
        uprv_free(table->elements[i]);
    }
    uprv_free(table->elements);
    uprv_free(table->CEs);
    uprv_free(table->offsets);
    uprv_free(table->codePoints);
    uprv_free(table);
}

/* this is for adding non contractions */
U_CAPI uint32_t  U_EXPORT2
uprv_cnttab_changeLastCE(CntTable *table, uint32_t element, uint32_t value, UErrorCode *status) {
    element &= 0xFFFFFF;

    ContractionTable *tbl = NULL;
    if(U_FAILURE(*status)) {
        return 0;
    }

    if((element == 0xFFFFFF) || (tbl = table->elements[element]) == NULL) {
        return 0;
    }

    tbl->CEs[tbl->position-1] = value;

    return(constructContractCE(table->currentTag, element));
}


/* inserts a part of contraction sequence in table. Sequences behind the offset are moved back. If element is non existent, it creates on. Returns element handle */
U_CAPI uint32_t  U_EXPORT2
uprv_cnttab_insertContraction(CntTable *table, uint32_t element, UChar codePoint, uint32_t value, UErrorCode *status) {

    ContractionTable *tbl = NULL;

    if(U_FAILURE(*status)) {
        return 0;
    }
    element &= 0xFFFFFF;

    if((element == 0xFFFFFF) || (tbl = table->elements[element]) == NULL) {
        tbl = addATableElement(table, &element, status);
        if (U_FAILURE(*status)) {
            return 0;
        }
    }

    uprv_growTable(tbl, status);

    uint32_t offset = 0;


    while(tbl->codePoints[offset] < codePoint && offset<tbl->position) {
        offset++;
    }

    uint32_t i = tbl->position;
    for(i = tbl->position; i > offset; i--) {
        tbl->CEs[i] = tbl->CEs[i-1];
        tbl->codePoints[i] = tbl->codePoints[i-1];
    }

    tbl->CEs[offset] = value;
    tbl->codePoints[offset] = codePoint;

    tbl->position++;

    return(constructContractCE(table->currentTag, element));
}


/* adds more contractions in table. If element is non existant, it creates on. Returns element handle */
U_CAPI uint32_t  U_EXPORT2
uprv_cnttab_addContraction(CntTable *table, uint32_t element, UChar codePoint, uint32_t value, UErrorCode *status) {

    element &= 0xFFFFFF;

    ContractionTable *tbl = NULL;

    if(U_FAILURE(*status)) {
        return 0;
    }

    if((element == 0xFFFFFF) || (tbl = table->elements[element]) == NULL) {
        tbl = addATableElement(table, &element, status);
        if (U_FAILURE(*status)) {
            return 0;
        }
    } 

    uprv_growTable(tbl, status);

    tbl->CEs[tbl->position] = value;
    tbl->codePoints[tbl->position] = codePoint;

    tbl->position++;

    return(constructContractCE(table->currentTag, element));
}

/* sets a part of contraction sequence in table. If element is non existant, it creates on. Returns element handle */
U_CAPI uint32_t  U_EXPORT2
uprv_cnttab_setContraction(CntTable *table, uint32_t element, uint32_t offset, UChar codePoint, uint32_t value, UErrorCode *status) {

    element &= 0xFFFFFF;
    ContractionTable *tbl = NULL;

    if(U_FAILURE(*status)) {
        return 0;
    }

    if((element == 0xFFFFFF) || (tbl = table->elements[element]) == NULL) {
        tbl = addATableElement(table, &element, status);
        if (U_FAILURE(*status)) {
            return 0;
        }
        
    }

    if(offset >= tbl->size) {
        *status = U_INDEX_OUTOFBOUNDS_ERROR;
        return 0;
    }
    tbl->CEs[offset] = value;
    tbl->codePoints[offset] = codePoint;

    //return(offset);
    return(constructContractCE(table->currentTag, element));
}

static ContractionTable *_cnttab_getContractionTable(CntTable *table, uint32_t element) {
    element &= 0xFFFFFF;
    ContractionTable *tbl = NULL;

    if(element != 0xFFFFFF) {
        tbl = table->elements[element]; /* This could also return NULL */
    }
    return tbl;
}

static int32_t _cnttab_findCP(ContractionTable *tbl, UChar codePoint) {
    uint32_t position = 0;
    if(tbl == NULL) {
        return -1;
    }

    while(codePoint > tbl->codePoints[position]) {
        position++;
        if(position > tbl->position) {
            return -1;
        }
    }
    if (codePoint == tbl->codePoints[position]) {
        return position;
    } else {
        return -1;
    }
}

static uint32_t _cnttab_getCE(ContractionTable *tbl, int32_t position) {
    if(tbl == NULL) {
        return UCOL_NOT_FOUND;
    }
    if((uint32_t)position > tbl->position || position == -1) {
        return UCOL_NOT_FOUND;
    } else {
        return tbl->CEs[position];
    }
}

U_CAPI int32_t  U_EXPORT2
uprv_cnttab_findCP(CntTable *table, uint32_t element, UChar codePoint, UErrorCode *status) {

    if(U_FAILURE(*status)) {
        return 0;
    }

    return _cnttab_findCP(_cnttab_getContractionTable(table, element), codePoint);
}

U_CAPI uint32_t  U_EXPORT2
uprv_cnttab_getCE(CntTable *table, uint32_t element, uint32_t position, UErrorCode *status) {
    if(U_FAILURE(*status)) {
        return UCOL_NOT_FOUND;
    }

    return(_cnttab_getCE(_cnttab_getContractionTable(table, element), position));
}

U_CAPI uint32_t  U_EXPORT2
uprv_cnttab_findCE(CntTable *table, uint32_t element, UChar codePoint, UErrorCode *status) {
    if(U_FAILURE(*status)) {
        return UCOL_NOT_FOUND;
    }
    ContractionTable *tbl = _cnttab_getContractionTable(table, element);
    return _cnttab_getCE(tbl, _cnttab_findCP(tbl, codePoint));
}

U_CAPI UBool  U_EXPORT2
uprv_cnttab_isTailored(CntTable *table, uint32_t element, UChar *ztString, UErrorCode *status) {
    if(U_FAILURE(*status)) {
        return FALSE;
    }

    while(*(ztString)!=0) {
        element = uprv_cnttab_findCE(table, element, *(ztString), status);
        if(element == UCOL_NOT_FOUND) {
            return FALSE;
        }
        if(!isCntTableElement(element)) {
            return TRUE;
        }
        ztString++;
    }
    return (UBool)(uprv_cnttab_getCE(table, element, 0, status) != UCOL_NOT_FOUND);
}

U_CAPI uint32_t  U_EXPORT2
uprv_cnttab_changeContraction(CntTable *table, uint32_t element, UChar codePoint, uint32_t newCE, UErrorCode *status) {

    element &= 0xFFFFFF;
    ContractionTable *tbl = NULL;

    if(U_FAILURE(*status)) {
        return 0;
    }

    if((element == 0xFFFFFF) || (tbl = table->elements[element]) == NULL) {
        return 0;
    }

    uint32_t position = 0;

    while(codePoint > tbl->codePoints[position]) {
        position++;
        if(position > tbl->position) {
            return UCOL_NOT_FOUND;
        }
    }
    if (codePoint == tbl->codePoints[position]) {
        tbl->CEs[position] = newCE;
        return element;
    } else {
        return UCOL_NOT_FOUND;
    }
}

#endif /* #if !UCONFIG_NO_COLLATION */