QGIS API Documentation 4.1.0-Master (60fea48833c)
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 public:
48 QgsStringReplacement( const QString &match, const QString &replacement, bool caseSensitive = false, bool wholeWordOnly = false );
49
51 QString match() const { return mMatch; }
52
54 QString replacement() const { return mReplacement; }
55
57 bool caseSensitive() const { return mCaseSensitive; }
58
60 bool wholeWordOnly() const { return mWholeWordOnly; }
61
67 QString process( const QString &input ) const;
68
69 bool operator==( const QgsStringReplacement &other ) const
70 {
71 return mMatch == other.mMatch && mReplacement == other.mReplacement && mCaseSensitive == other.mCaseSensitive && mWholeWordOnly == other.mWholeWordOnly;
72 }
73
78 QgsStringMap properties() const;
79
84 static QgsStringReplacement fromProperties( const QgsStringMap &properties );
85
86 private:
87 QString mMatch;
88
89 QString mReplacement;
90
91 bool mCaseSensitive;
92
93 bool mWholeWordOnly;
94
95 QRegularExpression mRx;
96};
97
98
104
106{
107 public:
112 QgsStringReplacementCollection( const QList< QgsStringReplacement > &replacements = QList< QgsStringReplacement >() )
113 : mReplacements( replacements )
114 {}
115
120 QList< QgsStringReplacement > replacements() const { return mReplacements; }
121
128 void setReplacements( const QList< QgsStringReplacement > &replacements ) { mReplacements = replacements; }
129
137 QString process( const QString &input ) const;
138
145 void writeXml( QDomElement &elem, QDomDocument &doc ) const;
146
152 void readXml( const QDomElement &elem );
153
154 private:
155 QList< QgsStringReplacement > mReplacements;
156};
157
163
164class CORE_EXPORT QgsStringUtils
165{
166 public:
173 static QString capitalize( const QString &string, Qgis::Capitalization capitalization );
174
183 static QString ampersandEncode( const QString &string );
184
194 static int levenshteinDistance( const QString &string1, const QString &string2, bool caseSensitive = false );
195
205 static QString longestCommonSubstring( const QString &string1, const QString &string2, bool caseSensitive = false );
206
216 static int hammingDistance( const QString &string1, const QString &string2, bool caseSensitive = false );
217
224 static QString soundex( const QString &string );
225
235 static double fuzzyScore( const QString &candidate, const QString &search );
236
244 static QString insertLinks( const QString &string, bool *foundLinks = nullptr );
245
252 static bool isUrl( const QString &string );
253
266 static QString wordWrap( const QString &string, int length, bool useMaxLineLength = true, const QString &customDelimiter = QString() );
267
274 static QString substituteVerticalCharacters( QString string );
275
282 static QString htmlToMarkdown( const QString &html );
283
290 static QString qRegExpEscape( const QString &string );
291
301 static QString truncateMiddleOfString( const QString &string, int maxLength );
302
312 static bool containsByWord( const QString &candidate, const QString &words, Qt::CaseSensitivity sensitivity = Qt::CaseInsensitive );
313
314
327 static QHash<QString, QString> createUnaccentMap();
328
342 static QHash<QString, QString> UNACCENT_MAP;
343
381 static QString unaccent( const QString &input );
382};
383
384#endif //QGSSTRINGUTILS_H
Capitalization
String capitalization options.
Definition qgis.h:3503
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:7475