19 #include <QStringList>
20 #include <QTextBoundaryFinder>
21 #include <QRegularExpression>
26 if (
string.isEmpty() )
29 switch ( capitalization )
35 return string.toUpper();
38 return string.toLower();
42 QString temp = string;
44 QTextBoundaryFinder wordSplitter( QTextBoundaryFinder::Word,
string.constData(),
string.length(),
nullptr, 0 );
45 QTextBoundaryFinder letterSplitter( QTextBoundaryFinder::Grapheme,
string.constData(),
string.length(),
nullptr, 0 );
47 wordSplitter.setPosition( 0 );
49 while ( ( first && wordSplitter.boundaryReasons() & QTextBoundaryFinder::StartOfItem )
50 || wordSplitter.toNextBoundary() >= 0 )
53 letterSplitter.setPosition( wordSplitter.position() );
54 letterSplitter.toNextBoundary();
55 QString substr =
string.mid( wordSplitter.position(), letterSplitter.position() - wordSplitter.position() );
56 temp.replace( wordSplitter.position(), substr.length(), substr.toUpper() );
65 static QStringList smallWords;
66 static QStringList newPhraseSeparators;
67 static QRegularExpression splitWords;
68 if ( smallWords.empty() )
70 smallWords = QObject::tr(
"a|an|and|as|at|but|by|en|for|if|in|nor|of|on|or|per|s|the|to|vs.|vs|via" ).split(
'|' );
71 newPhraseSeparators = QObject::tr(
".|:" ).split(
'|' );
72 splitWords = QRegularExpression( QStringLiteral(
"\\b" ), QRegularExpression::UseUnicodePropertiesOption );
75 const bool allSameCase =
string.toLower() ==
string ||
string.toUpper() == string;
76 #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
77 const QStringList parts = ( allSameCase ?
string.toLower() : string ).split( splitWords, QString::SkipEmptyParts );
79 const QStringList parts = ( allSameCase ?
string.toLower() : string ).split( splitWords, Qt::SkipEmptyParts );
82 bool firstWord =
true;
84 int lastWord = parts.count() - 1;
85 for (
const QString &word : std::as_const( parts ) )
87 if ( newPhraseSeparators.contains( word.trimmed() ) )
92 else if ( firstWord || ( i == lastWord ) || !smallWords.contains( word ) )
94 result += word.at( 0 ).toUpper() + word.mid( 1 );
108 result.remove(
' ' );
119 for (
int i = 0; i <
string.size(); ++i )
121 QChar ch =
string.at( i );
122 if ( ch.unicode() > 160 )
123 encoded += QStringLiteral(
"&#%1;" ).arg(
static_cast< int >( ch.unicode() ) );
124 else if ( ch.unicode() == 38 )
125 encoded += QLatin1String(
"&" );
126 else if ( ch.unicode() == 60 )
127 encoded += QLatin1String(
"<" );
128 else if ( ch.unicode() == 62 )
129 encoded += QLatin1String(
">" );
138 int length1 = string1.length();
139 int length2 = string2.length();
142 if ( string1.isEmpty() )
146 else if ( string2.isEmpty() )
152 QString s1( caseSensitive ? string1 : string1.toLower() );
153 QString s2( caseSensitive ? string2 : string2.toLower() );
155 const QChar *s1Char = s1.constData();
156 const QChar *s2Char = s2.constData();
159 int commonPrefixLen = 0;
160 while ( length1 > 0 && length2 > 0 && *s1Char == *s2Char )
170 while ( length1 > 0 && length2 > 0 && s1.at( commonPrefixLen + length1 - 1 ) == s2.at( commonPrefixLen + length2 - 1 ) )
181 else if ( length2 == 0 )
187 if ( length1 > length2 )
190 std::swap( length1, length2 );
195 col.fill( 0, length2 + 1 );
196 QVector< int > prevCol;
197 prevCol.reserve( length2 + 1 );
198 for (
int i = 0; i < length2 + 1; ++i )
202 const QChar *s2start = s2Char;
203 for (
int i = 0; i < length1; ++i )
207 for (
int j = 0; j < length2; ++j )
209 col[j + 1] = std::min( std::min( 1 + col[j], 1 + prevCol[1 + j] ), prevCol[j] + ( ( *s1Char == *s2Char ) ? 0 : 1 ) );
215 return prevCol[length2];
220 if ( string1.isEmpty() || string2.isEmpty() )
227 QString s1( caseSensitive ? string1 : string1.toLower() );
228 QString s2( caseSensitive ? string2 : string2.toLower() );
236 int *currentScores =
new int [ s2.length()];
237 int *previousScores =
new int [ s2.length()];
238 int maxCommonLength = 0;
239 int lastMaxBeginIndex = 0;
241 const QChar *s1Char = s1.constData();
242 const QChar *s2Char = s2.constData();
243 const QChar *s2Start = s2Char;
245 for (
int i = 0; i < s1.length(); ++i )
247 for (
int j = 0; j < s2.length(); ++j )
249 if ( *s1Char != *s2Char )
251 currentScores[j] = 0;
255 if ( i == 0 || j == 0 )
257 currentScores[j] = 1;
261 currentScores[j] = 1 + previousScores[j - 1];
264 if ( maxCommonLength < currentScores[j] )
266 maxCommonLength = currentScores[j];
267 lastMaxBeginIndex = i;
272 std::swap( currentScores, previousScores );
276 delete [] currentScores;
277 delete [] previousScores;
278 return string1.mid( lastMaxBeginIndex - maxCommonLength + 1, maxCommonLength );
283 if ( string1.isEmpty() && string2.isEmpty() )
289 if ( string1.length() != string2.length() )
296 QString s1( caseSensitive ? string1 : string1.toLower() );
297 QString s2( caseSensitive ? string2 : string2.toLower() );
306 const QChar *s1Char = s1.constData();
307 const QChar *s2Char = s2.constData();
309 for (
int i = 0; i < string1.length(); ++i )
311 if ( *s1Char != *s2Char )
322 if (
string.isEmpty() )
325 QString tmp =
string.toUpper();
328 QChar *char1 = tmp.data();
329 QChar *char2 = tmp.data();
331 for (
int i = 0; i < tmp.length(); ++i, ++char2 )
333 if ( ( *char2 ).unicode() >= 0x41 && ( *char2 ).unicode() <= 0x5A && ( i == 0 || ( ( *char2 ).unicode() != 0x41 && ( *char2 ).unicode() != 0x45
334 && ( *char2 ).unicode() != 0x48 && ( *char2 ).unicode() != 0x49
335 && ( *char2 ).unicode() != 0x4F && ( *char2 ).unicode() != 0x55
336 && ( *char2 ).unicode() != 0x57 && ( *char2 ).unicode() != 0x59 ) ) )
343 tmp.truncate( outLen );
345 QChar *tmpChar = tmp.data();
347 for (
int i = 1; i < tmp.length(); ++i, ++tmpChar )
349 switch ( ( *tmpChar ).unicode() )
355 tmp.replace( i, 1, QChar( 0x31 ) );
366 tmp.replace( i, 1, QChar( 0x32 ) );
371 tmp.replace( i, 1, QChar( 0x33 ) );
375 tmp.replace( i, 1, QChar( 0x34 ) );
380 tmp.replace( i, 1, QChar( 0x35 ) );
384 tmp.replace( i, 1, QChar( 0x36 ) );
394 for (
int i = 1; i < tmp.length(); ++i, ++char2 )
396 if ( *char2 != *char1 )
405 tmp.truncate( outLen );
406 if ( tmp.length() < 4 )
418 QString candidateNormalized = candidate.simplified().normalized( QString:: NormalizationForm_C ).toLower();
419 QString searchNormalized = search.simplified().normalized( QString:: NormalizationForm_C ).toLower();
421 int candidateLength = candidateNormalized.length();
422 int searchLength = searchNormalized.length();
426 if ( candidateLength == 0 || searchLength == 0 )
429 int candidateIdx = 0;
434 bool isPreviousIndexMatching =
false;
435 bool isWordOpen =
true;
438 while ( candidateIdx < candidateLength )
440 QChar candidateChar = candidateNormalized[ candidateIdx++ ];
441 bool isCandidateCharWordEnd = candidateChar ==
' ' || candidateChar.isPunct();
444 if ( candidateIdx == 1 )
447 else if ( isCandidateCharWordEnd )
454 if ( searchIdx >= searchLength )
457 QChar searchChar = searchNormalized[ searchIdx ];
458 bool isSearchCharWordEnd = searchChar ==
' ' || searchChar.isPunct();
461 if ( candidateChar == searchChar || ( isCandidateCharWordEnd && isSearchCharWordEnd ) )
466 if ( isSearchCharWordEnd )
470 else if ( isPreviousIndexMatching )
478 else if ( isPreviousIndexMatching )
488 isPreviousIndexMatching =
true;
493 isPreviousIndexMatching =
false;
498 if ( searchIdx >= searchLength )
500 bool isEndOfWord = ( candidateIdx >= candidateLength )
502 : candidateNormalized[candidateIdx] ==
' ' || candidateNormalized[candidateIdx].isPunct();
513 if ( searchIdx < searchLength )
516 return static_cast<float>( std::max( score, 0 ) ) / std::max( maxScore, 1 );
522 QString converted = string;
526 static thread_local QRegularExpression urlRegEx(
"(\\b(([\\w-]+://?|www[.])[^\\s()<>]+(?:\\([\\w\\d]+\\)|([^!\"#$%&'()*+,\\-./:;<=>?@[\\\\\\]^_`{|}~\\s]|/))))" );
527 static thread_local QRegularExpression protoRegEx(
"^(?:f|ht)tps?://|file://" );
528 static thread_local QRegularExpression emailRegEx(
"([\\w._%+-]+@[\\w.-]+\\.[A-Za-z]+)" );
532 QRegularExpressionMatch match = urlRegEx.match( converted );
533 while ( match.hasMatch() )
536 QString url = match.captured( 1 );
537 QString protoUrl = url;
538 if ( !protoRegEx.match( protoUrl ).hasMatch() )
540 protoUrl.prepend(
"http://" );
542 QString anchor = QStringLiteral(
"<a href=\"%1\">%2</a>" ).arg( protoUrl.toHtmlEscaped(), url.toHtmlEscaped() );
543 converted.replace( match.capturedStart( 1 ), url.length(), anchor );
544 offset = match.capturedStart( 1 ) + anchor.length();
545 match = urlRegEx.match( converted, offset );
549 match = emailRegEx.match( converted );
550 while ( match.hasMatch() )
553 QString email = match.captured( 1 );
554 QString anchor = QStringLiteral(
"<a href=\"mailto:%1\">%1</a>" ).arg( email.toHtmlEscaped() );
555 converted.replace( match.capturedStart( 1 ), email.length(), anchor );
556 offset = match.capturedStart( 1 ) + anchor.length();
557 match = emailRegEx.match( converted, offset );
568 const thread_local QRegularExpression rxUrl(
"^(http|https|ftp|file)://\\S+$" );
569 return rxUrl.match(
string ).hasMatch();
575 QString converted = html;
576 converted.replace( QLatin1String(
"<br>" ), QLatin1String(
"\n" ) );
577 converted.replace( QLatin1String(
"<b>" ), QLatin1String(
"**" ) );
578 converted.replace( QLatin1String(
"</b>" ), QLatin1String(
"**" ) );
580 static thread_local QRegularExpression hrefRegEx(
"<a\\s+href\\s*=\\s*([^<>]*)\\s*>([^<>]*)</a>" );
583 QRegularExpressionMatch match = hrefRegEx.match( converted );
584 while ( match.hasMatch() )
586 QString url = match.captured( 1 ).replace( QLatin1String(
"\"" ), QString() );
587 url.replace(
'\'', QString() );
588 QString name = match.captured( 2 );
589 QString anchor = QStringLiteral(
"[%1](%2)" ).arg( name, url );
590 converted.replace( match.capturedStart(), match.capturedLength(), anchor );
591 offset = match.capturedStart() + anchor.length();
592 match = hrefRegEx.match( converted, offset );
598 QString
QgsStringUtils::wordWrap(
const QString &
string,
const int length,
const bool useMaxLineLength,
const QString &customDelimiter )
600 if (
string.isEmpty() || length == 0 )
604 QRegularExpression rx;
605 int delimiterLength = 0;
607 if ( !customDelimiter.isEmpty() )
609 rx.setPattern( QRegularExpression::escape( customDelimiter ) );
610 delimiterLength = customDelimiter.length();
615 rx.setPattern( QStringLiteral(
"[\\x{200B}\\s]" ) );
619 const QStringList lines =
string.split(
'\n' );
620 int strLength, strCurrent, strHit, lastHit;
622 for (
int i = 0; i < lines.size(); i++ )
624 const QString line = lines.at( i );
625 strLength = line.length();
626 if ( strLength <= length )
629 newstr.append( line );
630 if ( i < lines.size() - 1 )
631 newstr.append(
'\n' );
638 while ( strCurrent < strLength )
642 if ( useMaxLineLength )
645 strHit = ( strCurrent + length >= strLength ) ? -1 : line.lastIndexOf( rx, strCurrent + length );
646 if ( strHit == lastHit || strHit == -1 )
649 strHit = ( strCurrent + std::abs( length ) >= strLength ) ? -1 : line.indexOf( rx, strCurrent + std::abs( length ) );
655 strHit = ( strCurrent + std::abs( length ) >= strLength ) ? -1 : line.indexOf( rx, strCurrent + std::abs( length ) );
659 #if QT_VERSION < QT_VERSION_CHECK(5, 15, 2)
660 newstr.append( line.midRef( strCurrent, strHit - strCurrent ) );
662 newstr.append( QStringView {line} .mid( strCurrent, strHit - strCurrent ) );
664 newstr.append(
'\n' );
665 strCurrent = strHit + delimiterLength;
669 #if QT_VERSION < QT_VERSION_CHECK(5, 15, 2)
670 newstr.append( line.midRef( strCurrent ) );
672 newstr.append( QStringView {line}.mid( strCurrent ) );
674 strCurrent = strLength;
677 if ( i < lines.size() - 1 )
678 newstr.append(
'\n' );
686 string =
string.replace(
',', QChar( 65040 ) ).replace( QChar( 8229 ), QChar( 65072 ) );
687 string =
string.replace( QChar( 12289 ), QChar( 65041 ) ).replace( QChar( 12290 ), QChar( 65042 ) );
688 string =
string.replace(
':', QChar( 65043 ) ).replace(
';', QChar( 65044 ) );
689 string =
string.replace(
'!', QChar( 65045 ) ).replace(
'?', QChar( 65046 ) );
690 string =
string.replace( QChar( 12310 ), QChar( 65047 ) ).replace( QChar( 12311 ), QChar( 65048 ) );
691 string =
string.replace( QChar( 8230 ), QChar( 65049 ) );
692 string =
string.replace( QChar( 8212 ), QChar( 65073 ) ).replace( QChar( 8211 ), QChar( 65074 ) );
693 string =
string.replace(
'_', QChar( 65075 ) ).replace( QChar( 65103 ), QChar( 65076 ) );
694 string =
string.replace(
'(', QChar( 65077 ) ).replace(
')', QChar( 65078 ) );
695 string =
string.replace(
'{', QChar( 65079 ) ).replace(
'}', QChar( 65080 ) );
696 string =
string.replace(
'<', QChar( 65087 ) ).replace(
'>', QChar( 65088 ) );
697 string =
string.replace(
'[', QChar( 65095 ) ).replace(
']', QChar( 65096 ) );
698 string =
string.replace( QChar( 12308 ), QChar( 65081 ) ).replace( QChar( 12309 ), QChar( 65082 ) );
699 string =
string.replace( QChar( 12304 ), QChar( 65083 ) ).replace( QChar( 12305 ), QChar( 65084 ) );
700 string =
string.replace( QChar( 12298 ), QChar( 65085 ) ).replace( QChar( 12299 ), QChar( 65086 ) );
701 string =
string.replace( QChar( 12300 ), QChar( 65089 ) ).replace( QChar( 12301 ), QChar( 65090 ) );
702 string =
string.replace( QChar( 12302 ), QChar( 65091 ) ).replace( QChar( 12303 ), QChar( 65092 ) );
709 const QLatin1Char backslash(
'\\' );
710 const int count =
string.count();
713 escaped.reserve( count * 2 );
714 for (
int i = 0; i < count; i++ )
716 switch (
string.at( i ).toLatin1() )
732 escaped.append( backslash );
734 escaped.append(
string.at( i ) );
741 const int charactersToTruncate =
string.length() - maxLength;
742 if ( charactersToTruncate <= 0 )
746 const int truncateFrom =
string.length() / 2 - ( charactersToTruncate + 1 ) / 2;
748 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
749 return string.leftRef( truncateFrom ) + QString( QChar( 0x2026 ) ) +
string.midRef( truncateFrom + charactersToTruncate + 1 );
751 return QStringView(
string ).first( truncateFrom ) + QString( QChar( 0x2026 ) ) + QStringView(
string ).sliced( truncateFrom + charactersToTruncate + 1 );
757 , mReplacement( replacement )
758 , mCaseSensitive( caseSensitive )
759 , mWholeWordOnly( wholeWordOnly )
761 if ( mWholeWordOnly )
763 mRx.setPattern( QString(
"\\b%1\\b" ).arg( mMatch ) );
764 mRx.setPatternOptions( mCaseSensitive ? QRegularExpression::NoPatternOption : QRegularExpression::CaseInsensitiveOption );
770 QString result = input;
771 if ( !mWholeWordOnly )
773 return result.replace( mMatch, mReplacement, mCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive );
777 return result.replace( mRx, mReplacement );
784 map.insert( QStringLiteral(
"match" ), mMatch );
785 map.insert( QStringLiteral(
"replace" ), mReplacement );
786 map.insert( QStringLiteral(
"caseSensitive" ), mCaseSensitive ?
"1" :
"0" );
787 map.insert( QStringLiteral(
"wholeWord" ), mWholeWordOnly ?
"1" :
"0" );
794 properties.value( QStringLiteral(
"replace" ) ),
795 properties.value( QStringLiteral(
"caseSensitive" ), QStringLiteral(
"0" ) ) == QLatin1String(
"1" ),
796 properties.value( QStringLiteral(
"wholeWord" ), QStringLiteral(
"0" ) ) == QLatin1String(
"1" ) );
801 QString result = input;
802 const auto constMReplacements = mReplacements;
805 result = r.process( result );
812 const auto constMReplacements = mReplacements;
816 QDomElement propEl = doc.createElement( QStringLiteral(
"replacement" ) );
817 QgsStringMap::const_iterator it = props.constBegin();
818 for ( ; it != props.constEnd(); ++it )
820 propEl.setAttribute( it.key(), it.value() );
822 elem.appendChild( propEl );
828 mReplacements.clear();
829 QDomNodeList nodelist = elem.elementsByTagName( QStringLiteral(
"replacement" ) );
830 for (
int i = 0; i < nodelist.count(); i++ )
832 QDomElement replacementElem = nodelist.at( i ).toElement();
833 QDomNamedNodeMap nodeMap = replacementElem.attributes();
836 for (
int j = 0; j < nodeMap.count(); ++j )
838 props.insert( nodeMap.item( j ).nodeName(), nodeMap.item( j ).nodeValue() );
void readXml(const QDomElement &elem)
Reads the collection state from an XML element.
QString process(const QString &input) const
Processes a given input string, applying any valid replacements which should be made using QgsStringR...
void writeXml(QDomElement &elem, QDomDocument &doc) const
Writes the collection state to an XML element.
A representation of a single string replacement.
static QgsStringReplacement fromProperties(const QgsStringMap &properties)
Creates a new QgsStringReplacement from an encoded properties map.
QString process(const QString &input) const
Processes a given input string, applying any valid replacements which should be made.
QgsStringReplacement(const QString &match, const QString &replacement, bool caseSensitive=false, bool wholeWordOnly=false)
Constructor for QgsStringReplacement.
QgsStringMap properties() const
Returns a map of the replacement properties.
static int hammingDistance(const QString &string1, const QString &string2, bool caseSensitive=false)
Returns the Hamming distance between two strings.
static QString soundex(const QString &string)
Returns the Soundex representation of a string.
static int levenshteinDistance(const QString &string1, const QString &string2, bool caseSensitive=false)
Returns the Levenshtein edit distance between two strings.
static QString htmlToMarkdown(const QString &html)
Convert simple HTML to markdown.
static QString longestCommonSubstring(const QString &string1, const QString &string2, bool caseSensitive=false)
Returns the longest common substring between two strings.
static QString substituteVerticalCharacters(QString string)
Returns a string with characters having vertical representation form substituted.
static QString capitalize(const QString &string, Capitalization capitalization)
Converts a string by applying capitalization rules to the string.
static QString insertLinks(const QString &string, bool *foundLinks=nullptr)
Returns a string with any URL (e.g., http(s)/ftp) and mailto: text converted to valid HTML <a ....
static double fuzzyScore(const QString &candidate, const QString &search)
Tests a candidate string to see how likely it is a match for a specified search string.
Capitalization
Capitalization options.
@ UpperCamelCase
Convert the string to upper camel case. Note that this method does not unaccent characters.
@ MixedCase
Mixed case, ie no change.
@ AllLowercase
Convert all characters to lowercase.
@ TitleCase
Simple title case conversion - does not fully grammatically parse the text and uses simple rules only...
@ AllUppercase
Convert all characters to uppercase.
@ ForceFirstLetterToCapital
Convert just the first letter of each word to uppercase, leave the rest untouched.
static QString qRegExpEscape(const QString &string)
Returns an escaped string matching the behavior of QRegExp::escape.
static QString ampersandEncode(const QString &string)
Makes a raw string safe for inclusion as a HTML/XML string literal.
static QString wordWrap(const QString &string, int length, bool useMaxLineLength=true, const QString &customDelimiter=QString())
Automatically wraps a string by inserting new line characters at appropriate locations in the string.
static bool isUrl(const QString &string)
Returns whether the string is a URL (http,https,ftp,file)
static QString truncateMiddleOfString(const QString &string, int maxLength)
Truncates a string to the specified maximum character length.
QMap< QString, QString > QgsStringMap
#define FUZZY_SCORE_CONSECUTIVE_MATCH
#define FUZZY_SCORE_WORD_MATCH
#define FUZZY_SCORE_NEW_MATCH