Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add shortcut path for string word wrapping
  • Loading branch information
nyalldawson committed Oct 20, 2021
1 parent 320260a commit 37fec6a
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/core/qgsstringutils.cpp
Expand Up @@ -621,7 +621,16 @@ QString QgsStringUtils::wordWrap( const QString &string, const int length, const

for ( int i = 0; i < lines.size(); i++ )
{
strLength = lines.at( i ).length();
const QString line = lines.at( i );
strLength = line.length();
if ( strLength <= length )
{
// shortcut, no wrapping required
newstr.append( line );
if ( i < lines.size() - 1 )
newstr.append( '\n' );
continue;
}
strCurrent = 0;
strHit = 0;
lastHit = 0;
Expand All @@ -633,34 +642,34 @@ QString QgsStringUtils::wordWrap( const QString &string, const int length, const
if ( useMaxLineLength )
{
//first try to locate delimiter backwards
strHit = ( strCurrent + length >= strLength ) ? -1 : lines.at( i ).lastIndexOf( rx, strCurrent + length );
strHit = ( strCurrent + length >= strLength ) ? -1 : line.lastIndexOf( rx, strCurrent + length );
if ( strHit == lastHit || strHit == -1 )
{
//if no new backward delimiter found, try to locate forward
strHit = ( strCurrent + std::abs( length ) >= strLength ) ? -1 : lines.at( i ).indexOf( rx, strCurrent + std::abs( length ) );
strHit = ( strCurrent + std::abs( length ) >= strLength ) ? -1 : line.indexOf( rx, strCurrent + std::abs( length ) );
}
lastHit = strHit;
}
else
{
strHit = ( strCurrent + std::abs( length ) >= strLength ) ? -1 : lines.at( i ).indexOf( rx, strCurrent + std::abs( length ) );
strHit = ( strCurrent + std::abs( length ) >= strLength ) ? -1 : line.indexOf( rx, strCurrent + std::abs( length ) );
}
if ( strHit > -1 )
{
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 2)
newstr.append( lines.at( i ).midRef( strCurrent, strHit - strCurrent ) );
newstr.append( line.midRef( strCurrent, strHit - strCurrent ) );
#else
newstr.append( QStringView {lines.at( i )} .mid( strCurrent, strHit - strCurrent ) );
newstr.append( QStringView {line} .mid( strCurrent, strHit - strCurrent ) );
#endif
newstr.append( '\n' );
strCurrent = strHit + delimiterLength;
}
else
{
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 2)
newstr.append( lines.at( i ).midRef( strCurrent ) );
newstr.append( line.midRef( strCurrent ) );
#else
newstr.append( QStringView {lines.at( i )}.mid( strCurrent ) );
newstr.append( QStringView {line}.mid( strCurrent ) );
#endif
strCurrent = strLength;
}
Expand Down

0 comments on commit 37fec6a

Please sign in to comment.