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