Visitar URL original
handle float suffix better · x29a/simplecpp@d086f5d · GitHub
Skip to content

Commit d086f5d

Browse files
committed
handle float suffix better
1 parent 7cb06fe commit d086f5d

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@
4343
#define SIMPLECPP_WINDOWS
4444
#endif
4545

46+
static bool isHex(const std::string &s) {
47+
return s.size()>2 && (s.compare(0,2,"0x")==0 || s.compare(0,2,"0X")==0);
48+
}
49+
4650
namespace {
4751
const simplecpp::TokenString DEFINE("define");
4852
const simplecpp::TokenString UNDEF("undef");
@@ -73,7 +77,7 @@ namespace {
7377
long long stringToLL(const std::string &s)
7478
{
7579
long long ret;
76-
const bool hex = (s.length()>2 && s.compare(0, 2, "0x") == 0);
80+
const bool hex = isHex(s);
7781
std::istringstream istr(hex ? s.substr(2) : s);
7882
if (hex)
7983
istr >> std::hex;
@@ -84,7 +88,7 @@ namespace {
8488
unsigned long long stringToULL(const std::string &s)
8589
{
8690
unsigned long long ret;
87-
const bool hex = (s.length()>2 && s.compare(0, 2, "0x") == 0);
91+
const bool hex = isHex(s);
8892
std::istringstream istr(hex ? s.substr(2) : s);
8993
if (hex)
9094
istr >> std::hex;
@@ -585,6 +589,14 @@ void simplecpp::TokenList::constFold()
585589
}
586590
}
587591

592+
static bool isFloatSuffix(const simplecpp::Token *tok) {
593+
if (!tok || tok->str.size() > 2)
594+
return false;
595+
std::string s = tok->str;
596+
std::transform(s.begin(), s.end(), s.begin(), static_cast<int (*)(int)>(std::tolower));
597+
return s == "lf" || s == "f";
598+
}
599+
588600
void simplecpp::TokenList::combineOperators()
589601
{
590602
for (Token *tok = front(); tok; tok = tok->next) {
@@ -597,7 +609,7 @@ void simplecpp::TokenList::combineOperators()
597609
if (tok->previous && tok->previous->number) {
598610
tok->setstr(tok->previous->str + '.');
599611
deleteToken(tok->previous);
600-
if (tok->next && tok->next->startsWithOneOf("Ee")) {
612+
if (isFloatSuffix(tok->next) || (tok->next && tok->next->startsWithOneOf("Ee"))) {
601613
tok->setstr(tok->str + tok->next->str);
602614
deleteToken(tok->next);
603615
}
@@ -609,7 +621,7 @@ void simplecpp::TokenList::combineOperators()
609621
}
610622
// match: [0-9.]+E [+-] [0-9]+
611623
const char lastChar = tok->str[tok->str.size() - 1];
612-
if (tok->number && tok->str.compare(0,2,"0x",0,2) != 0 && (lastChar == 'E' || lastChar == 'e') && tok->next && tok->next->isOneOf("+-") && tok->next->next && tok->next->next->number) {
624+
if (tok->number && !isHex(tok->str) && (lastChar == 'E' || lastChar == 'e') && tok->next && tok->next->isOneOf("+-") && tok->next->next && tok->next->next->number) {
613625
tok->setstr(tok->str + tok->next->op + tok->next->next->str);
614626
deleteToken(tok->next);
615627
deleteToken(tok->next);

‎test.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,9 @@ static std::string testConstFold(const char code[])
144144
void combineOperators_floatliteral()
145145
{
146146
ASSERT_EQUALS("1.", preprocess("1."));
147+
ASSERT_EQUALS("1.f", preprocess("1.f"));
147148
ASSERT_EQUALS(".1", preprocess(".1"));
149+
ASSERT_EQUALS(".1f", preprocess(".1f"));
148150
ASSERT_EQUALS("3.1", preprocess("3.1"));
149151
ASSERT_EQUALS("1E7", preprocess("1E7"));
150152
ASSERT_EQUALS("1E-7", preprocess("1E-7"));

0 commit comments

Comments
 (0)