Skip to content

Commit 5ee9eba

Browse files
committedJan 25, 2012
Fixed parsing of numbers in locales using other decimal point symbols
1 parent 785bc9f commit 5ee9eba

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed
 

‎src/core/qgsexpressionlexer.ll

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#include "qgsexpression.h"
3030
#include "qgsexpressionparser.hpp"
3131
#include <QRegExp>
32-
32+
#include <QLocale>
3333

3434
// if not defined, searches for isatty()
3535
// which doesn't in MSVC compiler
@@ -85,6 +85,8 @@ static QString stripColumnRef(QString text)
8585
return text;
8686
}
8787

88+
// C locale for correct parsing of numbers even if the system locale is different
89+
static QLocale cLocale("C");
8890

8991
%}
9092

@@ -150,8 +152,8 @@ string "'"{str_char}*"'"
150152
151153
"," { return COMMA; }
152154
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; }
155157
156158
{string} { TEXT_FILTER(stripText); return STRING; }
157159

‎tests/src/core/testqgsexpression.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,25 @@ class TestQgsExpression: public QObject
9292
QCOMPARE( !exp.hasParserError(), valid );
9393
}
9494

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+
95114
void evaluation_data()
96115
{
97116
QTest::addColumn<QString>( "string" );

0 commit comments

Comments
 (0)
Please sign in to comment.