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