Fixed broken function Token::getCharAt() (#7073)

This commit is contained in:
PKEuS 2015-10-26 11:51:05 +01:00
parent 297f2c78bd
commit 0370c66e41
2 changed files with 14 additions and 12 deletions

View File

@ -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);

View File

@ -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] ;"));