QGIS API Documentation 3.99.0-Master (a8882ad4560)
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.h"
17#include "qgis_core.h"
18
19#include <QDomDocument>
20#include <QList>
21#include <QRegularExpression>
22#include <QString>
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
36
37
38class CORE_EXPORT QgsStringReplacement
39{
40
41 public:
42
50 QgsStringReplacement( const QString &match,
51 const QString &replacement,
52 bool caseSensitive = false,
53 bool wholeWordOnly = false );
54
56 QString match() const { return mMatch; }
57
59 QString replacement() const { return mReplacement; }
60
62 bool caseSensitive() const { return mCaseSensitive; }
63
65 bool wholeWordOnly() const { return mWholeWordOnly; }
66
72 QString process( const QString &input ) const;
73
74 bool operator==( const QgsStringReplacement &other ) const
75 {
76 return mMatch == other.mMatch
77 && mReplacement == other.mReplacement
78 && mCaseSensitive == other.mCaseSensitive
79 && mWholeWordOnly == other.mWholeWordOnly;
80 }
81
86 QgsStringMap properties() const;
87
92 static QgsStringReplacement fromProperties( const QgsStringMap &properties );
93
94 private:
95
96 QString mMatch;
97
98 QString mReplacement;
99
100 bool mCaseSensitive;
101
102 bool mWholeWordOnly;
103
104 QRegularExpression mRx;
105};
106
107
113
115{
116
117 public:
118
123 QgsStringReplacementCollection( const QList< QgsStringReplacement > &replacements = QList< QgsStringReplacement >() )
124 : mReplacements( replacements )
125 {}
126
131 QList< QgsStringReplacement > replacements() const { return mReplacements; }
132
139 void setReplacements( const QList< QgsStringReplacement > &replacements )
140 {
141 mReplacements = replacements;
142 }
143
151 QString process( const QString &input ) const;
152
159 void writeXml( QDomElement &elem, QDomDocument &doc ) const;
160
166 void readXml( const QDomElement &elem );
167
168 private:
169 QList< QgsStringReplacement > mReplacements;
170
171
172};
173
179
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
344 static QHash<QString, QString> createUnaccentMap();
345
359 static QHash<QString, QString> UNACCENT_MAP;
360
398 static QString unaccent( const QString &input );
399
400
401};
402
403#endif //QGSSTRINGUTILS_H
Capitalization
String capitalization options.
Definition qgis.h:3436
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.
QgsStringReplacement(const QString &match, const QString &replacement, bool caseSensitive=false, bool wholeWordOnly=false)
Constructor for QgsStringReplacement.
bool operator==(const QgsStringReplacement &other) const
QString match() const
Returns the string matched by this object.
Utility functions for working with strings.
static int hammingDistance(const QString &string1, const QString &string2, bool caseSensitive=false)
Returns the Hamming distance between two strings.
static QString soundex(const QString &string)
Returns the Soundex representation of a string.
static QHash< QString, QString > UNACCENT_MAP
Lookup table used by unaccent().
static int levenshteinDistance(const QString &string1, const QString &string2, bool caseSensitive=false)
Returns the Levenshtein edit distance between two strings.
static QString htmlToMarkdown(const QString &html)
Convert simple HTML to markdown.
static QString longestCommonSubstring(const QString &string1, const QString &string2, bool caseSensitive=false)
Returns the longest common substring between two strings.
static QString capitalize(const QString &string, Qgis::Capitalization capitalization)
Converts a string by applying capitalization rules to the string.
static QString substituteVerticalCharacters(QString string)
Returns a string with characters having vertical representation form substituted.
static QString unaccent(const QString &input)
Removes accents and other diacritical marks from a string, replacing accented characters with their u...
static bool containsByWord(const QString &candidate, const QString &words, Qt::CaseSensitivity sensitivity=Qt::CaseInsensitive)
Given a candidate string, returns true if the candidate contains all the individual words from anothe...
static QString insertLinks(const QString &string, bool *foundLinks=nullptr)
Returns a string with any URL (e.g., http(s)/ftp) and mailto: text converted to valid HTML <a ....
static double fuzzyScore(const QString &candidate, const QString &search)
Tests a candidate string to see how likely it is a match for a specified search string.
static QString qRegExpEscape(const QString &string)
Returns an escaped string matching the behavior of QRegExp::escape.
static QString ampersandEncode(const QString &string)
Makes a raw string safe for inclusion as a HTML/XML string literal.
static QString wordWrap(const QString &string, int length, bool useMaxLineLength=true, const QString &customDelimiter=QString())
Automatically wraps a string by inserting new line characters at appropriate locations in the string.
static bool isUrl(const QString &string)
Returns whether the string is a URL (http,https,ftp,file).
static QString truncateMiddleOfString(const QString &string, int maxLength)
Truncates a string to the specified maximum character length.
static QHash< QString, QString > createUnaccentMap()
Generates the unaccent mapping table (auto-generated by script at build time).
QMap< QString, QString > QgsStringMap
Definition qgis.h:7401