Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | ******************************************************************************* |
michael@0 | 3 | * |
michael@0 | 4 | * Copyright (C) 1999-2012, International Business Machines |
michael@0 | 5 | * Corporation and others. All Rights Reserved. |
michael@0 | 6 | * |
michael@0 | 7 | ******************************************************************************* |
michael@0 | 8 | * file name: utf16.h |
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: 1999sep09 |
michael@0 | 14 | * created by: Markus W. Scherer |
michael@0 | 15 | */ |
michael@0 | 16 | |
michael@0 | 17 | /** |
michael@0 | 18 | * \file |
michael@0 | 19 | * \brief C API: 16-bit Unicode handling macros |
michael@0 | 20 | * |
michael@0 | 21 | * This file defines macros to deal with 16-bit Unicode (UTF-16) code units and strings. |
michael@0 | 22 | * |
michael@0 | 23 | * For more information see utf.h and the ICU User Guide Strings chapter |
michael@0 | 24 | * (http://userguide.icu-project.org/strings). |
michael@0 | 25 | * |
michael@0 | 26 | * <em>Usage:</em> |
michael@0 | 27 | * ICU coding guidelines for if() statements should be followed when using these macros. |
michael@0 | 28 | * Compound statements (curly braces {}) must be used for if-else-while... |
michael@0 | 29 | * bodies and all macro statements should be terminated with semicolon. |
michael@0 | 30 | */ |
michael@0 | 31 | |
michael@0 | 32 | #ifndef __UTF16_H__ |
michael@0 | 33 | #define __UTF16_H__ |
michael@0 | 34 | |
michael@0 | 35 | #include "unicode/umachine.h" |
michael@0 | 36 | #ifndef __UTF_H__ |
michael@0 | 37 | # include "unicode/utf.h" |
michael@0 | 38 | #endif |
michael@0 | 39 | |
michael@0 | 40 | /* single-code point definitions -------------------------------------------- */ |
michael@0 | 41 | |
michael@0 | 42 | /** |
michael@0 | 43 | * Does this code unit alone encode a code point (BMP, not a surrogate)? |
michael@0 | 44 | * @param c 16-bit code unit |
michael@0 | 45 | * @return TRUE or FALSE |
michael@0 | 46 | * @stable ICU 2.4 |
michael@0 | 47 | */ |
michael@0 | 48 | #define U16_IS_SINGLE(c) !U_IS_SURROGATE(c) |
michael@0 | 49 | |
michael@0 | 50 | /** |
michael@0 | 51 | * Is this code unit a lead surrogate (U+d800..U+dbff)? |
michael@0 | 52 | * @param c 16-bit code unit |
michael@0 | 53 | * @return TRUE or FALSE |
michael@0 | 54 | * @stable ICU 2.4 |
michael@0 | 55 | */ |
michael@0 | 56 | #define U16_IS_LEAD(c) (((c)&0xfffffc00)==0xd800) |
michael@0 | 57 | |
michael@0 | 58 | /** |
michael@0 | 59 | * Is this code unit a trail surrogate (U+dc00..U+dfff)? |
michael@0 | 60 | * @param c 16-bit code unit |
michael@0 | 61 | * @return TRUE or FALSE |
michael@0 | 62 | * @stable ICU 2.4 |
michael@0 | 63 | */ |
michael@0 | 64 | #define U16_IS_TRAIL(c) (((c)&0xfffffc00)==0xdc00) |
michael@0 | 65 | |
michael@0 | 66 | /** |
michael@0 | 67 | * Is this code unit a surrogate (U+d800..U+dfff)? |
michael@0 | 68 | * @param c 16-bit code unit |
michael@0 | 69 | * @return TRUE or FALSE |
michael@0 | 70 | * @stable ICU 2.4 |
michael@0 | 71 | */ |
michael@0 | 72 | #define U16_IS_SURROGATE(c) U_IS_SURROGATE(c) |
michael@0 | 73 | |
michael@0 | 74 | /** |
michael@0 | 75 | * Assuming c is a surrogate code point (U16_IS_SURROGATE(c)), |
michael@0 | 76 | * is it a lead surrogate? |
michael@0 | 77 | * @param c 16-bit code unit |
michael@0 | 78 | * @return TRUE or FALSE |
michael@0 | 79 | * @stable ICU 2.4 |
michael@0 | 80 | */ |
michael@0 | 81 | #define U16_IS_SURROGATE_LEAD(c) (((c)&0x400)==0) |
michael@0 | 82 | |
michael@0 | 83 | /** |
michael@0 | 84 | * Assuming c is a surrogate code point (U16_IS_SURROGATE(c)), |
michael@0 | 85 | * is it a trail surrogate? |
michael@0 | 86 | * @param c 16-bit code unit |
michael@0 | 87 | * @return TRUE or FALSE |
michael@0 | 88 | * @stable ICU 4.2 |
michael@0 | 89 | */ |
michael@0 | 90 | #define U16_IS_SURROGATE_TRAIL(c) (((c)&0x400)!=0) |
michael@0 | 91 | |
michael@0 | 92 | /** |
michael@0 | 93 | * Helper constant for U16_GET_SUPPLEMENTARY. |
michael@0 | 94 | * @internal |
michael@0 | 95 | */ |
michael@0 | 96 | #define U16_SURROGATE_OFFSET ((0xd800<<10UL)+0xdc00-0x10000) |
michael@0 | 97 | |
michael@0 | 98 | /** |
michael@0 | 99 | * Get a supplementary code point value (U+10000..U+10ffff) |
michael@0 | 100 | * from its lead and trail surrogates. |
michael@0 | 101 | * The result is undefined if the input values are not |
michael@0 | 102 | * lead and trail surrogates. |
michael@0 | 103 | * |
michael@0 | 104 | * @param lead lead surrogate (U+d800..U+dbff) |
michael@0 | 105 | * @param trail trail surrogate (U+dc00..U+dfff) |
michael@0 | 106 | * @return supplementary code point (U+10000..U+10ffff) |
michael@0 | 107 | * @stable ICU 2.4 |
michael@0 | 108 | */ |
michael@0 | 109 | #define U16_GET_SUPPLEMENTARY(lead, trail) \ |
michael@0 | 110 | (((UChar32)(lead)<<10UL)+(UChar32)(trail)-U16_SURROGATE_OFFSET) |
michael@0 | 111 | |
michael@0 | 112 | |
michael@0 | 113 | /** |
michael@0 | 114 | * Get the lead surrogate (0xd800..0xdbff) for a |
michael@0 | 115 | * supplementary code point (0x10000..0x10ffff). |
michael@0 | 116 | * @param supplementary 32-bit code point (U+10000..U+10ffff) |
michael@0 | 117 | * @return lead surrogate (U+d800..U+dbff) for supplementary |
michael@0 | 118 | * @stable ICU 2.4 |
michael@0 | 119 | */ |
michael@0 | 120 | #define U16_LEAD(supplementary) (UChar)(((supplementary)>>10)+0xd7c0) |
michael@0 | 121 | |
michael@0 | 122 | /** |
michael@0 | 123 | * Get the trail surrogate (0xdc00..0xdfff) for a |
michael@0 | 124 | * supplementary code point (0x10000..0x10ffff). |
michael@0 | 125 | * @param supplementary 32-bit code point (U+10000..U+10ffff) |
michael@0 | 126 | * @return trail surrogate (U+dc00..U+dfff) for supplementary |
michael@0 | 127 | * @stable ICU 2.4 |
michael@0 | 128 | */ |
michael@0 | 129 | #define U16_TRAIL(supplementary) (UChar)(((supplementary)&0x3ff)|0xdc00) |
michael@0 | 130 | |
michael@0 | 131 | /** |
michael@0 | 132 | * How many 16-bit code units are used to encode this Unicode code point? (1 or 2) |
michael@0 | 133 | * The result is not defined if c is not a Unicode code point (U+0000..U+10ffff). |
michael@0 | 134 | * @param c 32-bit code point |
michael@0 | 135 | * @return 1 or 2 |
michael@0 | 136 | * @stable ICU 2.4 |
michael@0 | 137 | */ |
michael@0 | 138 | #define U16_LENGTH(c) ((uint32_t)(c)<=0xffff ? 1 : 2) |
michael@0 | 139 | |
michael@0 | 140 | /** |
michael@0 | 141 | * The maximum number of 16-bit code units per Unicode code point (U+0000..U+10ffff). |
michael@0 | 142 | * @return 2 |
michael@0 | 143 | * @stable ICU 2.4 |
michael@0 | 144 | */ |
michael@0 | 145 | #define U16_MAX_LENGTH 2 |
michael@0 | 146 | |
michael@0 | 147 | /** |
michael@0 | 148 | * Get a code point from a string at a random-access offset, |
michael@0 | 149 | * without changing the offset. |
michael@0 | 150 | * "Unsafe" macro, assumes well-formed UTF-16. |
michael@0 | 151 | * |
michael@0 | 152 | * The offset may point to either the lead or trail surrogate unit |
michael@0 | 153 | * for a supplementary code point, in which case the macro will read |
michael@0 | 154 | * the adjacent matching surrogate as well. |
michael@0 | 155 | * The result is undefined if the offset points to a single, unpaired surrogate. |
michael@0 | 156 | * Iteration through a string is more efficient with U16_NEXT_UNSAFE or U16_NEXT. |
michael@0 | 157 | * |
michael@0 | 158 | * @param s const UChar * string |
michael@0 | 159 | * @param i string offset |
michael@0 | 160 | * @param c output UChar32 variable |
michael@0 | 161 | * @see U16_GET |
michael@0 | 162 | * @stable ICU 2.4 |
michael@0 | 163 | */ |
michael@0 | 164 | #define U16_GET_UNSAFE(s, i, c) { \ |
michael@0 | 165 | (c)=(s)[i]; \ |
michael@0 | 166 | if(U16_IS_SURROGATE(c)) { \ |
michael@0 | 167 | if(U16_IS_SURROGATE_LEAD(c)) { \ |
michael@0 | 168 | (c)=U16_GET_SUPPLEMENTARY((c), (s)[(i)+1]); \ |
michael@0 | 169 | } else { \ |
michael@0 | 170 | (c)=U16_GET_SUPPLEMENTARY((s)[(i)-1], (c)); \ |
michael@0 | 171 | } \ |
michael@0 | 172 | } \ |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | /** |
michael@0 | 176 | * Get a code point from a string at a random-access offset, |
michael@0 | 177 | * without changing the offset. |
michael@0 | 178 | * "Safe" macro, handles unpaired surrogates and checks for string boundaries. |
michael@0 | 179 | * |
michael@0 | 180 | * The offset may point to either the lead or trail surrogate unit |
michael@0 | 181 | * for a supplementary code point, in which case the macro will read |
michael@0 | 182 | * the adjacent matching surrogate as well. |
michael@0 | 183 | * |
michael@0 | 184 | * The length can be negative for a NUL-terminated string. |
michael@0 | 185 | * |
michael@0 | 186 | * If the offset points to a single, unpaired surrogate, then that itself |
michael@0 | 187 | * will be returned as the code point. |
michael@0 | 188 | * Iteration through a string is more efficient with U16_NEXT_UNSAFE or U16_NEXT. |
michael@0 | 189 | * |
michael@0 | 190 | * @param s const UChar * string |
michael@0 | 191 | * @param start starting string offset (usually 0) |
michael@0 | 192 | * @param i string offset, must be start<=i<length |
michael@0 | 193 | * @param length string length |
michael@0 | 194 | * @param c output UChar32 variable |
michael@0 | 195 | * @see U16_GET_UNSAFE |
michael@0 | 196 | * @stable ICU 2.4 |
michael@0 | 197 | */ |
michael@0 | 198 | #define U16_GET(s, start, i, length, c) { \ |
michael@0 | 199 | (c)=(s)[i]; \ |
michael@0 | 200 | if(U16_IS_SURROGATE(c)) { \ |
michael@0 | 201 | uint16_t __c2; \ |
michael@0 | 202 | if(U16_IS_SURROGATE_LEAD(c)) { \ |
michael@0 | 203 | if((i)+1!=(length) && U16_IS_TRAIL(__c2=(s)[(i)+1])) { \ |
michael@0 | 204 | (c)=U16_GET_SUPPLEMENTARY((c), __c2); \ |
michael@0 | 205 | } \ |
michael@0 | 206 | } else { \ |
michael@0 | 207 | if((i)>(start) && U16_IS_LEAD(__c2=(s)[(i)-1])) { \ |
michael@0 | 208 | (c)=U16_GET_SUPPLEMENTARY(__c2, (c)); \ |
michael@0 | 209 | } \ |
michael@0 | 210 | } \ |
michael@0 | 211 | } \ |
michael@0 | 212 | } |
michael@0 | 213 | |
michael@0 | 214 | /* definitions with forward iteration --------------------------------------- */ |
michael@0 | 215 | |
michael@0 | 216 | /** |
michael@0 | 217 | * Get a code point from a string at a code point boundary offset, |
michael@0 | 218 | * and advance the offset to the next code point boundary. |
michael@0 | 219 | * (Post-incrementing forward iteration.) |
michael@0 | 220 | * "Unsafe" macro, assumes well-formed UTF-16. |
michael@0 | 221 | * |
michael@0 | 222 | * The offset may point to the lead surrogate unit |
michael@0 | 223 | * for a supplementary code point, in which case the macro will read |
michael@0 | 224 | * the following trail surrogate as well. |
michael@0 | 225 | * If the offset points to a trail surrogate, then that itself |
michael@0 | 226 | * will be returned as the code point. |
michael@0 | 227 | * The result is undefined if the offset points to a single, unpaired lead surrogate. |
michael@0 | 228 | * |
michael@0 | 229 | * @param s const UChar * string |
michael@0 | 230 | * @param i string offset |
michael@0 | 231 | * @param c output UChar32 variable |
michael@0 | 232 | * @see U16_NEXT |
michael@0 | 233 | * @stable ICU 2.4 |
michael@0 | 234 | */ |
michael@0 | 235 | #define U16_NEXT_UNSAFE(s, i, c) { \ |
michael@0 | 236 | (c)=(s)[(i)++]; \ |
michael@0 | 237 | if(U16_IS_LEAD(c)) { \ |
michael@0 | 238 | (c)=U16_GET_SUPPLEMENTARY((c), (s)[(i)++]); \ |
michael@0 | 239 | } \ |
michael@0 | 240 | } |
michael@0 | 241 | |
michael@0 | 242 | /** |
michael@0 | 243 | * Get a code point from a string at a code point boundary offset, |
michael@0 | 244 | * and advance the offset to the next code point boundary. |
michael@0 | 245 | * (Post-incrementing forward iteration.) |
michael@0 | 246 | * "Safe" macro, handles unpaired surrogates and checks for string boundaries. |
michael@0 | 247 | * |
michael@0 | 248 | * The length can be negative for a NUL-terminated string. |
michael@0 | 249 | * |
michael@0 | 250 | * The offset may point to the lead surrogate unit |
michael@0 | 251 | * for a supplementary code point, in which case the macro will read |
michael@0 | 252 | * the following trail surrogate as well. |
michael@0 | 253 | * If the offset points to a trail surrogate or |
michael@0 | 254 | * to a single, unpaired lead surrogate, then that itself |
michael@0 | 255 | * will be returned as the code point. |
michael@0 | 256 | * |
michael@0 | 257 | * @param s const UChar * string |
michael@0 | 258 | * @param i string offset, must be i<length |
michael@0 | 259 | * @param length string length |
michael@0 | 260 | * @param c output UChar32 variable |
michael@0 | 261 | * @see U16_NEXT_UNSAFE |
michael@0 | 262 | * @stable ICU 2.4 |
michael@0 | 263 | */ |
michael@0 | 264 | #define U16_NEXT(s, i, length, c) { \ |
michael@0 | 265 | (c)=(s)[(i)++]; \ |
michael@0 | 266 | if(U16_IS_LEAD(c)) { \ |
michael@0 | 267 | uint16_t __c2; \ |
michael@0 | 268 | if((i)!=(length) && U16_IS_TRAIL(__c2=(s)[(i)])) { \ |
michael@0 | 269 | ++(i); \ |
michael@0 | 270 | (c)=U16_GET_SUPPLEMENTARY((c), __c2); \ |
michael@0 | 271 | } \ |
michael@0 | 272 | } \ |
michael@0 | 273 | } |
michael@0 | 274 | |
michael@0 | 275 | /** |
michael@0 | 276 | * Append a code point to a string, overwriting 1 or 2 code units. |
michael@0 | 277 | * The offset points to the current end of the string contents |
michael@0 | 278 | * and is advanced (post-increment). |
michael@0 | 279 | * "Unsafe" macro, assumes a valid code point and sufficient space in the string. |
michael@0 | 280 | * Otherwise, the result is undefined. |
michael@0 | 281 | * |
michael@0 | 282 | * @param s const UChar * string buffer |
michael@0 | 283 | * @param i string offset |
michael@0 | 284 | * @param c code point to append |
michael@0 | 285 | * @see U16_APPEND |
michael@0 | 286 | * @stable ICU 2.4 |
michael@0 | 287 | */ |
michael@0 | 288 | #define U16_APPEND_UNSAFE(s, i, c) { \ |
michael@0 | 289 | if((uint32_t)(c)<=0xffff) { \ |
michael@0 | 290 | (s)[(i)++]=(uint16_t)(c); \ |
michael@0 | 291 | } else { \ |
michael@0 | 292 | (s)[(i)++]=(uint16_t)(((c)>>10)+0xd7c0); \ |
michael@0 | 293 | (s)[(i)++]=(uint16_t)(((c)&0x3ff)|0xdc00); \ |
michael@0 | 294 | } \ |
michael@0 | 295 | } |
michael@0 | 296 | |
michael@0 | 297 | /** |
michael@0 | 298 | * Append a code point to a string, overwriting 1 or 2 code units. |
michael@0 | 299 | * The offset points to the current end of the string contents |
michael@0 | 300 | * and is advanced (post-increment). |
michael@0 | 301 | * "Safe" macro, checks for a valid code point. |
michael@0 | 302 | * If a surrogate pair is written, checks for sufficient space in the string. |
michael@0 | 303 | * If the code point is not valid or a trail surrogate does not fit, |
michael@0 | 304 | * then isError is set to TRUE. |
michael@0 | 305 | * |
michael@0 | 306 | * @param s const UChar * string buffer |
michael@0 | 307 | * @param i string offset, must be i<capacity |
michael@0 | 308 | * @param capacity size of the string buffer |
michael@0 | 309 | * @param c code point to append |
michael@0 | 310 | * @param isError output UBool set to TRUE if an error occurs, otherwise not modified |
michael@0 | 311 | * @see U16_APPEND_UNSAFE |
michael@0 | 312 | * @stable ICU 2.4 |
michael@0 | 313 | */ |
michael@0 | 314 | #define U16_APPEND(s, i, capacity, c, isError) { \ |
michael@0 | 315 | if((uint32_t)(c)<=0xffff) { \ |
michael@0 | 316 | (s)[(i)++]=(uint16_t)(c); \ |
michael@0 | 317 | } else if((uint32_t)(c)<=0x10ffff && (i)+1<(capacity)) { \ |
michael@0 | 318 | (s)[(i)++]=(uint16_t)(((c)>>10)+0xd7c0); \ |
michael@0 | 319 | (s)[(i)++]=(uint16_t)(((c)&0x3ff)|0xdc00); \ |
michael@0 | 320 | } else /* c>0x10ffff or not enough space */ { \ |
michael@0 | 321 | (isError)=TRUE; \ |
michael@0 | 322 | } \ |
michael@0 | 323 | } |
michael@0 | 324 | |
michael@0 | 325 | /** |
michael@0 | 326 | * Advance the string offset from one code point boundary to the next. |
michael@0 | 327 | * (Post-incrementing iteration.) |
michael@0 | 328 | * "Unsafe" macro, assumes well-formed UTF-16. |
michael@0 | 329 | * |
michael@0 | 330 | * @param s const UChar * string |
michael@0 | 331 | * @param i string offset |
michael@0 | 332 | * @see U16_FWD_1 |
michael@0 | 333 | * @stable ICU 2.4 |
michael@0 | 334 | */ |
michael@0 | 335 | #define U16_FWD_1_UNSAFE(s, i) { \ |
michael@0 | 336 | if(U16_IS_LEAD((s)[(i)++])) { \ |
michael@0 | 337 | ++(i); \ |
michael@0 | 338 | } \ |
michael@0 | 339 | } |
michael@0 | 340 | |
michael@0 | 341 | /** |
michael@0 | 342 | * Advance the string offset from one code point boundary to the next. |
michael@0 | 343 | * (Post-incrementing iteration.) |
michael@0 | 344 | * "Safe" macro, handles unpaired surrogates and checks for string boundaries. |
michael@0 | 345 | * |
michael@0 | 346 | * The length can be negative for a NUL-terminated string. |
michael@0 | 347 | * |
michael@0 | 348 | * @param s const UChar * string |
michael@0 | 349 | * @param i string offset, must be i<length |
michael@0 | 350 | * @param length string length |
michael@0 | 351 | * @see U16_FWD_1_UNSAFE |
michael@0 | 352 | * @stable ICU 2.4 |
michael@0 | 353 | */ |
michael@0 | 354 | #define U16_FWD_1(s, i, length) { \ |
michael@0 | 355 | if(U16_IS_LEAD((s)[(i)++]) && (i)!=(length) && U16_IS_TRAIL((s)[i])) { \ |
michael@0 | 356 | ++(i); \ |
michael@0 | 357 | } \ |
michael@0 | 358 | } |
michael@0 | 359 | |
michael@0 | 360 | /** |
michael@0 | 361 | * Advance the string offset from one code point boundary to the n-th next one, |
michael@0 | 362 | * i.e., move forward by n code points. |
michael@0 | 363 | * (Post-incrementing iteration.) |
michael@0 | 364 | * "Unsafe" macro, assumes well-formed UTF-16. |
michael@0 | 365 | * |
michael@0 | 366 | * @param s const UChar * string |
michael@0 | 367 | * @param i string offset |
michael@0 | 368 | * @param n number of code points to skip |
michael@0 | 369 | * @see U16_FWD_N |
michael@0 | 370 | * @stable ICU 2.4 |
michael@0 | 371 | */ |
michael@0 | 372 | #define U16_FWD_N_UNSAFE(s, i, n) { \ |
michael@0 | 373 | int32_t __N=(n); \ |
michael@0 | 374 | while(__N>0) { \ |
michael@0 | 375 | U16_FWD_1_UNSAFE(s, i); \ |
michael@0 | 376 | --__N; \ |
michael@0 | 377 | } \ |
michael@0 | 378 | } |
michael@0 | 379 | |
michael@0 | 380 | /** |
michael@0 | 381 | * Advance the string offset from one code point boundary to the n-th next one, |
michael@0 | 382 | * i.e., move forward by n code points. |
michael@0 | 383 | * (Post-incrementing iteration.) |
michael@0 | 384 | * "Safe" macro, handles unpaired surrogates and checks for string boundaries. |
michael@0 | 385 | * |
michael@0 | 386 | * The length can be negative for a NUL-terminated string. |
michael@0 | 387 | * |
michael@0 | 388 | * @param s const UChar * string |
michael@0 | 389 | * @param i int32_t string offset, must be i<length |
michael@0 | 390 | * @param length int32_t string length |
michael@0 | 391 | * @param n number of code points to skip |
michael@0 | 392 | * @see U16_FWD_N_UNSAFE |
michael@0 | 393 | * @stable ICU 2.4 |
michael@0 | 394 | */ |
michael@0 | 395 | #define U16_FWD_N(s, i, length, n) { \ |
michael@0 | 396 | int32_t __N=(n); \ |
michael@0 | 397 | while(__N>0 && ((i)<(length) || ((length)<0 && (s)[i]!=0))) { \ |
michael@0 | 398 | U16_FWD_1(s, i, length); \ |
michael@0 | 399 | --__N; \ |
michael@0 | 400 | } \ |
michael@0 | 401 | } |
michael@0 | 402 | |
michael@0 | 403 | /** |
michael@0 | 404 | * Adjust a random-access offset to a code point boundary |
michael@0 | 405 | * at the start of a code point. |
michael@0 | 406 | * If the offset points to the trail surrogate of a surrogate pair, |
michael@0 | 407 | * then the offset is decremented. |
michael@0 | 408 | * Otherwise, it is not modified. |
michael@0 | 409 | * "Unsafe" macro, assumes well-formed UTF-16. |
michael@0 | 410 | * |
michael@0 | 411 | * @param s const UChar * string |
michael@0 | 412 | * @param i string offset |
michael@0 | 413 | * @see U16_SET_CP_START |
michael@0 | 414 | * @stable ICU 2.4 |
michael@0 | 415 | */ |
michael@0 | 416 | #define U16_SET_CP_START_UNSAFE(s, i) { \ |
michael@0 | 417 | if(U16_IS_TRAIL((s)[i])) { \ |
michael@0 | 418 | --(i); \ |
michael@0 | 419 | } \ |
michael@0 | 420 | } |
michael@0 | 421 | |
michael@0 | 422 | /** |
michael@0 | 423 | * Adjust a random-access offset to a code point boundary |
michael@0 | 424 | * at the start of a code point. |
michael@0 | 425 | * If the offset points to the trail surrogate of a surrogate pair, |
michael@0 | 426 | * then the offset is decremented. |
michael@0 | 427 | * Otherwise, it is not modified. |
michael@0 | 428 | * "Safe" macro, handles unpaired surrogates and checks for string boundaries. |
michael@0 | 429 | * |
michael@0 | 430 | * @param s const UChar * string |
michael@0 | 431 | * @param start starting string offset (usually 0) |
michael@0 | 432 | * @param i string offset, must be start<=i |
michael@0 | 433 | * @see U16_SET_CP_START_UNSAFE |
michael@0 | 434 | * @stable ICU 2.4 |
michael@0 | 435 | */ |
michael@0 | 436 | #define U16_SET_CP_START(s, start, i) { \ |
michael@0 | 437 | if(U16_IS_TRAIL((s)[i]) && (i)>(start) && U16_IS_LEAD((s)[(i)-1])) { \ |
michael@0 | 438 | --(i); \ |
michael@0 | 439 | } \ |
michael@0 | 440 | } |
michael@0 | 441 | |
michael@0 | 442 | /* definitions with backward iteration -------------------------------------- */ |
michael@0 | 443 | |
michael@0 | 444 | /** |
michael@0 | 445 | * Move the string offset from one code point boundary to the previous one |
michael@0 | 446 | * and get the code point between them. |
michael@0 | 447 | * (Pre-decrementing backward iteration.) |
michael@0 | 448 | * "Unsafe" macro, assumes well-formed UTF-16. |
michael@0 | 449 | * |
michael@0 | 450 | * The input offset may be the same as the string length. |
michael@0 | 451 | * If the offset is behind a trail surrogate unit |
michael@0 | 452 | * for a supplementary code point, then the macro will read |
michael@0 | 453 | * the preceding lead surrogate as well. |
michael@0 | 454 | * If the offset is behind a lead surrogate, then that itself |
michael@0 | 455 | * will be returned as the code point. |
michael@0 | 456 | * The result is undefined if the offset is behind a single, unpaired trail surrogate. |
michael@0 | 457 | * |
michael@0 | 458 | * @param s const UChar * string |
michael@0 | 459 | * @param i string offset |
michael@0 | 460 | * @param c output UChar32 variable |
michael@0 | 461 | * @see U16_PREV |
michael@0 | 462 | * @stable ICU 2.4 |
michael@0 | 463 | */ |
michael@0 | 464 | #define U16_PREV_UNSAFE(s, i, c) { \ |
michael@0 | 465 | (c)=(s)[--(i)]; \ |
michael@0 | 466 | if(U16_IS_TRAIL(c)) { \ |
michael@0 | 467 | (c)=U16_GET_SUPPLEMENTARY((s)[--(i)], (c)); \ |
michael@0 | 468 | } \ |
michael@0 | 469 | } |
michael@0 | 470 | |
michael@0 | 471 | /** |
michael@0 | 472 | * Move the string offset from one code point boundary to the previous one |
michael@0 | 473 | * and get the code point between them. |
michael@0 | 474 | * (Pre-decrementing backward iteration.) |
michael@0 | 475 | * "Safe" macro, handles unpaired surrogates and checks for string boundaries. |
michael@0 | 476 | * |
michael@0 | 477 | * The input offset may be the same as the string length. |
michael@0 | 478 | * If the offset is behind a trail surrogate unit |
michael@0 | 479 | * for a supplementary code point, then the macro will read |
michael@0 | 480 | * the preceding lead surrogate as well. |
michael@0 | 481 | * If the offset is behind a lead surrogate or behind a single, unpaired |
michael@0 | 482 | * trail surrogate, then that itself |
michael@0 | 483 | * will be returned as the code point. |
michael@0 | 484 | * |
michael@0 | 485 | * @param s const UChar * string |
michael@0 | 486 | * @param start starting string offset (usually 0) |
michael@0 | 487 | * @param i string offset, must be start<i |
michael@0 | 488 | * @param c output UChar32 variable |
michael@0 | 489 | * @see U16_PREV_UNSAFE |
michael@0 | 490 | * @stable ICU 2.4 |
michael@0 | 491 | */ |
michael@0 | 492 | #define U16_PREV(s, start, i, c) { \ |
michael@0 | 493 | (c)=(s)[--(i)]; \ |
michael@0 | 494 | if(U16_IS_TRAIL(c)) { \ |
michael@0 | 495 | uint16_t __c2; \ |
michael@0 | 496 | if((i)>(start) && U16_IS_LEAD(__c2=(s)[(i)-1])) { \ |
michael@0 | 497 | --(i); \ |
michael@0 | 498 | (c)=U16_GET_SUPPLEMENTARY(__c2, (c)); \ |
michael@0 | 499 | } \ |
michael@0 | 500 | } \ |
michael@0 | 501 | } |
michael@0 | 502 | |
michael@0 | 503 | /** |
michael@0 | 504 | * Move the string offset from one code point boundary to the previous one. |
michael@0 | 505 | * (Pre-decrementing backward iteration.) |
michael@0 | 506 | * The input offset may be the same as the string length. |
michael@0 | 507 | * "Unsafe" macro, assumes well-formed UTF-16. |
michael@0 | 508 | * |
michael@0 | 509 | * @param s const UChar * string |
michael@0 | 510 | * @param i string offset |
michael@0 | 511 | * @see U16_BACK_1 |
michael@0 | 512 | * @stable ICU 2.4 |
michael@0 | 513 | */ |
michael@0 | 514 | #define U16_BACK_1_UNSAFE(s, i) { \ |
michael@0 | 515 | if(U16_IS_TRAIL((s)[--(i)])) { \ |
michael@0 | 516 | --(i); \ |
michael@0 | 517 | } \ |
michael@0 | 518 | } |
michael@0 | 519 | |
michael@0 | 520 | /** |
michael@0 | 521 | * Move the string offset from one code point boundary to the previous one. |
michael@0 | 522 | * (Pre-decrementing backward iteration.) |
michael@0 | 523 | * The input offset may be the same as the string length. |
michael@0 | 524 | * "Safe" macro, handles unpaired surrogates and checks for string boundaries. |
michael@0 | 525 | * |
michael@0 | 526 | * @param s const UChar * string |
michael@0 | 527 | * @param start starting string offset (usually 0) |
michael@0 | 528 | * @param i string offset, must be start<i |
michael@0 | 529 | * @see U16_BACK_1_UNSAFE |
michael@0 | 530 | * @stable ICU 2.4 |
michael@0 | 531 | */ |
michael@0 | 532 | #define U16_BACK_1(s, start, i) { \ |
michael@0 | 533 | if(U16_IS_TRAIL((s)[--(i)]) && (i)>(start) && U16_IS_LEAD((s)[(i)-1])) { \ |
michael@0 | 534 | --(i); \ |
michael@0 | 535 | } \ |
michael@0 | 536 | } |
michael@0 | 537 | |
michael@0 | 538 | /** |
michael@0 | 539 | * Move the string offset from one code point boundary to the n-th one before it, |
michael@0 | 540 | * i.e., move backward by n code points. |
michael@0 | 541 | * (Pre-decrementing backward iteration.) |
michael@0 | 542 | * The input offset may be the same as the string length. |
michael@0 | 543 | * "Unsafe" macro, assumes well-formed UTF-16. |
michael@0 | 544 | * |
michael@0 | 545 | * @param s const UChar * string |
michael@0 | 546 | * @param i string offset |
michael@0 | 547 | * @param n number of code points to skip |
michael@0 | 548 | * @see U16_BACK_N |
michael@0 | 549 | * @stable ICU 2.4 |
michael@0 | 550 | */ |
michael@0 | 551 | #define U16_BACK_N_UNSAFE(s, i, n) { \ |
michael@0 | 552 | int32_t __N=(n); \ |
michael@0 | 553 | while(__N>0) { \ |
michael@0 | 554 | U16_BACK_1_UNSAFE(s, i); \ |
michael@0 | 555 | --__N; \ |
michael@0 | 556 | } \ |
michael@0 | 557 | } |
michael@0 | 558 | |
michael@0 | 559 | /** |
michael@0 | 560 | * Move the string offset from one code point boundary to the n-th one before it, |
michael@0 | 561 | * i.e., move backward by n code points. |
michael@0 | 562 | * (Pre-decrementing backward iteration.) |
michael@0 | 563 | * The input offset may be the same as the string length. |
michael@0 | 564 | * "Safe" macro, handles unpaired surrogates and checks for string boundaries. |
michael@0 | 565 | * |
michael@0 | 566 | * @param s const UChar * string |
michael@0 | 567 | * @param start start of string |
michael@0 | 568 | * @param i string offset, must be start<i |
michael@0 | 569 | * @param n number of code points to skip |
michael@0 | 570 | * @see U16_BACK_N_UNSAFE |
michael@0 | 571 | * @stable ICU 2.4 |
michael@0 | 572 | */ |
michael@0 | 573 | #define U16_BACK_N(s, start, i, n) { \ |
michael@0 | 574 | int32_t __N=(n); \ |
michael@0 | 575 | while(__N>0 && (i)>(start)) { \ |
michael@0 | 576 | U16_BACK_1(s, start, i); \ |
michael@0 | 577 | --__N; \ |
michael@0 | 578 | } \ |
michael@0 | 579 | } |
michael@0 | 580 | |
michael@0 | 581 | /** |
michael@0 | 582 | * Adjust a random-access offset to a code point boundary after a code point. |
michael@0 | 583 | * If the offset is behind the lead surrogate of a surrogate pair, |
michael@0 | 584 | * then the offset is incremented. |
michael@0 | 585 | * Otherwise, it is not modified. |
michael@0 | 586 | * The input offset may be the same as the string length. |
michael@0 | 587 | * "Unsafe" macro, assumes well-formed UTF-16. |
michael@0 | 588 | * |
michael@0 | 589 | * @param s const UChar * string |
michael@0 | 590 | * @param i string offset |
michael@0 | 591 | * @see U16_SET_CP_LIMIT |
michael@0 | 592 | * @stable ICU 2.4 |
michael@0 | 593 | */ |
michael@0 | 594 | #define U16_SET_CP_LIMIT_UNSAFE(s, i) { \ |
michael@0 | 595 | if(U16_IS_LEAD((s)[(i)-1])) { \ |
michael@0 | 596 | ++(i); \ |
michael@0 | 597 | } \ |
michael@0 | 598 | } |
michael@0 | 599 | |
michael@0 | 600 | /** |
michael@0 | 601 | * Adjust a random-access offset to a code point boundary after a code point. |
michael@0 | 602 | * If the offset is behind the lead surrogate of a surrogate pair, |
michael@0 | 603 | * then the offset is incremented. |
michael@0 | 604 | * Otherwise, it is not modified. |
michael@0 | 605 | * The input offset may be the same as the string length. |
michael@0 | 606 | * "Safe" macro, handles unpaired surrogates and checks for string boundaries. |
michael@0 | 607 | * |
michael@0 | 608 | * The length can be negative for a NUL-terminated string. |
michael@0 | 609 | * |
michael@0 | 610 | * @param s const UChar * string |
michael@0 | 611 | * @param start int32_t starting string offset (usually 0) |
michael@0 | 612 | * @param i int32_t string offset, start<=i<=length |
michael@0 | 613 | * @param length int32_t string length |
michael@0 | 614 | * @see U16_SET_CP_LIMIT_UNSAFE |
michael@0 | 615 | * @stable ICU 2.4 |
michael@0 | 616 | */ |
michael@0 | 617 | #define U16_SET_CP_LIMIT(s, start, i, length) { \ |
michael@0 | 618 | if((start)<(i) && ((i)<(length) || (length)<0) && U16_IS_LEAD((s)[(i)-1]) && U16_IS_TRAIL((s)[i])) { \ |
michael@0 | 619 | ++(i); \ |
michael@0 | 620 | } \ |
michael@0 | 621 | } |
michael@0 | 622 | |
michael@0 | 623 | #endif |