[libcalamares] Fix up smart-string-truncation
- off-by-one when source ends with a newline - lastNewLine was being calculated as a left-index into the string, then used as a count-from-right
This commit is contained in:
parent
3be360e433
commit
b144d81979
@ -15,6 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "String.h"
|
#include "String.h"
|
||||||
|
#include "Logger.h"
|
||||||
|
|
||||||
#include <QStringList>
|
#include <QStringList>
|
||||||
|
|
||||||
@ -138,7 +139,7 @@ truncateMultiLine( const QString& string, CalamaresUtils::LinesStartEnd lines, C
|
|||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString shorter = string.simplified();
|
QString shorter = string;
|
||||||
QString front, back;
|
QString front, back;
|
||||||
if ( shorter.count( '\n' ) >= maxLines )
|
if ( shorter.count( '\n' ) >= maxLines )
|
||||||
{
|
{
|
||||||
@ -154,17 +155,17 @@ truncateMultiLine( const QString& string, CalamaresUtils::LinesStartEnd lines, C
|
|||||||
}
|
}
|
||||||
if ( from > 0 )
|
if ( from > 0 )
|
||||||
{
|
{
|
||||||
front = shorter.left( from );
|
front = shorter.left( from + 1 );
|
||||||
}
|
}
|
||||||
|
|
||||||
int lastNewLine = -1;
|
int lastNewLine = -1;
|
||||||
int lastCount = 0;
|
int lastCount = shorter.endsWith( '\n' ) ? -1 : 0;
|
||||||
for ( auto i = shorter.rbegin(); i != shorter.rend() && lastCount < lines.atEnd; ++i )
|
for ( auto i = shorter.rbegin(); i != shorter.rend() && lastCount < lines.atEnd; ++i )
|
||||||
{
|
{
|
||||||
if ( *i == '\n' )
|
if ( *i == '\n' )
|
||||||
{
|
{
|
||||||
++lastCount;
|
++lastCount;
|
||||||
lastNewLine = shorter.length() - int( i - shorter.rbegin() );
|
lastNewLine = int( i - shorter.rbegin() );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ( ( lastNewLine >= 0 ) && ( lastCount >= lines.atEnd ) )
|
if ( ( lastNewLine >= 0 ) && ( lastCount >= lines.atEnd ) )
|
||||||
|
@ -501,6 +501,8 @@ strings: [ aap, noot, mies ]
|
|||||||
void
|
void
|
||||||
LibCalamaresTests::testStringTruncation()
|
LibCalamaresTests::testStringTruncation()
|
||||||
{
|
{
|
||||||
|
Logger::setupLogLevel( Logger::LOGDEBUG );
|
||||||
|
|
||||||
using namespace CalamaresUtils;
|
using namespace CalamaresUtils;
|
||||||
|
|
||||||
const QString longString( R"(---
|
const QString longString( R"(---
|
||||||
@ -546,14 +548,18 @@ LibCalamaresTests::testStringTruncation()
|
|||||||
// Lines at the start
|
// Lines at the start
|
||||||
{
|
{
|
||||||
auto s = truncateMultiLine( longString, LinesStartEnd { 4, 0 }, CharCount { sufficientLength } );
|
auto s = truncateMultiLine( longString, LinesStartEnd { 4, 0 }, CharCount { sufficientLength } );
|
||||||
|
QVERIFY( s.length() > 1 );
|
||||||
QVERIFY( longString.startsWith( s ) );
|
QVERIFY( longString.startsWith( s ) );
|
||||||
|
cDebug() << "Result-line" << Logger::Quote << s;
|
||||||
QCOMPARE( s.count( '\n' ), 4 );
|
QCOMPARE( s.count( '\n' ), 4 );
|
||||||
}
|
}
|
||||||
|
|
||||||
// Lines at the end
|
// Lines at the end
|
||||||
{
|
{
|
||||||
auto s = truncateMultiLine( longString, LinesStartEnd { 0, 4 }, CharCount { sufficientLength } );
|
auto s = truncateMultiLine( longString, LinesStartEnd { 0, 4 }, CharCount { sufficientLength } );
|
||||||
|
QVERIFY( s.length() > 1 );
|
||||||
QVERIFY( longString.endsWith( s ) );
|
QVERIFY( longString.endsWith( s ) );
|
||||||
|
cDebug() << "Result-line" << Logger::Quote << s;
|
||||||
QCOMPARE( s.count( '\n' ), 4 );
|
QCOMPARE( s.count( '\n' ), 4 );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user