diff --git a/lib/token.cpp b/lib/token.cpp index 7d2c8a8c8..d63f04b3d 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -703,23 +703,25 @@ std::string Token::getCharAt(const Token *tok, std::size_t index) { assert(tok != nullptr); - 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) { + while (it != end) { if (index == 0) { - std::string ret; - if (*str == '\\') { - ret = *str; - ++str; + if (*it == '\0') + return "\\0"; + + std::string ret(1, *it); + if (*it == '\\') { + ++it; + ret += *it; } - ret += *str; return ret; } - if (*str == '\\') - ++str; - ++str; + if (*it == '\\') + ++it; + ++it; --index; } assert(index == 0); diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index c970e61d6..a5fcc2885 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -4033,7 +4033,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] ;"));