Skip to content
Permalink
Browse files

Addition of a QgsHstoreUtils parse function

  • Loading branch information
nirvn committed Sep 14, 2018
1 parent d41ffb0 commit 7f9edcafa03f45ef922ffa916d1a5400ceaca591
@@ -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 *
************************************************************************/
@@ -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
@@ -208,6 +208,7 @@ SET(QGIS_CORE_SRCS
qgsgml.cpp
qgsgmlschema.cpp
qgshistogram.cpp
qgshstoreutils.cpp
qgshtmlutils.cpp
qgsinterval.cpp
qgsjsonutils.cpp
@@ -869,6 +870,7 @@ SET(QGIS_CORE_HDRS
qgsgeometryfixes.h
qgsgeometrysimplifier.h
qgshistogram.h
qgshstoreutils.h
qgshtmlutils.h
qgsindexedfeature.h
qgsinterval.h
@@ -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"
@@ -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 * )
@@ -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;
}
@@ -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.
You can’t perform that action at this time.