From 3c29cd25a041a4d826894a9e921e950769d6f0dd Mon Sep 17 00:00:00 2001 From: Nathan Woodrow Date: Thu, 24 Jan 2013 22:47:04 +1000 Subject: [PATCH] Add string format() function to expression --- src/core/qgsexpression.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/core/qgsexpression.cpp b/src/core/qgsexpression.cpp index cc6908764c4..f8987c93810 100644 --- a/src/core/qgsexpression.cpp +++ b/src/core/qgsexpression.cpp @@ -569,6 +569,23 @@ static QVariant fcnLPad( const QVariantList& values, QgsFeature* , QgsExpression return string.leftJustified( length, fill.at( 0 ), true ); } +static QVariant fcnFormatString( const QVariantList& values, QgsFeature* , QgsExpression *parent ) +{ + QString string = getStringValue( values.at( 0 ), parent ); + for ( int n = 1; n <= values.length() - 1; n++ ) + { + QString arg = getStringValue( values.at( n ), parent ); + QString place = '%' + QString::number( n ); + if ( string.indexOf( place ) == -1 ) + { + continue; + } + string.replace( place, arg ); + } + return string; +} + + static QVariant fcnNow( const QVariantList&, QgsFeature* , QgsExpression * ) { return QVariant( QDateTime::currentDateTime() ); @@ -1100,6 +1117,7 @@ const QList &QgsExpression::Functions() << new StaticFunction( "right", 2, fcnRight, QObject::tr( "String" ) ) << new StaticFunction( "rpad", 3, fcnRPad, QObject::tr( "String" ) ) << new StaticFunction( "lpad", 3, fcnLPad, QObject::tr( "String" ) ) + << new StaticFunction( "format", -1, fcnFormatString, QObject::tr( "String" ) ) << new StaticFunction( "format_number", 2, fcnFormatNumber, QObject::tr( "String" ) ) << new StaticFunction( "format_date", 2, fcnFormatDate, QObject::tr( "String" ) ) << new StaticFunction( "xat", 1, fcnXat, QObject::tr( "Geometry" ), "", true )