Coverage Report

Created: 2025-06-24 06:43

/src/icu/source/common/ubidi_props.cpp
Line
Count
Source (jump to first uncovered line)
1
// © 2016 and later: Unicode, Inc. and others.
2
// License & terms of use: http://www.unicode.org/copyright.html
3
/*
4
*******************************************************************************
5
*
6
*   Copyright (C) 2004-2014, International Business Machines
7
*   Corporation and others.  All Rights Reserved.
8
*
9
*******************************************************************************
10
*   file name:  ubidi_props.c
11
*   encoding:   UTF-8
12
*   tab size:   8 (not used)
13
*   indentation:4
14
*
15
*   created on: 2004dec30
16
*   created by: Markus W. Scherer
17
*
18
*   Low-level Unicode bidi/shaping properties access.
19
*/
20
21
#include "unicode/utypes.h"
22
#include "unicode/uset.h"
23
#include "unicode/udata.h" /* UDataInfo */
24
#include "ucmndata.h" /* DataHeader */
25
#include "udatamem.h"
26
#include "uassert.h"
27
#include "cmemory.h"
28
#include "utrie2.h"
29
#include "ubidi_props.h"
30
#include "ucln_cmn.h"
31
32
struct UBiDiProps {
33
    UDataMemory *mem;
34
    const int32_t *indexes;
35
    const uint32_t *mirrors;
36
    const uint8_t *jgArray;
37
    const uint8_t *jgArray2;
38
39
    UTrie2 trie;
40
    uint8_t formatVersion[4];
41
};
42
43
/* ubidi_props_data.h is machine-generated by genbidi --csource */
44
#define INCLUDED_FROM_UBIDI_PROPS_C
45
#include "ubidi_props_data.h"
46
47
/* set of property starts for UnicodeSet ------------------------------------ */
48
49
static UBool U_CALLCONV
50
0
_enumPropertyStartsRange(const void *context, UChar32 start, UChar32 end, uint32_t value) {
51
0
    (void)end;
52
0
    (void)value;
53
    /* add the start code point to the USet */
54
0
    const USetAdder *sa=(const USetAdder *)context;
55
0
    sa->add(sa->set, start);
56
0
    return TRUE;
57
0
}
58
59
U_CFUNC void
60
0
ubidi_addPropertyStarts(const USetAdder *sa, UErrorCode *pErrorCode) {
61
0
    int32_t i, length;
62
0
    UChar32 c, start, limit;
63
64
0
    const uint8_t *jgArray;
65
0
    uint8_t prev, jg;
66
67
0
    if(U_FAILURE(*pErrorCode)) {
68
0
        return;
69
0
    }
70
71
    /* add the start code point of each same-value range of the trie */
72
0
    utrie2_enum(&ubidi_props_singleton.trie, NULL, _enumPropertyStartsRange, sa);
73
74
    /* add the code points from the bidi mirroring table */
75
0
    length=ubidi_props_singleton.indexes[UBIDI_IX_MIRROR_LENGTH];
76
0
    for(i=0; i<length; ++i) {
77
0
        c=UBIDI_GET_MIRROR_CODE_POINT(ubidi_props_singleton.mirrors[i]);
78
0
        sa->addRange(sa->set, c, c+1);
79
0
    }
80
81
    /* add the code points from the Joining_Group array where the value changes */
82
0
    start=ubidi_props_singleton.indexes[UBIDI_IX_JG_START];
83
0
    limit=ubidi_props_singleton.indexes[UBIDI_IX_JG_LIMIT];
84
0
    jgArray=ubidi_props_singleton.jgArray;
85
0
    for(;;) {
86
0
        prev=0;
87
0
        while(start<limit) {
88
0
            jg=*jgArray++;
89
0
            if(jg!=prev) {
90
0
                sa->add(sa->set, start);
91
0
                prev=jg;
92
0
            }
93
0
            ++start;
94
0
        }
95
0
        if(prev!=0) {
96
            /* add the limit code point if the last value was not 0 (it is now start==limit) */
97
0
            sa->add(sa->set, limit);
98
0
        }
99
0
        if(limit==ubidi_props_singleton.indexes[UBIDI_IX_JG_LIMIT]) {
100
            /* switch to the second Joining_Group range */
101
0
            start=ubidi_props_singleton.indexes[UBIDI_IX_JG_START2];
102
0
            limit=ubidi_props_singleton.indexes[UBIDI_IX_JG_LIMIT2];
103
0
            jgArray=ubidi_props_singleton.jgArray2;
104
0
        } else {
105
0
            break;
106
0
        }
107
0
    }
108
109
    /* add code points with hardcoded properties, plus the ones following them */
110
111
    /* (none right now) */
112
0
}
113
114
/* property access functions ------------------------------------------------ */
115
116
U_CFUNC int32_t
117
0
ubidi_getMaxValue(UProperty which) {
118
0
    int32_t max=ubidi_props_singleton.indexes[UBIDI_MAX_VALUES_INDEX];
119
0
    switch(which) {
120
0
    case UCHAR_BIDI_CLASS:
121
0
        return (max&UBIDI_CLASS_MASK);
122
0
    case UCHAR_JOINING_GROUP:
123
0
        return (max&UBIDI_MAX_JG_MASK)>>UBIDI_MAX_JG_SHIFT;
124
0
    case UCHAR_JOINING_TYPE:
125
0
        return (max&UBIDI_JT_MASK)>>UBIDI_JT_SHIFT;
126
0
    case UCHAR_BIDI_PAIRED_BRACKET_TYPE:
127
0
        return (max&UBIDI_BPT_MASK)>>UBIDI_BPT_SHIFT;
128
0
    default:
129
0
        return -1; /* undefined */
130
0
    }
131
0
}
132
133
U_CAPI UCharDirection
134
0
ubidi_getClass(UChar32 c) {
135
0
    uint16_t props=UTRIE2_GET16(&ubidi_props_singleton.trie, c);
136
0
    return (UCharDirection)UBIDI_GET_CLASS(props);
137
0
}
138
139
U_CFUNC UBool
140
0
ubidi_isMirrored(UChar32 c) {
141
0
    uint16_t props=UTRIE2_GET16(&ubidi_props_singleton.trie, c);
142
0
    return (UBool)UBIDI_GET_FLAG(props, UBIDI_IS_MIRRORED_SHIFT);
143
0
}
144
145
static UChar32
146
0
getMirror(UChar32 c, uint16_t props) {
147
0
    int32_t delta=UBIDI_GET_MIRROR_DELTA(props);
148
0
    if(delta!=UBIDI_ESC_MIRROR_DELTA) {
149
0
        return c+delta;
150
0
    } else {
151
        /* look for mirror code point in the mirrors[] table */
152
0
        const uint32_t *mirrors;
153
0
        uint32_t m;
154
0
        int32_t i, length;
155
0
        UChar32 c2;
156
157
0
        mirrors=ubidi_props_singleton.mirrors;
158
0
        length=ubidi_props_singleton.indexes[UBIDI_IX_MIRROR_LENGTH];
159
160
        /* linear search */
161
0
        for(i=0; i<length; ++i) {
162
0
            m=mirrors[i];
163
0
            c2=UBIDI_GET_MIRROR_CODE_POINT(m);
164
0
            if(c==c2) {
165
                /* found c, return its mirror code point using the index in m */
166
0
                return UBIDI_GET_MIRROR_CODE_POINT(mirrors[UBIDI_GET_MIRROR_INDEX(m)]);
167
0
            } else if(c<c2) {
168
0
                break;
169
0
            }
170
0
        }
171
172
        /* c not found, return it itself */
173
0
        return c;
174
0
    }
175
0
}
176
177
U_CFUNC UChar32
178
0
ubidi_getMirror(UChar32 c) {
179
0
    uint16_t props=UTRIE2_GET16(&ubidi_props_singleton.trie, c);
180
0
    return getMirror(c, props);
181
0
}
182
183
U_CFUNC UBool
184
0
ubidi_isBidiControl(UChar32 c) {
185
0
    uint16_t props=UTRIE2_GET16(&ubidi_props_singleton.trie, c);
186
0
    return (UBool)UBIDI_GET_FLAG(props, UBIDI_BIDI_CONTROL_SHIFT);
187
0
}
188
189
U_CFUNC UBool
190
0
ubidi_isJoinControl(UChar32 c) {
191
0
    uint16_t props=UTRIE2_GET16(&ubidi_props_singleton.trie, c);
192
0
    return (UBool)UBIDI_GET_FLAG(props, UBIDI_JOIN_CONTROL_SHIFT);
193
0
}
194
195
U_CFUNC UJoiningType
196
0
ubidi_getJoiningType(UChar32 c) {
197
0
    uint16_t props=UTRIE2_GET16(&ubidi_props_singleton.trie, c);
198
0
    return (UJoiningType)((props&UBIDI_JT_MASK)>>UBIDI_JT_SHIFT);
199
0
}
200
201
U_CFUNC UJoiningGroup
202
0
ubidi_getJoiningGroup(UChar32 c) {
203
0
    UChar32 start, limit;
204
205
0
    start=ubidi_props_singleton.indexes[UBIDI_IX_JG_START];
206
0
    limit=ubidi_props_singleton.indexes[UBIDI_IX_JG_LIMIT];
207
0
    if(start<=c && c<limit) {
208
0
        return (UJoiningGroup)ubidi_props_singleton.jgArray[c-start];
209
0
    }
210
0
    start=ubidi_props_singleton.indexes[UBIDI_IX_JG_START2];
211
0
    limit=ubidi_props_singleton.indexes[UBIDI_IX_JG_LIMIT2];
212
0
    if(start<=c && c<limit) {
213
0
        return (UJoiningGroup)ubidi_props_singleton.jgArray2[c-start];
214
0
    }
215
0
    return U_JG_NO_JOINING_GROUP;
216
0
}
217
218
U_CFUNC UBidiPairedBracketType
219
0
ubidi_getPairedBracketType(UChar32 c) {
220
0
    uint16_t props=UTRIE2_GET16(&ubidi_props_singleton.trie, c);
221
0
    return (UBidiPairedBracketType)((props&UBIDI_BPT_MASK)>>UBIDI_BPT_SHIFT);
222
0
}
223
224
U_CFUNC UChar32
225
0
ubidi_getPairedBracket(UChar32 c) {
226
0
    uint16_t props=UTRIE2_GET16(&ubidi_props_singleton.trie, c);
227
0
    if((props&UBIDI_BPT_MASK)==0) {
228
0
        return c;
229
0
    } else {
230
0
        return getMirror(c, props);
231
0
    }
232
0
}
233
234
/* public API (see uchar.h) ------------------------------------------------- */
235
236
U_CFUNC UCharDirection
237
0
u_charDirection(UChar32 c) {
238
0
    return ubidi_getClass(c);
239
0
}
240
241
U_CFUNC UBool
242
0
u_isMirrored(UChar32 c) {
243
0
    return ubidi_isMirrored(c);
244
0
}
245
246
U_CFUNC UChar32
247
0
u_charMirror(UChar32 c) {
248
0
    return ubidi_getMirror(c);
249
0
}
250
251
U_CAPI UChar32 U_EXPORT2
252
0
u_getBidiPairedBracket(UChar32 c) {
253
0
    return ubidi_getPairedBracket(c);
254
0
}