QGIS API Documentation 3.99.0-Master (e9821da5c6b)
Loading...
Searching...
No Matches
qgshstoreutils.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgshstoreutils.h
3 ---------------------
4 begin : Sept 2018
5 copyright : (C) 2018 by Mathieu Pellerin
6 email : nirvn dot asia at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgshstoreutils.h"
17
18#include <QRegularExpression>
19#include <QString>
20
21using namespace Qt::StringLiterals;
22
23QVariantMap QgsHstoreUtils::parse( const QString &string )
24{
25 QVariantMap map;
26 QList<QString> bits;
27 static const QList<QString > sSeps{ "=>", "," };
28
29 int i = 0;
30 while ( i < string.length() )
31 {
32 while ( i < string.length() && string.at( i ).isSpace() )
33 ++i;
34 const QString current = string.mid( i );
35 const QString sep = sSeps.at( bits.length() );
36 if ( current.startsWith( '"' ) )
37 {
38 const thread_local QRegularExpression re( u"^\"((?:\\\\.|[^\"\\\\])*)\".*"_s );
39 const QRegularExpressionMatch match = re.match( current );
40 bits << QString();
41 if ( match.hasMatch() )
42 {
43 bits[bits.length() - 1] = match.captured( 1 ).replace( "\\\""_L1, "\""_L1 ).replace( "\\\\"_L1, "\\"_L1 );
44 i += match.captured( 1 ).length() + 2;
45 while ( i < string.length() && string.at( i ).isSpace() )
46 ++i;
47
48 if ( QStringView{string}.mid( i ).startsWith( sep ) )
49 {
50 i += sep.length();
51 }
52 else if ( i < string.length() )
53 {
54 // hstore string format broken, end construction
55 i += current.length();
56 }
57 }
58 else
59 {
60 // hstore string format broken, end construction
61 i += current.length();
62 bits[bits.length() - 1] = current.trimmed();
63 }
64 }
65 else
66 {
67 const int sepPos = current.indexOf( sep );
68 if ( sepPos < 0 )
69 {
70 i += current.length();
71 bits << current.trimmed();
72 }
73 else
74 {
75 i += sepPos + sep.length();
76 bits << current.left( sepPos ).trimmed();
77 }
78 }
79
80 if ( bits.length() == 2 )
81 {
82 if ( !bits.at( 0 ).isEmpty() && !bits.at( 1 ).isEmpty() )
83 map[ bits.at( 0 ) ] = bits.at( 1 );
84 bits.clear();
85 }
86 }
87
88 return map;
89}
90
91QString QgsHstoreUtils::build( const QVariantMap &map )
92{
93 QStringList list;
94 for ( QVariantMap::const_iterator it = map.constBegin(); it != map.constEnd(); ++it )
95 {
96 QString key = it.key();
97 QString value = it.value().toString();
98 list << QString( "\"%1\"=>\"%2\"" ).arg( key.replace( "\\", "\\\\" ).replace( "\"", "\\\"" ),
99 value.replace( "\\", "\\\\" ).replace( "\"", "\\\"" ) );
100 }
101 return list.join( ',' );
102}
static QString build(const QVariantMap &map)
Build a hstore-formatted string from a QVariantMap.
static QVariantMap parse(const QString &string)
Returns a QVariantMap object containing the key and values from a hstore-formatted string.