cpp-common/bt2c: add `CStringView::startsWith`
authorSimon Marchi <simon.marchi@efficios.com>
Wed, 6 Mar 2024 18:14:11 +0000 (13:14 -0500)
committerSimon Marchi <simon.marchi@efficios.com>
Tue, 26 Mar 2024 18:56:36 +0000 (14:56 -0400)
Change-Id: I4a39561085850c06adc1f6e36466fba9a2e6dfa0
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/12011
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
src/cpp-common/bt2c/c-string-view.hpp
tests/cpp-common/test-c-string-view.cpp

index 3dca0299da4babf6495d26b88905126feb7104ab..0437cd65c4d87458ebe8eca1029d74e728201649 100644 (file)
@@ -176,6 +176,13 @@ public:
         return _mStr[i];
     }
 
+    bool startsWith(const bt2c::CStringView prefix) const noexcept
+    {
+        BT_ASSERT_DBG(_mStr);
+        BT_ASSERT_DBG(prefix);
+        return std::strncmp(_mStr, prefix, prefix.len()) == 0;
+    }
+
 private:
     const char *_mStr = nullptr;
 };
index 97eaf6dcf1349d2da1a8edc38dbb26bdd4d753c0..70e624d2cc8210e740bc952facfb7087089c90d6 100644 (file)
@@ -74,11 +74,26 @@ void testEquality()
     testNe(foo1, bt2c::CStringView {bar});
 }
 
+void testStartsWith()
+{
+    ok(bt2c::CStringView {"Moutarde choux"}.startsWith("Moutarde"),
+       "\"Moutarde Choux\" starts with \"Moutarde\"");
+    ok(!bt2c::CStringView {"Moutarde choux"}.startsWith("Choux"),
+       "\"Moutarde Choux\" does not start with \"Choux\"");
+    ok(bt2c::CStringView {"Moutarde choux"}.startsWith(""), "\"Moutarde Choux\" starts with \"\"");
+    ok(bt2c::CStringView {"Moutarde choux"}.startsWith("Moutarde choux"),
+       "\"Moutarde Choux\" starts with \"Moutarde choux\"");
+    ok(!bt2c::CStringView {"Moutarde"}.startsWith("Moutarde choux"),
+       "\"Moutarde\" does not start with \"Moutarde choux\"");
+    ok(bt2c::CStringView {""}.startsWith(""), "\"\" starts with \"\"");
+}
+
 } /* namespace */
 
 int main()
 {
-    plan_tests(10);
+    plan_tests(16);
     testEquality();
+    testStartsWith();
     return exit_status();
 }
This page took 0.026469 seconds and 4 git commands to generate.