From ccfdbfca588577a751986db6e27f5eea63d1e8c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 20 Mar 2009 18:50:11 +0100 Subject: [PATCH] improved Token::tokAt to handle negative argument too --- src/token.cpp | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/token.cpp b/src/token.cpp index 80167d98e..33883f9ed 100644 --- a/src/token.cpp +++ b/src/token.cpp @@ -114,10 +114,14 @@ void Token::replace(Token *replaceThis, Token *start, Token *end) const Token *Token::tokAt(int index) const { const Token *tok = this; - while (index > 0 && tok) + int num = abs(index); + while (num > 0 && tok) { - tok = tok->next(); - --index; + if (index > 0) + tok = tok->next(); + else + tok = tok->previous(); + --num; } return tok; } @@ -125,10 +129,14 @@ const Token *Token::tokAt(int index) const Token *Token::tokAt(int index) { Token *tok = this; - while (index > 0 && tok) + int num = abs(index); + while (num > 0 && tok) { - tok = tok->next(); - --index; + if (index > 0) + tok = tok->next(); + else + tok = tok->previous(); + --num; } return tok; }