[expression] check for maximum nb of arguments in wordwrap function

This commit is contained in:
Mathieu Pellerin (nirvn) 2014-02-20 10:33:53 +07:00
parent 5105f5f93e
commit a7cb2b27b5

View File

@ -633,17 +633,17 @@ static QVariant fcnTrim( const QVariantList& values, const QgsFeature* , QgsExpr
static QVariant fcnWordwrap( const QVariantList& values, const QgsFeature* , QgsExpression* parent ) static QVariant fcnWordwrap( const QVariantList& values, const QgsFeature* , QgsExpression* parent )
{ {
if (values.length() > 1) if ( values.length() == 2 || values.length() == 3 )
{ {
QString str = getStringValue( values.at( 0 ), parent ); QString str = getStringValue( values.at( 0 ), parent );
int wrap = getIntValue( values.at(1), parent ); int wrap = getIntValue( values.at( 1 ), parent );
if (!str.isEmpty() && wrap != 0) if ( !str.isEmpty() && wrap != 0 )
{ {
QString newstr; QString newstr;
QString delimiterstr; QString delimiterstr;
if (values.length() == 3) delimiterstr = getStringValue ( values.at(2), parent ); if ( values.length() == 3 ) delimiterstr = getStringValue( values.at( 2 ), parent );
if (delimiterstr.isEmpty()) delimiterstr = " "; if ( delimiterstr.isEmpty() ) delimiterstr = " ";
int delimiterlength = delimiterstr.length(); int delimiterlength = delimiterstr.length();
QStringList lines = str.split( "\n" ); QStringList lines = str.split( "\n" );
@ -656,32 +656,38 @@ static QVariant fcnWordwrap( const QVariantList& values, const QgsFeature* , Qgs
strhit = 0; strhit = 0;
lasthit = 0; lasthit = 0;
while (strcurrent < strlength) while ( strcurrent < strlength )
{ {
// positive wrap value = desired maximum line width to wrap // positive wrap value = desired maximum line width to wrap
// negative wrap value = desired minimum line width before wrap // negative wrap value = desired minimum line width before wrap
if (wrap > 0) if ( wrap > 0 )
{ {
//first try to locate delimiter backwards //first try to locate delimiter backwards
strhit = lines[i].lastIndexOf( delimiterstr, strcurrent + wrap); strhit = lines[i].lastIndexOf( delimiterstr, strcurrent + wrap );
if (strhit == lasthit || strhit == -1) { if ( strhit == lasthit || strhit == -1 )
{
//if no new backward delimiter found, try to locate forward //if no new backward delimiter found, try to locate forward
strhit = lines[i].indexOf( delimiterstr, strcurrent + qAbs(wrap) ); strhit = lines[i].indexOf( delimiterstr, strcurrent + qAbs( wrap ) );
} }
lasthit = strhit; lasthit = strhit;
} else {
strhit = lines[i].indexOf( delimiterstr, strcurrent + qAbs(wrap) );
} }
if (strhit > -1) { else
{
strhit = lines[i].indexOf( delimiterstr, strcurrent + qAbs( wrap ) );
}
if ( strhit > -1 )
{
newstr.append( lines[i].midRef( strcurrent , strhit - strcurrent ) ); newstr.append( lines[i].midRef( strcurrent , strhit - strcurrent ) );
newstr.append( "\n" ); newstr.append( "\n" );
strcurrent = strhit + delimiterlength; strcurrent = strhit + delimiterlength;
} else { }
else
{
newstr.append( lines[i].midRef( strcurrent ) ); newstr.append( lines[i].midRef( strcurrent ) );
strcurrent = strlength; strcurrent = strlength;
} }
} }
if (i < lines.size() - 1) newstr.append( "\n" ); if ( i < lines.size() - 1 ) newstr.append( "\n" );
} }
return QVariant( newstr ); return QVariant( newstr );