Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use std::string instead destructed char* #2

Merged
merged 3 commits into from
Jan 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions FormulaParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,19 +439,13 @@ namespace ExcelFormula
}

if ((pToken->getType() == Token::Operand) && (pToken->getSubtype() == Token::Nothing)) {
double d;
//bool isNumber = double.TryParse(pToken.Value, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.CurrentCulture, out d);
stringstream doubleConv;
doubleConv << pToken->getValue();
doubleConv >> d;

if (doubleConv.fail())
if ((pToken->getStrValue().compare("TRUE") == 0) || (pToken->getStrValue().compare("FALSE") == 0))
pToken->setSubtype(Token::Logical);
else
pToken->setSubtype(Token::Range);
if (StrUtils::is_number(pToken->getStrValue()))
pToken->setSubtype(Token::Number);
else
pToken->setSubtype(Token::Number);
if ((pToken->getStrValue().compare("TRUE") == 0) || (pToken->getStrValue().compare("FALSE") == 0))
pToken->setSubtype(Token::Logical);
else
pToken->setSubtype(Token::Range);

m_tmpAry.add(pToken);
continue;
Expand Down
7 changes: 7 additions & 0 deletions StrUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ string StrUtils::trim(string& value)
return value;
} //fn trim

bool StrUtils::is_number(const std::string& s)
{
std::string::const_iterator it = s.begin();
while (it != s.end() && std::isdigit(*it)) ++it;
return !s.empty() && it == s.end();
}

bool StrUtils::isEmpChar(const char c)
{
static const char empChar[] = {' ', '\t'};
Expand Down
2 changes: 2 additions & 0 deletions StrUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class StrUtils
static int indexOf(vector<string>&, string&);
static int indexOf(const int, const char*[], const char*);

static bool is_number(const std::string& s);

private:
static bool isEmpChar(const char);

Expand Down
4 changes: 2 additions & 2 deletions Token.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace ExcelFormula
}

//! return printable string
const char* Token::getPrintableString()
const string Token::getPrintableString()
{
string printableStr = m_value + " ";

Expand Down Expand Up @@ -104,7 +104,7 @@ namespace ExcelFormula
printableStr += "<Union>";
break;
}//token subtype switch
return printableStr.c_str();
return printableStr;
} //func getPrintableStr

Token* Token::clone()
Expand Down
2 changes: 1 addition & 1 deletion Token.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ namespace ExcelFormula
}

//! get printable token string
const char* getPrintableString();
const string getPrintableString();

//! duplicate a another token
Token* clone();
Expand Down
10 changes: 5 additions & 5 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ using std::string;
using ExcelFormula::Token;
using ExcelFormula::FormulaParser;

void printIntend(int i)
void printIndent(int i)
{
for(int j = 0; j < i; ++j)
{
Expand All @@ -23,20 +23,20 @@ void printParser(FormulaParser& parser)
cout << "Formula:" << parser.getFormula() << endl;
cout << "Tokens:" << endl;

int intend = 0;
int indent = 0;
for(vector<Token*>::iterator it = tokens.begin();
it != tokens.end();
++it)
{
if((*it)->getSubtype() == Token::Start)
{
++intend;
++indent;
}
printIntend(intend);
printIndent(indent);
cout << (*it)->getPrintableString() << endl;
if((*it)->getSubtype() == Token::Stop)
{
--intend;
--indent;
}
}

Expand Down