该代码在生产线中的警告瓦特/ return
:PVS-Studio是否了解Unicode字符?
// Checks if the symbol defines two-symbols Unicode sequence
bool doubleSymbol(const char c) {
static const char TWO_SYMBOLS_MASK = 0b110;
return (c >> 5) == TWO_SYMBOLS_MASK;
}
// Checks if the symbol defines three-symbols Unicode sequence
bool tripleSymbol(const char c) {
static const char THREE_SYMBOLS_MASK = 0b1110;
return (c >> 4) == THREE_SYMBOLS_MASK;
}
// Checks if the symbol defines four-symbols Unicode sequence
bool quadrupleSymbol(const char c) {
static const char FOUR_SYMBOLS_MASK = 0b11110;
return (c >> 3) == FOUR_SYMBOLS_MASK;
}
PVS说,表达式总是假的(V547),但它们实际上是不:char
可以是Unicode符号的一部分被读取到std::string
! 下面是符号的Unicode表示:
1 byte - 0xxx'xxxx - 7 bits
2 bytes - 110x'xxxx 10xx'xxxx - 11 bits
3 bytes - 1110'xxxx 10xx'xxxx 10xx'xxxx - 16 bits
4 bytes - 1111'0xxx 10xx'xxxx 10xx'xxxx 10xx'xxxx - 21 bits
下面的代码计数在Unicode文本符号数:
size_t symbolCount = 0;
std::string s;
while (getline(std::cin, s)) {
for (size_t i = 0; i < s.size(); ++i) {
const char c = s[i];
++symbolCount;
if (doubleSymbol(c)) {
i += 1;
} else if (tripleSymbol(c)) {
i += 2;
} else if (quadrupleSymbol(c)) {
i += 3;
}
}
}
std::cout << symbolCount << "\n";
对于Hello!
输入输出是6
和Привет, мир!
是12
—这是对的!
我错了还是不知道PVS知道些什么? ;)
这可能是一个''signed'签名char'转换的问题。 – user0042
@ user0042所以我不明白。如果有问题,它为什么会起作用? – SerVB
这是一个潜在的问题。你的'char'有符号还是无符号,因为移位运算符会给出不同的结果。并且PVS是否知道'char'是否被签名? –