Skip to content

Commit

Permalink
[expression] more work on wordwrap function based on wonder-dk comments
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Feb 21, 2014
1 parent 6319add commit 1fcb4f0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
40 changes: 25 additions & 15 deletions src/core/qgsexpression.cpp
Expand Up @@ -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 );
}
Expand Down
1 change: 1 addition & 0 deletions tests/src/core/testqgsexpression.cpp
Expand Up @@ -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
Expand Down

0 comments on commit 1fcb4f0

Please sign in to comment.