From bc2713ccbbb857ece8329b1b0eb0ef68e17e4d50 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 8 Dec 2021 14:08:37 +0100 Subject: [PATCH] [libcalamares] Add string functions for lstrip() and rstrip()-like --- src/libcalamares/utils/String.cpp | 21 ++++++++++ src/libcalamares/utils/String.h | 13 ++++++ src/libcalamares/utils/Tests.cpp | 68 +++++++++++++++++++++++++++++-- 3 files changed, 99 insertions(+), 3 deletions(-) diff --git a/src/libcalamares/utils/String.cpp b/src/libcalamares/utils/String.cpp index 615d30309..c88c23693 100644 --- a/src/libcalamares/utils/String.cpp +++ b/src/libcalamares/utils/String.cpp @@ -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 diff --git a/src/libcalamares/utils/String.h b/src/libcalamares/utils/String.h index e08255f86..1adc2336a 100644 --- a/src/libcalamares/utils/String.h +++ b/src/libcalamares/utils/String.h @@ -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 diff --git a/src/libcalamares/utils/Tests.cpp b/src/libcalamares/utils/Tests.cpp index 8fd11cd87..3dde75338 100644 --- a/src/libcalamares/utils/Tests.cpp +++ b/src/libcalamares/utils/Tests.cpp @@ -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,10 +1063,10 @@ LibCalamaresTests::testReadWriteFile() { auto fullPath = ss->createTargetFile( "test0", otherContents ); QVERIFY( !fullPath ); // Failed, because it won't overwrite - QCOMPARE( fullPath.code(), decltype(fullPath)::Code::AlreadyExists ); - QVERIFY( fullPath.path().isEmpty() ); // Because it wasn't written + 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 + QFileInfo fi( tempRoot.filePath( "test0" ) ); // Compute the name some other way QVERIFY( fi.exists() ); QVERIFY( fi.isFile() ); QCOMPARE( fi.size(), 0 );