From 1f141e4dc033977f34bcd6fa7ce7525e13bedc44 Mon Sep 17 00:00:00 2001 From: Nicolas Le Cam Date: Sun, 21 Dec 2008 17:35:33 +0000 Subject: [PATCH] Opimization: Remove combineWithNext Member, use an array instead --- token.cpp | 12 ---------- token.h | 7 ------ tokenize.cpp | 65 +++++++++++++++++++++++++++------------------------- 3 files changed, 34 insertions(+), 50 deletions(-) diff --git a/token.cpp b/token.cpp index 2829207de..d95671daa 100644 --- a/token.cpp +++ b/token.cpp @@ -64,18 +64,6 @@ void TOKEN::setstr( const char s[] ) _varId = 0; } -void TOKEN::combineWithNext(const char str1[], const char str2[]) -{ - if (!(_next)) - return; - if (_str!=str1 || _next->_str!=str2) - return; - - std::string newstr(std::string(str1) + std::string(str2)); - setstr( newstr.c_str() ); - deleteNext(); -} - void TOKEN::deleteNext() { TOKEN *n = _next; diff --git a/token.h b/token.h index a301d9340..6b51d6cd5 100644 --- a/token.h +++ b/token.h @@ -40,13 +40,6 @@ public: char aaaa1() const { return _cstr[1]; } - - /** - * Combine two tokens that belong to each other. - * Ex: "<" and "=" may become "<=" - */ - void combineWithNext(const char str1[], const char str2[]); - /** * Unlink and delete next token. */ diff --git a/tokenize.cpp b/tokenize.cpp index bcdb7c68e..205d5b587 100644 --- a/tokenize.cpp +++ b/tokenize.cpp @@ -499,38 +499,41 @@ void Tokenizer::tokenizeCode(std::istream &code, const unsigned int FileIndex) // Combine tokens.. for (TOKEN *tok = _tokens; tok && tok->next(); tok = tok->next()) { - tok->combineWithNext("<", "<"); - tok->combineWithNext(">", ">"); - - tok->combineWithNext("&", "&"); - tok->combineWithNext("|", "|"); - - tok->combineWithNext("+", "="); - tok->combineWithNext("-", "="); - tok->combineWithNext("*", "="); - tok->combineWithNext("/", "="); - tok->combineWithNext("&", "="); - tok->combineWithNext("|", "="); - - tok->combineWithNext("=", "="); - tok->combineWithNext("!", "="); - tok->combineWithNext("<", "="); - tok->combineWithNext(">", "="); - - tok->combineWithNext(":", ":"); - tok->combineWithNext("-", ">"); - - tok->combineWithNext("private", ":"); - tok->combineWithNext("protected", ":"); - tok->combineWithNext("public", ":"); - } - - // Replace "->" with "." - for ( TOKEN *tok = _tokens; tok; tok = tok->next() ) - { - if ( tok->str() == "->" ) + static const char* combineWithNext[][3] = { - tok->setstr("."); + { "<", "<", "<<" }, + { ">", ">", ">>" }, + + { "&", "&", "&&" }, + { "|", "|", "||" }, + + { "+", "=", "+=" }, + { "-", "=", "-=" }, + { "*", "=", "*=" }, + { "/", "=", "/=" }, + { "&", "=", "&=" }, + { "|", "=", "|=" }, + + { "=", "=", "==" }, + { "!", "=", "!=" }, + { "<", "=", "<=" }, + { ">", "=", ">=" }, + + { ":", ":", "::" }, + { "-", ">", "." }, // Replace "->" with "." + + { "private", ":", "private:" }, + { "protected", ":", "protected:" }, + { "public", ":", "public:" } + }; + + for (int i = 0; i < sizeof(combineWithNext) / sizeof(combineWithNext[0]); i++) + { + if ( tok->str() == combineWithNext[i][0] && tok->next()->str() == combineWithNext[i][1] ) + { + tok->setstr(combineWithNext[i][2]); + tok->deleteNext(); + } } }