Skip to content

Commit 75b0430

Browse files
committed
Token::strValue: fixed handling of backslash
1 parent 35a6a1d commit 75b0430

4 files changed

Lines changed: 48 additions & 10 deletions

File tree

lib/token.cpp

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,23 @@ void Token::concatStr(std::string const& b)
150150
std::string Token::strValue() const
151151
{
152152
assert(_type == eString);
153-
return _str.substr(1, _str.length() - 2);
153+
std::string ret(_str.substr(1, _str.length() - 2));
154+
std::string::size_type pos = 0U;
155+
while ((pos = ret.find("\\",pos)) != std::string::npos) {
156+
ret.erase(pos,1U);
157+
if (ret[pos] >= 'a') {
158+
if (ret[pos] == 'n')
159+
ret[pos] = '\n';
160+
else if (ret[pos] == 'r')
161+
ret[pos] = '\r';
162+
else if (ret[pos] == 't')
163+
ret[pos] = '\t';
164+
}
165+
if (ret[pos] == '0')
166+
return ret.substr(0,pos);
167+
pos++;
168+
}
169+
return ret;
154170
}
155171

156172
void Token::deleteNext(unsigned long index)
@@ -665,21 +681,25 @@ bool Token::Match(const Token *tok, const char pattern[], unsigned int varid)
665681
std::size_t Token::getStrLength(const Token *tok)
666682
{
667683
assert(tok != nullptr);
684+
assert(tok->_type == eString);
668685

669686
std::size_t len = 0;
670-
const std::string strValue(tok->strValue());
671-
const char *str = strValue.c_str();
687+
std::string::const_iterator it = tok->str().begin() + 1U;
688+
const std::string::const_iterator end = tok->str().end() - 1U;
672689

673-
while (*str) {
674-
if (*str == '\\') {
675-
++str;
690+
while (it != end) {
691+
if (*it == '\\') {
692+
++it;
676693

677694
// string ends at '\0'
678-
if (*str == '0')
679-
break;
695+
if (*it == '0')
696+
return len;
680697
}
681698

682-
++str;
699+
if (*it == '\0')
700+
return len;
701+
702+
++it;
683703
++len;
684704
}
685705

lib/valueflow.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,6 +1548,7 @@ static bool valueFlowForLoop2(const Token *tok,
15481548
std::map<unsigned int, MathLib::bigint> *memory2,
15491549
std::map<unsigned int, MathLib::bigint> *memoryAfter)
15501550
{
1551+
// for ( firstExpression ; secondExpression ; thirdExpression )
15511552
const Token *firstExpression = tok->next()->astOperand2()->astOperand1();
15521553
const Token *secondExpression = tok->next()->astOperand2()->astOperand2()->astOperand1();
15531554
const Token *thirdExpression = tok->next()->astOperand2()->astOperand2()->astOperand2();

test/testsimplifytokens.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4263,7 +4263,7 @@ class TestSimplifyTokens : public TestFixture {
42634263
ASSERT_EQUALS("'\\0' ;", tok("\"hello\"[5] ;"));
42644264
ASSERT_EQUALS("'\\0' ;", tok("\"\"[0] ;"));
42654265
ASSERT_EQUALS("'\\0' ;", tok("\"\\0\"[0] ;"));
4266-
ASSERT_EQUALS("'\\n' ;", tok("\"hello\\nworld\"[5] ;"));
4266+
ASSERT_EQUALS("'\n' ;", tok("\"hello\\nworld\"[5] ;"));
42674267
ASSERT_EQUALS("'w' ;", tok("\"hello\nworld\"[6] ;"));
42684268
ASSERT_EQUALS("\"hello\" [ 7 ] ;", tok("\"hello\"[7] ;"));
42694269
ASSERT_EQUALS("\"hello\" [ -1 ] ;", tok("\"hello\"[-1] ;"));

test/testtoken.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,28 @@ class TestToken : public TestFixture {
282282

283283
void strValue() const {
284284
Token tok(0);
285+
285286
tok.str("\"\"");
286287
ASSERT_EQUALS("", tok.strValue());
287288

288289
tok.str("\"0\"");
289290
ASSERT_EQUALS("0", tok.strValue());
291+
292+
tok.str("\"a\\n\"");
293+
ASSERT_EQUALS("a\n", tok.strValue());
294+
295+
tok.str("\"a\\r\"");
296+
ASSERT_EQUALS("a\r", tok.strValue());
297+
298+
tok.str("\"a\\t\"");
299+
ASSERT_EQUALS("a\t", tok.strValue());
300+
301+
tok.str("\"\\\\\"");
302+
ASSERT_EQUALS("\\", tok.strValue());
303+
304+
tok.str("\"a\\0\"");
305+
ASSERT_EQUALS("a", tok.strValue());
306+
290307
}
291308

292309

0 commit comments

Comments
 (0)