Skip to content

Commit 7588cb6

Browse files
committed
#202-add test on nullptr in CppStringT pointer constructors
Completed. Validated.
1 parent e3ff5ce commit 7588cb6

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

cpp-strings-tests/cpp-strings-tests.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,14 @@ namespace cppstringstests
560560
pcs::CppWString ws(L"ABCdefGH");
561561
Assert::AreEqual(std::size_t(8), ws.size());
562562
Assert::AreEqual(pcs::CppWString(L"ABCdefGH").c_str(), ws.c_str());
563+
564+
pcs::CppString s0(nullptr);
565+
Assert::AreEqual(std::size_t(0), s0.size());
566+
Assert::AreEqual("", s0.c_str());
567+
568+
pcs::CppWString ws0(nullptr);
569+
Assert::AreEqual(std::size_t(0), ws0.size());
570+
Assert::AreEqual(L"", ws0.c_str());
563571
}
564572

565573
TEST_METHOD(constructor_10)
@@ -571,6 +579,14 @@ namespace cppstringstests
571579
pcs::CppWString ws(L"ABCdefGH", 7);
572580
Assert::AreEqual(std::size_t(7), ws.size());
573581
Assert::AreEqual(pcs::CppWString(L"ABCdefG").c_str(), ws.c_str());
582+
583+
pcs::CppString s0(nullptr, 0);
584+
Assert::AreEqual(std::size_t(0), s0.size());
585+
Assert::AreEqual("", s0.c_str());
586+
587+
pcs::CppWString ws0(nullptr, 7);
588+
Assert::AreEqual(std::size_t(7), ws0.size());
589+
Assert::AreEqual(L"", ws0.c_str());
574590
}
575591

576592
TEST_METHOD(constructor_11)

cpp-strings/cppstrings.h

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)