Skip to content
Permalink
Browse files

[FEATURE] add a char() function to the expression engine (#3137)

  • Loading branch information
nirvn authored and NathanW2 committed May 29, 2016
1 parent 78bcd4b commit 36ff0ba333ef13cdc8eeed37e16d5b4bcbff2812
Showing with 8 additions and 0 deletions.
  1. +7 −0 src/core/qgsexpression.cpp
  2. +1 −0 tests/src/core/testqgsexpression.cpp
@@ -1077,6 +1077,12 @@ static QVariant fcnSoundex( const QVariantList& values, const QgsExpressionConte
return QVariant( QgsStringUtils::soundex( string ) );
}

static QVariant fcnChar( const QVariantList& values, const QgsExpressionContext*, QgsExpression* parent )
{
QChar character = QChar( getIntValue( values.at( 0 ), parent ) );
return QVariant( QString( character ) );
}

static QVariant fcnWordwrap( const QVariantList& values, const QgsExpressionContext*, QgsExpression* parent )
{
if ( values.length() == 2 || values.length() == 3 )
@@ -3281,6 +3287,7 @@ const QList<QgsExpression::Function*>& QgsExpression::Functions()
<< new StaticFunction( "longest_common_substring", 2, fcnLCS, "Fuzzy Matching" )
<< new StaticFunction( "hamming_distance", 2, fcnHamming, "Fuzzy Matching" )
<< new StaticFunction( "soundex", 1, fcnSoundex, "Fuzzy Matching" )
<< new StaticFunction( "char", 1, fcnChar, "String" )
<< new StaticFunction( "wordwrap", ParameterList() << Parameter( "text" ) << Parameter( "length" ) << Parameter( "delimiter", true, " " ), fcnWordwrap, "String" )
<< new StaticFunction( "length", 1, fcnLength, "String" )
<< new StaticFunction( "replace", 3, fcnReplace, "String" )
@@ -754,6 +754,7 @@ class TestQgsExpression: public QObject
QTest::newRow( "title" ) << "title(' HeLlO WORLD ')" << false << QVariant( " Hello World " );
QTest::newRow( "trim" ) << "trim(' Test String ')" << false << QVariant( "Test String" );
QTest::newRow( "trim empty string" ) << "trim('')" << false << QVariant( "" );
QTest::newRow( "char" ) << "char(81)" << false << QVariant( "Q" );
QTest::newRow( "wordwrap" ) << "wordwrap('university of qgis',13)" << false << QVariant( "university of\nqgis" );
QTest::newRow( "wordwrap" ) << "wordwrap('university of qgis',13,' ')" << false << QVariant( "university of\nqgis" );
QTest::newRow( "wordwrap" ) << "wordwrap('university of qgis',-3)" << false << QVariant( "university\nof qgis" );

0 comments on commit 36ff0ba

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