Skip to content

Commit

Permalink
Fixed parsing of numbers in locales using other decimal point symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Jan 25, 2012
1 parent 785bc9f commit 5ee9eba
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/core/qgsexpressionlexer.ll
Expand Up @@ -29,7 +29,7 @@
#include "qgsexpression.h"
#include "qgsexpressionparser.hpp"
#include <QRegExp>

#include <QLocale>

// if not defined, searches for isatty()
// which doesn't in MSVC compiler
Expand Down Expand Up @@ -85,6 +85,8 @@ static QString stripColumnRef(QString text)
return text;
}

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

%}

Expand Down Expand Up @@ -150,8 +152,8 @@ string "'"{str_char}*"'"
"," { return COMMA; }
{num_float} { exp_lval.numberFloat = atof(yytext); return NUMBER_FLOAT; }
{num_int} { exp_lval.numberInt = atoi(yytext); return NUMBER_INT; }
{num_float} { exp_lval.numberFloat = cLocale.toDouble( QString::fromAscii(yytext) ); return NUMBER_FLOAT; }
{num_int} { exp_lval.numberInt = cLocale.toInt( QString::fromAscii(yytext), 0, 10); return NUMBER_INT; }
{string} { TEXT_FILTER(stripText); return STRING; }
Expand Down
19 changes: 19 additions & 0 deletions tests/src/core/testqgsexpression.cpp
Expand Up @@ -92,6 +92,25 @@ class TestQgsExpression: public QObject
QCOMPARE( !exp.hasParserError(), valid );
}

void parsing_with_locale()
{
// check that parsing of numbers works correctly even when using some other locale

char* old_locale = setlocale(LC_NUMERIC, NULL);
qDebug("Old locale: %s", old_locale);
setlocale(LC_NUMERIC, "de_DE.UTF8");
char* new_locale = setlocale(LC_NUMERIC, NULL);
qDebug("New locale: %s", new_locale);

QgsExpression exp( "1.23 + 4.56" );
QVERIFY( !exp.hasParserError() );

setlocale(LC_NUMERIC, "");

QVariant v = exp.evaluate();
QCOMPARE( v.toDouble(), 5.79 );
}

void evaluation_data()
{
QTest::addColumn<QString>( "string" );
Expand Down

0 comments on commit 5ee9eba

Please sign in to comment.