我在下面有一个关于comparision const int的问题。
AK_FORCE_INLINE static int intArrayToCharArray(const int *const source, const int sourceSize,
char *dest, const int destSize) {
// We want to always terminate with a 0 char, so stop one short of the length to make
// sure there is room.
const int destLimit = destSize - 1;
int si = 0;
int di = 0;
while (si < sourceSize && di < destLimit && 0 != source[si]) {
const int codePoint = source[si++];
if (codePoint < 0x7F) { // One byte
dest[di++] = codePoint;
} else if (codePoint < 0x7FF) { // Two bytes
if (di + 1 >= destLimit) break;
dest[di++] = 0xC0 + (codePoint >> 6);
dest[di++] = 0x80 + (codePoint & 0x3F);
} else if (codePoint < 0xFFFF) { // Three bytes
if (di + 2 >= destLimit) break;
dest[di++] = 0xE0 + (codePoint >> 12);
dest[di++] = 0x80 + ((codePoint >> 6) & 0x3F);
dest[di++] = 0x80 + (codePoint & 0x3F);
} else if (codePoint <= 0x1FFFFF) { // Four bytes
if (di + 3 >= destLimit) break;
dest[di++] = 0xF0 + (codePoint >> 18);
dest[di++] = 0x80 + ((codePoint >> 12) & 0x3F);
dest[di++] = 0x80 + ((codePoint >> 6) & 0x3F);
dest[di++] = 0x80 + (codePoint & 0x3F);
} else if (codePoint <= 0x3FFFFFF) { // Five bytes
if (di + 4 >= destLimit) break;
dest[di++] = 0xF8 + (codePoint >> 24);
dest[di++] = 0x80 + ((codePoint >> 18) & 0x3F);
dest[di++] = 0x80 + ((codePoint >> 12) & 0x3F);
dest[di++] = 0x80 + ((codePoint >> 6) & 0x3F);
dest[di++] = codePoint & 0x3F;
} else if (codePoint <= 0x7FFFFFFF) { // Six bytes
if (di + 5 >= destLimit) break;
dest[di++] = 0xFC + (codePoint >> 30);
dest[di++] = 0x80 + ((codePoint >> 24) & 0x3F);
dest[di++] = 0x80 + ((codePoint >> 18) & 0x3F);
dest[di++] = 0x80 + ((codePoint >> 12) & 0x3F);
dest[di++] = 0x80 + ((codePoint >> 6) & 0x3F);
dest[di++] = codePoint & 0x3F;
} else {
// Not a code point... skip.
}
}
dest[di] = 0;
return di;
}
ndk 16及更低版本不是问题。但是当我达到ndk 17.它会在下面制造bug。 Android调试器显示在
行else if (codePoint <= 0x7FFFFFFF)
这就是错误:
Error:(88, 30) error: comparison 'const int' <= 2147483647 is always true [-Werror,-Wtautological-constant-compare]
感谢您的参加。
答案 0 :(得分:1)
由于codePoint
是int
,因此它是带符号的32位数字。我猜它NDK r17更严格(可能添加了-Wtautological-constant-compare
- 找不到发行说明)。
我认为您需要使用int
(或者unsigned int
甚至uint_least16_t
),而不是使用uint_fast16_t
。另请参阅http://en.cppreference.com/w/cpp/language/types(字符类型部分)。