Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[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 36ff0ba
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/core/qgsexpression.cpp
Expand Up @@ -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 )
Expand Down Expand Up @@ -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" )
Expand Down
1 change: 1 addition & 0 deletions tests/src/core/testqgsexpression.cpp
Expand Up @@ -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" );
Expand Down

0 comments on commit 36ff0ba

Please sign in to comment.