Skip to content
Permalink
Browse files

Add left,right,rpad,lpad string functions - Fix #6079

    - With tests :)
  • Loading branch information
NathanW2 committed Jul 22, 2012
1 parent 304a625 commit 7a9074f8fc7c708019de90ca546aa68ca9508177
@@ -0,0 +1,15 @@
<h3>left() function</h3>
Returns a substring that contains the <i>n</i> leftmost characters of the string.

<h4>Syntax</h4>
<code>left(string, pos)</code><br>

<h4>Arguments</h4>
<code>string</code> - is string. The string.
<br>
<code>length</code> - is int. The numbder of characters from the left to return.

<h4>Example</h4>
<!-- Show example of function.-->
<code>left('Hello World',5) -> 'Hello'</code><br>

@@ -0,0 +1,17 @@
<h3>lpad() function</h3>
Returns a string with supplied width padded
using the fill character.

<h4>Syntax</h4>
<code>lpad(string, width, fill)</code><br>

<h4>Arguments</h4>
<code>string</code> - is string. The string.
<br>
<code>width</code> - is int. The length of the new string.
<br>
<code>fill</code> - is char. The character to padd the remaining space with.

<h4>Example</h4>
<!-- Show example of function.-->
<code>lpad('Hello', 10, 'x') -> 'Helloxxxxx'</code><br>
@@ -0,0 +1,15 @@
<h3>right() function</h3>
Returns a substring that contains the <i>n</i> rightmost characters of the string.

<h4>Syntax</h4>
<code>right(string, pos)</code><br>

<h4>Arguments</h4>
<code>string</code> - is string. The string.
<br>
<code>length</code> - is int. The numbder of characters from the right to return.

<h4>Example</h4>
<!-- Show example of function.-->
<code>right('Hello World',5) -> 'World'</code><br>

@@ -0,0 +1,18 @@
<h3>rpad() function</h3>
Returns a string with supplied width padded
using the fill character.

<h4>Syntax</h4>
<code>rpad(string, width, fill)</code><br>

<h4>Arguments</h4>
<code>string</code> - is string. The string.
<br>
<code>width</code> - is int. The length of the new string.
<br>
<code>fill</code> - is char. The character to padd the remaining space with.

<h4>Example</h4>
<!-- Show example of function.-->
<code>rpad('Hello', 10, 'x') -> 'xxxxxHello'</code><br>

@@ -511,6 +511,36 @@ static QVariant fcnStrpos( const QVariantList& values, QgsFeature* , QgsExpressi
return string.indexOf( QRegExp( getStringValue( values.at( 1 ), parent ) ) );
}

static QVariant fcnRight( const QVariantList& values, QgsFeature* , QgsExpression *parent )
{
QString string = getStringValue( values.at( 0 ), parent );
int pos = getIntValue( values.at( 1 ), parent );
return string.right( pos );
}

static QVariant fcnLeft( const QVariantList& values, QgsFeature* , QgsExpression *parent )
{
QString string = getStringValue( values.at( 0 ), parent );
int pos = getIntValue( values.at( 1 ), parent );
return string.left( pos );
}

static QVariant fcnRPad( const QVariantList& values, QgsFeature* , QgsExpression *parent )
{
QString string = getStringValue( values.at( 0 ), parent );
int length = getIntValue( values.at( 1 ), parent );
QString fill = getStringValue( values.at( 2 ), parent );
return string.rightJustified( length, fill.at( 0 ), true );
}

static QVariant fcnLPad( const QVariantList& values, QgsFeature* , QgsExpression *parent )
{
QString string = getStringValue( values.at( 0 ), parent );
int length = getIntValue( values.at( 1 ), parent );
QString fill = getStringValue( values.at( 2 ), parent );
return string.leftJustified( length, fill.at( 0 ), true );
}

static QVariant fcnNow( const QVariantList&, QgsFeature* , QgsExpression * )
{
return QVariant( QDateTime::currentDateTime() );
@@ -775,6 +805,11 @@ const QList<QgsExpression::FunctionDef> &QgsExpression::BuiltinFunctions()
<< FunctionDef( "substr", 3, fcnSubstr, QObject::tr( "String" ) )
<< FunctionDef( "concat", -1, fcnConcat, QObject::tr( "String" ) )
<< FunctionDef( "strpos", 2, fcnStrpos, QObject::tr( "String" ) )
<< FunctionDef( "left", 2, fcnLeft, QObject::tr( "String" ) )
<< FunctionDef( "right", 2, fcnRight, QObject::tr( "String" ) )
<< FunctionDef( "rpad", 3, fcnRPad, QObject::tr( "String" ) )
<< FunctionDef( "lpad", 3, fcnLPad, QObject::tr( "String" ) )

// geometry accessors
<< FunctionDef( "xat", 1, fcnXat, QObject::tr( "Geometry" ), "", true )
<< FunctionDef( "yat", 1, fcnYat, QObject::tr( "Geometry" ), "", true )
@@ -257,6 +257,12 @@ class TestQgsExpression: public QObject
QTest::newRow( "substr outside" ) << "substr('HeLLo', -5,2)" << false << QVariant( "" );
QTest::newRow( "strpos" ) << "strpos('Hello World','World')" << false << QVariant( 6 );
QTest::newRow( "strpos outside" ) << "strpos('Hello World','blah')" << false << QVariant( -1 );
QTest::newRow( "left" ) << "left('Hello World',5)" << false << QVariant( "Hello" );
QTest::newRow( "right" ) << "right('Hello World', 5)" << false << QVariant( "World" );
QTest::newRow( "rpad" ) << "rpad('Hello', 10, 'x')" << false << QVariant( "xxxxxHello" );
QTest::newRow( "rpad truncate" ) << "rpad('Hello', 4, 'x')" << false << QVariant( "Hell" );
QTest::newRow( "lpad" ) << "lpad('Hello', 10, 'x')" << false << QVariant( "Helloxxxxx" );
QTest::newRow( "lpad truncate" ) << "rpad('Hello', 4, 'x')" << false << QVariant( "Hell" );

// implicit conversions
QTest::newRow( "implicit int->text" ) << "length(123)" << false << QVariant( 3 );

0 comments on commit 7a9074f

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