michael@0: /*
michael@0: *******************************************************************************
michael@0: *
michael@0: * Copyright (C) 1999-2012, International Business Machines
michael@0: * Corporation and others. All Rights Reserved.
michael@0: *
michael@0: *******************************************************************************
michael@0: * file name: utf16.h
michael@0: * encoding: US-ASCII
michael@0: * tab size: 8 (not used)
michael@0: * indentation:4
michael@0: *
michael@0: * created on: 1999sep09
michael@0: * created by: Markus W. Scherer
michael@0: */
michael@0:
michael@0: /**
michael@0: * \file
michael@0: * \brief C API: 16-bit Unicode handling macros
michael@0: *
michael@0: * This file defines macros to deal with 16-bit Unicode (UTF-16) code units and strings.
michael@0: *
michael@0: * For more information see utf.h and the ICU User Guide Strings chapter
michael@0: * (http://userguide.icu-project.org/strings).
michael@0: *
michael@0: * Usage:
michael@0: * ICU coding guidelines for if() statements should be followed when using these macros.
michael@0: * Compound statements (curly braces {}) must be used for if-else-while...
michael@0: * bodies and all macro statements should be terminated with semicolon.
michael@0: */
michael@0:
michael@0: #ifndef __UTF16_H__
michael@0: #define __UTF16_H__
michael@0:
michael@0: #include "unicode/umachine.h"
michael@0: #ifndef __UTF_H__
michael@0: # include "unicode/utf.h"
michael@0: #endif
michael@0:
michael@0: /* single-code point definitions -------------------------------------------- */
michael@0:
michael@0: /**
michael@0: * Does this code unit alone encode a code point (BMP, not a surrogate)?
michael@0: * @param c 16-bit code unit
michael@0: * @return TRUE or FALSE
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: #define U16_IS_SINGLE(c) !U_IS_SURROGATE(c)
michael@0:
michael@0: /**
michael@0: * Is this code unit a lead surrogate (U+d800..U+dbff)?
michael@0: * @param c 16-bit code unit
michael@0: * @return TRUE or FALSE
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: #define U16_IS_LEAD(c) (((c)&0xfffffc00)==0xd800)
michael@0:
michael@0: /**
michael@0: * Is this code unit a trail surrogate (U+dc00..U+dfff)?
michael@0: * @param c 16-bit code unit
michael@0: * @return TRUE or FALSE
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: #define U16_IS_TRAIL(c) (((c)&0xfffffc00)==0xdc00)
michael@0:
michael@0: /**
michael@0: * Is this code unit a surrogate (U+d800..U+dfff)?
michael@0: * @param c 16-bit code unit
michael@0: * @return TRUE or FALSE
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: #define U16_IS_SURROGATE(c) U_IS_SURROGATE(c)
michael@0:
michael@0: /**
michael@0: * Assuming c is a surrogate code point (U16_IS_SURROGATE(c)),
michael@0: * is it a lead surrogate?
michael@0: * @param c 16-bit code unit
michael@0: * @return TRUE or FALSE
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: #define U16_IS_SURROGATE_LEAD(c) (((c)&0x400)==0)
michael@0:
michael@0: /**
michael@0: * Assuming c is a surrogate code point (U16_IS_SURROGATE(c)),
michael@0: * is it a trail surrogate?
michael@0: * @param c 16-bit code unit
michael@0: * @return TRUE or FALSE
michael@0: * @stable ICU 4.2
michael@0: */
michael@0: #define U16_IS_SURROGATE_TRAIL(c) (((c)&0x400)!=0)
michael@0:
michael@0: /**
michael@0: * Helper constant for U16_GET_SUPPLEMENTARY.
michael@0: * @internal
michael@0: */
michael@0: #define U16_SURROGATE_OFFSET ((0xd800<<10UL)+0xdc00-0x10000)
michael@0:
michael@0: /**
michael@0: * Get a supplementary code point value (U+10000..U+10ffff)
michael@0: * from its lead and trail surrogates.
michael@0: * The result is undefined if the input values are not
michael@0: * lead and trail surrogates.
michael@0: *
michael@0: * @param lead lead surrogate (U+d800..U+dbff)
michael@0: * @param trail trail surrogate (U+dc00..U+dfff)
michael@0: * @return supplementary code point (U+10000..U+10ffff)
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: #define U16_GET_SUPPLEMENTARY(lead, trail) \
michael@0: (((UChar32)(lead)<<10UL)+(UChar32)(trail)-U16_SURROGATE_OFFSET)
michael@0:
michael@0:
michael@0: /**
michael@0: * Get the lead surrogate (0xd800..0xdbff) for a
michael@0: * supplementary code point (0x10000..0x10ffff).
michael@0: * @param supplementary 32-bit code point (U+10000..U+10ffff)
michael@0: * @return lead surrogate (U+d800..U+dbff) for supplementary
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: #define U16_LEAD(supplementary) (UChar)(((supplementary)>>10)+0xd7c0)
michael@0:
michael@0: /**
michael@0: * Get the trail surrogate (0xdc00..0xdfff) for a
michael@0: * supplementary code point (0x10000..0x10ffff).
michael@0: * @param supplementary 32-bit code point (U+10000..U+10ffff)
michael@0: * @return trail surrogate (U+dc00..U+dfff) for supplementary
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: #define U16_TRAIL(supplementary) (UChar)(((supplementary)&0x3ff)|0xdc00)
michael@0:
michael@0: /**
michael@0: * How many 16-bit code units are used to encode this Unicode code point? (1 or 2)
michael@0: * The result is not defined if c is not a Unicode code point (U+0000..U+10ffff).
michael@0: * @param c 32-bit code point
michael@0: * @return 1 or 2
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: #define U16_LENGTH(c) ((uint32_t)(c)<=0xffff ? 1 : 2)
michael@0:
michael@0: /**
michael@0: * The maximum number of 16-bit code units per Unicode code point (U+0000..U+10ffff).
michael@0: * @return 2
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: #define U16_MAX_LENGTH 2
michael@0:
michael@0: /**
michael@0: * Get a code point from a string at a random-access offset,
michael@0: * without changing the offset.
michael@0: * "Unsafe" macro, assumes well-formed UTF-16.
michael@0: *
michael@0: * The offset may point to either the lead or trail surrogate unit
michael@0: * for a supplementary code point, in which case the macro will read
michael@0: * the adjacent matching surrogate as well.
michael@0: * The result is undefined if the offset points to a single, unpaired surrogate.
michael@0: * Iteration through a string is more efficient with U16_NEXT_UNSAFE or U16_NEXT.
michael@0: *
michael@0: * @param s const UChar * string
michael@0: * @param i string offset
michael@0: * @param c output UChar32 variable
michael@0: * @see U16_GET
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: #define U16_GET_UNSAFE(s, i, c) { \
michael@0: (c)=(s)[i]; \
michael@0: if(U16_IS_SURROGATE(c)) { \
michael@0: if(U16_IS_SURROGATE_LEAD(c)) { \
michael@0: (c)=U16_GET_SUPPLEMENTARY((c), (s)[(i)+1]); \
michael@0: } else { \
michael@0: (c)=U16_GET_SUPPLEMENTARY((s)[(i)-1], (c)); \
michael@0: } \
michael@0: } \
michael@0: }
michael@0:
michael@0: /**
michael@0: * Get a code point from a string at a random-access offset,
michael@0: * without changing the offset.
michael@0: * "Safe" macro, handles unpaired surrogates and checks for string boundaries.
michael@0: *
michael@0: * The offset may point to either the lead or trail surrogate unit
michael@0: * for a supplementary code point, in which case the macro will read
michael@0: * the adjacent matching surrogate as well.
michael@0: *
michael@0: * The length can be negative for a NUL-terminated string.
michael@0: *
michael@0: * If the offset points to a single, unpaired surrogate, then that itself
michael@0: * will be returned as the code point.
michael@0: * Iteration through a string is more efficient with U16_NEXT_UNSAFE or U16_NEXT.
michael@0: *
michael@0: * @param s const UChar * string
michael@0: * @param start starting string offset (usually 0)
michael@0: * @param i string offset, must be start<=i(start) && U16_IS_LEAD(__c2=(s)[(i)-1])) { \
michael@0: (c)=U16_GET_SUPPLEMENTARY(__c2, (c)); \
michael@0: } \
michael@0: } \
michael@0: } \
michael@0: }
michael@0:
michael@0: /* definitions with forward iteration --------------------------------------- */
michael@0:
michael@0: /**
michael@0: * Get a code point from a string at a code point boundary offset,
michael@0: * and advance the offset to the next code point boundary.
michael@0: * (Post-incrementing forward iteration.)
michael@0: * "Unsafe" macro, assumes well-formed UTF-16.
michael@0: *
michael@0: * The offset may point to the lead surrogate unit
michael@0: * for a supplementary code point, in which case the macro will read
michael@0: * the following trail surrogate as well.
michael@0: * If the offset points to a trail surrogate, then that itself
michael@0: * will be returned as the code point.
michael@0: * The result is undefined if the offset points to a single, unpaired lead surrogate.
michael@0: *
michael@0: * @param s const UChar * string
michael@0: * @param i string offset
michael@0: * @param c output UChar32 variable
michael@0: * @see U16_NEXT
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: #define U16_NEXT_UNSAFE(s, i, c) { \
michael@0: (c)=(s)[(i)++]; \
michael@0: if(U16_IS_LEAD(c)) { \
michael@0: (c)=U16_GET_SUPPLEMENTARY((c), (s)[(i)++]); \
michael@0: } \
michael@0: }
michael@0:
michael@0: /**
michael@0: * Get a code point from a string at a code point boundary offset,
michael@0: * and advance the offset to the next code point boundary.
michael@0: * (Post-incrementing forward iteration.)
michael@0: * "Safe" macro, handles unpaired surrogates and checks for string boundaries.
michael@0: *
michael@0: * The length can be negative for a NUL-terminated string.
michael@0: *
michael@0: * The offset may point to the lead surrogate unit
michael@0: * for a supplementary code point, in which case the macro will read
michael@0: * the following trail surrogate as well.
michael@0: * If the offset points to a trail surrogate or
michael@0: * to a single, unpaired lead surrogate, then that itself
michael@0: * will be returned as the code point.
michael@0: *
michael@0: * @param s const UChar * string
michael@0: * @param i string offset, must be i>10)+0xd7c0); \
michael@0: (s)[(i)++]=(uint16_t)(((c)&0x3ff)|0xdc00); \
michael@0: } \
michael@0: }
michael@0:
michael@0: /**
michael@0: * Append a code point to a string, overwriting 1 or 2 code units.
michael@0: * The offset points to the current end of the string contents
michael@0: * and is advanced (post-increment).
michael@0: * "Safe" macro, checks for a valid code point.
michael@0: * If a surrogate pair is written, checks for sufficient space in the string.
michael@0: * If the code point is not valid or a trail surrogate does not fit,
michael@0: * then isError is set to TRUE.
michael@0: *
michael@0: * @param s const UChar * string buffer
michael@0: * @param i string offset, must be i>10)+0xd7c0); \
michael@0: (s)[(i)++]=(uint16_t)(((c)&0x3ff)|0xdc00); \
michael@0: } else /* c>0x10ffff or not enough space */ { \
michael@0: (isError)=TRUE; \
michael@0: } \
michael@0: }
michael@0:
michael@0: /**
michael@0: * Advance the string offset from one code point boundary to the next.
michael@0: * (Post-incrementing iteration.)
michael@0: * "Unsafe" macro, assumes well-formed UTF-16.
michael@0: *
michael@0: * @param s const UChar * string
michael@0: * @param i string offset
michael@0: * @see U16_FWD_1
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: #define U16_FWD_1_UNSAFE(s, i) { \
michael@0: if(U16_IS_LEAD((s)[(i)++])) { \
michael@0: ++(i); \
michael@0: } \
michael@0: }
michael@0:
michael@0: /**
michael@0: * Advance the string offset from one code point boundary to the next.
michael@0: * (Post-incrementing iteration.)
michael@0: * "Safe" macro, handles unpaired surrogates and checks for string boundaries.
michael@0: *
michael@0: * The length can be negative for a NUL-terminated string.
michael@0: *
michael@0: * @param s const UChar * string
michael@0: * @param i string offset, must be i0) { \
michael@0: U16_FWD_1_UNSAFE(s, i); \
michael@0: --__N; \
michael@0: } \
michael@0: }
michael@0:
michael@0: /**
michael@0: * Advance the string offset from one code point boundary to the n-th next one,
michael@0: * i.e., move forward by n code points.
michael@0: * (Post-incrementing iteration.)
michael@0: * "Safe" macro, handles unpaired surrogates and checks for string boundaries.
michael@0: *
michael@0: * The length can be negative for a NUL-terminated string.
michael@0: *
michael@0: * @param s const UChar * string
michael@0: * @param i int32_t string offset, must be i0 && ((i)<(length) || ((length)<0 && (s)[i]!=0))) { \
michael@0: U16_FWD_1(s, i, length); \
michael@0: --__N; \
michael@0: } \
michael@0: }
michael@0:
michael@0: /**
michael@0: * Adjust a random-access offset to a code point boundary
michael@0: * at the start of a code point.
michael@0: * If the offset points to the trail surrogate of a surrogate pair,
michael@0: * then the offset is decremented.
michael@0: * Otherwise, it is not modified.
michael@0: * "Unsafe" macro, assumes well-formed UTF-16.
michael@0: *
michael@0: * @param s const UChar * string
michael@0: * @param i string offset
michael@0: * @see U16_SET_CP_START
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: #define U16_SET_CP_START_UNSAFE(s, i) { \
michael@0: if(U16_IS_TRAIL((s)[i])) { \
michael@0: --(i); \
michael@0: } \
michael@0: }
michael@0:
michael@0: /**
michael@0: * Adjust a random-access offset to a code point boundary
michael@0: * at the start of a code point.
michael@0: * If the offset points to the trail surrogate of a surrogate pair,
michael@0: * then the offset is decremented.
michael@0: * Otherwise, it is not modified.
michael@0: * "Safe" macro, handles unpaired surrogates and checks for string boundaries.
michael@0: *
michael@0: * @param s const UChar * string
michael@0: * @param start starting string offset (usually 0)
michael@0: * @param i string offset, must be start<=i
michael@0: * @see U16_SET_CP_START_UNSAFE
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: #define U16_SET_CP_START(s, start, i) { \
michael@0: if(U16_IS_TRAIL((s)[i]) && (i)>(start) && U16_IS_LEAD((s)[(i)-1])) { \
michael@0: --(i); \
michael@0: } \
michael@0: }
michael@0:
michael@0: /* definitions with backward iteration -------------------------------------- */
michael@0:
michael@0: /**
michael@0: * Move the string offset from one code point boundary to the previous one
michael@0: * and get the code point between them.
michael@0: * (Pre-decrementing backward iteration.)
michael@0: * "Unsafe" macro, assumes well-formed UTF-16.
michael@0: *
michael@0: * The input offset may be the same as the string length.
michael@0: * If the offset is behind a trail surrogate unit
michael@0: * for a supplementary code point, then the macro will read
michael@0: * the preceding lead surrogate as well.
michael@0: * If the offset is behind a lead surrogate, then that itself
michael@0: * will be returned as the code point.
michael@0: * The result is undefined if the offset is behind a single, unpaired trail surrogate.
michael@0: *
michael@0: * @param s const UChar * string
michael@0: * @param i string offset
michael@0: * @param c output UChar32 variable
michael@0: * @see U16_PREV
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: #define U16_PREV_UNSAFE(s, i, c) { \
michael@0: (c)=(s)[--(i)]; \
michael@0: if(U16_IS_TRAIL(c)) { \
michael@0: (c)=U16_GET_SUPPLEMENTARY((s)[--(i)], (c)); \
michael@0: } \
michael@0: }
michael@0:
michael@0: /**
michael@0: * Move the string offset from one code point boundary to the previous one
michael@0: * and get the code point between them.
michael@0: * (Pre-decrementing backward iteration.)
michael@0: * "Safe" macro, handles unpaired surrogates and checks for string boundaries.
michael@0: *
michael@0: * The input offset may be the same as the string length.
michael@0: * If the offset is behind a trail surrogate unit
michael@0: * for a supplementary code point, then the macro will read
michael@0: * the preceding lead surrogate as well.
michael@0: * If the offset is behind a lead surrogate or behind a single, unpaired
michael@0: * trail surrogate, then that itself
michael@0: * will be returned as the code point.
michael@0: *
michael@0: * @param s const UChar * string
michael@0: * @param start starting string offset (usually 0)
michael@0: * @param i string offset, must be start(start) && U16_IS_LEAD(__c2=(s)[(i)-1])) { \
michael@0: --(i); \
michael@0: (c)=U16_GET_SUPPLEMENTARY(__c2, (c)); \
michael@0: } \
michael@0: } \
michael@0: }
michael@0:
michael@0: /**
michael@0: * Move the string offset from one code point boundary to the previous one.
michael@0: * (Pre-decrementing backward iteration.)
michael@0: * The input offset may be the same as the string length.
michael@0: * "Unsafe" macro, assumes well-formed UTF-16.
michael@0: *
michael@0: * @param s const UChar * string
michael@0: * @param i string offset
michael@0: * @see U16_BACK_1
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: #define U16_BACK_1_UNSAFE(s, i) { \
michael@0: if(U16_IS_TRAIL((s)[--(i)])) { \
michael@0: --(i); \
michael@0: } \
michael@0: }
michael@0:
michael@0: /**
michael@0: * Move the string offset from one code point boundary to the previous one.
michael@0: * (Pre-decrementing backward iteration.)
michael@0: * The input offset may be the same as the string length.
michael@0: * "Safe" macro, handles unpaired surrogates and checks for string boundaries.
michael@0: *
michael@0: * @param s const UChar * string
michael@0: * @param start starting string offset (usually 0)
michael@0: * @param i string offset, must be start(start) && U16_IS_LEAD((s)[(i)-1])) { \
michael@0: --(i); \
michael@0: } \
michael@0: }
michael@0:
michael@0: /**
michael@0: * Move the string offset from one code point boundary to the n-th one before it,
michael@0: * i.e., move backward by n code points.
michael@0: * (Pre-decrementing backward iteration.)
michael@0: * The input offset may be the same as the string length.
michael@0: * "Unsafe" macro, assumes well-formed UTF-16.
michael@0: *
michael@0: * @param s const UChar * string
michael@0: * @param i string offset
michael@0: * @param n number of code points to skip
michael@0: * @see U16_BACK_N
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: #define U16_BACK_N_UNSAFE(s, i, n) { \
michael@0: int32_t __N=(n); \
michael@0: while(__N>0) { \
michael@0: U16_BACK_1_UNSAFE(s, i); \
michael@0: --__N; \
michael@0: } \
michael@0: }
michael@0:
michael@0: /**
michael@0: * Move the string offset from one code point boundary to the n-th one before it,
michael@0: * i.e., move backward by n code points.
michael@0: * (Pre-decrementing backward iteration.)
michael@0: * The input offset may be the same as the string length.
michael@0: * "Safe" macro, handles unpaired surrogates and checks for string boundaries.
michael@0: *
michael@0: * @param s const UChar * string
michael@0: * @param start start of string
michael@0: * @param i string offset, must be start0 && (i)>(start)) { \
michael@0: U16_BACK_1(s, start, i); \
michael@0: --__N; \
michael@0: } \
michael@0: }
michael@0:
michael@0: /**
michael@0: * Adjust a random-access offset to a code point boundary after a code point.
michael@0: * If the offset is behind the lead surrogate of a surrogate pair,
michael@0: * then the offset is incremented.
michael@0: * Otherwise, it is not modified.
michael@0: * The input offset may be the same as the string length.
michael@0: * "Unsafe" macro, assumes well-formed UTF-16.
michael@0: *
michael@0: * @param s const UChar * string
michael@0: * @param i string offset
michael@0: * @see U16_SET_CP_LIMIT
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: #define U16_SET_CP_LIMIT_UNSAFE(s, i) { \
michael@0: if(U16_IS_LEAD((s)[(i)-1])) { \
michael@0: ++(i); \
michael@0: } \
michael@0: }
michael@0:
michael@0: /**
michael@0: * Adjust a random-access offset to a code point boundary after a code point.
michael@0: * If the offset is behind the lead surrogate of a surrogate pair,
michael@0: * then the offset is incremented.
michael@0: * Otherwise, it is not modified.
michael@0: * The input offset may be the same as the string length.
michael@0: * "Safe" macro, handles unpaired surrogates and checks for string boundaries.
michael@0: *
michael@0: * The length can be negative for a NUL-terminated string.
michael@0: *
michael@0: * @param s const UChar * string
michael@0: * @param start int32_t starting string offset (usually 0)
michael@0: * @param i int32_t string offset, start<=i<=length
michael@0: * @param length int32_t string length
michael@0: * @see U16_SET_CP_LIMIT_UNSAFE
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: #define U16_SET_CP_LIMIT(s, start, i, length) { \
michael@0: if((start)<(i) && ((i)<(length) || (length)<0) && U16_IS_LEAD((s)[(i)-1]) && U16_IS_TRAIL((s)[i])) { \
michael@0: ++(i); \
michael@0: } \
michael@0: }
michael@0:
michael@0: #endif