Skip to content

Commit 7a9074f

Browse files
committed
Add left,right,rpad,lpad string functions - Fix #6079
- With tests :)
1 parent 304a625 commit 7a9074f

6 files changed

Lines changed: 106 additions & 0 deletions

File tree

‎resources/function_help/left-en_US‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<h3>left() function</h3>
2+
Returns a substring that contains the <i>n</i> leftmost characters of the string.
3+
4+
<h4>Syntax</h4>
5+
<code>left(string, pos)</code><br>
6+
7+
<h4>Arguments</h4>
8+
<code>string</code> - is string. The string.
9+
<br>
10+
<code>length</code> - is int. The numbder of characters from the left to return.
11+
12+
<h4>Example</h4>
13+
<!-- Show example of function.-->
14+
<code>left('Hello World',5) -> 'Hello'</code><br>
15+

‎resources/function_help/lpad-en_US‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<h3>lpad() function</h3>
2+
Returns a string with supplied width padded
3+
using the fill character.
4+
5+
<h4>Syntax</h4>
6+
<code>lpad(string, width, fill)</code><br>
7+
8+
<h4>Arguments</h4>
9+
<code>string</code> - is string. The string.
10+
<br>
11+
<code>width</code> - is int. The length of the new string.
12+
<br>
13+
<code>fill</code> - is char. The character to padd the remaining space with.
14+
15+
<h4>Example</h4>
16+
<!-- Show example of function.-->
17+
<code>lpad('Hello', 10, 'x') -> 'Helloxxxxx'</code><br>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<h3>right() function</h3>
2+
Returns a substring that contains the <i>n</i> rightmost characters of the string.
3+
4+
<h4>Syntax</h4>
5+
<code>right(string, pos)</code><br>
6+
7+
<h4>Arguments</h4>
8+
<code>string</code> - is string. The string.
9+
<br>
10+
<code>length</code> - is int. The numbder of characters from the right to return.
11+
12+
<h4>Example</h4>
13+
<!-- Show example of function.-->
14+
<code>right('Hello World',5) -> 'World'</code><br>
15+

‎resources/function_help/rpad-en_US‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<h3>rpad() function</h3>
2+
Returns a string with supplied width padded
3+
using the fill character.
4+
5+
<h4>Syntax</h4>
6+
<code>rpad(string, width, fill)</code><br>
7+
8+
<h4>Arguments</h4>
9+
<code>string</code> - is string. The string.
10+
<br>
11+
<code>width</code> - is int. The length of the new string.
12+
<br>
13+
<code>fill</code> - is char. The character to padd the remaining space with.
14+
15+
<h4>Example</h4>
16+
<!-- Show example of function.-->
17+
<code>rpad('Hello', 10, 'x') -> 'xxxxxHello'</code><br>
18+

‎src/core/qgsexpression.cpp‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,36 @@ static QVariant fcnStrpos( const QVariantList& values, QgsFeature* , QgsExpressi
511511
return string.indexOf( QRegExp( getStringValue( values.at( 1 ), parent ) ) );
512512
}
513513

514+
static QVariant fcnRight( const QVariantList& values, QgsFeature* , QgsExpression *parent )
515+
{
516+
QString string = getStringValue( values.at( 0 ), parent );
517+
int pos = getIntValue( values.at( 1 ), parent );
518+
return string.right( pos );
519+
}
520+
521+
static QVariant fcnLeft( const QVariantList& values, QgsFeature* , QgsExpression *parent )
522+
{
523+
QString string = getStringValue( values.at( 0 ), parent );
524+
int pos = getIntValue( values.at( 1 ), parent );
525+
return string.left( pos );
526+
}
527+
528+
static QVariant fcnRPad( const QVariantList& values, QgsFeature* , QgsExpression *parent )
529+
{
530+
QString string = getStringValue( values.at( 0 ), parent );
531+
int length = getIntValue( values.at( 1 ), parent );
532+
QString fill = getStringValue( values.at( 2 ), parent );
533+
return string.rightJustified( length, fill.at( 0 ), true );
534+
}
535+
536+
static QVariant fcnLPad( const QVariantList& values, QgsFeature* , QgsExpression *parent )
537+
{
538+
QString string = getStringValue( values.at( 0 ), parent );
539+
int length = getIntValue( values.at( 1 ), parent );
540+
QString fill = getStringValue( values.at( 2 ), parent );
541+
return string.leftJustified( length, fill.at( 0 ), true );
542+
}
543+
514544
static QVariant fcnNow( const QVariantList&, QgsFeature* , QgsExpression * )
515545
{
516546
return QVariant( QDateTime::currentDateTime() );
@@ -775,6 +805,11 @@ const QList<QgsExpression::FunctionDef> &QgsExpression::BuiltinFunctions()
775805
<< FunctionDef( "substr", 3, fcnSubstr, QObject::tr( "String" ) )
776806
<< FunctionDef( "concat", -1, fcnConcat, QObject::tr( "String" ) )
777807
<< FunctionDef( "strpos", 2, fcnStrpos, QObject::tr( "String" ) )
808+
<< FunctionDef( "left", 2, fcnLeft, QObject::tr( "String" ) )
809+
<< FunctionDef( "right", 2, fcnRight, QObject::tr( "String" ) )
810+
<< FunctionDef( "rpad", 3, fcnRPad, QObject::tr( "String" ) )
811+
<< FunctionDef( "lpad", 3, fcnLPad, QObject::tr( "String" ) )
812+
778813
// geometry accessors
779814
<< FunctionDef( "xat", 1, fcnXat, QObject::tr( "Geometry" ), "", true )
780815
<< FunctionDef( "yat", 1, fcnYat, QObject::tr( "Geometry" ), "", true )

‎tests/src/core/testqgsexpression.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,12 @@ class TestQgsExpression: public QObject
257257
QTest::newRow( "substr outside" ) << "substr('HeLLo', -5,2)" << false << QVariant( "" );
258258
QTest::newRow( "strpos" ) << "strpos('Hello World','World')" << false << QVariant( 6 );
259259
QTest::newRow( "strpos outside" ) << "strpos('Hello World','blah')" << false << QVariant( -1 );
260+
QTest::newRow( "left" ) << "left('Hello World',5)" << false << QVariant( "Hello" );
261+
QTest::newRow( "right" ) << "right('Hello World', 5)" << false << QVariant( "World" );
262+
QTest::newRow( "rpad" ) << "rpad('Hello', 10, 'x')" << false << QVariant( "xxxxxHello" );
263+
QTest::newRow( "rpad truncate" ) << "rpad('Hello', 4, 'x')" << false << QVariant( "Hell" );
264+
QTest::newRow( "lpad" ) << "lpad('Hello', 10, 'x')" << false << QVariant( "Helloxxxxx" );
265+
QTest::newRow( "lpad truncate" ) << "rpad('Hello', 4, 'x')" << false << QVariant( "Hell" );
260266

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

0 commit comments

Comments
 (0)