@@ -1214,42 +1214,62 @@ static QVariant fcnLength( const QVariantList& values, const QgsExpressionContex
1214
1214
1215
1215
static QVariant fcnReplace ( const QVariantList& values, const QgsExpressionContext*, QgsExpression* parent )
1216
1216
{
1217
- QString str = getStringValue ( values.at ( 0 ), parent );
1218
- QVariantList before;
1219
- QVariantList after;
1220
- bool isSingleReplacement = false ;
1221
-
1222
- if ( values.at ( 1 ).type () != QVariant::List && values.at ( 2 ).type () != QVariant::StringList )
1217
+ if ( values.count () == 2 && values.at ( 1 ).type () == QVariant::Map )
1223
1218
{
1224
- before = QVariantList () << getStringValue ( values.at ( 1 ), parent );
1219
+ QString str = getStringValue ( values.at ( 0 ), parent );
1220
+ QVariantMap map = getMapValue ( values.at ( 1 ), parent );
1221
+
1222
+ for ( QVariantMap::const_iterator it = map.constBegin (); it != map.constEnd (); ++it )
1223
+ {
1224
+ str = str.replace ( it.key (), it.value ().toString () );
1225
+ }
1226
+
1227
+ return QVariant ( str );
1225
1228
}
1226
- else
1229
+ else if ( values. count () == 3 )
1227
1230
{
1228
- before = getListValue ( values.at ( 1 ), parent );
1229
- }
1231
+ QString str = getStringValue ( values.at ( 0 ), parent );
1232
+ QVariantList before;
1233
+ QVariantList after;
1234
+ bool isSingleReplacement = false ;
1230
1235
1231
- if ( values.at ( 2 ).type () != QVariant::List && values.at ( 2 ).type () != QVariant::StringList )
1232
- {
1233
- after = QVariantList () << getStringValue ( values.at ( 2 ), parent );
1234
- isSingleReplacement = true ;
1236
+ if ( values.at ( 1 ).type () != QVariant::List && values.at ( 2 ).type () != QVariant::StringList )
1237
+ {
1238
+ before = QVariantList () << getStringValue ( values.at ( 1 ), parent );
1239
+ }
1240
+ else
1241
+ {
1242
+ before = getListValue ( values.at ( 1 ), parent );
1243
+ }
1244
+
1245
+ if ( values.at ( 2 ).type () != QVariant::List && values.at ( 2 ).type () != QVariant::StringList )
1246
+ {
1247
+ after = QVariantList () << getStringValue ( values.at ( 2 ), parent );
1248
+ isSingleReplacement = true ;
1249
+ }
1250
+ else
1251
+ {
1252
+ after = getListValue ( values.at ( 2 ), parent );
1253
+ }
1254
+
1255
+ if ( !isSingleReplacement && before.length () != after.length () )
1256
+ {
1257
+ parent->setEvalErrorString ( QObject::tr ( " Invalid pair of array, length not identical" ) );
1258
+ return QVariant ();
1259
+ }
1260
+
1261
+ for ( int i = 0 ; i < before.length (); i++ )
1262
+ {
1263
+ str = str.replace ( before.at ( i ).toString (), after.at ( isSingleReplacement ? 0 : i ).toString () );
1264
+ }
1265
+
1266
+ return QVariant ( str );
1235
1267
}
1236
1268
else
1237
1269
{
1238
- after = getListValue ( values.at ( 2 ), parent );
1239
- }
1240
-
1241
- if ( !isSingleReplacement && before.length () != after.length () )
1242
- {
1243
- parent->setEvalErrorString ( QObject::tr ( " Invalid pair of array, length not identical" ) );
1270
+ parent->setEvalErrorString ( QObject::tr ( " Function replace requires 2 or 3 arguments" ) );
1244
1271
return QVariant ();
1245
1272
}
1246
-
1247
- for ( int i = 0 ; i < before.length (); i++ )
1248
- {
1249
- str = str.replace ( before.at ( i ).toString (), after.at ( isSingleReplacement ? 0 : i ).toString () );
1250
- }
1251
-
1252
- return QVariant ( str );
1253
1273
}
1254
1274
static QVariant fcnRegexpReplace ( const QVariantList& values, const QgsExpressionContext*, QgsExpression* parent )
1255
1275
{
@@ -3521,7 +3541,7 @@ const QList<QgsExpression::Function*>& QgsExpression::Functions()
3521
3541
<< new StaticFunction ( QStringLiteral ( " char" ), 1 , fcnChar, QStringLiteral ( " String" ) )
3522
3542
<< new StaticFunction ( QStringLiteral ( " wordwrap" ), ParameterList () << Parameter ( QStringLiteral ( " text" ) ) << Parameter ( QStringLiteral ( " length" ) ) << Parameter ( QStringLiteral ( " delimiter" ), true , " " ), fcnWordwrap, QStringLiteral ( " String" ) )
3523
3543
<< new StaticFunction ( QStringLiteral ( " length" ), ParameterList () << Parameter ( QStringLiteral ( " text" ), true , " " ), fcnLength, QStringList () << QStringLiteral ( " String" ) << QStringLiteral ( " GeometryGroup" ) )
3524
- << new StaticFunction ( QStringLiteral ( " replace" ), 3 , fcnReplace, QStringLiteral ( " String" ) )
3544
+ << new StaticFunction ( QStringLiteral ( " replace" ), - 1 , fcnReplace, QStringLiteral ( " String" ) )
3525
3545
<< new StaticFunction ( QStringLiteral ( " regexp_replace" ), 3 , fcnRegexpReplace, QStringLiteral ( " String" ) )
3526
3546
<< new StaticFunction ( QStringLiteral ( " regexp_substr" ), 2 , fcnRegexpSubstr, QStringLiteral ( " String" ) )
3527
3547
<< new StaticFunction ( QStringLiteral ( " substr" ), 3 , fcnSubstr, QStringLiteral ( " String" ) )
0 commit comments