Skip to content
Permalink
Browse files

Fixed parsing of numbers in locales using other decimal point symbols

  • Loading branch information
wonder-sk committed Jan 25, 2012
1 parent 785bc9f commit 5ee9ebada75a08f20b32eef60257125f19ec1752
Showing with 24 additions and 3 deletions.
  1. +5 −3 src/core/qgsexpressionlexer.ll
  2. +19 −0 tests/src/core/testqgsexpression.cpp
@@ -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
@@ -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");

%}

@@ -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; }
@@ -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" );

0 comments on commit 5ee9eba

Please sign in to comment.
You can’t perform that action at this time.