Skip to content

Commit a02aae0

Browse files
committed
Add C++23 "contains" to basic_string.
1 parent 548496f commit a02aae0

File tree

3 files changed

+40
-11
lines changed

3 files changed

+40
-11
lines changed

doc/container.qbk

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1459,7 +1459,9 @@ use [*Boost.Container]? There are several reasons for that:
14591459
* If available, uses C++17's utilities under the `__cpp_aligned_new` feature.
14601460
* Uses alternative aligned allocation functions (`posix_memalign`, `aligned_alloc`, `_aligned_malloc`...) otherwise.
14611461
* Implemented overaligned allocation support for `adaptive_pool`and `node_allocator`
1462-
* Updated `basic_string` to the latest standard API: Added missing `string_view` members and updated `operator[]` to be able to return the terminating null.
1462+
* Updated `basic_string` to the latest standard API:
1463+
* Added missing `string_view` members and updated `operator[]` to be able to return the terminating null.
1464+
* Added C++23 `contains` overloads
14631465
* Fixed bugs/issues:
14641466
* [@https://github.com/boostorg/container/issues/323 GitHub #323: ['"flat_tree::try_emplace UB"]].
14651467
* [@https://github.com/boostorg/container/issues/328 GitHub #328: ['"boost::container::deque stores a redundant copy of the allocator, increasing size"]].

include/boost/container/string.hpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2953,6 +2953,34 @@ class basic_string
29532953
int compare(size_type pos1, size_type n1, const CharT* s) const
29542954
{ return this->compare(pos1, n1, s, Traits::length(s)); }
29552955

2956+
//! <b>Effects</b>: Equivalent to find(sv) != npos
2957+
//!
2958+
//! <b>Throws</b>: Nothing
2959+
//!
2960+
//! <b>Returns</b>: true if the string contains the provided substring, false otherwise.
2961+
template<template <class, class> class BasicStringView>
2962+
BOOST_CONTAINER_NODISCARD inline
2963+
bool contains(BasicStringView<CharT, Traits> sv) const BOOST_NOEXCEPT
2964+
{ return this->find(sv) != npos; }
2965+
2966+
//! <b>Effects</b>: Equivalent to find(c) != npos
2967+
//!
2968+
//! <b>Throws</b>: Nothing
2969+
//!
2970+
//! <b>Returns</b>: true if the string contains the provided substring, false otherwise.
2971+
BOOST_CONTAINER_NODISCARD inline
2972+
bool contains(CharT c) const BOOST_NOEXCEPT
2973+
{ return this->find(c) != npos; }
2974+
2975+
//! <b>Effects</b>: Equivalent to find(c) != npos
2976+
//!
2977+
//! <b>Throws</b>: Nothing
2978+
//!
2979+
//! <b>Returns</b>: true if the string contains the provided substring, false otherwise.
2980+
BOOST_CONTAINER_NODISCARD inline
2981+
bool contains(const CharT* s) const BOOST_NOEXCEPT
2982+
{ return this->find(s) != npos; }
2983+
29562984
#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
29572985
private:
29582986
void priv_move_assign(BOOST_RV_REF(basic_string) x, dtl::bool_<true> /*steal_resources*/)

test/string_test.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,16 +1537,15 @@ void test_contains()
15371537
{
15381538
string s("Hello, World!");
15391539

1540-
using test_helpers::contains;
1541-
BOOST_TEST(contains(s, "World"));
1542-
BOOST_TEST(contains(s, "Hello"));
1543-
BOOST_TEST(contains(s, ", "));
1544-
BOOST_TEST(contains(s, ""));
1545-
BOOST_TEST(contains(s, 'W'));
1546-
1547-
BOOST_TEST(!contains(s, "world")); // case-sensitive
1548-
BOOST_TEST(!contains(s, "xyz"));
1549-
BOOST_TEST(!contains(s, 'X'));
1540+
BOOST_TEST(s.contains("World"));
1541+
BOOST_TEST(s.contains("Hello"));
1542+
BOOST_TEST(s.contains(", "));
1543+
BOOST_TEST(s.contains(""));
1544+
BOOST_TEST(s.contains('W'));
1545+
1546+
BOOST_TEST(!s.contains("world")); // case-sensitive
1547+
BOOST_TEST(!s.contains("xyz"));
1548+
BOOST_TEST(!s.contains('X'));
15501549
}
15511550

15521551
//==============================================================================

0 commit comments

Comments
 (0)