20 #include <QStringList>
21 #include <QTextBoundaryFinder>
22 #include <QRegularExpression>
27 if (
string.isEmpty() )
30 switch ( capitalization )
36 return string.toUpper();
39 return string.toLower();
43 QString temp = string;
45 QTextBoundaryFinder wordSplitter( QTextBoundaryFinder::Word,
string.constData(),
string.length(),
nullptr, 0 );
46 QTextBoundaryFinder letterSplitter( QTextBoundaryFinder::Grapheme,
string.constData(),
string.length(),
nullptr, 0 );
48 wordSplitter.setPosition( 0 );
50 while ( ( first && wordSplitter.boundaryReasons() & QTextBoundaryFinder::StartOfItem )
51 || wordSplitter.toNextBoundary() >= 0 )
54 letterSplitter.setPosition( wordSplitter.position() );
55 letterSplitter.toNextBoundary();
56 QString substr =
string.mid( wordSplitter.position(), letterSplitter.position() - wordSplitter.position() );
57 temp.replace( wordSplitter.position(), substr.length(), substr.toUpper() );
66 static QStringList smallWords;
67 static QStringList newPhraseSeparators;
68 static QRegularExpression splitWords;
69 if ( smallWords.empty() )
71 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(
'|' );
72 newPhraseSeparators = QObject::tr(
".|:" ).split(
'|' );
73 splitWords = QRegularExpression( QStringLiteral(
"\\b" ), QRegularExpression::UseUnicodePropertiesOption );
76 const bool allSameCase =
string.toLower() ==
string ||
string.toUpper() == string;
77 #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
78 const QStringList parts = ( allSameCase ?
string.toLower() : string ).split( splitWords, QString::SkipEmptyParts );
80 const QStringList parts = ( allSameCase ?
string.toLower() : string ).split( splitWords, Qt::SkipEmptyParts );
83 bool firstWord =
true;
85 int lastWord = parts.count() - 1;
86 for (
const QString &word : qgis::as_const( parts ) )
88 if ( newPhraseSeparators.contains( word.trimmed() ) )
93 else if ( firstWord || ( i == lastWord ) || !smallWords.contains( word ) )
95 result += word.at( 0 ).toUpper() + word.mid( 1 );
109 result.remove(
' ' );
120 for (
int i = 0; i <
string.size(); ++i )
122 QChar ch =
string.at( i );
123 if ( ch.unicode() > 160 )
124 encoded += QStringLiteral(
"&#%1;" ).arg(
static_cast< int >( ch.unicode() ) );
125 else if ( ch.unicode() == 38 )
126 encoded += QLatin1String(
"&" );
127 else if ( ch.unicode() == 60 )
128 encoded += QLatin1String(
"<" );
129 else if ( ch.unicode() == 62 )
130 encoded += QLatin1String(
">" );
139 int length1 = string1.length();
140 int length2 = string2.length();
143 if ( string1.isEmpty() )
147 else if ( string2.isEmpty() )
153 QString s1( caseSensitive ? string1 : string1.toLower() );
154 QString s2( caseSensitive ? string2 : string2.toLower() );
156 const QChar *s1Char = s1.constData();
157 const QChar *s2Char = s2.constData();
160 int commonPrefixLen = 0;
161 while ( length1 > 0 && length2 > 0 && *s1Char == *s2Char )
171 while ( length1 > 0 && length2 > 0 && s1.at( commonPrefixLen + length1 - 1 ) == s2.at( commonPrefixLen + length2 - 1 ) )
182 else if ( length2 == 0 )
188 if ( length1 > length2 )
191 std::swap( length1, length2 );
196 col.fill( 0, length2 + 1 );
197 QVector< int > prevCol;
198 prevCol.reserve( length2 + 1 );
199 for (
int i = 0; i < length2 + 1; ++i )
203 const QChar *s2start = s2Char;
204 for (
int i = 0; i < length1; ++i )
208 for (
int j = 0; j < length2; ++j )
210 col[j + 1] = std::min( std::min( 1 + col[j], 1 + prevCol[1 + j] ), prevCol[j] + ( ( *s1Char == *s2Char ) ? 0 : 1 ) );
216 return prevCol[length2];
221 if ( string1.isEmpty() || string2.isEmpty() )
228 QString s1( caseSensitive ? string1 : string1.toLower() );
229 QString s2( caseSensitive ? string2 : string2.toLower() );
237 int *currentScores =
new int [ s2.length()];
238 int *previousScores =
new int [ s2.length()];
239 int maxCommonLength = 0;
240 int lastMaxBeginIndex = 0;
242 const QChar *s1Char = s1.constData();
243 const QChar *s2Char = s2.constData();
244 const QChar *s2Start = s2Char;
246 for (
int i = 0; i < s1.length(); ++i )
248 for (
int j = 0; j < s2.length(); ++j )
250 if ( *s1Char != *s2Char )
252 currentScores[j] = 0;
256 if ( i == 0 || j == 0 )
258 currentScores[j] = 1;
262 currentScores[j] = 1 + previousScores[j - 1];
265 if ( maxCommonLength < currentScores[j] )
267 maxCommonLength = currentScores[j];
268 lastMaxBeginIndex = i;
273 std::swap( currentScores, previousScores );
277 delete [] currentScores;
278 delete [] previousScores;
279 return string1.mid( lastMaxBeginIndex - maxCommonLength + 1, maxCommonLength );
284 if ( string1.isEmpty() && string2.isEmpty() )
290 if ( string1.length() != string2.length() )
297 QString s1( caseSensitive ? string1 : string1.toLower() );
298 QString s2( caseSensitive ? string2 : string2.toLower() );
307 const QChar *s1Char = s1.constData();
308 const QChar *s2Char = s2.constData();
310 for (
int i = 0; i < string1.length(); ++i )
312 if ( *s1Char != *s2Char )
323 if (
string.isEmpty() )
326 QString tmp =
string.toUpper();
329 QChar *char1 = tmp.data();
330 QChar *char2 = tmp.data();
332 for (
int i = 0; i < tmp.length(); ++i, ++char2 )
334 if ( ( *char2 ).unicode() >= 0x41 && ( *char2 ).unicode() <= 0x5A && ( i == 0 || ( ( *char2 ).unicode() != 0x41 && ( *char2 ).unicode() != 0x45
335 && ( *char2 ).unicode() != 0x48 && ( *char2 ).unicode() != 0x49
336 && ( *char2 ).unicode() != 0x4F && ( *char2 ).unicode() != 0x55
337 && ( *char2 ).unicode() != 0x57 && ( *char2 ).unicode() != 0x59 ) ) )
344 tmp.truncate( outLen );
346 QChar *tmpChar = tmp.data();
348 for (
int i = 1; i < tmp.length(); ++i, ++tmpChar )
350 switch ( ( *tmpChar ).unicode() )
356 tmp.replace( i, 1, QChar( 0x31 ) );
367 tmp.replace( i, 1, QChar( 0x32 ) );
372 tmp.replace( i, 1, QChar( 0x33 ) );
376 tmp.replace( i, 1, QChar( 0x34 ) );
381 tmp.replace( i, 1, QChar( 0x35 ) );
385 tmp.replace( i, 1, QChar( 0x36 ) );
395 for (
int i = 1; i < tmp.length(); ++i, ++char2 )
397 if ( *char2 != *char1 )
406 tmp.truncate( outLen );
407 if ( tmp.length() < 4 )
419 QString candidateNormalized = candidate.simplified().normalized( QString:: NormalizationForm_C ).toLower();
420 QString searchNormalized = search.simplified().normalized( QString:: NormalizationForm_C ).toLower();
422 int candidateLength = candidateNormalized.length();
423 int searchLength = searchNormalized.length();
427 if ( candidateLength == 0 || searchLength == 0 )
430 int candidateIdx = 0;
435 bool isPreviousIndexMatching =
false;
436 bool isWordOpen =
true;
439 while ( candidateIdx < candidateLength )
441 QChar candidateChar = candidateNormalized[ candidateIdx++ ];
442 bool isCandidateCharWordEnd = candidateChar ==
' ' || candidateChar.isPunct();
445 if ( candidateIdx == 1 )
448 else if ( isCandidateCharWordEnd )
455 if ( searchIdx >= searchLength )
458 QChar searchChar = searchNormalized[ searchIdx ];
459 bool isSearchCharWordEnd = searchChar ==
' ' || searchChar.isPunct();
462 if ( candidateChar == searchChar || ( isCandidateCharWordEnd && isSearchCharWordEnd ) )
467 if ( isSearchCharWordEnd )
471 else if ( isPreviousIndexMatching )
479 else if ( isPreviousIndexMatching )
489 isPreviousIndexMatching =
true;
494 isPreviousIndexMatching =
false;
499 if ( searchIdx >= searchLength )
501 bool isEndOfWord = ( candidateIdx >= candidateLength )
503 : candidateNormalized[candidateIdx] ==
' ' || candidateNormalized[candidateIdx].isPunct();
514 if ( searchIdx < searchLength )
517 return static_cast<float>( std::max( score, 0 ) ) / std::max( maxScore, 1 );
523 QString converted = string;
527 static QRegExp urlRegEx(
"(\\b(([\\w-]+://?|www[.])[^\\s()<>]+(?:\\([\\w\\d]+\\)|([^!\"#$%&'()*+,\\-./:;<=>?@[\\\\\\]^_`{|}~\\s]|/))))" );
528 static QRegExp protoRegEx(
"^(?:f|ht)tps?://|file://" );
529 static QRegExp emailRegEx(
"([\\w._%+-]+@[\\w.-]+\\.[A-Za-z]+)" );
533 while ( urlRegEx.indexIn( converted, offset ) != -1 )
536 QString url = urlRegEx.cap( 1 );
537 QString protoUrl = url;
538 if ( protoRegEx.indexIn( protoUrl ) == -1 )
540 protoUrl.prepend(
"http://" );
542 QString anchor = QStringLiteral(
"<a href=\"%1\">%2</a>" ).arg( protoUrl.toHtmlEscaped(), url.toHtmlEscaped() );
543 converted.replace( urlRegEx.pos( 1 ), url.length(), anchor );
544 offset = urlRegEx.pos( 1 ) + anchor.length();
547 while ( emailRegEx.indexIn( converted, offset ) != -1 )
550 QString email = emailRegEx.cap( 1 );
551 QString anchor = QStringLiteral(
"<a href=\"mailto:%1\">%1</a>" ).arg( email.toHtmlEscaped() );
552 converted.replace( emailRegEx.pos( 1 ), email.length(), anchor );
553 offset = emailRegEx.pos( 1 ) + anchor.length();
565 QString converted = html;
566 converted.replace( QLatin1String(
"<br>" ), QLatin1String(
"\n" ) );
567 converted.replace( QLatin1String(
"<b>" ), QLatin1String(
"**" ) );
568 converted.replace( QLatin1String(
"</b>" ), QLatin1String(
"**" ) );
570 static QRegExp hrefRegEx(
"<a\\s+href\\s*=\\s*([^<>]*)\\s*>([^<>]*)</a>" );
572 while ( hrefRegEx.indexIn( converted, offset ) != -1 )
574 QString url = hrefRegEx.cap( 1 ).replace( QLatin1String(
"\"" ), QString() );
575 url.replace(
'\'', QString() );
576 QString name = hrefRegEx.cap( 2 );
577 QString anchor = QStringLiteral(
"[%1](%2)" ).arg( name, url );
578 converted.replace( hrefRegEx, anchor );
579 offset = hrefRegEx.pos( 1 ) + anchor.length();
585 QString
QgsStringUtils::wordWrap(
const QString &
string,
const int length,
const bool useMaxLineLength,
const QString &customDelimiter )
587 if (
string.isEmpty() || length == 0 )
592 int delimiterLength = 0;
594 if ( !customDelimiter.isEmpty() )
596 rx.setPatternSyntax( QRegExp::FixedString );
597 rx.setPattern( customDelimiter );
598 delimiterLength = customDelimiter.length();
603 rx.setPattern( QStringLiteral(
"[\\s\\x200B]" ) );
607 const QStringList lines =
string.split(
'\n' );
608 int strLength, strCurrent, strHit, lastHit;
610 for (
int i = 0; i < lines.size(); i++ )
612 strLength = lines.at( i ).length();
617 while ( strCurrent < strLength )
621 if ( useMaxLineLength )
624 strHit = lines.at( i ).lastIndexOf( rx, strCurrent + length );
625 if ( strHit == lastHit || strHit == -1 )
628 strHit = lines.at( i ).indexOf( rx, strCurrent + std::abs( length ) );
634 strHit = lines.at( i ).indexOf( rx, strCurrent + std::abs( length ) );
638 newstr.append( lines.at( i ).midRef( strCurrent, strHit - strCurrent ) );
639 newstr.append(
'\n' );
640 strCurrent = strHit + delimiterLength;
644 newstr.append( lines.at( i ).midRef( strCurrent ) );
645 strCurrent = strLength;
648 if ( i < lines.size() - 1 )
649 newstr.append(
'\n' );
657 string =
string.replace(
',', QChar( 65040 ) ).replace( QChar( 8229 ), QChar( 65072 ) );
658 string =
string.replace( QChar( 12289 ), QChar( 65041 ) ).replace( QChar( 12290 ), QChar( 65042 ) );
659 string =
string.replace(
':', QChar( 65043 ) ).replace(
';', QChar( 65044 ) );
660 string =
string.replace(
'!', QChar( 65045 ) ).replace(
'?', QChar( 65046 ) );
661 string =
string.replace( QChar( 12310 ), QChar( 65047 ) ).replace( QChar( 12311 ), QChar( 65048 ) );
662 string =
string.replace( QChar( 8230 ), QChar( 65049 ) );
663 string =
string.replace( QChar( 8212 ), QChar( 65073 ) ).replace( QChar( 8211 ), QChar( 65074 ) );
664 string =
string.replace(
'_', QChar( 65075 ) ).replace( QChar( 65103 ), QChar( 65076 ) );
665 string =
string.replace(
'(', QChar( 65077 ) ).replace(
')', QChar( 65078 ) );
666 string =
string.replace(
'{', QChar( 65079 ) ).replace(
'}', QChar( 65080 ) );
667 string =
string.replace(
'<', QChar( 65087 ) ).replace(
'>', QChar( 65088 ) );
668 string =
string.replace(
'[', QChar( 65095 ) ).replace(
']', QChar( 65096 ) );
669 string =
string.replace( QChar( 12308 ), QChar( 65081 ) ).replace( QChar( 12309 ), QChar( 65082 ) );
670 string =
string.replace( QChar( 12304 ), QChar( 65083 ) ).replace( QChar( 12305 ), QChar( 65084 ) );
671 string =
string.replace( QChar( 12298 ), QChar( 65085 ) ).replace( QChar( 12299 ), QChar( 65086 ) );
672 string =
string.replace( QChar( 12300 ), QChar( 65089 ) ).replace( QChar( 12301 ), QChar( 65090 ) );
673 string =
string.replace( QChar( 12302 ), QChar( 65091 ) ).replace( QChar( 12303 ), QChar( 65092 ) );
679 , mReplacement( replacement )
680 , mCaseSensitive( caseSensitive )
681 , mWholeWordOnly( wholeWordOnly )
683 if ( mWholeWordOnly )
684 mRx = QRegExp( QString(
"\\b%1\\b" ).arg( mMatch ),
685 mCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive );
690 QString result = input;
691 if ( !mWholeWordOnly )
693 return result.replace( mMatch, mReplacement, mCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive );
697 return result.replace( mRx, mReplacement );
704 map.insert( QStringLiteral(
"match" ), mMatch );
705 map.insert( QStringLiteral(
"replace" ), mReplacement );
706 map.insert( QStringLiteral(
"caseSensitive" ), mCaseSensitive ?
"1" :
"0" );
707 map.insert( QStringLiteral(
"wholeWord" ), mWholeWordOnly ?
"1" :
"0" );
714 properties.value( QStringLiteral(
"replace" ) ),
715 properties.value( QStringLiteral(
"caseSensitive" ), QStringLiteral(
"0" ) ) == QLatin1String(
"1" ),
716 properties.value( QStringLiteral(
"wholeWord" ), QStringLiteral(
"0" ) ) == QLatin1String(
"1" ) );
721 QString result = input;
722 const auto constMReplacements = mReplacements;
725 result = r.process( result );
732 const auto constMReplacements = mReplacements;
736 QDomElement propEl = doc.createElement( QStringLiteral(
"replacement" ) );
737 QgsStringMap::const_iterator it = props.constBegin();
738 for ( ; it != props.constEnd(); ++it )
740 propEl.setAttribute( it.key(), it.value() );
742 elem.appendChild( propEl );
748 mReplacements.clear();
749 QDomNodeList nodelist = elem.elementsByTagName( QStringLiteral(
"replacement" ) );
750 for (
int i = 0; i < nodelist.count(); i++ )
752 QDomElement replacementElem = nodelist.at( i ).toElement();
753 QDomNamedNodeMap nodeMap = replacementElem.attributes();
756 for (
int j = 0; j < nodeMap.count(); ++j )
758 props.insert( nodeMap.item( j ).nodeName(), nodeMap.item( j ).nodeValue() );