Add C++23 "contains" to basic_string.

This commit is contained in:
Ion Gaztañaga
2026-01-21 22:46:19 +01:00
parent 548496fa2e
commit a02aae0b9f
3 changed files with 39 additions and 10 deletions
+3 -1
View File
@@ -1459,7 +1459,9 @@ use [*Boost.Container]? There are several reasons for that:
* If available, uses C++17's utilities under the `__cpp_aligned_new` feature.
* Uses alternative aligned allocation functions (`posix_memalign`, `aligned_alloc`, `_aligned_malloc`...) otherwise.
* Implemented overaligned allocation support for `adaptive_pool`and `node_allocator`
* Updated `basic_string` to the latest standard API: Added missing `string_view` members and updated `operator[]` to be able to return the terminating null.
* Updated `basic_string` to the latest standard API:
* Added missing `string_view` members and updated `operator[]` to be able to return the terminating null.
* Added C++23 `contains` overloads
* Fixed bugs/issues:
* [@https://github.com/boostorg/container/issues/323 GitHub #323: ['"flat_tree::try_emplace UB"]].
* [@https://github.com/boostorg/container/issues/328 GitHub #328: ['"boost::container::deque stores a redundant copy of the allocator, increasing size"]].
+28
View File
@@ -2953,6 +2953,34 @@ class basic_string
int compare(size_type pos1, size_type n1, const CharT* s) const
{ return this->compare(pos1, n1, s, Traits::length(s)); }
//! <b>Effects</b>: Equivalent to find(sv) != npos
//!
//! <b>Throws</b>: Nothing
//!
//! <b>Returns</b>: true if the string contains the provided substring, false otherwise.
template<template <class, class> class BasicStringView>
BOOST_CONTAINER_NODISCARD inline
bool contains(BasicStringView<CharT, Traits> sv) const BOOST_NOEXCEPT
{ return this->find(sv) != npos; }
//! <b>Effects</b>: Equivalent to find(c) != npos
//!
//! <b>Throws</b>: Nothing
//!
//! <b>Returns</b>: true if the string contains the provided substring, false otherwise.
BOOST_CONTAINER_NODISCARD inline
bool contains(CharT c) const BOOST_NOEXCEPT
{ return this->find(c) != npos; }
//! <b>Effects</b>: Equivalent to find(c) != npos
//!
//! <b>Throws</b>: Nothing
//!
//! <b>Returns</b>: true if the string contains the provided substring, false otherwise.
BOOST_CONTAINER_NODISCARD inline
bool contains(const CharT* s) const BOOST_NOEXCEPT
{ return this->find(s) != npos; }
#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
private:
void priv_move_assign(BOOST_RV_REF(basic_string) x, dtl::bool_<true> /*steal_resources*/)
+8 -9
View File
@@ -1537,16 +1537,15 @@ void test_contains()
{
string s("Hello, World!");
using test_helpers::contains;
BOOST_TEST(contains(s, "World"));
BOOST_TEST(contains(s, "Hello"));
BOOST_TEST(contains(s, ", "));
BOOST_TEST(contains(s, ""));
BOOST_TEST(contains(s, 'W'));
BOOST_TEST(s.contains("World"));
BOOST_TEST(s.contains("Hello"));
BOOST_TEST(s.contains(", "));
BOOST_TEST(s.contains(""));
BOOST_TEST(s.contains('W'));
BOOST_TEST(!contains(s, "world")); // case-sensitive
BOOST_TEST(!contains(s, "xyz"));
BOOST_TEST(!contains(s, 'X'));
BOOST_TEST(!s.contains("world")); // case-sensitive
BOOST_TEST(!s.contains("xyz"));
BOOST_TEST(!s.contains('X'));
}
//==============================================================================