@@ -380,8 +380,12 @@ namespace pcs // i.e. "pythonic c++ strings"
380380 inline CppStringT (MyBaseClass::size_type count, CharT ch) : MyBaseClass(count, ch) {} // #6
381381 inline CppStringT (const CppStringT& other, size_type pos) : MyBaseClass(other, pos) {} // #7
382382 inline CppStringT (const CppStringT& other, size_type pos, size_type count) noexcept : MyBaseClass(other, pos, count) {} // #8
383- inline CppStringT (const CharT* s) : MyBaseClass(s) {} // #9
384- inline CppStringT (const CharT* s, size_type count) : MyBaseClass(s, count) {} // #10
383+ inline CppStringT (const CharT* s) // #9
384+ : MyBaseClass(s ? s : CppStringT().c_str())
385+ {}
386+ inline CppStringT (const CharT* s, size_type count) // #10
387+ : MyBaseClass(s ? s : CppStringT().c_str(), count)
388+ {}
385389 inline CppStringT (std::initializer_list<CharT> ilist) : MyBaseClass(ilist) {} // #11
386390
387391 inline CppStringT (const CharT ch) : MyBaseClass(1 , ch) {} // #19
@@ -452,6 +456,38 @@ namespace pcs // i.e. "pythonic c++ strings"
452456 }
453457
454458
459+ // --- contains() --------------------------------------
460+ /* * \brief Returns true if this string contains the passed string or char.
461+ *
462+ * This is the c++ implementation of Python keyword 'in' applied to strings.
463+ */
464+ const bool contains (const CppStringT& substr) const noexcept
465+ {
466+ if (substr.empty ())
467+ // the empty string is always contained in any string
468+ return true ;
469+
470+ #if (defined(_HAS_CXX23) && _HAS_CXX23) || (!defined(_HAS_CXX23) && __cplusplus >= 202302L)
471+ // c++23 and above already defines this method
472+ return MyBaseClass::contains (substr);
473+ #else
474+ // up to c++20, we have to implement this method
475+ const size_type substr_width{ substr.size () };
476+ const size_type width{ this ->size () };
477+
478+ if (substr_width > width)
479+ return false ;
480+
481+ for (size_type index = 0 ; index <= width - substr_width; ++index) {
482+ if (substr == this ->substr (index, substr_width))
483+ return true ;
484+ }
485+
486+ return false ;
487+ #endif
488+ }
489+
490+
455491 // --- count() -----------------------------------------
456492 /* * \brief Returns the number of non-overlapping occurrences of substring sub in the range [start, end]. */
457493 constexpr size_type count (const CppStringT& sub, const size_type start = 0 , const size_type end = -1 ) const noexcept
0 commit comments