QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgsstringutils.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsstringutils.h
3 ----------------
4 begin : June 2015
5 copyright : (C) 2015 by Nyall Dawson
6 email : nyall dot dawson 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 "qgis_core.h"
17#include "qgis.h"
18
19#include <QString>
20#include <QRegularExpression>
21#include <QList>
22#include <QDomDocument>
23
24#ifndef QGSSTRINGUTILS_H
25#define QGSSTRINGUTILS_H
26
27#define FUZZY_SCORE_WORD_MATCH 5
28#define FUZZY_SCORE_NEW_MATCH 3
29#define FUZZY_SCORE_CONSECUTIVE_MATCH 4
30
37class CORE_EXPORT QgsStringReplacement
38{
39
40 public:
41
49 QgsStringReplacement( const QString &match,
50 const QString &replacement,
51 bool caseSensitive = false,
52 bool wholeWordOnly = false );
53
55 QString match() const { return mMatch; }
56
58 QString replacement() const { return mReplacement; }
59
61 bool caseSensitive() const { return mCaseSensitive; }
62
64 bool wholeWordOnly() const { return mWholeWordOnly; }
65
71 QString process( const QString &input ) const;
72
73 bool operator==( const QgsStringReplacement &other ) const
74 {
75 return mMatch == other.mMatch
76 && mReplacement == other.mReplacement
77 && mCaseSensitive == other.mCaseSensitive
78 && mWholeWordOnly == other.mWholeWordOnly;
79 }
80
85 QgsStringMap properties() const;
86
91 static QgsStringReplacement fromProperties( const QgsStringMap &properties );
92
93 private:
94
95 QString mMatch;
96
97 QString mReplacement;
98
99 bool mCaseSensitive;
100
101 bool mWholeWordOnly;
102
103 QRegularExpression mRx;
104};
105
106
114{
115
116 public:
117
122 QgsStringReplacementCollection( const QList< QgsStringReplacement > &replacements = QList< QgsStringReplacement >() )
123 : mReplacements( replacements )
124 {}
125
130 QList< QgsStringReplacement > replacements() const { return mReplacements; }
131
138 void setReplacements( const QList< QgsStringReplacement > &replacements )
139 {
140 mReplacements = replacements;
141 }
142
150 QString process( const QString &input ) const;
151
158 void writeXml( QDomElement &elem, QDomDocument &doc ) const;
159
165 void readXml( const QDomElement &elem );
166
167 private:
168
169 QList< QgsStringReplacement > mReplacements;
170
171
172};
173
180class CORE_EXPORT QgsStringUtils
181{
182 public:
183
190 static QString capitalize( const QString &string, Qgis::Capitalization capitalization );
191
200 static QString ampersandEncode( const QString &string );
201
211 static int levenshteinDistance( const QString &string1, const QString &string2, bool caseSensitive = false );
212
222 static QString longestCommonSubstring( const QString &string1, const QString &string2, bool caseSensitive = false );
223
233 static int hammingDistance( const QString &string1, const QString &string2, bool caseSensitive = false );
234
241 static QString soundex( const QString &string );
242
252 static double fuzzyScore( const QString &candidate, const QString &search );
253
261 static QString insertLinks( const QString &string, bool *foundLinks = nullptr );
262
269 static bool isUrl( const QString &string );
270
283 static QString wordWrap( const QString &string, int length, bool useMaxLineLength = true, const QString &customDelimiter = QString() );
284
291 static QString substituteVerticalCharacters( QString string );
292
299 static QString htmlToMarkdown( const QString &html );
300
307 static QString qRegExpEscape( const QString &string );
308
318 static QString truncateMiddleOfString( const QString &string, int maxLength );
319
329 static bool containsByWord( const QString &candidate, const QString &words, Qt::CaseSensitivity sensitivity = Qt::CaseInsensitive );
330
331};
332
333#endif //QGSSTRINGUTILS_H
Capitalization
String capitalization options.
Definition qgis.h:3203
A collection of string replacements (specified using QgsStringReplacement objects).
void setReplacements(const QList< QgsStringReplacement > &replacements)
Sets the list of string replacements in this collection.
QgsStringReplacementCollection(const QList< QgsStringReplacement > &replacements=QList< QgsStringReplacement >())
Constructor for QgsStringReplacementCollection.
QList< QgsStringReplacement > replacements() const
Returns the list of string replacements in this collection.
A representation of a single string replacement.
bool wholeWordOnly() const
Returns true if match only applies to whole words, or false if partial word matches are permitted.
QString replacement() const
Returns the string to replace matches with.
bool caseSensitive() const
Returns true if match is case sensitive.
bool operator==(const QgsStringReplacement &other) const
QString match() const
Returns the string matched by this object.
Utility functions for working with strings.
QMap< QString, QString > QgsStringMap
Definition qgis.h:6604