Token::getStrLength(): introduce new static method.

No functional change.
This commit is contained in:
Slava Semushin 2009-08-30 18:07:10 +07:00
parent 9b78c6dd32
commit 843ede94f3
3 changed files with 53 additions and 0 deletions

View File

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

View File

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

View File

@ -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<unsigned int>(-1), static_cast<unsigned int>(Token::multiCompare("abc|def", "abcd")));
ASSERT_EQUALS(static_cast<unsigned int>(-1), static_cast<unsigned int>(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)