Skip to content

Commit

Permalink
Add left,right,rpad,lpad string functions - Fix #6079
Browse files Browse the repository at this point in the history
    - With tests :)
  • Loading branch information
NathanW2 committed Jul 22, 2012
1 parent 304a625 commit 7a9074f
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 0 deletions.
15 changes: 15 additions & 0 deletions resources/function_help/left-en_US
@@ -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>

17 changes: 17 additions & 0 deletions resources/function_help/lpad-en_US
@@ -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>
15 changes: 15 additions & 0 deletions resources/function_help/right-en_US
@@ -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>

18 changes: 18 additions & 0 deletions resources/function_help/rpad-en_US
@@ -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>

35 changes: 35 additions & 0 deletions src/core/qgsexpression.cpp
Expand Up @@ -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() );
Expand Down Expand Up @@ -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 )
Expand Down
6 changes: 6 additions & 0 deletions tests/src/core/testqgsexpression.cpp
Expand Up @@ -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 );
Expand Down

0 comments on commit 7a9074f

Please sign in to comment.