~ubuntu-branches/ubuntu/gutsy/icu/gutsy-updates

« back to all changes in this revision

Viewing changes to source/common/uset.c

  • Committer: Package Import Robot
  • Author(s): Jay Berkenbilt
  • Date: 2005-11-19 11:29:31 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20051119112931-vcizkrp10tli4enw
Tags: 3.4-3
Explicitly build with g++ 3.4.  The current ICU fails its test suite
with 4.0 but not with 3.4.  Future versions should work properly with
4.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
*******************************************************************************
3
 
*
4
 
*   Copyright (C) 2002, International Business Machines
5
 
*   Corporation and others.  All Rights Reserved.
6
 
*
7
 
*******************************************************************************
8
 
*   file name:  uset.c
9
 
*   encoding:   US-ASCII
10
 
*   tab size:   8 (not used)
11
 
*   indentation:4
12
 
*
13
 
*   created on: 2002mar07
14
 
*   created by: Markus W. Scherer
15
 
*
16
 
*   Poor man's C version of UnicodeSet, with only basic functions.
17
 
*   The main data structure, the array of range limits, is
18
 
*   the same as in UnicodeSet, except that the HIGH value is not stored.
19
 
*
20
 
*   There are functions to efficiently serialize a USet into an array of uint16_t
21
 
*   and functions to use such a serialized form efficiently without
22
 
*   instantiating a new USet.
23
 
*
24
 
*   If we needed more of UnicodeSet's functionality, then we should
25
 
*   move UnicodeSet from the i18n to the common library and
26
 
*   use it directly.
27
 
*   The only part of this code that would still be useful is the serialization
28
 
*   and the functions that use the serialized form directly.
29
 
*/
30
 
 
31
 
#include "unicode/utypes.h"
32
 
#include "cmemory.h"
33
 
#include "uset.h"
34
 
 
35
 
#define USET_STATIC_CAPACITY 12
36
 
#define USET_GROW_DELTA 20
37
 
 
38
 
struct USet {
39
 
    UChar32 *array;
40
 
    int32_t length, capacity;
41
 
    UChar32 staticBuffer[USET_STATIC_CAPACITY];
42
 
};
43
 
 
44
 
U_CAPI USet * U_EXPORT2
45
 
uset_open(UChar32 start, UChar32 limit) {
46
 
    USet *set;
47
 
 
48
 
    set=(USet *)uprv_malloc(sizeof(USet));
49
 
    if(set!=NULL) {
50
 
        /* initialize to an empty set */
51
 
        set->array=set->staticBuffer;
52
 
        set->length=0;
53
 
        set->capacity=USET_STATIC_CAPACITY;
54
 
 
55
 
        /* set initial range */
56
 
        if(start<=0) {
57
 
            start=0; /* UChar32 may be signed! */
58
 
        }
59
 
        if(limit>0x110000) {
60
 
            limit=0x110000;
61
 
        }
62
 
        if(start<limit) {
63
 
            set->array[0]=start;
64
 
            if(limit<0x110000) {
65
 
                set->array[1]=limit;
66
 
                set->length=2;
67
 
            } else {
68
 
                set->length=1;
69
 
            }
70
 
        }
71
 
    }
72
 
    return set;
73
 
}
74
 
 
75
 
U_CAPI void U_EXPORT2
76
 
uset_close(USet *set) {
77
 
    if(set!=NULL) {
78
 
        if(set->array!=set->staticBuffer) {
79
 
            uprv_free(set->array);
80
 
        }
81
 
        uprv_free(set);
82
 
    }
83
 
}
84
 
 
85
 
static U_INLINE int32_t
86
 
findChar(const UChar32 *array, int32_t length, UChar32 c) {
87
 
    int32_t i;
88
 
 
89
 
    /* check the last range limit first for more efficient appending */
90
 
    if(length>0) {
91
 
        if(c>=array[length-1]) {
92
 
            return length;
93
 
        }
94
 
 
95
 
        /* do not check the last range limit again in the loop below */
96
 
        --length;
97
 
    }
98
 
 
99
 
    for(i=0; i<length && c>=array[i]; ++i) {}
100
 
    return i;
101
 
}
102
 
 
103
 
static UBool
104
 
addRemove(USet *set, UChar32 c, int32_t doRemove) {
105
 
    int32_t i, length, more;
106
 
 
107
 
    if(set==NULL || (uint32_t)c>0x10ffff) {
108
 
        return FALSE;
109
 
    }
110
 
 
111
 
    length=set->length;
112
 
    i=findChar(set->array, length, c);
113
 
    if((i&1)^doRemove) {
114
 
        /* c is already in the set */
115
 
        return TRUE;
116
 
    }
117
 
 
118
 
    /* how many more array items do we need? */
119
 
    if(i<length && (c+1)==set->array[i]) {
120
 
        /* c is just before the following range, extend that in-place by one */
121
 
        set->array[i]=c;
122
 
        if(i>0) {
123
 
            --i;
124
 
            if(c==set->array[i]) {
125
 
                /* the previous range collapsed, remove it */
126
 
                set->length=length-=2;
127
 
                if(i<length) {
128
 
                    uprv_memmove(set->array+i, set->array+i+2, (length-i)*4);
129
 
                }
130
 
            }
131
 
        }
132
 
        return TRUE;
133
 
    } else if(i>0 && c==set->array[i-1]) {
134
 
        /* c is just after the previous range, extend that in-place by one */
135
 
        if(++c<=0x10ffff) {
136
 
            set->array[i-1]=c;
137
 
            if(i<length && c==set->array[i]) {
138
 
                /* the following range collapsed, remove it */
139
 
                --i;
140
 
                set->length=length-=2;
141
 
                if(i<length) {
142
 
                    uprv_memmove(set->array+i, set->array+i+2, (length-i)*4);
143
 
                }
144
 
            }
145
 
        } else {
146
 
            /* extend the previous range (had limit 0x10ffff) to the end of Unicode */
147
 
            set->length=i-1;
148
 
        }
149
 
        return TRUE;
150
 
    } else if(i==length && c==0x10ffff) {
151
 
        /* insert one range limit c */
152
 
        more=1;
153
 
    } else {
154
 
        /* insert two range limits c, c+1 */
155
 
        more=2;
156
 
    }
157
 
 
158
 
    /* insert <more> range limits */
159
 
    if(length+more>set->capacity) {
160
 
        /* reallocate */
161
 
        int32_t newCapacity=set->capacity+set->capacity/2+USET_GROW_DELTA;
162
 
        UChar32 *newArray=(UChar32 *)uprv_malloc(newCapacity*4);
163
 
        if(newArray==NULL) {
164
 
            return FALSE;
165
 
        }
166
 
        set->capacity=newCapacity;
167
 
        uprv_memcpy(newArray, set->array, length*4);
168
 
 
169
 
        if(set->array!=set->staticBuffer) {
170
 
            uprv_free(set->array);
171
 
        }
172
 
        set->array=newArray;
173
 
    }
174
 
 
175
 
    if(i<length) {
176
 
        uprv_memmove(set->array+i+more, set->array+i, (length-i)*4);
177
 
    }
178
 
    set->array[i]=c;
179
 
    if(more==2) {
180
 
        set->array[i+1]=c+1;
181
 
    }
182
 
    set->length+=more;
183
 
 
184
 
    return TRUE;
185
 
}
186
 
 
187
 
U_CAPI UBool U_EXPORT2
188
 
uset_add(USet *set, UChar32 c) {
189
 
    return addRemove(set, c, 0);
190
 
}
191
 
 
192
 
U_CAPI void U_EXPORT2
193
 
uset_remove(USet *set, UChar32 c) {
194
 
    addRemove(set, c, 1);
195
 
}
196
 
 
197
 
U_CAPI UBool U_EXPORT2
198
 
uset_isEmpty(const USet *set) {
199
 
    return set==NULL || set->length<=0;
200
 
}
201
 
 
202
 
U_CAPI UBool U_EXPORT2
203
 
uset_contains(const USet *set, UChar32 c) {
204
 
    int32_t i;
205
 
 
206
 
    if(set==NULL || (uint32_t)c>0x10ffff) {
207
 
        return FALSE;
208
 
    }
209
 
 
210
 
    i=findChar(set->array, set->length, c);
211
 
    return (UBool)(i&1);
212
 
}
213
 
 
214
 
U_CAPI int32_t U_EXPORT2
215
 
uset_containsOne(const USet *set) {
216
 
    if( set!=NULL &&
217
 
        ((set->length==2 && set->array[0]==(set->array[1]-1)) ||
218
 
         (set->length==1 && set->array[0]==0x10ffff))
219
 
    ) {
220
 
        return (int32_t)set->array[0];
221
 
    } else {
222
 
        return -1;
223
 
    }
224
 
}
225
 
 
226
 
U_CAPI int32_t U_EXPORT2
227
 
uset_countRanges(const USet *set) {
228
 
    if(set==NULL) {
229
 
        return 0;
230
 
    } else {
231
 
        return (set->length+1)/2;
232
 
    }
233
 
}
234
 
 
235
 
U_CAPI UBool U_EXPORT2
236
 
uset_getRange(const USet *set, int32_t rangeIndex,
237
 
              UChar32 *pStart, UChar32 *pLimit) {
238
 
    if(set==NULL || rangeIndex<0) {
239
 
        return FALSE;
240
 
    }
241
 
 
242
 
    rangeIndex*=2;
243
 
    if(rangeIndex<set->length) {
244
 
        *pStart=set->array[rangeIndex++];
245
 
        if(rangeIndex<set->length) {
246
 
            *pLimit=set->array[rangeIndex];
247
 
        } else {
248
 
            *pLimit=0x110000;
249
 
        }
250
 
        return TRUE;
251
 
    } else {
252
 
        return FALSE;
253
 
    }
254
 
}
255
 
 
256
 
/*
257
 
 * Serialize a USet into 16-bit units.
258
 
 * Store BMP code points as themselves with one 16-bit unit each.
259
 
 *
260
 
 * Important: the code points in the array are in ascending order,
261
 
 * therefore all BMP code points precede all supplementary code points.
262
 
 *
263
 
 * Store each supplementary code point in 2 16-bit units,
264
 
 * simply with higher-then-lower 16-bit halfs.
265
 
 *
266
 
 * Precede the entire list with the length.
267
 
 * If there are supplementary code points, then set bit 15 in the length
268
 
 * and add the bmpLength between it and the array.
269
 
 *
270
 
 * In other words:
271
 
 * - all BMP:            (length=bmpLength) BMP, .., BMP
272
 
 * - some supplementary: (length|0x8000) (bmpLength<length) BMP, .., BMP, supp-high, supp-low, ..
273
 
 */
274
 
U_CAPI int32_t U_EXPORT2
275
 
uset_serialize(const USet *set, uint16_t *dest, int32_t destCapacity, UErrorCode *pErrorCode) {
276
 
    int32_t bmpLength, length, destLength;
277
 
 
278
 
    if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
279
 
        return 0;
280
 
    }
281
 
 
282
 
    if(set==NULL || destCapacity<0 || (destCapacity>0 && dest==NULL)) {
283
 
        *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
284
 
        return 0;
285
 
    }
286
 
 
287
 
    /* count necessary 16-bit units */
288
 
    length=set->length;
289
 
    if(length==0) {
290
 
        /* empty set */
291
 
        if(destCapacity>0) {
292
 
            *dest=0;
293
 
        }
294
 
        return 1;
295
 
    }
296
 
    /* now length>0 */
297
 
 
298
 
    if(set->array[length-1]<=0xffff) {
299
 
        /* all BMP */
300
 
        bmpLength=length;
301
 
    } else if(set->array[0]>=0x10000) {
302
 
        /* all supplementary */
303
 
        bmpLength=0;
304
 
        length*=2;
305
 
    } else {
306
 
        /* some BMP, some supplementary */
307
 
        for(bmpLength=0; bmpLength<length && set->array[bmpLength]<=0xffff; ++bmpLength) {}
308
 
        length=bmpLength+2*(length-bmpLength);
309
 
    }
310
 
 
311
 
    /* length: number of 16-bit array units */
312
 
    if(length>0x7fff) {
313
 
        /* there are only 15 bits for the length in the first serialized word */
314
 
        *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
315
 
        return 0;
316
 
    }
317
 
 
318
 
    /*
319
 
     * total serialized length:
320
 
     * number of 16-bit array units (length) +
321
 
     * 1 length unit (always) +
322
 
     * 1 bmpLength unit (if there are supplementary values)
323
 
     */
324
 
    destLength=length+1+(length>bmpLength);
325
 
    if(destLength<=destCapacity) {
326
 
        const UChar32 *p;
327
 
        int32_t i;
328
 
 
329
 
        *dest=(uint16_t)length;
330
 
        if(length>bmpLength) {
331
 
            *dest|=0x8000;
332
 
            *++dest=(uint16_t)bmpLength;
333
 
        }
334
 
        ++dest;
335
 
 
336
 
        /* write the BMP part of the array */
337
 
        p=set->array;
338
 
        for(i=0; i<bmpLength; ++i) {
339
 
            *dest++=(uint16_t)*p++;
340
 
        }
341
 
 
342
 
        /* write the supplementary part of the array */
343
 
        for(; i<length; i+=2) {
344
 
            *dest++=(uint16_t)(*p>>16);
345
 
            *dest++=(uint16_t)*p++;
346
 
        }
347
 
    } else {
348
 
        *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
349
 
    }
350
 
    return destLength;
351
 
}
352
 
 
353
 
U_CAPI UBool U_EXPORT2
354
 
uset_getSerializedSet(USerializedSet *fillSet, const uint16_t *src, int32_t srcCapacity) {
355
 
    int32_t length;
356
 
 
357
 
    if(fillSet==NULL) {
358
 
        return FALSE;
359
 
    }
360
 
    if(src==NULL || srcCapacity<=0) {
361
 
        fillSet->length=fillSet->bmpLength=0;
362
 
        return FALSE;
363
 
    }
364
 
 
365
 
    length=*src++;
366
 
    if(length&0x8000) {
367
 
        /* there are supplementary values */
368
 
        length&=0x7fff;
369
 
        if(srcCapacity<(2+length)) {
370
 
            fillSet->length=fillSet->bmpLength=0;
371
 
            return FALSE;
372
 
        }
373
 
        fillSet->bmpLength=*src++;
374
 
    } else {
375
 
        /* only BMP values */
376
 
        if(srcCapacity<(1+length)) {
377
 
            fillSet->length=fillSet->bmpLength=0;
378
 
            return FALSE;
379
 
        }
380
 
        fillSet->bmpLength=length;
381
 
    }
382
 
    fillSet->array=src;
383
 
    fillSet->length=length;
384
 
    return TRUE;
385
 
}
386
 
 
387
 
U_CAPI void U_EXPORT2
388
 
uset_setSerializedToOne(USerializedSet *fillSet, UChar32 c) {
389
 
    if(fillSet==NULL || (uint32_t)c>0x10ffff) {
390
 
        return;
391
 
    }
392
 
 
393
 
    fillSet->array=fillSet->staticArray;
394
 
    if(c<0xffff) {
395
 
        fillSet->bmpLength=fillSet->length=2;
396
 
        fillSet->staticArray[0]=(uint16_t)c;
397
 
        fillSet->staticArray[1]=(uint16_t)c+1;
398
 
    } else if(c==0xffff) {
399
 
        fillSet->bmpLength=1;
400
 
        fillSet->length=3;
401
 
        fillSet->staticArray[0]=0xffff;
402
 
        fillSet->staticArray[1]=1;
403
 
        fillSet->staticArray[2]=0;
404
 
    } else if(c<0x10ffff) {
405
 
        fillSet->bmpLength=0;
406
 
        fillSet->length=4;
407
 
        fillSet->staticArray[0]=(uint16_t)(c>>16);
408
 
        fillSet->staticArray[1]=(uint16_t)c;
409
 
        ++c;
410
 
        fillSet->staticArray[2]=(uint16_t)(c>>16);
411
 
        fillSet->staticArray[3]=(uint16_t)c;
412
 
    } else /* c==0x10ffff */ {
413
 
        fillSet->bmpLength=0;
414
 
        fillSet->length=2;
415
 
        fillSet->staticArray[0]=0x10;
416
 
        fillSet->staticArray[1]=0xffff;
417
 
    }
418
 
}
419
 
 
420
 
U_CAPI UBool U_EXPORT2
421
 
uset_serializedContains(const USerializedSet *set, UChar32 c) {
422
 
    const uint16_t *array;
423
 
 
424
 
    if(set==NULL || (uint32_t)c>0x10ffff) {
425
 
        return FALSE;
426
 
    }
427
 
 
428
 
    array=set->array;
429
 
    if(c<=0xffff) {
430
 
        /* find c in the BMP part */
431
 
        int32_t i, bmpLength=set->bmpLength;
432
 
        for(i=0; i<bmpLength && (uint16_t)c>=array[i]; ++i) {}
433
 
        return (UBool)(i&1);
434
 
    } else {
435
 
        /* find c in the supplementary part */
436
 
        int32_t i, length=set->length;
437
 
        uint16_t high=(uint16_t)(c>>16), low=(uint16_t)c;
438
 
        for(i=set->bmpLength;
439
 
            i<length && (high>array[i] || (high==array[i] && low>=array[i+1]));
440
 
            i+=2) {}
441
 
 
442
 
        /* count pairs of 16-bit units even per BMP and check if the number of pairs is odd */
443
 
        return (UBool)(((i+set->bmpLength)&2)!=0);
444
 
    }
445
 
}
446
 
 
447
 
U_CAPI int32_t U_EXPORT2
448
 
uset_countSerializedRanges(const USerializedSet *set) {
449
 
    if(set==NULL) {
450
 
        return 0;
451
 
    }
452
 
 
453
 
    return (set->bmpLength+(set->length-set->bmpLength)/2+1)/2;
454
 
}
455
 
 
456
 
U_CAPI UBool U_EXPORT2
457
 
uset_getSerializedRange(const USerializedSet *set, int32_t rangeIndex,
458
 
                        UChar32 *pStart, UChar32 *pLimit) {
459
 
    const uint16_t *array;
460
 
    int32_t bmpLength, length;
461
 
 
462
 
    if(set==NULL || rangeIndex<0 || pStart==NULL || pLimit==NULL) {
463
 
        return FALSE;
464
 
    }
465
 
 
466
 
    array=set->array;
467
 
    length=set->length;
468
 
    bmpLength=set->bmpLength;
469
 
 
470
 
    rangeIndex*=2; /* address start/limit pairs */
471
 
    if(rangeIndex<bmpLength) {
472
 
        *pStart=array[rangeIndex++];
473
 
        if(rangeIndex<bmpLength) {
474
 
            *pLimit=array[rangeIndex];
475
 
        } else if(rangeIndex<length) {
476
 
            *pLimit=(((int32_t)array[rangeIndex])<<16)|array[rangeIndex+1];
477
 
        } else {
478
 
            *pLimit=0x110000;
479
 
        }
480
 
        return TRUE;
481
 
    } else {
482
 
        rangeIndex-=bmpLength;
483
 
        rangeIndex*=2; /* address pairs of pairs of units */
484
 
        length-=bmpLength;
485
 
        if(rangeIndex<length) {
486
 
            array+=bmpLength;
487
 
            *pStart=(((int32_t)array[rangeIndex])<<16)|array[rangeIndex+1];
488
 
            rangeIndex+=2;
489
 
            if(rangeIndex<length) {
490
 
                *pLimit=(((int32_t)array[rangeIndex])<<16)|array[rangeIndex+1];
491
 
            } else {
492
 
                *pLimit=0x110000;
493
 
            }
494
 
            return TRUE;
495
 
        } else {
496
 
            return FALSE;
497
 
        }
498
 
    }
499
 
}