Skip to content

Commit c269f62

Browse files
committed
#183-Implement method CppStringT::contains_n()
Done. Still to be tested.
1 parent 60f5163 commit c269f62

File tree

1 file changed

+43
-3
lines changed

1 file changed

+43
-3
lines changed

cpp-strings/cppstrings.h

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ namespace pcs // i.e. "pythonic c++ strings"
461461
*
462462
* This is the c++ implementation of Python keyword 'in' applied to strings.
463463
*/
464-
const bool contains(const CppStringT& substr) const noexcept
464+
constexpr bool contains(const CppStringT& substr) const noexcept
465465
{
466466
if (substr.empty())
467467
// the empty string is always contained in any string
@@ -488,7 +488,7 @@ namespace pcs // i.e. "pythonic c++ strings"
488488
}
489489

490490
/** \brief Returns true if this string contains the passed C-string, or false otherwise. */
491-
inline const bool contains(const CharT* substr) const noexcept
491+
inline constexpr bool contains(const CharT* substr) const noexcept
492492
{
493493
if (substr == nullptr)
494494
// just to avoid system error on invalid access
@@ -501,8 +501,9 @@ namespace pcs // i.e. "pythonic c++ strings"
501501
return contains(CppStringT(substr));
502502
#endif
503503
}
504+
504505
/** \brief Returns true if this string contains the passed char, or false otherwise. */
505-
const bool contains(const CharT& ch) const noexcept
506+
inline constexpr bool contains(const CharT& ch) const noexcept
506507
{
507508
#if (defined(_HAS_CXX23) && _HAS_CXX23) || (!defined(_HAS_CXX23) && __cplusplus >= 202302L)
508509
// c++23 and above already defines this method
@@ -514,6 +515,45 @@ namespace pcs // i.e. "pythonic c++ strings"
514515
}
515516

516517

518+
//--- contains_n() ------------------------------------
519+
/** Returns true if the passed string is found within the slice str[start:start+count-1], or false otherwise. */
520+
inline constexpr bool contains_n(const CppStringT& sub, const size_type start, const size_type count = -1) const noexcept
521+
{
522+
try {
523+
return this->substr(start, count).contains(sub);
524+
}
525+
catch (...) {
526+
return false;
527+
}
528+
}
529+
530+
/** Returns true if the passed C-string is found within the slice str[start:start+count-1], or false otherwise. */
531+
inline constexpr bool contains_n(const CharT* sub, const size_type start, const size_type count = -1) const noexcept
532+
{
533+
if (sub == nullptr)
534+
// just to avoid system error on invalid access
535+
return true;
536+
537+
try {
538+
return this->substr(start, count).contains(sub);
539+
}
540+
catch (...) {
541+
return false;
542+
}
543+
}
544+
545+
/** Returns true if the passed char is found within the slice str[start:start+count-1], or false otherwise. */
546+
inline constexpr bool contains_n(const CharT ch, const size_type start, const size_type count = -1) const noexcept
547+
{
548+
try {
549+
return this->substr(start, count).contains(ch);
550+
}
551+
catch (...) {
552+
return false;
553+
}
554+
}
555+
556+
517557
//--- count() -----------------------------------------
518558
/** \brief Returns the number of non-overlapping occurrences of substring sub in the range [start, end]. */
519559
constexpr size_type count(const CppStringT& sub, const size_type start = 0, const size_type end = -1) const noexcept

0 commit comments

Comments
 (0)