QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
38 class 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 
116 {
117 
118  public:
119 
124  QgsStringReplacementCollection( const QList< QgsStringReplacement > &replacements = QList< QgsStringReplacement >() )
125  : mReplacements( replacements )
126  {}
127 
132  QList< QgsStringReplacement > replacements() const { return mReplacements; }
133 
140  void setReplacements( const QList< QgsStringReplacement > &replacements )
141  {
142  mReplacements = replacements;
143  }
144 
152  QString process( const QString &input ) const;
153 
160  void writeXml( QDomElement &elem, QDomDocument &doc ) const;
161 
167  void readXml( const QDomElement &elem );
168 
169  private:
170 
171  QList< QgsStringReplacement > mReplacements;
172 
173 
174 };
175 
183 class CORE_EXPORT QgsStringUtils
184 {
185  public:
186 
194  static QString capitalize( const QString &string, Qgis::Capitalization capitalization );
195 
204  static QString ampersandEncode( const QString &string );
205 
215  static int levenshteinDistance( const QString &string1, const QString &string2, bool caseSensitive = false );
216 
226  static QString longestCommonSubstring( const QString &string1, const QString &string2, bool caseSensitive = false );
227 
237  static int hammingDistance( const QString &string1, const QString &string2, bool caseSensitive = false );
238 
245  static QString soundex( const QString &string );
246 
256  static double fuzzyScore( const QString &candidate, const QString &search );
257 
266  static QString insertLinks( const QString &string, bool *foundLinks = nullptr );
267 
274  static bool isUrl( const QString &string );
275 
288  static QString wordWrap( const QString &string, int length, bool useMaxLineLength = true, const QString &customDelimiter = QString() );
289 
296  static QString substituteVerticalCharacters( QString string );
297 
304  static QString htmlToMarkdown( const QString &html );
305 
312  static QString qRegExpEscape( const QString &string );
313 
323  static QString truncateMiddleOfString( const QString &string, int maxLength );
324 
325 };
326 
327 #endif //QGSSTRINGUTILS_H
QgsStringReplacement::wholeWordOnly
bool wholeWordOnly() const
Returns true if match only applies to whole words, or false if partial word matches are permitted.
Definition: qgsstringutils.h:65
qgis.h
QgsStringReplacementCollection::replacements
QList< QgsStringReplacement > replacements() const
Returns the list of string replacements in this collection.
Definition: qgsstringutils.h:132
QgsStringReplacement::operator==
bool operator==(const QgsStringReplacement &other) const
Definition: qgsstringutils.h:74
QgsStringReplacement::caseSensitive
bool caseSensitive() const
Returns true if match is case sensitive.
Definition: qgsstringutils.h:62
QgsStringReplacementCollection::setReplacements
void setReplacements(const QList< QgsStringReplacement > &replacements)
Sets the list of string replacements in this collection.
Definition: qgsstringutils.h:140
Qgis::Capitalization
Capitalization
String capitalization options.
Definition: qgis.h:1592
QgsStringReplacement
A representation of a single string replacement.
Definition: qgsstringutils.h:38
QgsStringReplacementCollection
A collection of string replacements (specified using QgsStringReplacement objects).
Definition: qgsstringutils.h:115
QgsStringReplacement::replacement
QString replacement() const
Returns the string to replace matches with.
Definition: qgsstringutils.h:59
QgsStringMap
QMap< QString, QString > QgsStringMap
Definition: qgis.h:2781
QgsStringReplacement::match
QString match() const
Returns the string matched by this object.
Definition: qgsstringutils.h:56
QgsStringUtils
Utility functions for working with strings.
Definition: qgsstringutils.h:183
QgsStringReplacementCollection::QgsStringReplacementCollection
QgsStringReplacementCollection(const QList< QgsStringReplacement > &replacements=QList< QgsStringReplacement >())
Constructor for QgsStringReplacementCollection.
Definition: qgsstringutils.h:124