// © 2016 and later: Unicode, Inc. and others. // License & terms of use: http://www.unicode.org/copyright.html /* ****************************************************************************** * * Copyright (C) 1999-2012, International Business Machines * Corporation and others. All Rights Reserved. * ****************************************************************************** * file name: utf_impl.cpp * encoding: UTF-8 * tab size: 8 (not used) * indentation:4 * * created on: 1999sep13 * created by: Markus W. Scherer * * This file provides implementation functions for macros in the utfXX.h * that would otherwise be too long as macros. */ /* set import/export definitions */ #ifndef U_UTF8_IMPL #define U_UTF8_IMPL #endif #include "unicode/utypes.h" #include "unicode/utf.h" #include "unicode/utf8.h" #include "uassert.h" /* * Table of the number of utf8 trail bytes, indexed by the lead byte. * Used by the deprecated macro UTF8_COUNT_TRAIL_BYTES, defined in utf_old.h * * The current macro, U8_COUNT_TRAIL_BYTES, does _not_ use this table. * * Note that this table cannot be removed, even if UTF8_COUNT_TRAIL_BYTES were * changed to no longer use it. References to the table from expansions of UTF8_COUNT_TRAIL_BYTES * may exist in old client code that must continue to run with newer icu library versions. * * This table could be replaced on many machines by * a few lines of assembler code using an * "index of first 0-bit from msb" instruction and * one or two more integer instructions. * * For example, on an i386, do something like * - MOV AL, leadByte * - NOT AL (8-bit, leave b15..b8==0..0, reverse only b7..b0) * - MOV AH, 0 * - BSR BX, AX (16-bit) * - MOV AX, 6 (result) * - JZ finish (ZF==1 if leadByte==0xff) * - SUB AX, BX (result) * -finish: * (BSR: Bit Scan Reverse, scans for a 1-bit, starting from the MSB) */ U_CAPI const uint8_t utf8_countTrailBytes[256]= …; static const UChar32 utf8_errorValue[6]= …; static UChar32 errorValue(int32_t count, int8_t strict) { … } /* * Handle the non-inline part of the U8_NEXT() and U8_NEXT_FFFD() macros * and their obsolete sibling UTF8_NEXT_CHAR_SAFE(). * * U8_NEXT() supports NUL-terminated strings indicated via length<0. * * The "strict" parameter controls the error behavior: * <0 "Safe" behavior of U8_NEXT(): * -1: All illegal byte sequences yield U_SENTINEL=-1. * -2: Same as -1, except for lenient treatment of surrogate code points as legal. * Some implementations use this for roundtripping of * Unicode 16-bit strings that are not well-formed UTF-16, that is, they * contain unpaired surrogates. * -3: All illegal byte sequences yield U+FFFD. * 0 Obsolete "safe" behavior of UTF8_NEXT_CHAR_SAFE(..., false): * All illegal byte sequences yield a positive code point such that this * result code point would be encoded with the same number of bytes as * the illegal sequence. * >0 Obsolete "strict" behavior of UTF8_NEXT_CHAR_SAFE(..., true): * Same as the obsolete "safe" behavior, but non-characters are also treated * like illegal sequences. * * Note that a UBool is the same as an int8_t. */ U_CAPI UChar32 U_EXPORT2 utf8_nextCharSafeBody(const uint8_t *s, int32_t *pi, int32_t length, UChar32 c, UBool strict) { … } U_CAPI int32_t U_EXPORT2 utf8_appendCharSafeBody(uint8_t *s, int32_t i, int32_t length, UChar32 c, UBool *pIsError) { … } U_CAPI UChar32 U_EXPORT2 utf8_prevCharSafeBody(const uint8_t *s, int32_t start, int32_t *pi, UChar32 c, UBool strict) { … } U_CAPI int32_t U_EXPORT2 utf8_back1SafeBody(const uint8_t *s, int32_t start, int32_t i) { … }