QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 <QString>
18 #include <QRegExp>
19 #include <QList>
20 #include <QDomDocument>
21 #include <QFont> // for enum values
22 #include "qgis.h"
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 )
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  QRegExp 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 
189  {
190  MixedCase = QFont::MixedCase,
191  AllUppercase = QFont::AllUppercase,
192  AllLowercase = QFont::AllLowercase,
193  ForceFirstLetterToCapital = QFont::Capitalize,
194  TitleCase = QFont::Capitalize + 1000,
195  UpperCamelCase = QFont::Capitalize + 1001,
196  };
197 
205  static QString capitalize( const QString &string, Capitalization capitalization );
206 
215  static QString ampersandEncode( const QString &string );
216 
226  static int levenshteinDistance( const QString &string1, const QString &string2, bool caseSensitive = false );
227 
237  static QString longestCommonSubstring( const QString &string1, const QString &string2, bool caseSensitive = false );
238 
248  static int hammingDistance( const QString &string1, const QString &string2, bool caseSensitive = false );
249 
256  static QString soundex( const QString &string );
257 
267  static double fuzzyScore( const QString &candidate, const QString &search );
268 
277  static QString insertLinks( const QString &string, bool *foundLinks = nullptr );
278 
291  static QString wordWrap( const QString &string, int length, bool useMaxLineLength = true, const QString &customDelimiter = QString() );
292 
299  static QString substituteVerticalCharacters( QString string );
300 
307  static QString htmlToMarkdown( const QString &html );
308 
309 };
310 
311 #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)
Definition: qgsstringutils.h:74
QgsStringUtils::Capitalization
Capitalization
Capitalization options.
Definition: qgsstringutils.h:189
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
QgsStringReplacement
A representation of a single string replacement.
Definition: qgsstringutils.h:39
QgsStringReplacementCollection
A collection of string replacements (specified using QgsStringReplacement objects).
Definition: qgsstringutils.h:116
QgsStringReplacement::replacement
QString replacement() const
Returns the string to replace matches with.
Definition: qgsstringutils.h:59
QgsStringMap
QMap< QString, QString > QgsStringMap
Definition: qgis.h:758
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:184
QgsStringReplacementCollection::QgsStringReplacementCollection
QgsStringReplacementCollection(const QList< QgsStringReplacement > &replacements=QList< QgsStringReplacement >())
Constructor for QgsStringReplacementCollection.
Definition: qgsstringutils.h:124