From 5703808781a16dede960c15390384ba2050c3b05 Mon Sep 17 00:00:00 2001 From: Michael Uleysky Date: Thu, 5 Nov 2015 19:02:10 +1000 Subject: [PATCH] Using ToString instead of std::to_string with different format conversion for double --- include/common.h | 13 +++++++++++++ src/parser/lexical.l | 6 +++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/include/common.h b/include/common.h index c017d4c..6a61cff 100644 --- a/include/common.h +++ b/include/common.h @@ -1,6 +1,7 @@ #ifndef COMMON_H #define COMMON_H #include +#include #include #include #include @@ -413,4 +414,16 @@ inline WordList Split(const char* str, const std::string& delims) {return Split( inline WordList Split(const std::string& str, const char* delims) {return Split(str,std::string(delims),false);} inline WordList Split(const char* str, const char* delims) {return Split(std::string(str),std::string(delims),false);} +// We use different format for conversion of double to string +template +inline std::string ToString(T n) { return std::to_string(n); } + +template<> +inline std::string ToString(double n) +{ + char buffer[32]; + int i; + i=snprintf(buffer,32,"%.*G",DBL_DIG,n); + return std::string(buffer,i); +} #endif diff --git a/src/parser/lexical.l b/src/parser/lexical.l index 1025fa0..0913fa1 100644 --- a/src/parser/lexical.l +++ b/src/parser/lexical.l @@ -104,7 +104,7 @@ std::string str; [+\-*/^] COUT(MOREDEBUG)<<" OPERATION("<[0-9]+ { COUT(MOREDEBUG)<<" INTEGER("<i)) yyerrormessage(std::string("failed conversion to integer of ")+yytext+" at position "+std::to_string(yyextra->state.curpos),-1); + if(!str2int(yytext,&yylval_param->i)) yyerrormessage(std::string("failed conversion to integer of ")+yytext+" at position "+ToString(yyextra->state.curpos),-1); yylval_param->i=atoll(yytext); setyyllocp; yynextsym; @@ -112,7 +112,7 @@ std::string str; } [0-9]+(\.[0-9]*)?([eE][+-][0-9]+)? { COUT(MOREDEBUG)<<" REAL("<r)) yyerrormessage(std::string("failed conversion to double of ")+yytext+" at position "+std::to_string(yyextra->state.curpos),-1); + if(!str2double(yytext,&yylval_param->r)) yyerrormessage(std::string("failed conversion to double of ")+yytext+" at position "+ToString(yyextra->state.curpos),-1); setyyllocp; yynextsym; return REAL; @@ -130,7 +130,7 @@ std::string str; \n BEGIN(0); yynextline; \#.* yynextsym; \" yy_push_state(PSTRING,yyscanner); str.erase(); yyextra->states.push(yyextra->state); yynextsym; -. yyerrormessage(std::string("unknown symbol ")+yytext+" at position "+std::to_string(yyextra->state.curpos),-1); +. yyerrormessage(std::string("unknown symbol ")+yytext+" at position "+ToString(yyextra->state.curpos),-1); \\\\ str+='\\'; yynextsym; \\\" str+='\"'; yynextsym; \n str+=yytext[0]; yynextline;