]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Add Args::erase_last method
authorJoel Rosdahl <joel@rosdahl.net>
Mon, 2 Nov 2020 06:50:13 +0000 (07:50 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Tue, 3 Nov 2020 18:57:04 +0000 (19:57 +0100)
src/Args.cpp
src/Args.hpp
unittest/test_Args.cpp

index 92a1e41ba1cf249aefebe8ad7ed015fdeca09188..5270989d7dadf80ce94b01e867db432f0c092143 100644 (file)
@@ -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)
 {
index be917ec75dd4dc735d51455ff23dbaa1400550aa..ae428096a56b3643668fd57102c9e8ac8d184f16 100644 (file)
@@ -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);
 
index 8c2b9cd94a82e91226a570b13aec8b8cc496a5c9..7d21b3dc48f4b34d81ba568aa2746320e46ef2b8 100644 (file)
@@ -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");