michael@0: /* michael@0: ******************************************************************************* michael@0: * michael@0: * Copyright (C) 1999-2004, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: * michael@0: ******************************************************************************* michael@0: * file name: utf.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: #ifndef BASE_THIRD_PARTY_ICU_ICU_UTF_H_ michael@0: #define BASE_THIRD_PARTY_ICU_ICU_UTF_H_ michael@0: michael@0: #include "base/basictypes.h" michael@0: michael@0: namespace base_icu { michael@0: michael@0: typedef uint32 UChar32; michael@0: typedef int8 UBool; michael@0: michael@0: // General --------------------------------------------------------------------- michael@0: // from utf.h michael@0: michael@0: /** michael@0: * This value is intended for sentinel values for APIs that michael@0: * (take or) return single code points (UChar32). michael@0: * It is outside of the Unicode code point range 0..0x10ffff. michael@0: * michael@0: * For example, a "done" or "error" value in a new API michael@0: * could be indicated with CBU_SENTINEL. michael@0: * michael@0: * ICU APIs designed before ICU 2.4 usually define service-specific "done" michael@0: * values, mostly 0xffff. michael@0: * Those may need to be distinguished from michael@0: * actual U+ffff text contents by calling functions like michael@0: * CharacterIterator::hasNext() or UnicodeString::length(). michael@0: * michael@0: * @return -1 michael@0: * @see UChar32 michael@0: * @stable ICU 2.4 michael@0: */ michael@0: #define CBU_SENTINEL (-1) michael@0: michael@0: /** michael@0: * Is this code point a Unicode noncharacter? michael@0: * @param c 32-bit code point michael@0: * @return TRUE or FALSE michael@0: * @stable ICU 2.4 michael@0: */ michael@0: #define CBU_IS_UNICODE_NONCHAR(c) \ michael@0: ((c)>=0xfdd0 && \ michael@0: ((uint32)(c)<=0xfdef || ((c)&0xfffe)==0xfffe) && \ michael@0: (uint32)(c)<=0x10ffff) michael@0: michael@0: /** michael@0: * Is c a Unicode code point value (0..U+10ffff) michael@0: * that can be assigned a character? michael@0: * michael@0: * Code points that are not characters include: michael@0: * - single surrogate code points (U+d800..U+dfff, 2048 code points) michael@0: * - the last two code points on each plane (U+__fffe and U+__ffff, 34 code points) michael@0: * - U+fdd0..U+fdef (new with Unicode 3.1, 32 code points) michael@0: * - the highest Unicode code point value is U+10ffff michael@0: * michael@0: * This means that all code points below U+d800 are character code points, michael@0: * and that boundary is tested first for performance. michael@0: * michael@0: * @param c 32-bit code point michael@0: * @return TRUE or FALSE michael@0: * @stable ICU 2.4 michael@0: */ michael@0: #define CBU_IS_UNICODE_CHAR(c) \ michael@0: ((uint32)(c)<0xd800 || \ michael@0: ((uint32)(c)>0xdfff && \ michael@0: (uint32)(c)<=0x10ffff && \ michael@0: !CBU_IS_UNICODE_NONCHAR(c))) michael@0: michael@0: /** michael@0: * Is this code point a surrogate (U+d800..U+dfff)? michael@0: * @param c 32-bit code point michael@0: * @return TRUE or FALSE michael@0: * @stable ICU 2.4 michael@0: */ michael@0: #define CBU_IS_SURROGATE(c) (((c)&0xfffff800)==0xd800) michael@0: michael@0: /** michael@0: * Assuming c is a surrogate code point (U_IS_SURROGATE(c)), michael@0: * is it a lead surrogate? michael@0: * @param c 32-bit code point michael@0: * @return TRUE or FALSE michael@0: * @stable ICU 2.4 michael@0: */ michael@0: #define CBU_IS_SURROGATE_LEAD(c) (((c)&0x400)==0) michael@0: michael@0: michael@0: // UTF-8 macros ---------------------------------------------------------------- michael@0: // from utf8.h michael@0: michael@0: extern const uint8 utf8_countTrailBytes[256]; michael@0: michael@0: /** michael@0: * Count the trail bytes for a UTF-8 lead byte. michael@0: * @internal michael@0: */ michael@0: #define CBU8_COUNT_TRAIL_BYTES(leadByte) (base_icu::utf8_countTrailBytes[(uint8)leadByte]) michael@0: michael@0: /** michael@0: * Mask a UTF-8 lead byte, leave only the lower bits that form part of the code point value. michael@0: * @internal michael@0: */ michael@0: #define CBU8_MASK_LEAD_BYTE(leadByte, countTrailBytes) ((leadByte)&=(1<<(6-(countTrailBytes)))-1) michael@0: michael@0: /** michael@0: * Does this code unit (byte) encode a code point by itself (US-ASCII 0..0x7f)? michael@0: * @param c 8-bit code unit (byte) michael@0: * @return TRUE or FALSE michael@0: * @stable ICU 2.4 michael@0: */ michael@0: #define CBU8_IS_SINGLE(c) (((c)&0x80)==0) michael@0: michael@0: /** michael@0: * Is this code unit (byte) a UTF-8 lead byte? michael@0: * @param c 8-bit code unit (byte) michael@0: * @return TRUE or FALSE michael@0: * @stable ICU 2.4 michael@0: */ michael@0: #define CBU8_IS_LEAD(c) ((uint8)((c)-0xc0)<0x3e) michael@0: michael@0: /** michael@0: * Is this code unit (byte) a UTF-8 trail byte? michael@0: * @param c 8-bit code unit (byte) michael@0: * @return TRUE or FALSE michael@0: * @stable ICU 2.4 michael@0: */ michael@0: #define CBU8_IS_TRAIL(c) (((c)&0xc0)==0x80) michael@0: michael@0: /** michael@0: * How many code units (bytes) are used for the UTF-8 encoding michael@0: * of this Unicode code point? michael@0: * @param c 32-bit code point michael@0: * @return 1..4, or 0 if c is a surrogate or not a Unicode code point michael@0: * @stable ICU 2.4 michael@0: */ michael@0: #define CBU8_LENGTH(c) \ michael@0: ((uint32)(c)<=0x7f ? 1 : \ michael@0: ((uint32)(c)<=0x7ff ? 2 : \ michael@0: ((uint32)(c)<=0xd7ff ? 3 : \ michael@0: ((uint32)(c)<=0xdfff || (uint32)(c)>0x10ffff ? 0 : \ michael@0: ((uint32)(c)<=0xffff ? 3 : 4)\ michael@0: ) \ michael@0: ) \ michael@0: ) \ michael@0: ) michael@0: michael@0: /** michael@0: * The maximum number of UTF-8 code units (bytes) per Unicode code point (U+0000..U+10ffff). michael@0: * @return 4 michael@0: * @stable ICU 2.4 michael@0: */ michael@0: #define CBU8_MAX_LENGTH 4 michael@0: michael@0: /** michael@0: * Function for handling "next code point" with error-checking. michael@0: * @internal michael@0: */ michael@0: UChar32 utf8_nextCharSafeBody(const uint8 *s, int32 *pi, int32 length, UChar32 c, UBool strict); 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, checks for illegal sequences and for string boundaries. michael@0: * michael@0: * The offset may point to the lead byte of a multi-byte sequence, michael@0: * in which case the macro will read the whole sequence. michael@0: * If the offset points to a trail byte or an illegal UTF-8 sequence, then michael@0: * c is set to a negative value. michael@0: * michael@0: * @param s const uint8 * string michael@0: * @param i string offset, i=0x80) { \ michael@0: if(CBU8_IS_LEAD(c)) { \ michael@0: (c)=base_icu::utf8_nextCharSafeBody((const uint8 *)s, &(i), (int32)(length), c, -1); \ michael@0: } else { \ michael@0: (c)=CBU_SENTINEL; \ michael@0: } \ michael@0: } \ michael@0: } michael@0: michael@0: /** michael@0: * Append a code point to a string, overwriting 1 to 4 bytes. michael@0: * The offset points to the current end of the string contents michael@0: * and is advanced (post-increment). michael@0: * "Unsafe" macro, assumes a valid code point and sufficient space in the string. michael@0: * Otherwise, the result is undefined. michael@0: * michael@0: * @param s const uint8 * string buffer michael@0: * @param i string offset michael@0: * @param c code point to append michael@0: * @see CBU8_APPEND michael@0: * @stable ICU 2.4 michael@0: */ michael@0: #define CBU8_APPEND_UNSAFE(s, i, c) { \ michael@0: if((uint32)(c)<=0x7f) { \ michael@0: (s)[(i)++]=(uint8)(c); \ michael@0: } else { \ michael@0: if((uint32)(c)<=0x7ff) { \ michael@0: (s)[(i)++]=(uint8)(((c)>>6)|0xc0); \ michael@0: } else { \ michael@0: if((uint32)(c)<=0xffff) { \ michael@0: (s)[(i)++]=(uint8)(((c)>>12)|0xe0); \ michael@0: } else { \ michael@0: (s)[(i)++]=(uint8)(((c)>>18)|0xf0); \ michael@0: (s)[(i)++]=(uint8)((((c)>>12)&0x3f)|0x80); \ michael@0: } \ michael@0: (s)[(i)++]=(uint8)((((c)>>6)&0x3f)|0x80); \ michael@0: } \ michael@0: (s)[(i)++]=(uint8)(((c)&0x3f)|0x80); \ michael@0: } \ michael@0: } michael@0: michael@0: // UTF-16 macros --------------------------------------------------------------- michael@0: // from utf16.h 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 CBU16_IS_SINGLE(c) !CBU_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 CBU16_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 CBU16_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 CBU16_IS_SURROGATE(c) CBU_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 CBU16_IS_SURROGATE_LEAD(c) (((c)&0x400)==0) michael@0: michael@0: /** michael@0: * Helper constant for CBU16_GET_SUPPLEMENTARY. michael@0: * @internal michael@0: */ michael@0: #define CBU16_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 CBU16_GET_SUPPLEMENTARY(lead, trail) \ michael@0: (((base_icu::UChar32)(lead)<<10UL)+(base_icu::UChar32)(trail)-CBU16_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 CBU16_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 CBU16_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 CBU16_LENGTH(c) ((uint32)(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 CBU16_MAX_LENGTH 2 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 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, i>10)+0xd7c0); \ michael@0: (s)[(i)++]=(uint16)(((c)&0x3ff)|0xdc00); \ michael@0: } \ michael@0: } michael@0: michael@0: } // namesapce base_icu michael@0: michael@0: #endif // BASE_THIRD_PARTY_ICU_ICU_UTF_H_