Bugfix: Update token properties on string changes

This commit is contained in:
Thomas Jarosch 2011-10-23 20:38:03 +02:00
parent 9e50b7cb68
commit 5b97cc1440
3 changed files with 69 additions and 3 deletions

View File

@ -54,10 +54,8 @@ Token::~Token()
} }
void Token::str(const std::string &s) void Token::update_property_info()
{ {
_str = s;
if (!_str.empty()) { if (!_str.empty()) {
_isName = bool(_str[0] == '_' || std::isalpha(_str[0])); _isName = bool(_str[0] == '_' || std::isalpha(_str[0]));
@ -72,15 +70,27 @@ void Token::str(const std::string &s)
_isBoolean = true; _isBoolean = true;
else else
_isBoolean = false; _isBoolean = false;
} else {
_isName = false;
_isNumber = false;
_isBoolean = false;
} }
}
void Token::str(const std::string &s)
{
_str = s;
_varId = 0; _varId = 0;
update_property_info();
} }
void Token::concatStr(std::string const& b) void Token::concatStr(std::string const& b)
{ {
_str.erase(_str.length() - 1); _str.erase(_str.length() - 1);
_str.append(b.begin() + 1, b.end()); _str.append(b.begin() + 1, b.end());
update_property_info();
} }
std::string Token::strValue() const std::string Token::strValue() const

View File

@ -440,6 +440,10 @@ private:
unsigned int _fileIndex; unsigned int _fileIndex;
unsigned int _linenr; unsigned int _linenr;
/** Updates internal property cache like _isName or _isBoolean.
Called after any _str() modification. */
void update_property_info();
/** /**
* A value from 0-100 that provides a rough idea about where in the token * A value from 0-100 that provides a rough idea about where in the token
* list this token is located. * list this token is located.

View File

@ -46,6 +46,13 @@ private:
TEST_CASE(matchNumeric); TEST_CASE(matchNumeric);
TEST_CASE(matchBoolean); TEST_CASE(matchBoolean);
TEST_CASE(matchOr); TEST_CASE(matchOr);
TEST_CASE(updateProperties)
TEST_CASE(isNameGuarantees1)
TEST_CASE(isNameGuarantees2)
TEST_CASE(isNameGuarantees3)
TEST_CASE(isNameGuarantees4)
TEST_CASE(isNameGuarantees5)
} }
void nextprevious() { void nextprevious() {
@ -253,6 +260,51 @@ private:
givenACodeSampleToTokenize op("+"); givenACodeSampleToTokenize op("+");
ASSERT_EQUALS(true, Token::Match(op.tokens(), "%op%")); ASSERT_EQUALS(true, Token::Match(op.tokens(), "%op%"));
} }
void updateProperties() {
Token tok(NULL);
tok.str("foobar");
ASSERT_EQUALS(true, tok.isName());
ASSERT_EQUALS(false, tok.isNumber());
tok.str("123456");
ASSERT_EQUALS(false, tok.isName());
ASSERT_EQUALS(true, tok.isNumber());
}
void isNameGuarantees1() {
Token tok(NULL);
tok.str("Name");
ASSERT_EQUALS(true, tok.isName());
}
void isNameGuarantees2() {
Token tok(NULL);
tok.str("_name");
ASSERT_EQUALS(true, tok.isName());
}
void isNameGuarantees3() {
Token tok(NULL);
tok.str("_123");
ASSERT_EQUALS(true, tok.isName());
}
void isNameGuarantees4() {
Token tok(NULL);
tok.str("123456");
ASSERT_EQUALS(false, tok.isName());
ASSERT_EQUALS(true, tok.isNumber());
}
void isNameGuarantees5() {
Token tok(NULL);
tok.str("a123456");
ASSERT_EQUALS(true, tok.isName());
ASSERT_EQUALS(false, tok.isNumber());
}
}; };
REGISTER_TEST(TestToken) REGISTER_TEST(TestToken)