QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 
35 class CORE_EXPORT QgsStringReplacement
36 {
37 
38  public:
39 
47  QgsStringReplacement( const QString &match,
48  const QString &replacement,
49  bool caseSensitive = false,
50  bool wholeWordOnly = false );
51 
53  QString match() const { return mMatch; }
54 
56  QString replacement() const { return mReplacement; }
57 
59  bool caseSensitive() const { return mCaseSensitive; }
60 
62  bool wholeWordOnly() const { return mWholeWordOnly; }
63 
69  QString process( const QString &input ) const;
70 
71  bool operator==( const QgsStringReplacement &other )
72  {
73  return mMatch == other.mMatch
74  && mReplacement == other.mReplacement
75  && mCaseSensitive == other.mCaseSensitive
76  && mWholeWordOnly == other.mWholeWordOnly;
77  }
78 
83  QgsStringMap properties() const;
84 
89  static QgsStringReplacement fromProperties( const QgsStringMap &properties );
90 
91  private:
92 
93  QString mMatch;
94 
95  QString mReplacement;
96 
97  bool mCaseSensitive;
98 
99  bool mWholeWordOnly;
100 
101  QRegExp mRx;
102 };
103 
104 
113 {
114 
115  public:
116 
121  QgsStringReplacementCollection( const QList< QgsStringReplacement > &replacements = QList< QgsStringReplacement >() )
122  : mReplacements( replacements )
123  {}
124 
129  QList< QgsStringReplacement > replacements() const { return mReplacements; }
130 
137  void setReplacements( const QList< QgsStringReplacement > &replacements )
138  {
139  mReplacements = replacements;
140  }
141 
149  QString process( const QString &input ) const;
150 
157  void writeXml( QDomElement &elem, QDomDocument &doc ) const;
158 
164  void readXml( const QDomElement &elem );
165 
166  private:
167 
168  QList< QgsStringReplacement > mReplacements;
169 
170 
171 };
172 
180 class CORE_EXPORT QgsStringUtils
181 {
182  public:
183 
186  {
187  MixedCase = QFont::MixedCase,
188  AllUppercase = QFont::AllUppercase,
189  AllLowercase = QFont::AllLowercase,
190  ForceFirstLetterToCapital = QFont::Capitalize,
191  TitleCase = QFont::Capitalize + 1000,
192  };
193 
201  static QString capitalize( const QString &string, Capitalization capitalization );
202 
211  static QString ampersandEncode( const QString &string );
212 
222  static int levenshteinDistance( const QString &string1, const QString &string2, bool caseSensitive = false );
223 
233  static QString longestCommonSubstring( const QString &string1, const QString &string2, bool caseSensitive = false );
234 
244  static int hammingDistance( const QString &string1, const QString &string2, bool caseSensitive = false );
245 
252  static QString soundex( const QString &string );
253 
262  static QString insertLinks( const QString &string, bool *foundLinks = nullptr );
263 
276  static QString wordWrap( const QString &string, int length, bool useMaxLineLength = true, const QString &customDelimiter = QString() );
277 };
278 
279 #endif //QGSSTRINGUTILS_H
bool operator==(const QgsStringReplacement &other)
A representation of a single string replacement.
QgsStringReplacementCollection(const QList< QgsStringReplacement > &replacements=QList< QgsStringReplacement >())
Constructor for QgsStringReplacementCollection.
QMap< QString, QString > QgsStringMap
Definition: qgis.h:577
QString match() const
Returns the string matched by this object.
bool caseSensitive() const
Returns true if match is case sensitive.
bool wholeWordOnly() const
Returns true if match only applies to whole words, or false if partial word matches are permitted...
Capitalization
Capitalization options.
QList< QgsStringReplacement > replacements() const
Returns the list of string replacements in this collection.
A collection of string replacements (specified using QgsStringReplacement objects).
Utility functions for working with strings.
QString replacement() const
Returns the string to replace matches with.
void setReplacements(const QList< QgsStringReplacement > &replacements)
Sets the list of string replacements in this collection.