diff --git a/include/boost/compute/container/basic_string.hpp b/include/boost/compute/container/basic_string.hpp index be9a4026..64db0fc0 100644 --- a/include/boost/compute/container/basic_string.hpp +++ b/include/boost/compute/container/basic_string.hpp @@ -16,6 +16,7 @@ #include #include +#include #include #include #include @@ -247,6 +248,7 @@ public: return basic_string(*this, pos, count); } + /// Finds the first character \p ch size_type find(CharT ch, size_type pos = 0) const { const_iterator iter = ::boost::compute::find(begin() + pos, end(), ch); @@ -258,6 +260,40 @@ public: } } + /// Finds the first substring equal to \p str + size_type find(basic_string& str, size_type pos = 0) const + { + const_iterator iter = ::boost::compute::search(begin() + pos, end(), + str.begin(), str.end()); + if(iter == end()){ + return npos; + } + else { + return static_cast(std::distance(begin(), iter)); + } + } + + /// Finds the first substring equal to the character string + /// pointed to by \p s. + /// The length of the string is determined by the first null character. + /// + /// For example, the following code + /// \snippet test/test_string.cpp string_find + /// + /// will return 5 as position. + size_type find(const char* s, size_type pos = 0) const + { + basic_string str(s); + const_iterator iter = ::boost::compute::search(begin() + pos, end(), + str.begin(), str.end()); + if(iter == end()){ + return npos; + } + else { + return static_cast(std::distance(begin(), iter)); + } + } + private: ::boost::compute::vector m_data; }; diff --git a/test/test_string.cpp b/test/test_string.cpp index 9241de30..c541179f 100644 --- a/test/test_string.cpp +++ b/test/test_string.cpp @@ -54,12 +54,19 @@ BOOST_AUTO_TEST_CASE(size) BOOST_CHECK_EQUAL(str.length(), size_t(6)); } -BOOST_AUTO_TEST_CASE(find) +BOOST_AUTO_TEST_CASE(find_doctest) { - boost::compute::string str = "string"; +//! [string_find] +boost::compute::string str = "boost::compute::string"; +int pos = str.find("::"); +//! [string_find] + boost::compute::string pattern = "string"; BOOST_VERIFY(!str.empty()); - BOOST_CHECK_EQUAL(str.find('r'), 2); - BOOST_CHECK_NE(str.find('r'), 3); + BOOST_CHECK_EQUAL(str.find('o'), 1); + BOOST_CHECK_NE(str.find('o'), 2); + BOOST_CHECK_EQUAL(str.find(pattern), 16); + BOOST_CHECK_EQUAL(pos, 5); + BOOST_CHECK_EQUAL(str.find("@#$"), size_t(-1)); } BOOST_AUTO_TEST_CASE(outStream)