Tokenizer: Improved handling of '\0' in strings

This commit is contained in:
Daniel Marjamäki 2012-12-25 08:56:12 +01:00
parent 83da4125e3
commit db2a129557
3 changed files with 21 additions and 10 deletions

View File

@ -976,7 +976,16 @@ void Token::stringify(std::ostream& os, bool varid, bool attributes) const
if (isLong()) if (isLong())
os << "long "; os << "long ";
} }
if (_str[0] != '\"' || _str.find("\0") == std::string::npos)
os << _str; os << _str;
else {
for (std::size_t i = 0U; i < _str.size(); ++i) {
if (_str[i] == '\0')
os << "\\0";
else
os << _str[i];
}
}
if (varid && _varId != 0) if (varid && _varId != 0)
os << '@' << _varId; os << '@' << _varId;
} }

View File

@ -8048,12 +8048,9 @@ std::string Tokenizer::simplifyString(const std::string &source)
if (sz <= 2) if (sz <= 2)
i++; i++;
else if (i+sz < str.size()) { else if (i+sz < str.size())
if (c == 0)
str = str.substr(0,i) + "\"";
else
str.replace(i, sz, std::string(1U, (char)c)); str.replace(i, sz, std::string(1U, (char)c));
} else else
str.replace(i, str.size() - i - 1U, "a"); str.replace(i, str.size() - i - 1U, "a");
} }

View File

@ -842,9 +842,14 @@ private:
} }
void declareArray() { void declareArray() {
const char code[] = "void f ( ) { char str [ ] = \"100\" ; }"; const char code1[] = "void f ( ) { char str [ ] = \"100\" ; }";
const char expected[] = "void f ( ) { char str [ 4 ] = \"100\" ; }"; const char expected1[] = "void f ( ) { char str [ 4 ] = \"100\" ; }";
ASSERT_EQUALS(expected, tok(code)); ASSERT_EQUALS(expected1, tok(code1));
const char code2[] = "char str [ ] = \"\\x00\";";
const char expected2[] = "char str [ 2 ] = \"\\0\" ;";
std::string actual = tok(code2);
ASSERT_EQUALS(expected2, actual);
} }
void dontRemoveIncrement() { void dontRemoveIncrement() {