os/textandloc/fontservices/textshaperplugin/IcuSource/common/unicode/utf.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:  utf.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: Code point macros
sl@0
    20
 *
sl@0
    21
 * This file defines macros for checking whether a code point is
sl@0
    22
 * a surrogate or a non-character etc.
sl@0
    23
 *
sl@0
    24
 * The UChar and UChar32 data types for Unicode code units and code points
sl@0
    25
 * are defined in umachines.h because they can be machine-dependent.
sl@0
    26
 *
sl@0
    27
 * utf.h is included by utypes.h and itself includes utf8.h and utf16.h after some
sl@0
    28
 * common definitions. Those files define macros for efficiently getting code points
sl@0
    29
 * in and out of UTF-8/16 strings.
sl@0
    30
 * utf16.h macros have "U16_" prefixes.
sl@0
    31
 * utf8.h defines similar macros with "U8_" prefixes for UTF-8 string handling.
sl@0
    32
 *
sl@0
    33
 * ICU processes 16-bit Unicode strings.
sl@0
    34
 * Most of the time, such strings are well-formed UTF-16.
sl@0
    35
 * Single, unpaired surrogates must be handled as well, and are treated in ICU
sl@0
    36
 * like regular code points where possible.
sl@0
    37
 * (Pairs of surrogate code points are indistinguishable from supplementary
sl@0
    38
 * code points encoded as pairs of supplementary code units.)
sl@0
    39
 *
sl@0
    40
 * In fact, almost all Unicode code points in normal text (>99%)
sl@0
    41
 * are on the BMP (<=U+ffff) and even <=U+d7ff.
sl@0
    42
 * ICU functions handle supplementary code points (U+10000..U+10ffff)
sl@0
    43
 * but are optimized for the much more frequently occurring BMP code points.
sl@0
    44
 *
sl@0
    45
 * utf.h defines UChar to be an unsigned 16-bit integer. If this matches wchar_t, then
sl@0
    46
 * UChar is defined to be exactly wchar_t, otherwise uint16_t.
sl@0
    47
 *
sl@0
    48
 * UChar32 is defined to be a signed 32-bit integer (int32_t), large enough for a 21-bit
sl@0
    49
 * Unicode code point (Unicode scalar value, 0..0x10ffff).
sl@0
    50
 * Before ICU 2.4, the definition of UChar32 was similarly platform-dependent as
sl@0
    51
 * the definition of UChar. For details see the documentation for UChar32 itself.
sl@0
    52
 *
sl@0
    53
 * utf.h also defines a small number of C macros for single Unicode code points.
sl@0
    54
 * These are simple checks for surrogates and non-characters.
sl@0
    55
 * For actual Unicode character properties see uchar.h.
sl@0
    56
 *
sl@0
    57
 * By default, string operations must be done with error checking in case
sl@0
    58
 * a string is not well-formed UTF-16.
sl@0
    59
 * The macros will detect if a surrogate code unit is unpaired
sl@0
    60
 * (lead unit without trail unit or vice versa) and just return the unit itself
sl@0
    61
 * as the code point.
sl@0
    62
 * (It is an accidental property of Unicode and UTF-16 that all
sl@0
    63
 * malformed sequences can be expressed unambiguously with a distinct subrange
sl@0
    64
 * of Unicode code points.)
sl@0
    65
 *
sl@0
    66
 * When it is safe to assume that text is well-formed UTF-16
sl@0
    67
 * (does not contain single, unpaired surrogates), then one can use
sl@0
    68
 * U16_..._UNSAFE macros.
sl@0
    69
 * These do not check for proper code unit sequences or truncated text and may
sl@0
    70
 * yield wrong results or even cause a crash if they are used with "malformed"
sl@0
    71
 * text.
sl@0
    72
 * In practice, U16_..._UNSAFE macros will produce slightly less code but
sl@0
    73
 * should not be faster because the processing is only different when a
sl@0
    74
 * surrogate code unit is detected, which will be rare.
sl@0
    75
 *
sl@0
    76
 * Similarly for UTF-8, there are "safe" macros without a suffix,
sl@0
    77
 * and U8_..._UNSAFE versions.
sl@0
    78
 * The performance differences are much larger here because UTF-8 provides so
sl@0
    79
 * many opportunities for malformed sequences.
sl@0
    80
 * The unsafe UTF-8 macros are entirely implemented inside the macro definitions
sl@0
    81
 * and are fast, while the safe UTF-8 macros call functions for all but the
sl@0
    82
 * trivial (ASCII) cases.
sl@0
    83
 *
sl@0
    84
 * Unlike with UTF-16, malformed sequences cannot be expressed with distinct
sl@0
    85
 * code point values (0..U+10ffff). They are indicated with negative values instead.
sl@0
    86
 *
sl@0
    87
 * For more information see the ICU User Guide Strings chapter
sl@0
    88
 * (http://icu.sourceforge.net/userguide/strings.html).
sl@0
    89
 *
sl@0
    90
 * <em>Usage:</em>
sl@0
    91
 * ICU coding guidelines for if() statements should be followed when using these macros.
sl@0
    92
 * Compound statements (curly braces {}) must be used  for if-else-while... 
sl@0
    93
 * bodies and all macro statements should be terminated with semicolon.
sl@0
    94
 *
sl@0
    95
 * @stable ICU 2.4
sl@0
    96
 */
sl@0
    97
sl@0
    98
#ifndef __UTF_H__
sl@0
    99
#define __UTF_H__
sl@0
   100
sl@0
   101
#include "unicode/utypes.h"
sl@0
   102
/* include the utfXX.h after the following definitions */
sl@0
   103
sl@0
   104
/* single-code point definitions -------------------------------------------- */
sl@0
   105
sl@0
   106
/**
sl@0
   107
 * This value is intended for sentinel values for APIs that
sl@0
   108
 * (take or) return single code points (UChar32).
sl@0
   109
 * It is outside of the Unicode code point range 0..0x10ffff.
sl@0
   110
 * 
sl@0
   111
 * For example, a "done" or "error" value in a new API
sl@0
   112
 * could be indicated with U_SENTINEL.
sl@0
   113
 *
sl@0
   114
 * ICU APIs designed before ICU 2.4 usually define service-specific "done"
sl@0
   115
 * values, mostly 0xffff.
sl@0
   116
 * Those may need to be distinguished from
sl@0
   117
 * actual U+ffff text contents by calling functions like
sl@0
   118
 * CharacterIterator::hasNext() or UnicodeString::length().
sl@0
   119
 *
sl@0
   120
 * @return -1
sl@0
   121
 * @see UChar32
sl@0
   122
 * @stable ICU 2.4
sl@0
   123
 */
sl@0
   124
#define U_SENTINEL (-1)
sl@0
   125
sl@0
   126
/**
sl@0
   127
 * Is this code point a Unicode noncharacter?
sl@0
   128
 * @param c 32-bit code point
sl@0
   129
 * @return TRUE or FALSE
sl@0
   130
 * @stable ICU 2.4
sl@0
   131
 */
sl@0
   132
#define U_IS_UNICODE_NONCHAR(c) \
sl@0
   133
    ((c)>=0xfdd0 && \
sl@0
   134
     ((uint32_t)(c)<=0xfdef || ((c)&0xfffe)==0xfffe) && \
sl@0
   135
     (uint32_t)(c)<=0x10ffff)
sl@0
   136
sl@0
   137
/**
sl@0
   138
 * Is c a Unicode code point value (0..U+10ffff)
sl@0
   139
 * that can be assigned a character?
sl@0
   140
 *
sl@0
   141
 * Code points that are not characters include:
sl@0
   142
 * - single surrogate code points (U+d800..U+dfff, 2048 code points)
sl@0
   143
 * - the last two code points on each plane (U+__fffe and U+__ffff, 34 code points)
sl@0
   144
 * - U+fdd0..U+fdef (new with Unicode 3.1, 32 code points)
sl@0
   145
 * - the highest Unicode code point value is U+10ffff
sl@0
   146
 *
sl@0
   147
 * This means that all code points below U+d800 are character code points,
sl@0
   148
 * and that boundary is tested first for performance.
sl@0
   149
 *
sl@0
   150
 * @param c 32-bit code point
sl@0
   151
 * @return TRUE or FALSE
sl@0
   152
 * @stable ICU 2.4
sl@0
   153
 */
sl@0
   154
#define U_IS_UNICODE_CHAR(c) \
sl@0
   155
    ((uint32_t)(c)<0xd800 || \
sl@0
   156
        ((uint32_t)(c)>0xdfff && \
sl@0
   157
         (uint32_t)(c)<=0x10ffff && \
sl@0
   158
         !U_IS_UNICODE_NONCHAR(c)))
sl@0
   159
sl@0
   160
#ifndef U_HIDE_DRAFT_API
sl@0
   161
sl@0
   162
/**
sl@0
   163
 * Is this code point a BMP code point (U+0000..U+ffff)?
sl@0
   164
 * @param c 32-bit code point
sl@0
   165
 * @return TRUE or FALSE
sl@0
   166
 * @stable ICU 2.8
sl@0
   167
 */
sl@0
   168
#define U_IS_BMP(c) ((uint32_t)(c)<=0xffff)
sl@0
   169
sl@0
   170
/**
sl@0
   171
 * Is this code point a supplementary code point (U+10000..U+10ffff)?
sl@0
   172
 * @param c 32-bit code point
sl@0
   173
 * @return TRUE or FALSE
sl@0
   174
 * @stable ICU 2.8
sl@0
   175
 */
sl@0
   176
#define U_IS_SUPPLEMENTARY(c) ((uint32_t)((c)-0x10000)<=0xfffff)
sl@0
   177
sl@0
   178
#endif /*U_HIDE_DRAFT_API*/
sl@0
   179
 
sl@0
   180
/**
sl@0
   181
 * Is this code point a lead surrogate (U+d800..U+dbff)?
sl@0
   182
 * @param c 32-bit code point
sl@0
   183
 * @return TRUE or FALSE
sl@0
   184
 * @stable ICU 2.4
sl@0
   185
 */
sl@0
   186
#define U_IS_LEAD(c) (((c)&0xfffffc00)==0xd800)
sl@0
   187
sl@0
   188
/**
sl@0
   189
 * Is this code point a trail surrogate (U+dc00..U+dfff)?
sl@0
   190
 * @param c 32-bit code point
sl@0
   191
 * @return TRUE or FALSE
sl@0
   192
 * @stable ICU 2.4
sl@0
   193
 */
sl@0
   194
#define U_IS_TRAIL(c) (((c)&0xfffffc00)==0xdc00)
sl@0
   195
sl@0
   196
/**
sl@0
   197
 * Is this code point a surrogate (U+d800..U+dfff)?
sl@0
   198
 * @param c 32-bit code point
sl@0
   199
 * @return TRUE or FALSE
sl@0
   200
 * @stable ICU 2.4
sl@0
   201
 */
sl@0
   202
#define U_IS_SURROGATE(c) (((c)&0xfffff800)==0xd800)
sl@0
   203
sl@0
   204
/**
sl@0
   205
 * Assuming c is a surrogate code point (U_IS_SURROGATE(c)),
sl@0
   206
 * is it a lead surrogate?
sl@0
   207
 * @param c 32-bit code point
sl@0
   208
 * @return TRUE or FALSE
sl@0
   209
 * @stable ICU 2.4
sl@0
   210
 */
sl@0
   211
#define U_IS_SURROGATE_LEAD(c) (((c)&0x400)==0)
sl@0
   212
sl@0
   213
/* include the utfXX.h ------------------------------------------------------ */
sl@0
   214
sl@0
   215
#include "unicode/utf8.h"
sl@0
   216
#include "unicode/utf16.h"
sl@0
   217
sl@0
   218
/* utf_old.h contains deprecated, pre-ICU 2.4 definitions */
sl@0
   219
#include "unicode/utf_old.h"
sl@0
   220
sl@0
   221
#endif