Skip to content

Commit

Permalink
Add qgsPermissiveToLongLong
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Sep 1, 2018
1 parent 0b35147 commit bb01ef3
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
15 changes: 15 additions & 0 deletions python/core/auto_generated/qgis.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,21 @@ numbers of digits between thousand separators
.. versionadded:: 2.9
%End

qlonglong qgsPermissiveToLongLong( QString string, bool &ok );
%Docstring
Converts a string to an qlonglong in a permissive way, e.g., allowing for incorrect
numbers of digits between thousand separators

:param string: string to convert
:param ok: will be set to true if conversion was successful

:return: string converted to int if possible

.. seealso:: :py:func:`permissiveToInt`

.. versionadded:: 3.4
%End

bool qgsVariantLessThan( const QVariant &lhs, const QVariant &rhs );
%Docstring
Compares two QVariant values and returns whether the first is less than the second.
Expand Down
7 changes: 7 additions & 0 deletions src/core/qgis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ int qgsPermissiveToInt( QString string, bool &ok )
return QLocale().toInt( string, &ok );
}

qlonglong qgsPermissiveToLongLong( QString string, bool &ok )
{
//remove any thousands separators
string.remove( QLocale().groupSeparator() );
return QLocale().toLongLong( string, &ok );
}

void *qgsMalloc( size_t size )
{
if ( size == 0 || long( size ) < 0 )
Expand Down
11 changes: 11 additions & 0 deletions src/core/qgis.h
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,17 @@ CORE_EXPORT double qgsPermissiveToDouble( QString string, bool &ok );
*/
CORE_EXPORT int qgsPermissiveToInt( QString string, bool &ok );

/**
* Converts a string to an qlonglong in a permissive way, e.g., allowing for incorrect
* numbers of digits between thousand separators
* \param string string to convert
* \param ok will be set to true if conversion was successful
* \returns string converted to int if possible
* \see permissiveToInt
* \since QGIS 3.4
*/
CORE_EXPORT qlonglong qgsPermissiveToLongLong( QString string, bool &ok );

/**
* Compares two QVariant values and returns whether the first is less than the second.
* Useful for sorting lists of variants, correctly handling sorting of the various
Expand Down
26 changes: 26 additions & 0 deletions tests/src/core/testqgis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class TestQgis : public QObject

void permissiveToDouble();
void permissiveToInt();
void permissiveToLongLong();
void doubleToString();
void signalBlocker();
void qVariantCompare_data();
Expand Down Expand Up @@ -127,6 +128,31 @@ void TestQgis::permissiveToInt()
QCOMPARE( result, 1000 );
}

void TestQgis::permissiveToLongLong()
{
//good inputs
bool ok = false;
qlonglong result = qgsPermissiveToLongLong( QStringLiteral( "1000" ), ok );
QVERIFY( ok );
QCOMPARE( result, 1000 );
ok = false;
result = qgsPermissiveToLongLong( QStringLiteral( "1%01000" ).arg( QLocale().groupSeparator() ), ok );
QVERIFY( ok );
QCOMPARE( result, 1000 );

//bad input
ok = false;
( void ) qgsPermissiveToLongLong( QStringLiteral( "a" ), ok );
QVERIFY( !ok );

//messy input (invalid thousand separator position), should still be converted
ok = false;
result = qgsPermissiveToLongLong( QStringLiteral( "10%0100" ).arg( QLocale().groupSeparator() ), ok );
QVERIFY( ok );
QCOMPARE( result, 1000 );

}

void TestQgis::doubleToString()
{
QCOMPARE( qgsDoubleToString( 5.6783212, 5 ), QString( "5.67832" ) );
Expand Down

0 comments on commit bb01ef3

Please sign in to comment.