Skip to content
Permalink
Browse files

[expression] more work on wordwrap function based on wonder-dk comments

  • Loading branch information
nirvn committed Feb 17, 2014
1 parent 6319add commit 1fcb4f06bb3617cdc12ee237a8a27f2092b5d65b
Showing with 26 additions and 15 deletions.
  1. +25 −15 src/core/qgsexpression.cpp
  2. +1 −0 tests/src/core/testqgsexpression.cpp
@@ -634,25 +634,35 @@ static QVariant fcnTrim( const QVariantList& values, const QgsFeature* , QgsExpr
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;
QString delimiterstr = getStringValue ( values.at(1), parent );
int delimiterlength = delimiterstr.length();

int wrapmin = getIntValue( values.at(2), parent );

QStringList lines = str.split( "\n" );
int strlength, strcurrent, strhit;

for ( int i = 0; i < lines.size(); i++ )
{
strlength = lines[i].length();
strcurrent = 0;
strhit = 0;

while (strcurrent < strlength)
{
strhit = lines[i].indexOf( delimiterstr, strcurrent + wrapmin );
if (strhit > -1) {
newstr.append( lines[i].midRef( strcurrent , strhit - strcurrent ) );
newstr.append( "\n" );
strcurrent = strhit + delimiterlength;
} else {
newstr.append( str.midRef( current ) );
current = length;
newstr.append( lines[i].midRef( strcurrent ) );
strcurrent = strlength;
}
}
if (i < lines.size() - 1) newstr.append( "\n" );
}
return QVariant( newstr );
}
@@ -315,6 +315,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( "wordwrap" ) << "wordwrap('university of qgis')" << false << QVariant( "university\nof qgis" );
QTest::newRow( "format" ) << "format('%1 %2 %3 %1', 'One', 'Two', 'Three')" << false << QVariant( "One Two Three One" );

// implicit conversions

0 comments on commit 1fcb4f0

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