File tree Expand file tree Collapse file tree 2 files changed +24
-3
lines changed Expand file tree Collapse file tree 2 files changed +24
-3
lines changed Original file line number Diff line number Diff line change 29
29
#include "qgsexpression.h"
30
30
#include "qgsexpressionparser.hpp"
31
31
#include <QRegExp>
32
-
32
+ #include <QLocale>
33
33
34
34
// if not defined, searches for isatty()
35
35
// which doesn't in MSVC compiler
@@ -85,6 +85,8 @@ static QString stripColumnRef(QString text)
85
85
return text;
86
86
}
87
87
88
+ // C locale for correct parsing of numbers even if the system locale is different
89
+ static QLocale cLocale("C" );
88
90
89
91
%}
90
92
@@ -150,8 +152,8 @@ string "'"{str_char}*"'"
150
152
151
153
" ," { return COMMA; }
152
154
153
- {num_float} { exp_lval.numberFloat = atof( yytext); return NUMBER_FLOAT; }
154
- {num_int} { exp_lval.numberInt = atoi( yytext); return NUMBER_INT; }
155
+ {num_float} { exp_lval.numberFloat = cLocale.toDouble( QString::fromAscii( yytext) ); return NUMBER_FLOAT; }
156
+ {num_int} { exp_lval.numberInt = cLocale.toInt( QString::fromAscii( yytext), 0, 10 ); return NUMBER_INT; }
155
157
156
158
{string} { TEXT_FILTER(stripText); return STRING; }
157
159
Original file line number Diff line number Diff line change @@ -92,6 +92,25 @@ class TestQgsExpression: public QObject
92
92
QCOMPARE ( !exp.hasParserError (), valid );
93
93
}
94
94
95
+ void parsing_with_locale ()
96
+ {
97
+ // check that parsing of numbers works correctly even when using some other locale
98
+
99
+ char * old_locale = setlocale (LC_NUMERIC, NULL );
100
+ qDebug (" Old locale: %s" , old_locale);
101
+ setlocale (LC_NUMERIC, " de_DE.UTF8" );
102
+ char * new_locale = setlocale (LC_NUMERIC, NULL );
103
+ qDebug (" New locale: %s" , new_locale);
104
+
105
+ QgsExpression exp ( " 1.23 + 4.56" );
106
+ QVERIFY ( !exp.hasParserError () );
107
+
108
+ setlocale (LC_NUMERIC, " " );
109
+
110
+ QVariant v = exp.evaluate ();
111
+ QCOMPARE ( v.toDouble (), 5.79 );
112
+ }
113
+
95
114
void evaluation_data ()
96
115
{
97
116
QTest::addColumn<QString>( " string" );
You can’t perform that action at this time.
0 commit comments