QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
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 #include <QFont> // for enum values
24 
25 #ifndef QGSSTRINGUTILS_H
26 #define QGSSTRINGUTILS_H
27 
28 #define FUZZY_SCORE_WORD_MATCH 5
29 #define FUZZY_SCORE_NEW_MATCH 3
30 #define FUZZY_SCORE_CONSECUTIVE_MATCH 4
31 
39 class CORE_EXPORT QgsStringReplacement
40 {
41 
42  public:
43 
51  QgsStringReplacement( const QString &match,
52  const QString &replacement,
53  bool caseSensitive = false,
54  bool wholeWordOnly = false );
55 
57  QString match() const { return mMatch; }
58 
60  QString replacement() const { return mReplacement; }
61 
63  bool caseSensitive() const { return mCaseSensitive; }
64 
66  bool wholeWordOnly() const { return mWholeWordOnly; }
67 
73  QString process( const QString &input ) const;
74 
75  bool operator==( const QgsStringReplacement &other )
76  {
77  return mMatch == other.mMatch
78  && mReplacement == other.mReplacement
79  && mCaseSensitive == other.mCaseSensitive
80  && mWholeWordOnly == other.mWholeWordOnly;
81  }
82 
87  QgsStringMap properties() const;
88 
93  static QgsStringReplacement fromProperties( const QgsStringMap &properties );
94 
95  private:
96 
97  QString mMatch;
98 
99  QString mReplacement;
100 
101  bool mCaseSensitive;
102 
103  bool mWholeWordOnly;
104 
105  QRegularExpression mRx;
106 };
107 
108 
117 {
118 
119  public:
120 
125  QgsStringReplacementCollection( const QList< QgsStringReplacement > &replacements = QList< QgsStringReplacement >() )
126  : mReplacements( replacements )
127  {}
128 
133  QList< QgsStringReplacement > replacements() const { return mReplacements; }
134 
141  void setReplacements( const QList< QgsStringReplacement > &replacements )
142  {
143  mReplacements = replacements;
144  }
145 
153  QString process( const QString &input ) const;
154 
161  void writeXml( QDomElement &elem, QDomDocument &doc ) const;
162 
168  void readXml( const QDomElement &elem );
169 
170  private:
171 
172  QList< QgsStringReplacement > mReplacements;
173 
174 
175 };
176 
184 class CORE_EXPORT QgsStringUtils
185 {
186  public:
187 
190  {
191  MixedCase = QFont::MixedCase,
192  AllUppercase = QFont::AllUppercase,
193  AllLowercase = QFont::AllLowercase,
194  ForceFirstLetterToCapital = QFont::Capitalize,
195  TitleCase = QFont::Capitalize + 1000,
196  UpperCamelCase = QFont::Capitalize + 1001,
197  };
198 
206  static QString capitalize( const QString &string, Capitalization capitalization );
207 
216  static QString ampersandEncode( const QString &string );
217 
227  static int levenshteinDistance( const QString &string1, const QString &string2, bool caseSensitive = false );
228 
238  static QString longestCommonSubstring( const QString &string1, const QString &string2, bool caseSensitive = false );
239 
249  static int hammingDistance( const QString &string1, const QString &string2, bool caseSensitive = false );
250 
257  static QString soundex( const QString &string );
258 
268  static double fuzzyScore( const QString &candidate, const QString &search );
269 
278  static QString insertLinks( const QString &string, bool *foundLinks = nullptr );
279 
286  static bool isUrl( const QString &string );
287 
300  static QString wordWrap( const QString &string, int length, bool useMaxLineLength = true, const QString &customDelimiter = QString() );
301 
308  static QString substituteVerticalCharacters( QString string );
309 
316  static QString htmlToMarkdown( const QString &html );
317 
324  static QString qRegExpEscape( const QString &string );
325 
335  static QString truncateMiddleOfString( const QString &string, int maxLength );
336 
337 };
338 
339 #endif //QGSSTRINGUTILS_H
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.
QString match() const
Returns the string matched by this object.
bool operator==(const QgsStringReplacement &other)
Utility functions for working with strings.
Capitalization
Capitalization options.
QMap< QString, QString > QgsStringMap
Definition: qgis.h:1703