ICU 49.1.1  49.1.1
utf16.h
Go to the documentation of this file.
00001 /*
00002 *******************************************************************************
00003 *
00004 *   Copyright (C) 1999-2011, 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 
00032 #ifndef __UTF16_H__
00033 #define __UTF16_H__
00034 
00035 #include "unicode/umachine.h"
00036 #ifndef __UTF_H__
00037 #   include "unicode/utf.h"
00038 #endif
00039 
00040 /* single-code point definitions -------------------------------------------- */
00041 
00048 #define U16_IS_SINGLE(c) !U_IS_SURROGATE(c)
00049 
00056 #define U16_IS_LEAD(c) (((c)&0xfffffc00)==0xd800)
00057 
00064 #define U16_IS_TRAIL(c) (((c)&0xfffffc00)==0xdc00)
00065 
00072 #define U16_IS_SURROGATE(c) U_IS_SURROGATE(c)
00073 
00081 #define U16_IS_SURROGATE_LEAD(c) (((c)&0x400)==0)
00082 
00090 #define U16_IS_SURROGATE_TRAIL(c) (((c)&0x400)!=0)
00091 
00096 #define U16_SURROGATE_OFFSET ((0xd800<<10UL)+0xdc00-0x10000)
00097 
00109 #define U16_GET_SUPPLEMENTARY(lead, trail) \
00110     (((UChar32)(lead)<<10UL)+(UChar32)(trail)-U16_SURROGATE_OFFSET)
00111 
00112 
00120 #define U16_LEAD(supplementary) (UChar)(((supplementary)>>10)+0xd7c0)
00121 
00129 #define U16_TRAIL(supplementary) (UChar)(((supplementary)&0x3ff)|0xdc00)
00130 
00138 #define U16_LENGTH(c) ((uint32_t)(c)<=0xffff ? 1 : 2)
00139 
00145 #define U16_MAX_LENGTH 2
00146 
00164 #define U16_GET_UNSAFE(s, i, c) { \
00165     (c)=(s)[i]; \
00166     if(U16_IS_SURROGATE(c)) { \
00167         if(U16_IS_SURROGATE_LEAD(c)) { \
00168             (c)=U16_GET_SUPPLEMENTARY((c), (s)[(i)+1]); \
00169         } else { \
00170             (c)=U16_GET_SUPPLEMENTARY((s)[(i)-1], (c)); \
00171         } \
00172     } \
00173 }
00174 
00195 #define U16_GET(s, start, i, length, c) { \
00196     (c)=(s)[i]; \
00197     if(U16_IS_SURROGATE(c)) { \
00198         uint16_t __c2; \
00199         if(U16_IS_SURROGATE_LEAD(c)) { \
00200             if((i)+1<(length) && U16_IS_TRAIL(__c2=(s)[(i)+1])) { \
00201                 (c)=U16_GET_SUPPLEMENTARY((c), __c2); \
00202             } \
00203         } else { \
00204             if((i)>(start) && U16_IS_LEAD(__c2=(s)[(i)-1])) { \
00205                 (c)=U16_GET_SUPPLEMENTARY(__c2, (c)); \
00206             } \
00207         } \
00208     } \
00209 }
00210 
00211 /* definitions with forward iteration --------------------------------------- */
00212 
00232 #define U16_NEXT_UNSAFE(s, i, c) { \
00233     (c)=(s)[(i)++]; \
00234     if(U16_IS_LEAD(c)) { \
00235         (c)=U16_GET_SUPPLEMENTARY((c), (s)[(i)++]); \
00236     } \
00237 }
00238 
00259 #define U16_NEXT(s, i, length, c) { \
00260     (c)=(s)[(i)++]; \
00261     if(U16_IS_LEAD(c)) { \
00262         uint16_t __c2; \
00263         if((i)<(length) && U16_IS_TRAIL(__c2=(s)[(i)])) { \
00264             ++(i); \
00265             (c)=U16_GET_SUPPLEMENTARY((c), __c2); \
00266         } \
00267     } \
00268 }
00269 
00283 #define U16_APPEND_UNSAFE(s, i, c) { \
00284     if((uint32_t)(c)<=0xffff) { \
00285         (s)[(i)++]=(uint16_t)(c); \
00286     } else { \
00287         (s)[(i)++]=(uint16_t)(((c)>>10)+0xd7c0); \
00288         (s)[(i)++]=(uint16_t)(((c)&0x3ff)|0xdc00); \
00289     } \
00290 }
00291 
00309 #define U16_APPEND(s, i, capacity, c, isError) { \
00310     if((uint32_t)(c)<=0xffff) { \
00311         (s)[(i)++]=(uint16_t)(c); \
00312     } else if((uint32_t)(c)<=0x10ffff && (i)+1<(capacity)) { \
00313         (s)[(i)++]=(uint16_t)(((c)>>10)+0xd7c0); \
00314         (s)[(i)++]=(uint16_t)(((c)&0x3ff)|0xdc00); \
00315     } else /* c>0x10ffff or not enough space */ { \
00316         (isError)=TRUE; \
00317     } \
00318 }
00319 
00330 #define U16_FWD_1_UNSAFE(s, i) { \
00331     if(U16_IS_LEAD((s)[(i)++])) { \
00332         ++(i); \
00333     } \
00334 }
00335 
00347 #define U16_FWD_1(s, i, length) { \
00348     if(U16_IS_LEAD((s)[(i)++]) && (i)<(length) && U16_IS_TRAIL((s)[i])) { \
00349         ++(i); \
00350     } \
00351 }
00352 
00365 #define U16_FWD_N_UNSAFE(s, i, n) { \
00366     int32_t __N=(n); \
00367     while(__N>0) { \
00368         U16_FWD_1_UNSAFE(s, i); \
00369         --__N; \
00370     } \
00371 }
00372 
00386 #define U16_FWD_N(s, i, length, n) { \
00387     int32_t __N=(n); \
00388     while(__N>0 && (i)<(length)) { \
00389         U16_FWD_1(s, i, length); \
00390         --__N; \
00391     } \
00392 }
00393 
00407 #define U16_SET_CP_START_UNSAFE(s, i) { \
00408     if(U16_IS_TRAIL((s)[i])) { \
00409         --(i); \
00410     } \
00411 }
00412 
00427 #define U16_SET_CP_START(s, start, i) { \
00428     if(U16_IS_TRAIL((s)[i]) && (i)>(start) && U16_IS_LEAD((s)[(i)-1])) { \
00429         --(i); \
00430     } \
00431 }
00432 
00433 /* definitions with backward iteration -------------------------------------- */
00434 
00455 #define U16_PREV_UNSAFE(s, i, c) { \
00456     (c)=(s)[--(i)]; \
00457     if(U16_IS_TRAIL(c)) { \
00458         (c)=U16_GET_SUPPLEMENTARY((s)[--(i)], (c)); \
00459     } \
00460 }
00461 
00483 #define U16_PREV(s, start, i, c) { \
00484     (c)=(s)[--(i)]; \
00485     if(U16_IS_TRAIL(c)) { \
00486         uint16_t __c2; \
00487         if((i)>(start) && U16_IS_LEAD(__c2=(s)[(i)-1])) { \
00488             --(i); \
00489             (c)=U16_GET_SUPPLEMENTARY(__c2, (c)); \
00490         } \
00491     } \
00492 }
00493 
00505 #define U16_BACK_1_UNSAFE(s, i) { \
00506     if(U16_IS_TRAIL((s)[--(i)])) { \
00507         --(i); \
00508     } \
00509 }
00510 
00523 #define U16_BACK_1(s, start, i) { \
00524     if(U16_IS_TRAIL((s)[--(i)]) && (i)>(start) && U16_IS_LEAD((s)[(i)-1])) { \
00525         --(i); \
00526     } \
00527 }
00528 
00542 #define U16_BACK_N_UNSAFE(s, i, n) { \
00543     int32_t __N=(n); \
00544     while(__N>0) { \
00545         U16_BACK_1_UNSAFE(s, i); \
00546         --__N; \
00547     } \
00548 }
00549 
00564 #define U16_BACK_N(s, start, i, n) { \
00565     int32_t __N=(n); \
00566     while(__N>0 && (i)>(start)) { \
00567         U16_BACK_1(s, start, i); \
00568         --__N; \
00569     } \
00570 }
00571 
00585 #define U16_SET_CP_LIMIT_UNSAFE(s, i) { \
00586     if(U16_IS_LEAD((s)[(i)-1])) { \
00587         ++(i); \
00588     } \
00589 }
00590 
00606 #define U16_SET_CP_LIMIT(s, start, i, length) { \
00607     if((start)<(i) && (i)<(length) && U16_IS_LEAD((s)[(i)-1]) && U16_IS_TRAIL((s)[i])) { \
00608         ++(i); \
00609     } \
00610 }
00611 
00612 #endif