Skip to content

Commit

Permalink
Add trim function
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 12, 2013
1 parent ff92b02 commit bedd99e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
13 changes: 13 additions & 0 deletions resources/function_help/trim-en_US
@@ -0,0 +1,13 @@
<h3>trim() function</h3>
Removes all leading and trailing whitespace (spaces, tabs, etc) from a string.

<p><h4>Syntax</h4>
trim(<i>string</i>)</p>

<p><h4>Arguments</h4>
<!-- List args for functions here-->
<i> string</i> &rarr; is string. The string to trim.</p>

<p><h4>Example</h4>
<!-- Show example of function.-->
trim(' hello world ') &rarr; 'hello world'</p>
11 changes: 10 additions & 1 deletion src/core/qgsexpression.cpp
Expand Up @@ -536,6 +536,13 @@ static QVariant fcnTitle( const QVariantList& values, QgsFeature* , QgsExpressio
}
return QVariant( elems.join( " " ) );
}

static QVariant fcnTrim( const QVariantList& values, QgsFeature* , QgsExpression* parent )
{
QString str = getStringValue( values.at( 0 ), parent );
return QVariant( str.trimmed() );
}

static QVariant fcnLength( const QVariantList& values, QgsFeature* , QgsExpression* parent )
{
QString str = getStringValue( values.at( 0 ), parent );
Expand Down Expand Up @@ -1316,7 +1323,8 @@ const QStringList &QgsExpression::BuiltinFunctions()
<< "coalesce" << "regexp_match" << "$now" << "age" << "year"
<< "month" << "week" << "day" << "hour"
<< "minute" << "second" << "lower" << "upper"
<< "title" << "length" << "replace" << "regexp_replace" << "regexp_substr"
<< "title" << "length" << "replace" << "trim"
<< "regexp_replace" << "regexp_substr"
<< "substr" << "concat" << "strpos" << "left"
<< "right" << "rpad" << "lpad"
<< "format_number" << "format_date"
Expand Down Expand Up @@ -1379,6 +1387,7 @@ const QList<QgsExpression::Function*> &QgsExpression::Functions()
<< new StaticFunction( "lower", 1, fcnLower, QObject::tr( "String" ) )
<< new StaticFunction( "upper", 1, fcnUpper, QObject::tr( "String" ) )
<< new StaticFunction( "title", 1, fcnTitle, QObject::tr( "String" ) )
<< new StaticFunction( "trim", 1, fcnTrim, QObject::tr( "String" ) )
<< new StaticFunction( "length", 1, fcnLength, QObject::tr( "String" ) )
<< new StaticFunction( "replace", 3, fcnReplace, QObject::tr( "String" ) )
<< new StaticFunction( "regexp_replace", 3, fcnRegexpReplace, QObject::tr( "String" ) )
Expand Down
2 changes: 2 additions & 0 deletions tests/src/core/testqgsexpression.cpp
Expand Up @@ -296,6 +296,8 @@ class TestQgsExpression: public QObject
QTest::newRow( "lpad" ) << "lpad('Hello', 10, 'x')" << false << QVariant( "Helloxxxxx" );
QTest::newRow( "lpad truncate" ) << "rpad('Hello', 4, 'x')" << false << QVariant( "Hell" );
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( "format" ) << "format('%1 %2 %3 %1', 'One', 'Two', 'Three')" << false << QVariant( "One Two Three One" );

// implicit conversions
Expand Down

0 comments on commit bedd99e

Please sign in to comment.