intl/icu/source/common/ubidi_props.c

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

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

mercurial