Go to the documentation of this file.
146 #ifndef __UTF_OLD_H__
147 #define __UTF_OLD_H__
149 #ifndef U_HIDE_DEPRECATED_API
158 #ifdef U_USE_UTF_DEPRECATES
166 typedef int32_t UTextOffset;
198 #define UTF8_ERROR_VALUE_1 0x15
205 #define UTF8_ERROR_VALUE_2 0x9f
213 #define UTF_ERROR_VALUE 0xffff
221 #define UTF_IS_ERROR(c) \
222 (((c)&0xfffe)==0xfffe || (c)==UTF8_ERROR_VALUE_1 || (c)==UTF8_ERROR_VALUE_2)
229 #define UTF_IS_VALID(c) \
230 (UTF_IS_UNICODE_CHAR(c) && \
231 (c)!=UTF8_ERROR_VALUE_1 && (c)!=UTF8_ERROR_VALUE_2)
237 #define UTF_IS_SURROGATE(uchar) (((uchar)&0xfffff800)==0xd800)
244 #define UTF_IS_UNICODE_NONCHAR(c) \
246 ((uint32_t)(c)<=0xfdef || ((c)&0xfffe)==0xfffe) && \
247 (uint32_t)(c)<=0x10ffff)
264 #define UTF_IS_UNICODE_CHAR(c) \
265 ((uint32_t)(c)<0xd800 || \
266 ((uint32_t)(c)>0xdfff && \
267 (uint32_t)(c)<=0x10ffff && \
268 !UTF_IS_UNICODE_NONCHAR(c)))
276 #define UTF8_COUNT_TRAIL_BYTES(leadByte) (utf8_countTrailBytes[(uint8_t)leadByte])
282 #define UTF8_MASK_LEAD_BYTE(leadByte, countTrailBytes) ((leadByte)&=(1<<(6-(countTrailBytes)))-1)
285 #define UTF8_IS_SINGLE(uchar) (((uchar)&0x80)==0)
287 #define UTF8_IS_LEAD(uchar) ((uint8_t)((uchar)-0xc0)<0x3e)
289 #define UTF8_IS_TRAIL(uchar) (((uchar)&0xc0)==0x80)
292 #define UTF8_NEED_MULTIPLE_UCHAR(c) ((uint32_t)(c)>0x7f)
308 # define UTF8_CHAR_LENGTH(c) \
309 ((uint32_t)(c)<=0x7f ? 1 : \
310 ((uint32_t)(c)<=0x7ff ? 2 : \
311 ((uint32_t)((c)-0x10000)>0xfffff ? 3 : 4) \
315 # define UTF8_CHAR_LENGTH(c) \
316 ((uint32_t)(c)<=0x7f ? 1 : \
317 ((uint32_t)(c)<=0x7ff ? 2 : \
318 ((uint32_t)(c)<=0xffff ? 3 : \
319 ((uint32_t)(c)<=0x10ffff ? 4 : \
320 ((uint32_t)(c)<=0x3ffffff ? 5 : \
321 ((uint32_t)(c)<=0x7fffffff ? 6 : 3) \
330 #define UTF8_MAX_CHAR_LENGTH 4
333 #define UTF8_ARRAY_SIZE(size) ((5*(size))/2)
336 #define UTF8_GET_CHAR_UNSAFE(s, i, c) { \
337 int32_t _utf8_get_char_unsafe_index=(int32_t)(i); \
338 UTF8_SET_CHAR_START_UNSAFE(s, _utf8_get_char_unsafe_index); \
339 UTF8_NEXT_CHAR_UNSAFE(s, _utf8_get_char_unsafe_index, c); \
343 #define UTF8_GET_CHAR_SAFE(s, start, i, length, c, strict) { \
344 int32_t _utf8_get_char_safe_index=(int32_t)(i); \
345 UTF8_SET_CHAR_START_SAFE(s, start, _utf8_get_char_safe_index); \
346 UTF8_NEXT_CHAR_SAFE(s, _utf8_get_char_safe_index, length, c, strict); \
350 #define UTF8_NEXT_CHAR_UNSAFE(s, i, c) { \
352 if((uint8_t)((c)-0xc0)<0x35) { \
353 uint8_t __count=UTF8_COUNT_TRAIL_BYTES(c); \
354 UTF8_MASK_LEAD_BYTE(c, __count); \
358 (c)=((c)<<6)|((s)[(i)++]&0x3f); \
360 (c)=((c)<<6)|((s)[(i)++]&0x3f); \
362 (c)=((c)<<6)|((s)[(i)++]&0x3f); \
370 #define UTF8_APPEND_CHAR_UNSAFE(s, i, c) { \
371 if((uint32_t)(c)<=0x7f) { \
372 (s)[(i)++]=(uint8_t)(c); \
374 if((uint32_t)(c)<=0x7ff) { \
375 (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
377 if((uint32_t)(c)<=0xffff) { \
378 (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
380 (s)[(i)++]=(uint8_t)(((c)>>18)|0xf0); \
381 (s)[(i)++]=(uint8_t)((((c)>>12)&0x3f)|0x80); \
383 (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
385 (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
390 #define UTF8_FWD_1_UNSAFE(s, i) { \
391 (i)+=1+UTF8_COUNT_TRAIL_BYTES((s)[i]); \
395 #define UTF8_FWD_N_UNSAFE(s, i, n) { \
398 UTF8_FWD_1_UNSAFE(s, i); \
404 #define UTF8_SET_CHAR_START_UNSAFE(s, i) { \
405 while(UTF8_IS_TRAIL((s)[i])) { --(i); } \
409 #define UTF8_NEXT_CHAR_SAFE(s, i, length, c, strict) { \
412 if(UTF8_IS_LEAD(c)) { \
413 (c)=utf8_nextCharSafeBody(s, &(i), (int32_t)(length), c, strict); \
415 (c)=UTF8_ERROR_VALUE_1; \
421 #define UTF8_APPEND_CHAR_SAFE(s, i, length, c) { \
422 if((uint32_t)(c)<=0x7f) { \
423 (s)[(i)++]=(uint8_t)(c); \
425 (i)=utf8_appendCharSafeBody(s, (int32_t)(i), (int32_t)(length), c, NULL); \
430 #define UTF8_FWD_1_SAFE(s, i, length) U8_FWD_1(s, i, length)
433 #define UTF8_FWD_N_SAFE(s, i, length, n) U8_FWD_N(s, i, length, n)
436 #define UTF8_SET_CHAR_START_SAFE(s, start, i) U8_SET_CP_START(s, start, i)
439 #define UTF8_PREV_CHAR_UNSAFE(s, i, c) { \
441 if(UTF8_IS_TRAIL(c)) { \
442 uint8_t __b, __count=1, __shift=6; \
449 UTF8_MASK_LEAD_BYTE(__b, __count); \
450 (c)|=(UChar32)__b<<__shift; \
453 (c)|=(UChar32)(__b&0x3f)<<__shift; \
462 #define UTF8_BACK_1_UNSAFE(s, i) { \
463 while(UTF8_IS_TRAIL((s)[--(i)])) {} \
467 #define UTF8_BACK_N_UNSAFE(s, i, n) { \
470 UTF8_BACK_1_UNSAFE(s, i); \
476 #define UTF8_SET_CHAR_LIMIT_UNSAFE(s, i) { \
477 UTF8_BACK_1_UNSAFE(s, i); \
478 UTF8_FWD_1_UNSAFE(s, i); \
482 #define UTF8_PREV_CHAR_SAFE(s, start, i, c, strict) { \
486 (c)=utf8_prevCharSafeBody(s, start, &(i), c, strict); \
488 (c)=UTF8_ERROR_VALUE_1; \
494 #define UTF8_BACK_1_SAFE(s, start, i) U8_BACK_1(s, start, i)
497 #define UTF8_BACK_N_SAFE(s, start, i, n) U8_BACK_N(s, start, i, n)
500 #define UTF8_SET_CHAR_LIMIT_SAFE(s, start, i, length) U8_SET_CP_LIMIT(s, start, i, length)
505 #define UTF_IS_FIRST_SURROGATE(uchar) (((uchar)&0xfffffc00)==0xd800)
508 #define UTF_IS_SECOND_SURROGATE(uchar) (((uchar)&0xfffffc00)==0xdc00)
511 #define UTF_IS_SURROGATE_FIRST(c) (((c)&0x400)==0)
514 #define UTF_SURROGATE_OFFSET ((0xd800<<10UL)+0xdc00-0x10000)
517 #define UTF16_GET_PAIR_VALUE(first, second) \
518 (((first)<<10UL)+(second)-UTF_SURROGATE_OFFSET)
521 #define UTF_FIRST_SURROGATE(supplementary) (UChar)(((supplementary)>>10)+0xd7c0)
524 #define UTF_SECOND_SURROGATE(supplementary) (UChar)(((supplementary)&0x3ff)|0xdc00)
527 #define UTF16_LEAD(supplementary) UTF_FIRST_SURROGATE(supplementary)
530 #define UTF16_TRAIL(supplementary) UTF_SECOND_SURROGATE(supplementary)
533 #define UTF16_IS_SINGLE(uchar) !UTF_IS_SURROGATE(uchar)
536 #define UTF16_IS_LEAD(uchar) UTF_IS_FIRST_SURROGATE(uchar)
539 #define UTF16_IS_TRAIL(uchar) UTF_IS_SECOND_SURROGATE(uchar)
542 #define UTF16_NEED_MULTIPLE_UCHAR(c) ((uint32_t)(c)>0xffff)
545 #define UTF16_CHAR_LENGTH(c) ((uint32_t)(c)<=0xffff ? 1 : 2)
548 #define UTF16_MAX_CHAR_LENGTH 2
551 #define UTF16_ARRAY_SIZE(size) (size)
564 #define UTF16_GET_CHAR_UNSAFE(s, i, c) { \
566 if(UTF_IS_SURROGATE(c)) { \
567 if(UTF_IS_SURROGATE_FIRST(c)) { \
568 (c)=UTF16_GET_PAIR_VALUE((c), (s)[(i)+1]); \
570 (c)=UTF16_GET_PAIR_VALUE((s)[(i)-1], (c)); \
576 #define UTF16_GET_CHAR_SAFE(s, start, i, length, c, strict) { \
578 if(UTF_IS_SURROGATE(c)) { \
580 if(UTF_IS_SURROGATE_FIRST(c)) { \
581 if((i)+1<(length) && UTF_IS_SECOND_SURROGATE(__c2=(s)[(i)+1])) { \
582 (c)=UTF16_GET_PAIR_VALUE((c), __c2); \
586 (c)=UTF_ERROR_VALUE; \
589 if((i)-1>=(start) && UTF_IS_FIRST_SURROGATE(__c2=(s)[(i)-1])) { \
590 (c)=UTF16_GET_PAIR_VALUE(__c2, (c)); \
594 (c)=UTF_ERROR_VALUE; \
597 } else if((strict) && !UTF_IS_UNICODE_CHAR(c)) { \
598 (c)=UTF_ERROR_VALUE; \
603 #define UTF16_NEXT_CHAR_UNSAFE(s, i, c) { \
605 if(UTF_IS_FIRST_SURROGATE(c)) { \
606 (c)=UTF16_GET_PAIR_VALUE((c), (s)[(i)++]); \
611 #define UTF16_APPEND_CHAR_UNSAFE(s, i, c) { \
612 if((uint32_t)(c)<=0xffff) { \
613 (s)[(i)++]=(uint16_t)(c); \
615 (s)[(i)++]=(uint16_t)(((c)>>10)+0xd7c0); \
616 (s)[(i)++]=(uint16_t)(((c)&0x3ff)|0xdc00); \
621 #define UTF16_FWD_1_UNSAFE(s, i) { \
622 if(UTF_IS_FIRST_SURROGATE((s)[(i)++])) { \
628 #define UTF16_FWD_N_UNSAFE(s, i, n) { \
631 UTF16_FWD_1_UNSAFE(s, i); \
637 #define UTF16_SET_CHAR_START_UNSAFE(s, i) { \
638 if(UTF_IS_SECOND_SURROGATE((s)[i])) { \
644 #define UTF16_NEXT_CHAR_SAFE(s, i, length, c, strict) { \
646 if(UTF_IS_FIRST_SURROGATE(c)) { \
648 if((i)<(length) && UTF_IS_SECOND_SURROGATE(__c2=(s)[(i)])) { \
650 (c)=UTF16_GET_PAIR_VALUE((c), __c2); \
654 (c)=UTF_ERROR_VALUE; \
656 } else if((strict) && !UTF_IS_UNICODE_CHAR(c)) { \
658 (c)=UTF_ERROR_VALUE; \
663 #define UTF16_APPEND_CHAR_SAFE(s, i, length, c) { \
664 if((uint32_t)(c)<=0xffff) { \
665 (s)[(i)++]=(uint16_t)(c); \
666 } else if((uint32_t)(c)<=0x10ffff) { \
667 if((i)+1<(length)) { \
668 (s)[(i)++]=(uint16_t)(((c)>>10)+0xd7c0); \
669 (s)[(i)++]=(uint16_t)(((c)&0x3ff)|0xdc00); \
671 (s)[(i)++]=UTF_ERROR_VALUE; \
674 (s)[(i)++]=UTF_ERROR_VALUE; \
679 #define UTF16_FWD_1_SAFE(s, i, length) U16_FWD_1(s, i, length)
682 #define UTF16_FWD_N_SAFE(s, i, length, n) U16_FWD_N(s, i, length, n)
685 #define UTF16_SET_CHAR_START_SAFE(s, start, i) U16_SET_CP_START(s, start, i)
688 #define UTF16_PREV_CHAR_UNSAFE(s, i, c) { \
690 if(UTF_IS_SECOND_SURROGATE(c)) { \
691 (c)=UTF16_GET_PAIR_VALUE((s)[--(i)], (c)); \
696 #define UTF16_BACK_1_UNSAFE(s, i) { \
697 if(UTF_IS_SECOND_SURROGATE((s)[--(i)])) { \
703 #define UTF16_BACK_N_UNSAFE(s, i, n) { \
706 UTF16_BACK_1_UNSAFE(s, i); \
712 #define UTF16_SET_CHAR_LIMIT_UNSAFE(s, i) { \
713 if(UTF_IS_FIRST_SURROGATE((s)[(i)-1])) { \
719 #define UTF16_PREV_CHAR_SAFE(s, start, i, c, strict) { \
721 if(UTF_IS_SECOND_SURROGATE(c)) { \
723 if((i)>(start) && UTF_IS_FIRST_SURROGATE(__c2=(s)[(i)-1])) { \
725 (c)=UTF16_GET_PAIR_VALUE(__c2, (c)); \
729 (c)=UTF_ERROR_VALUE; \
731 } else if((strict) && !UTF_IS_UNICODE_CHAR(c)) { \
733 (c)=UTF_ERROR_VALUE; \
738 #define UTF16_BACK_1_SAFE(s, start, i) U16_BACK_1(s, start, i)
741 #define UTF16_BACK_N_SAFE(s, start, i, n) U16_BACK_N(s, start, i, n)
744 #define UTF16_SET_CHAR_LIMIT_SAFE(s, start, i, length) U16_SET_CP_LIMIT(s, start, i, length)
764 #define UTF32_IS_SAFE(c, strict) \
766 (uint32_t)(c)<=0x10ffff : \
767 UTF_IS_UNICODE_CHAR(c))
780 #define UTF32_IS_SINGLE(uchar) 1
782 #define UTF32_IS_LEAD(uchar) 0
784 #define UTF32_IS_TRAIL(uchar) 0
789 #define UTF32_NEED_MULTIPLE_UCHAR(c) 0
791 #define UTF32_CHAR_LENGTH(c) 1
793 #define UTF32_MAX_CHAR_LENGTH 1
798 #define UTF32_ARRAY_SIZE(size) (size)
801 #define UTF32_GET_CHAR_UNSAFE(s, i, c) { \
806 #define UTF32_GET_CHAR_SAFE(s, start, i, length, c, strict) { \
808 if(!UTF32_IS_SAFE(c, strict)) { \
809 (c)=UTF_ERROR_VALUE; \
816 #define UTF32_NEXT_CHAR_UNSAFE(s, i, c) { \
821 #define UTF32_APPEND_CHAR_UNSAFE(s, i, c) { \
826 #define UTF32_FWD_1_UNSAFE(s, i) { \
831 #define UTF32_FWD_N_UNSAFE(s, i, n) { \
836 #define UTF32_SET_CHAR_START_UNSAFE(s, i) { \
840 #define UTF32_NEXT_CHAR_SAFE(s, i, length, c, strict) { \
842 if(!UTF32_IS_SAFE(c, strict)) { \
843 (c)=UTF_ERROR_VALUE; \
848 #define UTF32_APPEND_CHAR_SAFE(s, i, length, c) { \
849 if((uint32_t)(c)<=0x10ffff) { \
857 #define UTF32_FWD_1_SAFE(s, i, length) { \
862 #define UTF32_FWD_N_SAFE(s, i, length, n) { \
863 if(((i)+=(n))>(length)) { \
869 #define UTF32_SET_CHAR_START_SAFE(s, start, i) { \
875 #define UTF32_PREV_CHAR_UNSAFE(s, i, c) { \
880 #define UTF32_BACK_1_UNSAFE(s, i) { \
885 #define UTF32_BACK_N_UNSAFE(s, i, n) { \
890 #define UTF32_SET_CHAR_LIMIT_UNSAFE(s, i) { \
894 #define UTF32_PREV_CHAR_SAFE(s, start, i, c, strict) { \
896 if(!UTF32_IS_SAFE(c, strict)) { \
897 (c)=UTF_ERROR_VALUE; \
902 #define UTF32_BACK_1_SAFE(s, start, i) { \
907 #define UTF32_BACK_N_SAFE(s, start, i, n) { \
915 #define UTF32_SET_CHAR_LIMIT_SAFE(s, i, length) { \
925 #define UTF_ARRAY_SIZE(size) UTF16_ARRAY_SIZE(size)
928 #define UTF_GET_CHAR_UNSAFE(s, i, c) UTF16_GET_CHAR_UNSAFE(s, i, c)
931 #define UTF_GET_CHAR_SAFE(s, start, i, length, c, strict) UTF16_GET_CHAR_SAFE(s, start, i, length, c, strict)
935 #define UTF_NEXT_CHAR_UNSAFE(s, i, c) UTF16_NEXT_CHAR_UNSAFE(s, i, c)
938 #define UTF_NEXT_CHAR_SAFE(s, i, length, c, strict) UTF16_NEXT_CHAR_SAFE(s, i, length, c, strict)
942 #define UTF_APPEND_CHAR_UNSAFE(s, i, c) UTF16_APPEND_CHAR_UNSAFE(s, i, c)
945 #define UTF_APPEND_CHAR_SAFE(s, i, length, c) UTF16_APPEND_CHAR_SAFE(s, i, length, c)
949 #define UTF_FWD_1_UNSAFE(s, i) UTF16_FWD_1_UNSAFE(s, i)
952 #define UTF_FWD_1_SAFE(s, i, length) UTF16_FWD_1_SAFE(s, i, length)
956 #define UTF_FWD_N_UNSAFE(s, i, n) UTF16_FWD_N_UNSAFE(s, i, n)
959 #define UTF_FWD_N_SAFE(s, i, length, n) UTF16_FWD_N_SAFE(s, i, length, n)
963 #define UTF_SET_CHAR_START_UNSAFE(s, i) UTF16_SET_CHAR_START_UNSAFE(s, i)
966 #define UTF_SET_CHAR_START_SAFE(s, start, i) UTF16_SET_CHAR_START_SAFE(s, start, i)
970 #define UTF_PREV_CHAR_UNSAFE(s, i, c) UTF16_PREV_CHAR_UNSAFE(s, i, c)
973 #define UTF_PREV_CHAR_SAFE(s, start, i, c, strict) UTF16_PREV_CHAR_SAFE(s, start, i, c, strict)
977 #define UTF_BACK_1_UNSAFE(s, i) UTF16_BACK_1_UNSAFE(s, i)
980 #define UTF_BACK_1_SAFE(s, start, i) UTF16_BACK_1_SAFE(s, start, i)
984 #define UTF_BACK_N_UNSAFE(s, i, n) UTF16_BACK_N_UNSAFE(s, i, n)
987 #define UTF_BACK_N_SAFE(s, start, i, n) UTF16_BACK_N_SAFE(s, start, i, n)
991 #define UTF_SET_CHAR_LIMIT_UNSAFE(s, i) UTF16_SET_CHAR_LIMIT_UNSAFE(s, i)
994 #define UTF_SET_CHAR_LIMIT_SAFE(s, start, i, length) UTF16_SET_CHAR_LIMIT_SAFE(s, start, i, length)
1003 #define UTF_IS_SINGLE(uchar) U16_IS_SINGLE(uchar)
1010 #define UTF_IS_LEAD(uchar) U16_IS_LEAD(uchar)
1017 #define UTF_IS_TRAIL(uchar) U16_IS_TRAIL(uchar)
1024 #define UTF_NEED_MULTIPLE_UCHAR(c) UTF16_NEED_MULTIPLE_UCHAR(c)
1031 #define UTF_CHAR_LENGTH(c) U16_LENGTH(c)
1038 #define UTF_MAX_CHAR_LENGTH U16_MAX_LENGTH
1049 #define UTF_GET_CHAR(s, start, i, length, c) U16_GET(s, start, i, length, c)
1062 #define UTF_NEXT_CHAR(s, i, length, c) U16_NEXT(s, i, length, c)
1075 #define UTF_APPEND_CHAR(s, i, length, c) UTF16_APPEND_CHAR_SAFE(s, i, length, c)
1086 #define UTF_FWD_1(s, i, length) U16_FWD_1(s, i, length)
1097 #define UTF_FWD_N(s, i, length, n) U16_FWD_N(s, i, length, n)
1113 #define UTF_SET_CHAR_START(s, start, i) U16_SET_CP_START(s, start, i)
1126 #define UTF_PREV_CHAR(s, start, i, c) U16_PREV(s, start, i, c)
1139 #define UTF_BACK_1(s, start, i) U16_BACK_1(s, start, i)
1152 #define UTF_BACK_N(s, start, i, n) U16_BACK_N(s, start, i, n)
1168 #define UTF_SET_CHAR_LIMIT(s, start, i, length) U16_SET_CP_LIMIT(s, start, i, length)