From: Joel Rosdahl Date: Mon, 2 Nov 2020 06:50:13 +0000 (+0100) Subject: Add Args::erase_last method X-Git-Tag: v4.1~34 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b62b3d735c27a0f47fe1cdaca263df7ee7983daf;p=thirdparty%2Fccache.git Add Args::erase_last method --- diff --git a/src/Args.cpp b/src/Args.cpp index 92a1e41ba..5270989d7 100644 --- a/src/Args.cpp +++ b/src/Args.cpp @@ -155,6 +155,15 @@ Args::to_string() const return result; } +void +Args::erase_last(string_view arg) +{ + const auto it = std::find(m_args.rbegin(), m_args.rend(), arg); + if (it != m_args.rend()) { + m_args.erase(std::next(it).base()); + } +} + void Args::erase_with_prefix(string_view prefix) { diff --git a/src/Args.hpp b/src/Args.hpp index be917ec75..ae428096a 100644 --- a/src/Args.hpp +++ b/src/Args.hpp @@ -60,6 +60,9 @@ public: // in arguments is performed. std::string to_string() const; + // Remove last argument equal to `arg`, if any. + void erase_last(nonstd::string_view arg); + // Remove all arguments with prefix `prefix`. void erase_with_prefix(nonstd::string_view prefix); diff --git a/unittest/test_Args.cpp b/unittest/test_Args.cpp index 8c2b9cd94..7d21b3dc4 100644 --- a/unittest/test_Args.cpp +++ b/unittest/test_Args.cpp @@ -226,6 +226,23 @@ TEST_CASE("Args operations") Args more_args = Args::from_string("x y"); Args no_args; + SUBCASE("erase_last") + { + Args repeated_args = Args::from_string("one two twotwo one two twotwo"); + + repeated_args.erase_last("three"); + CHECK(repeated_args == Args::from_string("one two twotwo one two twotwo")); + + repeated_args.erase_last("two"); + CHECK(repeated_args == Args::from_string("one two twotwo one twotwo")); + + repeated_args.erase_last("two"); + CHECK(repeated_args == Args::from_string("one twotwo one twotwo")); + + repeated_args.erase_last("two"); + CHECK(repeated_args == Args::from_string("one twotwo one twotwo")); + } + SUBCASE("erase_with_prefix") { args.erase_with_prefix("m");