Token::strValue: fixed handling of backslash

This commit is contained in:
Daniel Marjamäki 2015-06-07 11:25:33 +02:00
parent 35a6a1d9ea
commit 75b0430ba5
4 changed files with 48 additions and 10 deletions

View File

@ -150,7 +150,23 @@ void Token::concatStr(std::string const& b)
std::string Token::strValue() const
{
assert(_type == eString);
return _str.substr(1, _str.length() - 2);
std::string ret(_str.substr(1, _str.length() - 2));
std::string::size_type pos = 0U;
while ((pos = ret.find("\\",pos)) != std::string::npos) {
ret.erase(pos,1U);
if (ret[pos] >= 'a') {
if (ret[pos] == 'n')
ret[pos] = '\n';
else if (ret[pos] == 'r')
ret[pos] = '\r';
else if (ret[pos] == 't')
ret[pos] = '\t';
}
if (ret[pos] == '0')
return ret.substr(0,pos);
pos++;
}
return ret;
}
void Token::deleteNext(unsigned long index)
@ -665,21 +681,25 @@ bool Token::Match(const Token *tok, const char pattern[], unsigned int varid)
std::size_t Token::getStrLength(const Token *tok)
{
assert(tok != nullptr);
assert(tok->_type == eString);
std::size_t len = 0;
const std::string strValue(tok->strValue());
const char *str = strValue.c_str();
std::string::const_iterator it = tok->str().begin() + 1U;
const std::string::const_iterator end = tok->str().end() - 1U;
while (*str) {
if (*str == '\\') {
++str;
while (it != end) {
if (*it == '\\') {
++it;
// string ends at '\0'
if (*str == '0')
break;
if (*it == '0')
return len;
}
++str;
if (*it == '\0')
return len;
++it;
++len;
}

View File

@ -1548,6 +1548,7 @@ static bool valueFlowForLoop2(const Token *tok,
std::map<unsigned int, MathLib::bigint> *memory2,
std::map<unsigned int, MathLib::bigint> *memoryAfter)
{
// for ( firstExpression ; secondExpression ; thirdExpression )
const Token *firstExpression = tok->next()->astOperand2()->astOperand1();
const Token *secondExpression = tok->next()->astOperand2()->astOperand2()->astOperand1();
const Token *thirdExpression = tok->next()->astOperand2()->astOperand2()->astOperand2();

View File

@ -4263,7 +4263,7 @@ private:
ASSERT_EQUALS("'\\0' ;", tok("\"hello\"[5] ;"));
ASSERT_EQUALS("'\\0' ;", tok("\"\"[0] ;"));
ASSERT_EQUALS("'\\0' ;", tok("\"\\0\"[0] ;"));
ASSERT_EQUALS("'\\n' ;", tok("\"hello\\nworld\"[5] ;"));
ASSERT_EQUALS("'\n' ;", tok("\"hello\\nworld\"[5] ;"));
ASSERT_EQUALS("'w' ;", tok("\"hello\nworld\"[6] ;"));
ASSERT_EQUALS("\"hello\" [ 7 ] ;", tok("\"hello\"[7] ;"));
ASSERT_EQUALS("\"hello\" [ -1 ] ;", tok("\"hello\"[-1] ;"));

View File

@ -282,11 +282,28 @@ private:
void strValue() const {
Token tok(0);
tok.str("\"\"");
ASSERT_EQUALS("", tok.strValue());
tok.str("\"0\"");
ASSERT_EQUALS("0", tok.strValue());
tok.str("\"a\\n\"");
ASSERT_EQUALS("a\n", tok.strValue());
tok.str("\"a\\r\"");
ASSERT_EQUALS("a\r", tok.strValue());
tok.str("\"a\\t\"");
ASSERT_EQUALS("a\t", tok.strValue());
tok.str("\"\\\\\"");
ASSERT_EQUALS("\\", tok.strValue());
tok.str("\"a\\0\"");
ASSERT_EQUALS("a", tok.strValue());
}