From 7fa9188df86ab48882f4cd8df7c1c8b1510e1c2a Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Tue, 17 Dec 2013 12:19:29 +0100 Subject: [PATCH] Add String::Contains(). Refs #5355 --- lib/base/qstring.cpp | 7 +++++++ lib/base/qstring.h | 1 + test/CMakeLists.txt | 1 + test/base-string.cpp | 15 +++++++++++++++ 4 files changed, 24 insertions(+) diff --git a/lib/base/qstring.cpp b/lib/base/qstring.cpp index 180fe532f..24cc970df 100644 --- a/lib/base/qstring.cpp +++ b/lib/base/qstring.cpp @@ -22,6 +22,8 @@ #include #include #include +#include +#include using namespace icinga; @@ -179,6 +181,11 @@ void String::Trim(void) boost::algorithm::trim(m_Data); } +bool String::Contains(const String& str) const +{ + return boost::algorithm::contains(m_Data, str); +} + void String::swap(String& str) { m_Data.swap(str.m_Data); diff --git a/lib/base/qstring.h b/lib/base/qstring.h index 47f7e5ad7..e5bd45adf 100644 --- a/lib/base/qstring.h +++ b/lib/base/qstring.h @@ -91,6 +91,7 @@ public: void Replace(size_t first, size_t second, const String& str); void Trim(void); + bool Contains(const String& str) const; void swap(String& str); Iterator erase(Iterator first, Iterator last); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index f4867e128..752914d58 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -62,6 +62,7 @@ add_boost_test(base base_string/clear base_string/append base_string/trim + base_string/contains base_string/replace base_string/index base_string/find diff --git a/test/base-string.cpp b/test/base-string.cpp index 0de138f7a..f4c0970c2 100644 --- a/test/base-string.cpp +++ b/test/base-string.cpp @@ -79,6 +79,21 @@ BOOST_AUTO_TEST_CASE(trim) BOOST_CHECK(s4 == "hello"); } +BOOST_AUTO_TEST_CASE(contains) +{ + String s1 = "hello world"; + String s2 = "hello"; + BOOST_CHECK(s1.Contains(s2)); + + String s3 = " hello world "; + String s4 = " hello"; + BOOST_CHECK(s3.Contains(s4)); + + String s5 = " hello world "; + String s6 = "world "; + BOOST_CHECK(s5.Contains(s6)); +} + BOOST_AUTO_TEST_CASE(replace) { String s = "hello";