improved Token::tokAt to handle negative argument too

This commit is contained in:
Daniel Marjamäki 2009-03-20 18:50:11 +01:00
parent 9f1c3cc535
commit ccfdbfca58
1 changed files with 14 additions and 6 deletions

View File

@ -114,10 +114,14 @@ void Token::replace(Token *replaceThis, Token *start, Token *end)
const Token *Token::tokAt(int index) const const Token *Token::tokAt(int index) const
{ {
const Token *tok = this; const Token *tok = this;
while (index > 0 && tok) int num = abs(index);
while (num > 0 && tok)
{ {
tok = tok->next(); if (index > 0)
--index; tok = tok->next();
else
tok = tok->previous();
--num;
} }
return tok; return tok;
} }
@ -125,10 +129,14 @@ const Token *Token::tokAt(int index) const
Token *Token::tokAt(int index) Token *Token::tokAt(int index)
{ {
Token *tok = this; Token *tok = this;
while (index > 0 && tok) int num = abs(index);
while (num > 0 && tok)
{ {
tok = tok->next(); if (index > 0)
--index; tok = tok->next();
else
tok = tok->previous();
--num;
} }
return tok; return tok;
} }