Skip to content

Commit

Permalink
Addition of a QgsHstoreUtils parse function
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Sep 14, 2018
1 parent d41ffb0 commit 7f9edca
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 64 deletions.
35 changes: 35 additions & 0 deletions python/core/auto_generated/qgshstoreutils.sip.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgshstoreutils.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/



%ModuleHeaderCode
#include "qgshstoreutils.h"
%End

namespace QgsHstoreUtils
{

QVariantMap parse( const QString &string );
%Docstring
Returns a QVariantMap object containing the key and values from a hstore-formatted string.

:param string: The hstored-formatted string

.. versionadded:: 3.4
%End

};

/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgshstoreutils.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
1 change: 1 addition & 0 deletions python/core/core_auto.sip
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
%Include auto_generated/qgsgeometryfixes.sip
%Include auto_generated/qgsgeometrysimplifier.sip
%Include auto_generated/qgshistogram.sip
%Include auto_generated/qgshstoreutils.sip
%Include auto_generated/qgshtmlutils.sip
%Include auto_generated/qgsinterval.sip
%Include auto_generated/qgsjsonutils.sip
Expand Down
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ SET(QGIS_CORE_SRCS
qgsgml.cpp
qgsgmlschema.cpp
qgshistogram.cpp
qgshstoreutils.cpp
qgshtmlutils.cpp
qgsinterval.cpp
qgsjsonutils.cpp
Expand Down Expand Up @@ -869,6 +870,7 @@ SET(QGIS_CORE_HDRS
qgsgeometryfixes.h
qgsgeometrysimplifier.h
qgshistogram.h
qgshstoreutils.h
qgshtmlutils.h
qgsindexedfeature.h
qgsinterval.h
Expand Down
66 changes: 2 additions & 64 deletions src/core/expression/qgsexpressionfunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "qgsstringutils.h"
#include "qgsmultipoint.h"
#include "qgsgeometryutils.h"
#include "qgshstoreutils.h"
#include "qgsmultilinestring.h"
#include "qgslinestring.h"
#include "qgscurvepolygon.h"
Expand Down Expand Up @@ -4374,70 +4375,7 @@ static QVariant fcnHstoreToMap( const QVariantList &values, const QgsExpressionC
return QVariantMap();
str = str.trimmed();

QVariantMap map;
QList<QString> bits;
QList<QString> seps;
seps << "=>" << ",";
int i = 0;
while ( i < str.length() )
{
while ( i < str.length() && str.at( i ).isSpace() )
++i;
QString current = str.mid( i );
QString sep = seps.at( bits.length() );
if ( current.startsWith( '"' ) )
{
QRegularExpression re( "^\"((?:\\\\.|[^\"\\\\])*)\".*" );
QRegularExpressionMatch match = re.match( current );
bits << QString();
if ( match.hasMatch() )
{
bits[bits.length() - 1] = match.captured( 1 ).replace( QLatin1String( "\\\"" ), QLatin1String( "\"" ) ).replace( QLatin1String( "\\\\" ), QLatin1String( "\\" ) );
i += match.captured( 1 ).length() + 2;
while ( i < str.length() && str.at( i ).isSpace() )
++i;

if ( str.midRef( i ).startsWith( sep ) )
{
i += sep.length();
}
else if ( i < str.length() )
{
// hstore string format broken, end construction
i += current.length();
}
}
else
{
// hstore string format broken, end construction
i += current.length();
bits[bits.length() - 1] = current.trimmed();
}
}
else
{
int sepPos = current.indexOf( sep );
if ( sepPos < 0 )
{
i += current.length();
bits << current.trimmed();
}
else
{
i += sepPos + sep.length();
bits << current.left( sepPos ).trimmed();
}
}

if ( bits.length() == 2 )
{
if ( !bits.at( 0 ).isEmpty() && !bits.at( 1 ).isEmpty() )
map[ bits.at( 0 ) ] = bits.at( 1 );
bits.clear();
}
}

return map;
return QgsHstoreUtils::parse( str );
}

static QVariant fcnMapToHstore( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
Expand Down
86 changes: 86 additions & 0 deletions src/core/qgshstoreutils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/***************************************************************************
qgshstoreutils.h
---------------------
begin : Sept 2018
copyright : (C) 2018 by Mathieu Pellerin
email : nirvn dot asia at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgshstoreutils.h"

#include <QRegularExpression>

QVariantMap QgsHstoreUtils::parse( const QString &string )
{
QVariantMap map;
QList<QString> bits;
static const QList<QString > sSeps{ "=>", "," };

int i = 0;
while ( i < string.length() )
{
while ( i < string.length() && string.at( i ).isSpace() )
++i;
QString current = string.mid( i );
QString sep = sSeps.at( bits.length() );
if ( current.startsWith( '"' ) )
{
QRegularExpression re( "^\"((?:\\\\.|[^\"\\\\])*)\".*" );
QRegularExpressionMatch match = re.match( current );
bits << QString();
if ( match.hasMatch() )
{
bits[bits.length() - 1] = match.captured( 1 ).replace( QLatin1String( "\\\"" ), QLatin1String( "\"" ) ).replace( QLatin1String( "\\\\" ), QLatin1String( "\\" ) );
i += match.captured( 1 ).length() + 2;
while ( i < string.length() && string.at( i ).isSpace() )
++i;

if ( string.midRef( i ).startsWith( sep ) )
{
i += sep.length();
}
else if ( i < string.length() )
{
// hstore string format broken, end construction
i += current.length();
}
}
else
{
// hstore string format broken, end construction
i += current.length();
bits[bits.length() - 1] = current.trimmed();
}
}
else
{
int sepPos = current.indexOf( sep );
if ( sepPos < 0 )
{
i += current.length();
bits << current.trimmed();
}
else
{
i += sepPos + sep.length();
bits << current.left( sepPos ).trimmed();
}
}

if ( bits.length() == 2 )
{
if ( !bits.at( 0 ).isEmpty() && !bits.at( 1 ).isEmpty() )
map[ bits.at( 0 ) ] = bits.at( 1 );
bits.clear();
}
}

return map;
}
45 changes: 45 additions & 0 deletions src/core/qgshstoreutils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/***************************************************************************
qgshstoreutils.h
---------------------
begin : Sept 2018
copyright : (C) 2018 by Mathieu Pellerin
email : nirvn dot asia at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSHSTOREUTILS_H
#define QGSHSTOREUTILS_H

#include "qgis_core.h"
#include "qgis.h"

#ifdef SIP_RUN
% ModuleHeaderCode
#include "qgshstoreutils.h"
% End
#endif

/**
* \ingroup core
* The QgsHstoreUtils namespace provides functions to handle hstore-formatted strings.
* \since QGIS 3.4
*/
namespace QgsHstoreUtils
{

/**
* Returns a QVariantMap object containing the key and values from a hstore-formatted string.
* \param string The hstored-formatted string
* \since QGIS 3.4
*/
CORE_EXPORT QVariantMap parse( const QString &string );

};

#endif //QGSHSTOREUTILS_H

0 comments on commit 7f9edca

Please sign in to comment.