ICU 4.8.1.1  4.8.1.1
utf16.h
Go to the documentation of this file.
00001 /*
00002 *******************************************************************************
00003 *
00004 *   Copyright (C) 1999-2010, International Business Machines
00005 *   Corporation and others.  All Rights Reserved.
00006 *
00007 *******************************************************************************
00008 *   file name:  utf16.h
00009 *   encoding:   US-ASCII
00010 *   tab size:   8 (not used)
00011 *   indentation:4
00012 *
00013 *   created on: 1999sep09
00014 *   created by: Markus W. Scherer
00015 */
00016 
00034 #ifndef __UTF16_H__
00035 #define __UTF16_H__
00036 
00037 /* utf.h must be included first. */
00038 #ifndef __UTF_H__
00039 #   include "unicode/utf.h"
00040 #endif
00041 
00042 /* single-code point definitions -------------------------------------------- */
00043 
00050 #define U16_IS_SINGLE(c) !U_IS_SURROGATE(c)
00051 
00058 #define U16_IS_LEAD(c) (((c)&0xfffffc00)==0xd800)
00059 
00066 #define U16_IS_TRAIL(c) (((c)&0xfffffc00)==0xdc00)
00067 
00074 #define U16_IS_SURROGATE(c) U_IS_SURROGATE(c)
00075 
00083 #define U16_IS_SURROGATE_LEAD(c) (((c)&0x400)==0)
00084 
00092 #define U16_IS_SURROGATE_TRAIL(c) (((c)&0x400)!=0)
00093 
00098 #define U16_SURROGATE_OFFSET ((0xd800<<10UL)+0xdc00-0x10000)
00099 
00111 #define U16_GET_SUPPLEMENTARY(lead, trail) \
00112     (((UChar32)(lead)<<10UL)+(UChar32)(trail)-U16_SURROGATE_OFFSET)
00113 
00114 
00122 #define U16_LEAD(supplementary) (UChar)(((supplementary)>>10)+0xd7c0)
00123 
00131 #define U16_TRAIL(supplementary) (UChar)(((supplementary)&0x3ff)|0xdc00)
00132 
00140 #define U16_LENGTH(c) ((uint32_t)(c)<=0xffff ? 1 : 2)
00141 
00147 #define U16_MAX_LENGTH 2
00148 
00166 #define U16_GET_UNSAFE(s, i, c) { \
00167     (c)=(s)[i]; \
00168     if(U16_IS_SURROGATE(c)) { \
00169         if(U16_IS_SURROGATE_LEAD(c)) { \
00170             (c)=U16_GET_SUPPLEMENTARY((c), (s)[(i)+1]); \
00171         } else { \
00172             (c)=U16_GET_SUPPLEMENTARY((s)[(i)-1], (c)); \
00173         } \
00174     } \
00175 }
00176 
00197 #define U16_GET(s, start, i, length, c) { \
00198     (c)=(s)[i]; \
00199     if(U16_IS_SURROGATE(c)) { \
00200         uint16_t __c2; \
00201         if(U16_IS_SURROGATE_LEAD(c)) { \
00202             if((i)+1<(length) && U16_IS_TRAIL(__c2=(s)[(i)+1])) { \
00203                 (c)=U16_GET_SUPPLEMENTARY((c), __c2); \
00204             } \
00205         } else { \
00206             if((i)>(start) && U16_IS_LEAD(__c2=(s)[(i)-1])) { \
00207                 (c)=U16_GET_SUPPLEMENTARY(__c2, (c)); \
00208             } \
00209         } \
00210     } \
00211 }
00212 
00213 /* definitions with forward iteration --------------------------------------- */
00214 
00234 #define U16_NEXT_UNSAFE(s, i, c) { \
00235     (c)=(s)[(i)++]; \
00236     if(U16_IS_LEAD(c)) { \
00237         (c)=U16_GET_SUPPLEMENTARY((c), (s)[(i)++]); \
00238     } \
00239 }
00240 
00261 #define U16_NEXT(s, i, length, c) { \
00262     (c)=(s)[(i)++]; \
00263     if(U16_IS_LEAD(c)) { \
00264         uint16_t __c2; \
00265         if((i)<(length) && U16_IS_TRAIL(__c2=(s)[(i)])) { \
00266             ++(i); \
00267             (c)=U16_GET_SUPPLEMENTARY((c), __c2); \
00268         } \
00269     } \
00270 }
00271 
00285 #define U16_APPEND_UNSAFE(s, i, c) { \
00286     if((uint32_t)(c)<=0xffff) { \
00287         (s)[(i)++]=(uint16_t)(c); \
00288     } else { \
00289         (s)[(i)++]=(uint16_t)(((c)>>10)+0xd7c0); \
00290         (s)[(i)++]=(uint16_t)(((c)&0x3ff)|0xdc00); \
00291     } \
00292 }
00293 
00311 #define U16_APPEND(s, i, capacity, c, isError) { \
00312     if((uint32_t)(c)<=0xffff) { \
00313         (s)[(i)++]=(uint16_t)(c); \
00314     } else if((uint32_t)(c)<=0x10ffff && (i)+1<(capacity)) { \
00315         (s)[(i)++]=(uint16_t)(((c)>>10)+0xd7c0); \
00316         (s)[(i)++]=(uint16_t)(((c)&0x3ff)|0xdc00); \
00317     } else /* c>0x10ffff or not enough space */ { \
00318         (isError)=TRUE; \
00319     } \
00320 }
00321 
00332 #define U16_FWD_1_UNSAFE(s, i) { \
00333     if(U16_IS_LEAD((s)[(i)++])) { \
00334         ++(i); \
00335     } \
00336 }
00337 
00349 #define U16_FWD_1(s, i, length) { \
00350     if(U16_IS_LEAD((s)[(i)++]) && (i)<(length) && U16_IS_TRAIL((s)[i])) { \
00351         ++(i); \
00352     } \
00353 }
00354 
00367 #define U16_FWD_N_UNSAFE(s, i, n) { \
00368     int32_t __N=(n); \
00369     while(__N>0) { \
00370         U16_FWD_1_UNSAFE(s, i); \
00371         --__N; \
00372     } \
00373 }
00374 
00388 #define U16_FWD_N(s, i, length, n) { \
00389     int32_t __N=(n); \
00390     while(__N>0 && (i)<(length)) { \
00391         U16_FWD_1(s, i, length); \
00392         --__N; \
00393     } \
00394 }
00395 
00409 #define U16_SET_CP_START_UNSAFE(s, i) { \
00410     if(U16_IS_TRAIL((s)[i])) { \
00411         --(i); \
00412     } \
00413 }
00414 
00429 #define U16_SET_CP_START(s, start, i) { \
00430     if(U16_IS_TRAIL((s)[i]) && (i)>(start) && U16_IS_LEAD((s)[(i)-1])) { \
00431         --(i); \
00432     } \
00433 }
00434 
00435 /* definitions with backward iteration -------------------------------------- */
00436 
00457 #define U16_PREV_UNSAFE(s, i, c) { \
00458     (c)=(s)[--(i)]; \
00459     if(U16_IS_TRAIL(c)) { \
00460         (c)=U16_GET_SUPPLEMENTARY((s)[--(i)], (c)); \
00461     } \
00462 }
00463 
00485 #define U16_PREV(s, start, i, c) { \
00486     (c)=(s)[--(i)]; \
00487     if(U16_IS_TRAIL(c)) { \
00488         uint16_t __c2; \
00489         if((i)>(start) && U16_IS_LEAD(__c2=(s)[(i)-1])) { \
00490             --(i); \
00491             (c)=U16_GET_SUPPLEMENTARY(__c2, (c)); \
00492         } \
00493     } \
00494 }
00495 
00507 #define U16_BACK_1_UNSAFE(s, i) { \
00508     if(U16_IS_TRAIL((s)[--(i)])) { \
00509         --(i); \
00510     } \
00511 }
00512 
00525 #define U16_BACK_1(s, start, i) { \
00526     if(U16_IS_TRAIL((s)[--(i)]) && (i)>(start) && U16_IS_LEAD((s)[(i)-1])) { \
00527         --(i); \
00528     } \
00529 }
00530 
00544 #define U16_BACK_N_UNSAFE(s, i, n) { \
00545     int32_t __N=(n); \
00546     while(__N>0) { \
00547         U16_BACK_1_UNSAFE(s, i); \
00548         --__N; \
00549     } \
00550 }
00551 
00566 #define U16_BACK_N(s, start, i, n) { \
00567     int32_t __N=(n); \
00568     while(__N>0 && (i)>(start)) { \
00569         U16_BACK_1(s, start, i); \
00570         --__N; \
00571     } \
00572 }
00573 
00587 #define U16_SET_CP_LIMIT_UNSAFE(s, i) { \
00588     if(U16_IS_LEAD((s)[(i)-1])) { \
00589         ++(i); \
00590     } \
00591 }
00592 
00608 #define U16_SET_CP_LIMIT(s, start, i, length) { \
00609     if((start)<(i) && (i)<(length) && U16_IS_LEAD((s)[(i)-1]) && U16_IS_TRAIL((s)[i])) { \
00610         ++(i); \
00611     } \
00612 }
00613 
00614 #endif
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines