From 843ede94f3d770222011aad4d683fed8edf40bbf Mon Sep 17 00:00:00 2001 From: Slava Semushin Date: Sun, 30 Aug 2009 18:07:10 +0700 Subject: [PATCH] Token::getStrLength(): introduce new static method. No functional change. --- src/token.cpp | 26 ++++++++++++++++++++++++++ src/token.h | 9 +++++++++ test/testtoken.cpp | 18 ++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/src/token.cpp b/src/token.cpp index 25dfaae72..f0d1295df 100644 --- a/src/token.cpp +++ b/src/token.cpp @@ -491,6 +491,32 @@ bool Token::Match(const Token *tok, const char pattern[], unsigned int varid) return true; } +size_t Token::getStrLength(const Token *tok) +{ + assert(tok != NULL); + + size_t len = 0; + const char *str = tok->strAt(0); + + assert(str[0] == '"'); + assert(str[strlen(str)-1] == '"'); + + while (*str) + { + if (*str == '\\') + ++str; + ++str; + ++len; + } + + assert(len >= 2); + + // don't count quotes + len -= 2; + + return len; +} + bool Token::isStandardType() const { bool ret = false; diff --git a/src/token.h b/src/token.h index a40831d69..4d48d7d80 100644 --- a/src/token.h +++ b/src/token.h @@ -117,6 +117,15 @@ public: */ static bool Match(const Token *tok, const char pattern[], unsigned int varid = 0); + /** + * Return length of C-string. + * + * Should be called for %str% tokens only. + * + * @param tok token with C-string + **/ + static size_t getStrLength(const Token *tok); + bool isName() const { return _isName; diff --git a/test/testtoken.cpp b/test/testtoken.cpp index 8b2292a37..0ba458634 100644 --- a/test/testtoken.cpp +++ b/test/testtoken.cpp @@ -34,6 +34,7 @@ private: { TEST_CASE(nextprevious); TEST_CASE(multiCompare); + TEST_CASE(getStrLength); } void nextprevious() @@ -78,6 +79,23 @@ private: ASSERT_EQUALS(static_cast(-1), static_cast(Token::multiCompare("abc|def", "abcd"))); ASSERT_EQUALS(static_cast(-1), static_cast(Token::multiCompare("abc|def", "default"))); } + + void getStrLength() + { + Token *tok = new Token(); + + tok->str("\"\""); + ASSERT_EQUALS(0, Token::getStrLength(tok)); + + tok->str("\"test\""); + ASSERT_EQUALS(4, Token::getStrLength(tok)); + + tok->str("\"test \\\\test\""); + ASSERT_EQUALS(10, Token::getStrLength(tok)); + + Tokenizer::deleteTokens(tok); + } + }; REGISTER_TEST(TestTOKEN)