mirror of
https://github.com/qgis/QGIS.git
synced 2025-02-23 00:02:38 -05:00
parent
4d67781fb3
commit
fbda356a74
@ -13,9 +13,10 @@
|
||||
{ "expression":"replace('QGIS ABC',array('A','B','C'),array('X','Y','Z'))", "returns":"'QGIS XYZ'"},
|
||||
{ "expression":"replace('QGIS',array('Q','S'),'')", "returns":"'GI'"} ] },
|
||||
{ "variant": "Map variant",
|
||||
"variant_description": "Returns a string with the supplied map keys replaced by paired values.",
|
||||
"variant_description": "Returns a string with the supplied map keys replaced by paired values. Longer map keys are evaluated first.",
|
||||
"arguments": [ {"arg":"string","description":"the input string"},
|
||||
{"arg":"map","description":"the map containing keys and values"} ],
|
||||
"examples": [ { "expression":"replace('APP SHOULD ROCK',map('APP','QGIS','SHOULD','DOES'))", "returns":"'QGIS DOES ROCK'"} ]
|
||||
"examples": [ { "expression":"replace('APP SHOULD ROCK',map('APP','QGIS','SHOULD','DOES'))", "returns":"'QGIS DOES ROCK'"},
|
||||
{ "expression":"replace('forty two',map('for','4','two','2','forty two','42'))", "returns":"'42'"} ]
|
||||
}]
|
||||
}
|
||||
|
@ -1367,10 +1367,25 @@ static QVariant fcnReplace( const QVariantList &values, const QgsExpressionConte
|
||||
{
|
||||
QString str = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
|
||||
QVariantMap map = QgsExpressionUtils::getMapValue( values.at( 1 ), parent );
|
||||
QVector< QPair< QString, QString > > mapItems;
|
||||
|
||||
for ( QVariantMap::const_iterator it = map.constBegin(); it != map.constEnd(); ++it )
|
||||
{
|
||||
str = str.replace( it.key(), it.value().toString() );
|
||||
mapItems.append( qMakePair( it.key(), it.value().toString() ) );
|
||||
}
|
||||
|
||||
// larger keys should be replaced first since they may contain whole smaller keys
|
||||
std::sort( mapItems.begin(),
|
||||
mapItems.end(),
|
||||
[]( const QPair< QString, QString > &pair1,
|
||||
const QPair< QString, QString > &pair2 )
|
||||
{
|
||||
return ( pair1.first.length() > pair2.first.length() );
|
||||
} );
|
||||
|
||||
for ( auto it = mapItems.constBegin(); it != mapItems.constEnd(); ++it )
|
||||
{
|
||||
str = str.replace( it->first, it->second );
|
||||
}
|
||||
|
||||
return QVariant( str );
|
||||
|
@ -1312,6 +1312,7 @@ class TestQgsExpression: public QObject
|
||||
QTest::newRow( "replace (unbalanced array, before > after)" ) << "replace('12345', array('1','2','3'), array('6','7'))" << true << QVariant();
|
||||
QTest::newRow( "replace (unbalanced array, before < after)" ) << "replace('12345', array('1','2'), array('6','7','8'))" << true << QVariant();
|
||||
QTest::newRow( "replace (map)" ) << "replace('APP SHOULD ROCK',map('APP','QGIS','SHOULD','DOES'))" << false << QVariant( "QGIS DOES ROCK" );
|
||||
QTest::newRow( "replace (map with overlapping keys)" ) << "replace('11111',map('1','small','11','large'))" << false << QVariant( "largelargesmall" );
|
||||
QTest::newRow( "regexp_replace" ) << "regexp_replace('HeLLo','[eL]+', '-')" << false << QVariant( "H-o" );
|
||||
QTest::newRow( "regexp_replace greedy" ) << "regexp_replace('HeLLo','(?<=H).*L', '-')" << false << QVariant( "H-o" );
|
||||
QTest::newRow( "regexp_replace non greedy" ) << "regexp_replace('HeLLo','(?<=H).*?L', '-')" << false << QVariant( "H-Lo" );
|
||||
|
Loading…
x
Reference in New Issue
Block a user