3434#include < stack>
3535#include < utility>
3636
37- static const std::string literal_prefix[4 ] = {" u8" , " u" , " U" , " L" };
38-
39- static bool isStringCharLiteral (const std::string &str, char q)
40- {
41-
42- if (!endsWith (str, q))
43- return false ;
44- if (str[0 ] == q && str.length () > 1 )
45- return true ;
46-
47- for (const std::string & p: literal_prefix) {
48- if ((str.length () + 1 ) > p.length () && (str.compare (0 , p.size () + 1 , (p + q)) == 0 ))
49- return true ;
50- }
51- return false ;
52- }
5337const std::list<ValueFlow::Value> TokenImpl::mEmptyValueList ;
5438
5539Token::Token (TokensFrontBack *tokensFrontBack) :
@@ -89,9 +73,9 @@ void Token::update_property_info()
8973 if (!mStr .empty ()) {
9074 if (mStr == " true" || mStr == " false" )
9175 tokType (eBoolean);
92- else if (isStringCharLiteral (mStr , ' \" ' ))
76+ else if (isStringLiteral (mStr ))
9377 tokType (eString);
94- else if (isStringCharLiteral (mStr , ' \' ' ))
78+ else if (isCharLiteral (mStr ))
9579 tokType (eChar);
9680 else if (std::isalpha ((unsigned char )mStr [0 ]) || mStr [0 ] == ' _' || mStr [0 ] == ' $' ) { // Name
9781 if (mImpl ->mVarId )
@@ -168,17 +152,11 @@ void Token::update_property_isStandardType()
168152
169153void Token::update_property_char_string_literal ()
170154{
171- if (!( mTokType == Token::eString || mTokType == Token::eChar)) // Token has already been updated
155+ if (mTokType != Token::eString && mTokType != Token::eChar)
172156 return ;
173157
174- for (const std::string & p : literal_prefix) {
175- if (((mTokType == Token::eString) && mStr .compare (0 , p.size () + 1 , p + " \" " ) == 0 ) ||
176- ((mTokType == Token::eChar) && (mStr .compare (0 , p.size () + 1 , p + " \' " ) == 0 ))) {
177- mStr = mStr .substr (p.size ());
178- isLong (p != " u8" );
179- break ;
180- }
181- }
158+ isLong (((mTokType == Token::eString) && isPrefixStringCharLiteral (mStr , ' "' , " L" )) ||
159+ ((mTokType == Token::eChar) && isPrefixStringCharLiteral (mStr , ' \' ' , " L" )));
182160}
183161
184162bool Token::isUpperCaseName () const
@@ -195,15 +173,15 @@ bool Token::isUpperCaseName() const
195173void Token::concatStr (std::string const & b)
196174{
197175 mStr .erase (mStr .length () - 1 );
198- mStr .append (b. begin ( ) + 1 , b. end () );
176+ mStr .append (getStringLiteral (b ) + " \" " );
199177
200178 update_property_info ();
201179}
202180
203181std::string Token::strValue () const
204182{
205183 assert (mTokType == eString);
206- std::string ret (mStr . substr ( 1 , mStr . length () - 2 ));
184+ std::string ret (getStringLiteral ( mStr ));
207185 std::string::size_type pos = 0U ;
208186 while ((pos = ret.find (' \\ ' , pos)) != std::string::npos) {
209187 ret.erase (pos,1U );
@@ -721,8 +699,9 @@ nonneg int Token::getStrLength(const Token *tok)
721699 assert (tok->mTokType == eString);
722700
723701 int len = 0 ;
724- std::string::const_iterator it = tok->str ().begin () + 1U ;
725- const std::string::const_iterator end = tok->str ().end () - 1U ;
702+ const std::string str (getStringLiteral (tok->str ()));
703+ std::string::const_iterator it = str.begin ();
704+ const std::string::const_iterator end = str.end ();
726705
727706 while (it != end) {
728707 if (*it == ' \\ ' ) {
@@ -747,9 +726,9 @@ nonneg int Token::getStrSize(const Token *tok)
747726{
748727 assert (tok != nullptr );
749728 assert (tok->tokType () == eString);
750- const std::string & str = tok->str ();
729+ const std::string str ( getStringLiteral ( tok->str ()) );
751730 int sizeofstring = 1 ;
752- for (int i = 1 ; i < (int )str.size () - 1 ; i++) {
731+ for (int i = 0 ; i < (int )str.size (); i++) {
753732 if (str[i] == ' \\ ' )
754733 ++i;
755734 ++sizeofstring;
@@ -760,9 +739,9 @@ nonneg int Token::getStrSize(const Token *tok)
760739std::string Token::getCharAt (const Token *tok, MathLib::bigint index)
761740{
762741 assert (tok != nullptr );
763-
764- std::string::const_iterator it = tok-> str () .begin () + 1U ;
765- const std::string::const_iterator end = tok-> str () .end () - 1U ;
742+ std::string str ( getStringLiteral (tok-> str ()));
743+ std::string::const_iterator it = str.begin ();
744+ const std::string::const_iterator end = str.end ();
766745
767746 while (it != end) {
768747 if (index == 0 ) {
@@ -1161,9 +1140,7 @@ void Token::stringify(std::ostream& os, bool varid, bool attributes, bool macro)
11611140 if (isComplex ())
11621141 os << " _Complex " ;
11631142 if (isLong ()) {
1164- if (mTokType == eString || mTokType == eChar)
1165- os << " L" ;
1166- else
1143+ if (!(mTokType == eString || mTokType == eChar))
11671144 os << " long " ;
11681145 }
11691146 }
@@ -1428,8 +1405,8 @@ static std::string stringFromTokenRange(const Token* start, const Token* end)
14281405 for (const Token *tok = start; tok && tok != end; tok = tok->next ()) {
14291406 if (tok->isUnsigned ())
14301407 ret << " unsigned " ;
1431- if (tok->isLong ())
1432- ret << (tok-> isLiteral () ? " L " : " long " ) ;
1408+ if (tok->isLong () && !tok-> isLiteral () )
1409+ ret << " long " ;
14331410 if (tok->originalName ().empty () || tok->isUnsigned () || tok->isLong ()) {
14341411 ret << tok->str ();
14351412 } else
0 commit comments