Skip to content

Commit bb01ef3

Browse files
committed
Add qgsPermissiveToLongLong
1 parent 0b35147 commit bb01ef3

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

python/core/auto_generated/qgis.sip.in

+15
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,21 @@ numbers of digits between thousand separators
162162
.. versionadded:: 2.9
163163
%End
164164

165+
qlonglong qgsPermissiveToLongLong( QString string, bool &ok );
166+
%Docstring
167+
Converts a string to an qlonglong in a permissive way, e.g., allowing for incorrect
168+
numbers of digits between thousand separators
169+
170+
:param string: string to convert
171+
:param ok: will be set to true if conversion was successful
172+
173+
:return: string converted to int if possible
174+
175+
.. seealso:: :py:func:`permissiveToInt`
176+
177+
.. versionadded:: 3.4
178+
%End
179+
165180
bool qgsVariantLessThan( const QVariant &lhs, const QVariant &rhs );
166181
%Docstring
167182
Compares two QVariant values and returns whether the first is less than the second.

src/core/qgis.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ int qgsPermissiveToInt( QString string, bool &ok )
108108
return QLocale().toInt( string, &ok );
109109
}
110110

111+
qlonglong qgsPermissiveToLongLong( QString string, bool &ok )
112+
{
113+
//remove any thousands separators
114+
string.remove( QLocale().groupSeparator() );
115+
return QLocale().toLongLong( string, &ok );
116+
}
117+
111118
void *qgsMalloc( size_t size )
112119
{
113120
if ( size == 0 || long( size ) < 0 )

src/core/qgis.h

+11
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,17 @@ CORE_EXPORT double qgsPermissiveToDouble( QString string, bool &ok );
409409
*/
410410
CORE_EXPORT int qgsPermissiveToInt( QString string, bool &ok );
411411

412+
/**
413+
* Converts a string to an qlonglong in a permissive way, e.g., allowing for incorrect
414+
* numbers of digits between thousand separators
415+
* \param string string to convert
416+
* \param ok will be set to true if conversion was successful
417+
* \returns string converted to int if possible
418+
* \see permissiveToInt
419+
* \since QGIS 3.4
420+
*/
421+
CORE_EXPORT qlonglong qgsPermissiveToLongLong( QString string, bool &ok );
422+
412423
/**
413424
* Compares two QVariant values and returns whether the first is less than the second.
414425
* Useful for sorting lists of variants, correctly handling sorting of the various

tests/src/core/testqgis.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class TestQgis : public QObject
3838

3939
void permissiveToDouble();
4040
void permissiveToInt();
41+
void permissiveToLongLong();
4142
void doubleToString();
4243
void signalBlocker();
4344
void qVariantCompare_data();
@@ -127,6 +128,31 @@ void TestQgis::permissiveToInt()
127128
QCOMPARE( result, 1000 );
128129
}
129130

131+
void TestQgis::permissiveToLongLong()
132+
{
133+
//good inputs
134+
bool ok = false;
135+
qlonglong result = qgsPermissiveToLongLong( QStringLiteral( "1000" ), ok );
136+
QVERIFY( ok );
137+
QCOMPARE( result, 1000 );
138+
ok = false;
139+
result = qgsPermissiveToLongLong( QStringLiteral( "1%01000" ).arg( QLocale().groupSeparator() ), ok );
140+
QVERIFY( ok );
141+
QCOMPARE( result, 1000 );
142+
143+
//bad input
144+
ok = false;
145+
( void ) qgsPermissiveToLongLong( QStringLiteral( "a" ), ok );
146+
QVERIFY( !ok );
147+
148+
//messy input (invalid thousand separator position), should still be converted
149+
ok = false;
150+
result = qgsPermissiveToLongLong( QStringLiteral( "10%0100" ).arg( QLocale().groupSeparator() ), ok );
151+
QVERIFY( ok );
152+
QCOMPARE( result, 1000 );
153+
154+
}
155+
130156
void TestQgis::doubleToString()
131157
{
132158
QCOMPARE( qgsDoubleToString( 5.6783212, 5 ), QString( "5.67832" ) );

0 commit comments

Comments
 (0)