Skip to content
Permalink
Browse files

[expression] implement a wordwrap function (feature request #9412)

  • Loading branch information
nirvn committed Feb 16, 2014
1 parent 882dd0c commit 6319addab03041d0a089191c6474b7a777bcc443
Showing with 27 additions and 0 deletions.
  1. +27 −0 src/core/qgsexpression.cpp
@@ -631,6 +631,32 @@ static QVariant fcnTrim( const QVariantList& values, const QgsFeature* , QgsExpr
return QVariant( str.trimmed() );
}

static QVariant fcnWordwrap( const QVariantList& values, const QgsFeature* , QgsExpression* parent )
{
QString str = getStringValue( values.at( 0 ), parent );
QString delimiterstr = getStringValue ( values.at(1), parent );
QString wrapstr = "\n";
QString newstr;

int length = str.length();
int min = getIntValue( values.at(2), parent );
int current = 0;
int hit = 0;

while (current < length) {
hit = str.indexOf( delimiterstr, current + min );
if (hit > -1) {
newstr.append( str.midRef( current , hit - current ) );
newstr.append( wrapstr );
current = hit + 1;
} else {
newstr.append( str.midRef( current ) );
current = length;
}
}
return QVariant( newstr );
}

static QVariant fcnLength( const QVariantList& values, const QgsFeature* , QgsExpression* parent )
{
QString str = getStringValue( values.at( 0 ), parent );
@@ -1484,6 +1510,7 @@ const QList<QgsExpression::Function*> &QgsExpression::Functions()
<< new StaticFunction( "upper", 1, fcnUpper, "String" )
<< new StaticFunction( "title", 1, fcnTitle, "String" )
<< new StaticFunction( "trim", 1, fcnTrim, "String" )
<< new StaticFunction( "wordwrap", 3, fcnWordwrap, "String" )
<< new StaticFunction( "length", 1, fcnLength, "String" )
<< new StaticFunction( "replace", 3, fcnReplace, "String" )
<< new StaticFunction( "regexp_replace", 3, fcnRegexpReplace, "String" )

0 comments on commit 6319add

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