Skip to content

Commit 6319add

Browse files
committed
[expression] implement a wordwrap function (feature request #9412)
1 parent 882dd0c commit 6319add

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/core/qgsexpression.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,32 @@ static QVariant fcnTrim( const QVariantList& values, const QgsFeature* , QgsExpr
631631
return QVariant( str.trimmed() );
632632
}
633633

634+
static QVariant fcnWordwrap( const QVariantList& values, const QgsFeature* , QgsExpression* parent )
635+
{
636+
QString str = getStringValue( values.at( 0 ), parent );
637+
QString delimiterstr = getStringValue ( values.at(1), parent );
638+
QString wrapstr = "\n";
639+
QString newstr;
640+
641+
int length = str.length();
642+
int min = getIntValue( values.at(2), parent );
643+
int current = 0;
644+
int hit = 0;
645+
646+
while (current < length) {
647+
hit = str.indexOf( delimiterstr, current + min );
648+
if (hit > -1) {
649+
newstr.append( str.midRef( current , hit - current ) );
650+
newstr.append( wrapstr );
651+
current = hit + 1;
652+
} else {
653+
newstr.append( str.midRef( current ) );
654+
current = length;
655+
}
656+
}
657+
return QVariant( newstr );
658+
}
659+
634660
static QVariant fcnLength( const QVariantList& values, const QgsFeature* , QgsExpression* parent )
635661
{
636662
QString str = getStringValue( values.at( 0 ), parent );
@@ -1484,6 +1510,7 @@ const QList<QgsExpression::Function*> &QgsExpression::Functions()
14841510
<< new StaticFunction( "upper", 1, fcnUpper, "String" )
14851511
<< new StaticFunction( "title", 1, fcnTitle, "String" )
14861512
<< new StaticFunction( "trim", 1, fcnTrim, "String" )
1513+
<< new StaticFunction( "wordwrap", 3, fcnWordwrap, "String" )
14871514
<< new StaticFunction( "length", 1, fcnLength, "String" )
14881515
<< new StaticFunction( "replace", 3, fcnReplace, "String" )
14891516
<< new StaticFunction( "regexp_replace", 3, fcnRegexpReplace, "String" )

0 commit comments

Comments
 (0)