ICU 51.2  51.2
utf16.h
Go to the documentation of this file.
00001 /*
00002 *******************************************************************************
00003 *
00004 *   Copyright (C) 1999-2012, 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 
00198 #define U16_GET(s, start, i, length, c) { \
00199     (c)=(s)[i]; \
00200     if(U16_IS_SURROGATE(c)) { \
00201         uint16_t __c2; \
00202         if(U16_IS_SURROGATE_LEAD(c)) { \
00203             if((i)+1!=(length) && U16_IS_TRAIL(__c2=(s)[(i)+1])) { \
00204                 (c)=U16_GET_SUPPLEMENTARY((c), __c2); \
00205             } \
00206         } else { \
00207             if((i)>(start) && U16_IS_LEAD(__c2=(s)[(i)-1])) { \
00208                 (c)=U16_GET_SUPPLEMENTARY(__c2, (c)); \
00209             } \
00210         } \
00211     } \
00212 }
00213 
00214 /* definitions with forward iteration --------------------------------------- */
00215 
00235 #define U16_NEXT_UNSAFE(s, i, c) { \
00236     (c)=(s)[(i)++]; \
00237     if(U16_IS_LEAD(c)) { \
00238         (c)=U16_GET_SUPPLEMENTARY((c), (s)[(i)++]); \
00239     } \
00240 }
00241 
00264 #define U16_NEXT(s, i, length, c) { \
00265     (c)=(s)[(i)++]; \
00266     if(U16_IS_LEAD(c)) { \
00267         uint16_t __c2; \
00268         if((i)!=(length) && U16_IS_TRAIL(__c2=(s)[(i)])) { \
00269             ++(i); \
00270             (c)=U16_GET_SUPPLEMENTARY((c), __c2); \
00271         } \
00272     } \
00273 }
00274 
00288 #define U16_APPEND_UNSAFE(s, i, c) { \
00289     if((uint32_t)(c)<=0xffff) { \
00290         (s)[(i)++]=(uint16_t)(c); \
00291     } else { \
00292         (s)[(i)++]=(uint16_t)(((c)>>10)+0xd7c0); \
00293         (s)[(i)++]=(uint16_t)(((c)&0x3ff)|0xdc00); \
00294     } \
00295 }
00296 
00314 #define U16_APPEND(s, i, capacity, c, isError) { \
00315     if((uint32_t)(c)<=0xffff) { \
00316         (s)[(i)++]=(uint16_t)(c); \
00317     } else if((uint32_t)(c)<=0x10ffff && (i)+1<(capacity)) { \
00318         (s)[(i)++]=(uint16_t)(((c)>>10)+0xd7c0); \
00319         (s)[(i)++]=(uint16_t)(((c)&0x3ff)|0xdc00); \
00320     } else /* c>0x10ffff or not enough space */ { \
00321         (isError)=TRUE; \
00322     } \
00323 }
00324 
00335 #define U16_FWD_1_UNSAFE(s, i) { \
00336     if(U16_IS_LEAD((s)[(i)++])) { \
00337         ++(i); \
00338     } \
00339 }
00340 
00354 #define U16_FWD_1(s, i, length) { \
00355     if(U16_IS_LEAD((s)[(i)++]) && (i)!=(length) && U16_IS_TRAIL((s)[i])) { \
00356         ++(i); \
00357     } \
00358 }
00359 
00372 #define U16_FWD_N_UNSAFE(s, i, n) { \
00373     int32_t __N=(n); \
00374     while(__N>0) { \
00375         U16_FWD_1_UNSAFE(s, i); \
00376         --__N; \
00377     } \
00378 }
00379 
00395 #define U16_FWD_N(s, i, length, n) { \
00396     int32_t __N=(n); \
00397     while(__N>0 && ((i)<(length) || ((length)<0 && (s)[i]!=0))) { \
00398         U16_FWD_1(s, i, length); \
00399         --__N; \
00400     } \
00401 }
00402 
00416 #define U16_SET_CP_START_UNSAFE(s, i) { \
00417     if(U16_IS_TRAIL((s)[i])) { \
00418         --(i); \
00419     } \
00420 }
00421 
00436 #define U16_SET_CP_START(s, start, i) { \
00437     if(U16_IS_TRAIL((s)[i]) && (i)>(start) && U16_IS_LEAD((s)[(i)-1])) { \
00438         --(i); \
00439     } \
00440 }
00441 
00442 /* definitions with backward iteration -------------------------------------- */
00443 
00464 #define U16_PREV_UNSAFE(s, i, c) { \
00465     (c)=(s)[--(i)]; \
00466     if(U16_IS_TRAIL(c)) { \
00467         (c)=U16_GET_SUPPLEMENTARY((s)[--(i)], (c)); \
00468     } \
00469 }
00470 
00492 #define U16_PREV(s, start, i, c) { \
00493     (c)=(s)[--(i)]; \
00494     if(U16_IS_TRAIL(c)) { \
00495         uint16_t __c2; \
00496         if((i)>(start) && U16_IS_LEAD(__c2=(s)[(i)-1])) { \
00497             --(i); \
00498             (c)=U16_GET_SUPPLEMENTARY(__c2, (c)); \
00499         } \
00500     } \
00501 }
00502 
00514 #define U16_BACK_1_UNSAFE(s, i) { \
00515     if(U16_IS_TRAIL((s)[--(i)])) { \
00516         --(i); \
00517     } \
00518 }
00519 
00532 #define U16_BACK_1(s, start, i) { \
00533     if(U16_IS_TRAIL((s)[--(i)]) && (i)>(start) && U16_IS_LEAD((s)[(i)-1])) { \
00534         --(i); \
00535     } \
00536 }
00537 
00551 #define U16_BACK_N_UNSAFE(s, i, n) { \
00552     int32_t __N=(n); \
00553     while(__N>0) { \
00554         U16_BACK_1_UNSAFE(s, i); \
00555         --__N; \
00556     } \
00557 }
00558 
00573 #define U16_BACK_N(s, start, i, n) { \
00574     int32_t __N=(n); \
00575     while(__N>0 && (i)>(start)) { \
00576         U16_BACK_1(s, start, i); \
00577         --__N; \
00578     } \
00579 }
00580 
00594 #define U16_SET_CP_LIMIT_UNSAFE(s, i) { \
00595     if(U16_IS_LEAD((s)[(i)-1])) { \
00596         ++(i); \
00597     } \
00598 }
00599 
00617 #define U16_SET_CP_LIMIT(s, start, i, length) { \
00618     if((start)<(i) && ((i)<(length) || (length)<0) && U16_IS_LEAD((s)[(i)-1]) && U16_IS_TRAIL((s)[i])) { \
00619         ++(i); \
00620     } \
00621 }
00622 
00623 #endif
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines