Addition of a QgsHstoreUtils parse function

This commit is contained in:
nirvn 2018-09-13 17:01:41 +07:00 committed by Mathieu Pellerin
parent d41ffb0cb7
commit 7f9edcafa0
6 changed files with 171 additions and 64 deletions

View File

@ -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 *
************************************************************************/

View File

@ -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

View File

@ -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

View File

@ -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 * )

View File

@ -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
src/core/qgshstoreutils.h Normal file
View File

@ -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