[libcalamares] Add string functions for lstrip() and rstrip()-like
This commit is contained in:
parent
bb948c47dc
commit
bc2713ccbb
@ -224,5 +224,26 @@ truncateMultiLine( const QString& string, CalamaresUtils::LinesStartEnd lines, C
|
||||
return front + back.right( chars.total / 2 );
|
||||
}
|
||||
|
||||
void
|
||||
removeLeading( QString& string, QChar c )
|
||||
{
|
||||
int count = 0;
|
||||
while ( string.length() > count && string[ count ] == c )
|
||||
{
|
||||
count++;
|
||||
}
|
||||
string.remove( 0, count );
|
||||
}
|
||||
|
||||
void
|
||||
removeTrailing( QString& string, QChar c )
|
||||
{
|
||||
int lastIndex = string.length();
|
||||
while ( lastIndex > 0 && string[ lastIndex - 1 ] == c )
|
||||
{
|
||||
lastIndex--;
|
||||
}
|
||||
string.remove( lastIndex, string.length() );
|
||||
}
|
||||
|
||||
} // namespace CalamaresUtils
|
||||
|
@ -100,6 +100,19 @@ DLLEXPORT QString truncateMultiLine( const QString& string,
|
||||
LinesStartEnd lines = LinesStartEnd { 3, 5 },
|
||||
CharCount chars = CharCount { 812 } );
|
||||
|
||||
/** @brief Remove all @p c at the beginning of @p string
|
||||
*
|
||||
* Modifies the @p string in-place. If @p c is not the first character
|
||||
* of @p string, the string is left unchanged; otherwise the first character
|
||||
* is removed and the process repeats.
|
||||
*/
|
||||
DLLEXPORT void removeLeading( QString& string, QChar c );
|
||||
/** @brief Remove all @p c at the end of @p string
|
||||
*
|
||||
* Like removeLeading(), but at the end of the string.
|
||||
*/
|
||||
DLLEXPORT void removeTrailing( QString& string, QChar c );
|
||||
|
||||
} // namespace CalamaresUtils
|
||||
|
||||
#endif
|
||||
|
@ -70,6 +70,10 @@ private Q_SLOTS:
|
||||
void testStringTruncation();
|
||||
void testStringTruncationShorter();
|
||||
void testStringTruncationDegenerate();
|
||||
void testStringRemoveLeading_data();
|
||||
void testStringRemoveLeading();
|
||||
void testStringRemoveTrailing_data();
|
||||
void testStringRemoveTrailing();
|
||||
|
||||
/** @section Test Runner directory-manipulation. */
|
||||
void testRunnerDirs();
|
||||
@ -754,6 +758,64 @@ LibCalamaresTests::testStringTruncationDegenerate()
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
LibCalamaresTests::testStringRemoveLeading_data()
|
||||
{
|
||||
QTest::addColumn< QString >( "string" );
|
||||
QTest::addColumn< char >( "c" );
|
||||
QTest::addColumn< QString >( "result" );
|
||||
|
||||
QTest::newRow( "empty" ) << QString() << '/' << QString();
|
||||
QTest::newRow( "one-slash" ) << QStringLiteral( "/tmp" ) << '/' << QStringLiteral( "tmp" );
|
||||
QTest::newRow( "two-slash" ) << QStringLiteral( "//tmp" ) << '/' << QStringLiteral( "tmp" );
|
||||
QTest::newRow( "multi-slash" ) << QStringLiteral( "/tmp/p" ) << '/' << QStringLiteral( "tmp/p" );
|
||||
QTest::newRow( "later-slash" ) << QStringLiteral( "@/tmp" ) << '/' << QStringLiteral( "@/tmp" );
|
||||
QTest::newRow( "all-one-slash" ) << QStringLiteral( "/" ) << '/' << QString();
|
||||
QTest::newRow( "all-many-slash" ) << QStringLiteral( "////////////////////" ) << '/' << QString();
|
||||
QTest::newRow( "trailing" ) << QStringLiteral( "tmp/" ) << '/' << QStringLiteral( "tmp/" );
|
||||
}
|
||||
|
||||
void
|
||||
LibCalamaresTests::testStringRemoveLeading()
|
||||
{
|
||||
QFETCH( QString, string );
|
||||
QFETCH( char, c );
|
||||
QFETCH( QString, result );
|
||||
|
||||
const QString initial = string;
|
||||
CalamaresUtils::removeLeading( string, c );
|
||||
QCOMPARE( string, result );
|
||||
}
|
||||
|
||||
void
|
||||
LibCalamaresTests::testStringRemoveTrailing_data()
|
||||
{
|
||||
QTest::addColumn< QString >( "string" );
|
||||
QTest::addColumn< char >( "c" );
|
||||
QTest::addColumn< QString >( "result" );
|
||||
|
||||
QTest::newRow( "empty" ) << QString() << '/' << QString();
|
||||
QTest::newRow( "one-slash" ) << QStringLiteral( "/tmp" ) << '/' << QStringLiteral( "/tmp" );
|
||||
QTest::newRow( "two-slash" ) << QStringLiteral( "//tmp" ) << '/' << QStringLiteral( "//tmp" );
|
||||
QTest::newRow( "multi-slash" ) << QStringLiteral( "/tmp//p/" ) << '/' << QStringLiteral( "/tmp//p" );
|
||||
QTest::newRow( "later-slash" ) << QStringLiteral( "@/tmp/@" ) << '/' << QStringLiteral( "@/tmp/@" );
|
||||
QTest::newRow( "later-slash2" ) << QStringLiteral( "@/tmp/@//" ) << '/' << QStringLiteral( "@/tmp/@" );
|
||||
QTest::newRow( "all-one-slash" ) << QStringLiteral( "/" ) << '/' << QString();
|
||||
QTest::newRow( "all-many-slash" ) << QStringLiteral( "////////////////////" ) << '/' << QString();
|
||||
QTest::newRow( "trailing" ) << QStringLiteral( "tmp/" ) << '/' << QStringLiteral( "tmp" );
|
||||
}
|
||||
|
||||
void
|
||||
LibCalamaresTests::testStringRemoveTrailing()
|
||||
{
|
||||
QFETCH( QString, string );
|
||||
QFETCH( char, c );
|
||||
QFETCH( QString, result );
|
||||
|
||||
const QString initial = string;
|
||||
CalamaresUtils::removeTrailing( string, c );
|
||||
QCOMPARE( string, result );
|
||||
}
|
||||
|
||||
static QString
|
||||
dirname( const QTemporaryDir& d )
|
||||
@ -1001,7 +1063,7 @@ LibCalamaresTests::testReadWriteFile()
|
||||
{
|
||||
auto fullPath = ss->createTargetFile( "test0", otherContents );
|
||||
QVERIFY( !fullPath ); // Failed, because it won't overwrite
|
||||
QCOMPARE( fullPath.code(), decltype(fullPath)::Code::AlreadyExists );
|
||||
QCOMPARE( fullPath.code(), decltype( fullPath )::Code::AlreadyExists );
|
||||
QVERIFY( fullPath.path().isEmpty() ); // Because it wasn't written
|
||||
|
||||
QFileInfo fi( tempRoot.filePath( "test0" ) ); // Compute the name some other way
|
||||
|
Loading…
Reference in New Issue
Block a user