Skip to content
Permalink
Browse files

Add string position function to expression engine

  • Loading branch information
NathanW2 committed Jul 2, 2012
1 parent fab2fe1 commit 071866ff0406c8e39cc8428753261b7110985adb
Showing with 31 additions and 21 deletions.
  1. +29 −21 src/core/qgsexpression.cpp
  2. +2 −0 tests/src/core/testqgsexpression.cpp
@@ -64,33 +64,33 @@ QgsExpression::Interval QgsExpression::Interval::fromString( QString string )
continue;
}

if ( match.contains( "day", Qt::CaseInsensitive ) ||
match.contains( QObject::tr("day", "Note: Word is part matched in code"), Qt::CaseInsensitive )||
match.contains( QObject::tr("days", "Note: Word is part matched in code"), Qt::CaseInsensitive) )
if ( match.contains( "day", Qt::CaseInsensitive ) ||
match.contains( QObject::tr( "day", "Note: Word is part matched in code" ), Qt::CaseInsensitive ) ||
match.contains( QObject::tr( "days", "Note: Word is part matched in code" ), Qt::CaseInsensitive ) )
seconds += value * QgsExpression::Interval::DAY;
if ( match.contains( "week", Qt::CaseInsensitive ) ||
match.contains( QObject::tr("week", "Note: Word is part matched in code"), Qt::CaseInsensitive ) ||
match.contains( QObject::tr("weeks", "Note: Word is part matched in code"), Qt::CaseInsensitive ) )
if ( match.contains( "week", Qt::CaseInsensitive ) ||
match.contains( QObject::tr( "week", "Note: Word is part matched in code" ), Qt::CaseInsensitive ) ||
match.contains( QObject::tr( "weeks", "Note: Word is part matched in code" ), Qt::CaseInsensitive ) )
seconds += value * QgsExpression::Interval::WEEKS;
if ( match.contains( "month", Qt::CaseInsensitive ) ||
match.contains( QObject::tr("month", "Note: Word is part matched in code"), Qt::CaseInsensitive ) ||
match.contains( QObject::tr("months", "Note: Word is part matched in code"), Qt::CaseInsensitive ) )
if ( match.contains( "month", Qt::CaseInsensitive ) ||
match.contains( QObject::tr( "month", "Note: Word is part matched in code" ), Qt::CaseInsensitive ) ||
match.contains( QObject::tr( "months", "Note: Word is part matched in code" ), Qt::CaseInsensitive ) )
seconds += value * QgsExpression::Interval::MONTHS;
if ( match.contains( "year", Qt::CaseInsensitive ) ||
match.contains( QObject::tr("year", "Note: Word is part matched in code"), Qt::CaseInsensitive ) ||
match.contains( QObject::tr("years", "Note: Word is part matched in code"), Qt::CaseInsensitive ) )
match.contains( QObject::tr( "year", "Note: Word is part matched in code" ), Qt::CaseInsensitive ) ||
match.contains( QObject::tr( "years", "Note: Word is part matched in code" ), Qt::CaseInsensitive ) )
seconds += value * QgsExpression::Interval::YEARS;
if ( match.contains( "second", Qt::CaseInsensitive ) ||
match.contains( QObject::tr("second", "Note: Word is part matched in code"), Qt::CaseInsensitive ) ||
match.contains( QObject::tr("seconds", "Note: Word is part matched in code"), Qt::CaseInsensitive ) )
if ( match.contains( "second", Qt::CaseInsensitive ) ||
match.contains( QObject::tr( "second", "Note: Word is part matched in code" ), Qt::CaseInsensitive ) ||
match.contains( QObject::tr( "seconds", "Note: Word is part matched in code" ), Qt::CaseInsensitive ) )
seconds += value;
if ( match.contains( "minute", Qt::CaseInsensitive ) ||
match.contains( QObject::tr("minute", "Note: Word is part matched in code"), Qt::CaseInsensitive ) ||
match.contains( QObject::tr("minutes", "Note: Word is part matched in code"), Qt::CaseInsensitive ) )
if ( match.contains( "minute", Qt::CaseInsensitive ) ||
match.contains( QObject::tr( "minute", "Note: Word is part matched in code" ), Qt::CaseInsensitive ) ||
match.contains( QObject::tr( "minutes", "Note: Word is part matched in code" ), Qt::CaseInsensitive ) )
seconds += value * QgsExpression::Interval::MINUTE;
if ( match.contains( "hour", Qt::CaseInsensitive ) ||
match.contains( QObject::tr("hour", "Note: Word is part matched in code"), Qt::CaseInsensitive ) ||
match.contains( QObject::tr("hours", "Note: Word is part matched in code"), Qt::CaseInsensitive ) )
if ( match.contains( "hour", Qt::CaseInsensitive ) ||
match.contains( QObject::tr( "hour", "Note: Word is part matched in code" ), Qt::CaseInsensitive ) ||
match.contains( QObject::tr( "hours", "Note: Word is part matched in code" ), Qt::CaseInsensitive ) )
seconds += value * QgsExpression::Interval::HOUR;
}

@@ -494,6 +494,7 @@ static QVariant fcnFeatureId( const QVariantList& , QgsFeature* f, QgsExpression
// TODO: handling of 64-bit feature ids?
return f ? QVariant(( int )f->id() ) : QVariant();
}

static QVariant fcnConcat( const QVariantList& values, QgsFeature* , QgsExpression *parent )
{
QString concat;
@@ -504,6 +505,12 @@ static QVariant fcnConcat( const QVariantList& values, QgsFeature* , QgsExpressi
return concat;
}

static QVariant fcnStrpos( const QVariantList& values, QgsFeature* , QgsExpression *parent )
{
QString string = getStringValue( values.at( 0 ), parent );
return string.indexOf( QRegExp( getStringValue( values.at( 1 ), parent ) ) );
}

static QVariant fcnNow( const QVariantList&, QgsFeature* , QgsExpression * )
{
return QVariant( QDateTime::currentDateTime() );
@@ -767,6 +774,7 @@ const QList<QgsExpression::FunctionDef> &QgsExpression::BuiltinFunctions()
<< FunctionDef( "regexp_replace", 3, fcnRegexpReplace, QObject::tr( "String" ) )
<< FunctionDef( "substr", 3, fcnSubstr, QObject::tr( "String" ) )
<< FunctionDef( "concat", -1, fcnConcat, QObject::tr( "String" ) )
<< FunctionDef( "strpos", 2, fcnStrpos, QObject::tr( "String" ) )
// geometry accessors
<< FunctionDef( "xat", 1, fcnXat, QObject::tr( "Geometry" ), "", true )
<< FunctionDef( "yat", 1, fcnYat, QObject::tr( "Geometry" ), "", true )
@@ -1219,7 +1227,7 @@ QVariant QgsExpression::NodeBinaryOperator::eval( QgsExpression* parent, QgsFeat
QgsExpression::Interval iL = getInterval( vR, parent ); ENSURE_NO_EVAL_ERROR;
if ( mOp == boDiv || mOp == boMul || mOp == boMod )
{
parent->setEvalErrorString( QObject::tr("Can't preform /, *, or % on DateTime and Interval") );
parent->setEvalErrorString( QObject::tr( "Can't preform /, *, or % on DateTime and Interval" ) );
return QVariant();
}
return QVariant( computeDateTimeFromInterval( dL, &iL ) );
@@ -255,6 +255,8 @@ class TestQgsExpression: public QObject
QTest::newRow( "regexp_replace invalid" ) << "regexp_replace('HeLLo','[[[', '-')" << true << QVariant();
QTest::newRow( "substr" ) << "substr('HeLLo', 3,2)" << false << QVariant( "LL" );
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 );

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

0 comments on commit 071866f

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