QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 <QString>
17 #include <QRegExp>
18 #include <QList>
19 #include <QDomDocument>
20 #include <QFont> // for enum values
21 #include "qgis.h"
22 
23 #ifndef QGSSTRINGUTILS_H
24 #define QGSSTRINGUTILS_H
25 
26 
33 class CORE_EXPORT QgsStringReplacement
34 {
35 
36  public:
37 
44  QgsStringReplacement( const QString& match,
45  const QString& replacement,
46  bool caseSensitive = false,
47  bool wholeWordOnly = false );
48 
50  QString match() const { return mMatch; }
51 
53  QString replacement() const { return mReplacement; }
54 
56  bool caseSensitive() const { return mCaseSensitive; }
57 
59  bool wholeWordOnly() const { return mWholeWordOnly; }
60 
65  QString process( const QString& input ) const;
66 
67  bool operator==( const QgsStringReplacement& other )
68  {
69  return mMatch == other.mMatch
70  && mReplacement == other.mReplacement
71  && mCaseSensitive == other.mCaseSensitive
72  && mWholeWordOnly == other.mWholeWordOnly;
73  }
74 
78  QgsStringMap properties() const;
79 
83  static QgsStringReplacement fromProperties( const QgsStringMap& properties );
84 
85  private:
86 
87  QString mMatch;
88 
89  QString mReplacement;
90 
91  bool mCaseSensitive;
92 
93  bool mWholeWordOnly;
94 
95  QRegExp mRx;
96 };
97 
98 
106 {
107 
108  public:
109 
114  : mReplacements( replacements )
115  {}
116 
120  QList< QgsStringReplacement > replacements() const { return mReplacements; }
121 
127  void setReplacements( const QList< QgsStringReplacement >& replacements )
128  {
129  mReplacements = replacements;
130  }
131 
138  QString process( const QString& input ) const;
139 
145  void writeXml( QDomElement& elem, QDomDocument& doc ) const;
146 
151  void readXml( const QDomElement& elem );
152 
153  private:
154 
155  QList< QgsStringReplacement > mReplacements;
156 
157 
158 };
159 
166 class CORE_EXPORT QgsStringUtils
167 {
168  public:
169 
172  {
173  MixedCase = QFont::MixedCase,
174  AllUppercase = QFont::AllUppercase,
175  AllLowercase = QFont::AllLowercase,
176  ForceFirstLetterToCapital = QFont::Capitalize,
177  };
178 
185  static QString capitalize( const QString& string, Capitalization capitalization );
186 
195  static int levenshteinDistance( const QString &string1, const QString &string2, bool caseSensitive = false );
196 
205  static QString longestCommonSubstring( const QString &string1, const QString &string2, bool caseSensitive = false );
206 
215  static int hammingDistance( const QString &string1, const QString &string2, bool caseSensitive = false );
216 
222  static QString soundex( const QString &string );
223 
231  static QString insertLinks( const QString& string, bool* foundLinks = nullptr );
232 };
233 
234 #endif //QGSSTRINGUTILS_H
bool operator==(const QgsStringReplacement &other)
A representation of a single string replacement.
bool caseSensitive() const
Returns true if match is case sensitive.
QgsStringReplacementCollection(const QList< QgsStringReplacement > &replacements=QList< QgsStringReplacement >())
Constructor for QgsStringReplacementCollection.
QList< QgsStringReplacement > replacements() const
Returns the list of string replacements in this collection.
QString match() const
Returns the string matched by this object.
QString replacement() const
Returns the string to replace matches with.
Capitalization
Capitalization options.
bool wholeWordOnly() const
Returns true if match only applies to whole words, or false if partial word matches are permitted...
A collection of string replacements (specified using QgsStringReplacement objects).
Utility functions for working with strings.
void setReplacements(const QList< QgsStringReplacement > &replacements)
Sets the list of string replacements in this collection.