• Home
  • Raw
  • Download

Lines Matching +full:string +full:- +full:length

6 *   Copyright (C) 1999-2015, International Business Machines
11 * encoding: UTF-8
21 * \brief C API: 8-bit Unicode handling macros
23 * This file defines macros to deal with 8-bit Unicode (UTF-8) code units (bytes) and strings.
26 * (https://unicode-org.github.io/icu/userguide/strings).
30 * Compound statements (curly braces {}) must be used for if-else-while...
43 /* internal definitions ----------------------------------------------------- */
46 * Counts the trail bytes for a UTF-8 lead byte.
53 * @param leadByte The first byte of a UTF-8 sequence. Must be 0..0xff.
61 * Counts the trail bytes for a UTF-8 lead byte of a valid UTF-8 sequence.
68 * @param leadByte The first byte of a UTF-8 sequence. Must be 0..0xff.
75 * Mask a UTF-8 lead byte, leave only the lower bits that form part of the code point value.
81 #define U8_MASK_LEAD_BYTE(leadByte, countTrailBytes) ((leadByte)&=(1<<(6-(countTrailBytes)))-1)
84 * Internal bit vector for 3-byte UTF-8 validity check, for use in U8_IS_VALID_LEAD3_AND_T1.
94 * Internal 3-byte UTF-8 validity check.
95 * Non-zero if lead byte E0..EF and first trail byte 00..FF start a valid sequence.
101 * Internal bit vector for 4-byte UTF-8 validity check, for use in U8_IS_VALID_LEAD4_AND_T1.
111 * Internal 4-byte UTF-8 validity check.
112 * Non-zero if lead byte F0..F4 and first trail byte 00..FF start a valid sequence.
118 * Function for handling "next code point" with error-checking.
127 utf8_nextCharSafeBody(const uint8_t *s, int32_t *pi, int32_t length, UChar32 c, UBool strict);
130 * Function for handling "append code point" with error-checking.
139 utf8_appendCharSafeBody(uint8_t *s, int32_t i, int32_t length, UChar32 c, UBool *pIsError);
142 * Function for handling "previous code point" with error-checking.
154 * Function for handling "skip backward one code point" with error-checking.
165 /* single-code point definitions -------------------------------------------- */
168 * Does this code unit (byte) encode a code point by itself (US-ASCII 0..0x7f)?
169 * @param c 8-bit code unit (byte)
176 * Is this code unit (byte) a UTF-8 lead byte? (0xC2..0xF4)
177 * @param c 8-bit code unit (byte)
181 #define U8_IS_LEAD(c) ((uint8_t)((c)-0xc2)<=0x32)
182 // 0x32=0xf4-0xc2
185 * Is this code unit (byte) a UTF-8 trail byte? (0x80..0xBF)
186 * @param c 8-bit code unit (byte)
190 #define U8_IS_TRAIL(c) ((int8_t)(c)<-0x40)
193 * How many code units (bytes) are used for the UTF-8 encoding
195 * @param c 32-bit code point
211 * The maximum number of UTF-8 code units (bytes) per Unicode code point (U+0000..U+10ffff).
218 * Get a code point from a string at a random-access offset,
223 * The result is undefined if the offset points to an illegal UTF-8
225 * Iteration through a string is more efficient with U8_NEXT_UNSAFE or U8_NEXT.
227 * @param s const uint8_t * string
228 * @param i string offset
240 * Get a code point from a string at a random-access offset,
246 * The length can be negative for a NUL-terminated string.
248 * If the offset points to an illegal UTF-8 byte sequence, then
250 * Iteration through a string is more efficient with U8_NEXT_UNSAFE or U8_NEXT.
252 * @param s const uint8_t * string
253 * @param start int32_t starting string offset
254 * @param i int32_t string offset, must be start<=i<length
255 * @param length int32_t string length
260 #define U8_GET(s, start, i, length, c) UPRV_BLOCK_MACRO_BEGIN { \ argument
263 U8_NEXT(s, _u8_get_index, length, c); \
267 * Get a code point from a string at a random-access offset,
273 * The length can be negative for a NUL-terminated string.
275 * If the offset points to an illegal UTF-8 byte sequence, then
277 * Iteration through a string is more efficient with U8_NEXT_UNSAFE or U8_NEXT_OR_FFFD.
280 * and U+FFFD returned for an ill-formed sequence.
283 * @param s const uint8_t * string
284 * @param start int32_t starting string offset
285 * @param i int32_t string offset, must be start<=i<length
286 * @param length int32_t string length
291 #define U8_GET_OR_FFFD(s, start, i, length, c) UPRV_BLOCK_MACRO_BEGIN { \ argument
294 U8_NEXT_OR_FFFD(s, _u8_get_index, length, c); \
297 /* definitions with forward iteration --------------------------------------- */
300 * Get a code point from a string at a code point boundary offset,
302 * (Post-incrementing forward iteration.)
303 * "Unsafe" macro, assumes well-formed UTF-8.
305 * The offset may point to the lead byte of a multi-byte sequence,
308 * or an illegal UTF-8 sequence.
310 * @param s const uint8_t * string
311 * @param i string offset
333 * Get a code point from a string at a code point boundary offset,
335 * (Post-incrementing forward iteration.)
336 * "Safe" macro, checks for illegal sequences and for string boundaries.
338 * The length can be negative for a NUL-terminated string.
340 * The offset may point to the lead byte of a multi-byte sequence,
342 * If the offset points to a trail byte or an illegal UTF-8 sequence, then
345 * @param s const uint8_t * string
346 * @param i int32_t string offset, must be i<length
347 * @param length int32_t string length
352 #define U8_NEXT(s, i, length, c) U8_INTERNAL_NEXT_OR_SUB(s, i, length, c, U_SENTINEL) argument
355 * Get a code point from a string at a code point boundary offset,
357 * (Post-incrementing forward iteration.)
358 * "Safe" macro, checks for illegal sequences and for string boundaries.
360 * The length can be negative for a NUL-terminated string.
362 * The offset may point to the lead byte of a multi-byte sequence,
364 * If the offset points to a trail byte or an illegal UTF-8 sequence, then
368 * and U+FFFD returned for an ill-formed sequence.
371 * @param s const uint8_t * string
372 * @param i int32_t string offset, must be i<length
373 * @param length int32_t string length
378 #define U8_NEXT_OR_FFFD(s, i, length, c) U8_INTERNAL_NEXT_OR_SUB(s, i, length, c, 0xfffd) argument
381 #define U8_INTERNAL_NEXT_OR_SUB(s, i, length, c, sub) UPRV_BLOCK_MACRO_BEGIN { \ argument
385 if((i)!=(length) && \
392 ((c)-=0xf0)<=4 && \
394 ((c)=((c)<<6)|(__t&0x3f), ++(i)!=(length)) && \
395 (__t=(s)[i]-0x80)<=0x3f) && \
396 /* valid second-to-last trail byte */ \
397 ((c)=((c)<<6)|__t, ++(i)!=(length)) \
401 (__t=(s)[i]-0x80)<=0x3f && \
404 (c)=(sub); /* ill-formed*/ \
410 * Append a code point to a string, overwriting 1 to 4 bytes.
411 * The offset points to the current end of the string contents
412 * and is advanced (post-increment).
413 * "Unsafe" macro, assumes a valid code point and sufficient space in the string.
416 * @param s const uint8_t * string buffer
417 * @param i string offset
443 * Append a code point to a string, overwriting 1 to 4 bytes.
444 * The offset points to the current end of the string contents
445 * and is advanced (post-increment).
447 * If a non-ASCII code point is written, checks for sufficient space in the string.
451 * @param s const uint8_t * string buffer
452 * @param i int32_t string offset, must be i<capacity
453 * @param capacity int32_t size of the string buffer
481 * Advance the string offset from one code point boundary to the next.
482 * (Post-incrementing iteration.)
483 * "Unsafe" macro, assumes well-formed UTF-8.
485 * @param s const uint8_t * string
486 * @param i string offset
495 * Advance the string offset from one code point boundary to the next.
496 * (Post-incrementing iteration.)
497 * "Safe" macro, checks for illegal sequences and for string boundaries.
499 * The length can be negative for a NUL-terminated string.
501 * @param s const uint8_t * string
502 * @param i int32_t string offset, must be i<length
503 * @param length int32_t string length
507 #define U8_FWD_1(s, i, length) UPRV_BLOCK_MACRO_BEGIN { \ argument
509 if(U8_IS_LEAD(__b) && (i)!=(length)) { \
513 ++(i)!=(length) && U8_IS_TRAIL((s)[i])) { \
522 ++(i)!=(length) && U8_IS_TRAIL((s)[i]) && \
523 ++(i)!=(length) && U8_IS_TRAIL((s)[i])) { \
531 * Advance the string offset from one code point boundary to the n-th next one,
533 * (Post-incrementing iteration.)
534 * "Unsafe" macro, assumes well-formed UTF-8.
536 * @param s const uint8_t * string
537 * @param i string offset
546 --__N; \
551 * Advance the string offset from one code point boundary to the n-th next one,
553 * (Post-incrementing iteration.)
554 * "Safe" macro, checks for illegal sequences and for string boundaries.
556 * The length can be negative for a NUL-terminated string.
558 * @param s const uint8_t * string
559 * @param i int32_t string offset, must be i<length
560 * @param length int32_t string length
565 #define U8_FWD_N(s, i, length, n) UPRV_BLOCK_MACRO_BEGIN { \ argument
567 while(__N>0 && ((i)<(length) || ((length)<0 && (s)[i]!=0))) { \
568 U8_FWD_1(s, i, length); \
569 --__N; \
574 * Adjust a random-access offset to a code point boundary
576 * If the offset points to a UTF-8 trail byte,
579 * "Unsafe" macro, assumes well-formed UTF-8.
581 * @param s const uint8_t * string
582 * @param i string offset
587 while(U8_IS_TRAIL((s)[i])) { --(i); } \
591 * Adjust a random-access offset to a code point boundary
593 * If the offset points to a UTF-8 trail byte,
597 * "Safe" macro, checks for illegal sequences and for string boundaries.
600 * @param s const uint8_t * string
601 * @param start int32_t starting string offset (usually 0)
602 * @param i int32_t string offset, must be start<=i
614 * If the string ends with a UTF-8 byte sequence that is valid so far
615 * but incomplete, then reduce the length of the string to end before
617 * For example, if the string ends with E1 80, the length is reduced by 2.
619 * In all other cases (the string ends with a complete sequence, or it is not
621 * the length remains unchanged.
626 * (check for string length only once per character).
628 * "Safe" macro, checks for illegal sequences and for string boundaries.
629 * Unlike U8_SET_CP_START(), this macro never reads s[length].
631 * (In UTF-16, simply check for U16_IS_LEAD(last code unit).)
633 * @param s const uint8_t * string
634 * @param start int32_t starting string offset (usually 0)
635 * @param length int32_t string length (usually start<=length)
639 #define U8_TRUNCATE_IF_INCOMPLETE(s, start, length) UPRV_BLOCK_MACRO_BEGIN { \ argument
640 if((length)>(start)) { \
641 uint8_t __b1=s[(length)-1]; \
645 --(length); \
646 } else if(U8_IS_TRAIL(__b1) && ((length)-2)>=(start)) { \
647 uint8_t __b2=s[(length)-2]; \
651 (length)-=2; \
653 } else if(U8_IS_TRAIL(__b2) && ((length)-3)>=(start)) { \
654 uint8_t __b3=s[(length)-3]; \
656 (length)-=3; \
663 /* definitions with backward iteration -------------------------------------- */
666 * Move the string offset from one code point boundary to the previous one
668 * (Pre-decrementing backward iteration.)
669 * "Unsafe" macro, assumes well-formed UTF-8.
671 * The input offset may be the same as the string length.
672 * If the offset is behind a multi-byte sequence, then the macro will read
676 * The result is undefined if the offset is behind an illegal UTF-8 sequence.
678 * @param s const uint8_t * string
679 * @param i string offset
685 (c)=(uint8_t)(s)[--(i)]; \
692 __b=(s)[--(i)]; \
707 * Move the string offset from one code point boundary to the previous one
709 * (Pre-decrementing backward iteration.)
710 * "Safe" macro, checks for illegal sequences and for string boundaries.
712 * The input offset may be the same as the string length.
713 * If the offset is behind a multi-byte sequence, then the macro will read
717 * If the offset is behind an illegal UTF-8 sequence, then c is set to a negative value.
719 * @param s const uint8_t * string
720 * @param start int32_t starting string offset (usually 0)
721 * @param i int32_t string offset, must be start<i
727 (c)=(uint8_t)(s)[--(i)]; \
729 (c)=utf8_prevCharSafeBody((const uint8_t *)s, start, &(i), c, -1); \
734 * Move the string offset from one code point boundary to the previous one
736 * (Pre-decrementing backward iteration.)
737 * "Safe" macro, checks for illegal sequences and for string boundaries.
739 * The input offset may be the same as the string length.
740 * If the offset is behind a multi-byte sequence, then the macro will read
744 * If the offset is behind an illegal UTF-8 sequence, then c is set to U+FFFD.
747 * and U+FFFD returned for an ill-formed sequence.
750 * @param s const uint8_t * string
751 * @param start int32_t starting string offset (usually 0)
752 * @param i int32_t string offset, must be start<i
758 (c)=(uint8_t)(s)[--(i)]; \
760 (c)=utf8_prevCharSafeBody((const uint8_t *)s, start, &(i), c, -3); \
765 * Move the string offset from one code point boundary to the previous one.
766 * (Pre-decrementing backward iteration.)
767 * The input offset may be the same as the string length.
768 * "Unsafe" macro, assumes well-formed UTF-8.
770 * @param s const uint8_t * string
771 * @param i string offset
776 while(U8_IS_TRAIL((s)[--(i)])) {} \
780 * Move the string offset from one code point boundary to the previous one.
781 * (Pre-decrementing backward iteration.)
782 * The input offset may be the same as the string length.
783 * "Safe" macro, checks for illegal sequences and for string boundaries.
785 * @param s const uint8_t * string
786 * @param start int32_t starting string offset (usually 0)
787 * @param i int32_t string offset, must be start<i
792 if(U8_IS_TRAIL((s)[--(i)])) { \
798 * Move the string offset from one code point boundary to the n-th one before it,
800 * (Pre-decrementing backward iteration.)
801 * The input offset may be the same as the string length.
802 * "Unsafe" macro, assumes well-formed UTF-8.
804 * @param s const uint8_t * string
805 * @param i string offset
814 --__N; \
819 * Move the string offset from one code point boundary to the n-th one before it,
821 * (Pre-decrementing backward iteration.)
822 * The input offset may be the same as the string length.
823 * "Safe" macro, checks for illegal sequences and for string boundaries.
825 * @param s const uint8_t * string
826 * @param start int32_t index of the start of the string
827 * @param i int32_t string offset, must be start<i
836 --__N; \
841 * Adjust a random-access offset to a code point boundary after a code point.
842 * If the offset is behind a partial multi-byte sequence,
845 * The input offset may be the same as the string length.
846 * "Unsafe" macro, assumes well-formed UTF-8.
848 * @param s const uint8_t * string
849 * @param i string offset
859 * Adjust a random-access offset to a code point boundary after a code point.
860 * If the offset is behind a partial multi-byte sequence,
863 * The input offset may be the same as the string length.
864 * "Safe" macro, checks for illegal sequences and for string boundaries.
866 * The length can be negative for a NUL-terminated string.
868 * @param s const uint8_t * string
869 * @param start int32_t starting string offset (usually 0)
870 * @param i int32_t string offset, must be start<=i<=length
871 * @param length int32_t string length
875 #define U8_SET_CP_LIMIT(s, start, i, length) UPRV_BLOCK_MACRO_BEGIN { \ argument
876 if((start)<(i) && ((i)<(length) || (length)<0)) { \
878 U8_FWD_1(s, i, length); \