Skip to content
Permalink
Browse files

Add qgsPermissiveToLongLong

  • Loading branch information
elpaso committed Sep 1, 2018
1 parent 0b35147 commit bb01ef3456638cf8fc0f71f9e197eeddcd9e00f1
Showing with 59 additions and 0 deletions.
  1. +15 −0 python/core/auto_generated/qgis.sip.in
  2. +7 −0 src/core/qgis.cpp
  3. +11 −0 src/core/qgis.h
  4. +26 −0 tests/src/core/testqgis.cpp
@@ -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.
@@ -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 )
@@ -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
@@ -38,6 +38,7 @@ class TestQgis : public QObject

void permissiveToDouble();
void permissiveToInt();
void permissiveToLongLong();
void doubleToString();
void signalBlocker();
void qVariantCompare_data();
@@ -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" ) );

0 comments on commit bb01ef3

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