Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7588cb6

Browse files
committed
#202-add test on nullptr in CppStringT pointer constructors
Completed. Validated.
1 parente3ff5ce commit7588cb6

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::CppWStringws(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::CppStrings0(nullptr);
565+
Assert::AreEqual(std::size_t(0), s0.size());
566+
Assert::AreEqual("", s0.c_str());
567+
568+
pcs::CppWStringws0(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::CppWStringws(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::CppStrings0(nullptr,0);
584+
Assert::AreEqual(std::size_t(0), s0.size());
585+
Assert::AreEqual("", s0.c_str());
586+
587+
pcs::CppWStringws0(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
inlineCppStringT(MyBaseClass::size_type count, CharT ch) : MyBaseClass(count, ch) {}// #6
381381
inlineCppStringT(const CppStringT& other, size_type pos) : MyBaseClass(other, pos) {}// #7
382382
inlineCppStringT(const CppStringT& other, size_type pos, size_type count)noexcept : MyBaseClass(other, pos, count) {}// #8
383-
inlineCppStringT(const CharT* s) : MyBaseClass(s) {}// #9
384-
inlineCppStringT(const CharT* s, size_type count) : MyBaseClass(s, count) {}// #10
383+
inlineCppStringT(const CharT* s)// #9
384+
: MyBaseClass(s ? s : CppStringT().c_str())
385+
{}
386+
inlineCppStringT(const CharT* s, size_type count)// #10
387+
: MyBaseClass(s ? s : CppStringT().c_str(), count)
388+
{}
385389
inlineCppStringT(std::initializer_list<CharT> ilist) : MyBaseClass(ilist) {}// #11
386390

387391
inlineCppStringT(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+
constboolcontains(const CppStringT& substr)constnoexcept
465+
{
466+
if (substr.empty())
467+
// the empty string is always contained in any string
468+
returntrue;
469+
470+
#if (defined(_HAS_CXX23) && _HAS_CXX23) || (!defined(_HAS_CXX23) && __cplusplus >= 202302L)
471+
// c++23 and above already defines this method
472+
returnMyBaseClass::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+
returnfalse;
480+
481+
for (size_type index =0; index <= width - substr_width; ++index) {
482+
if (substr ==this->substr(index, substr_width))
483+
returntrue;
484+
}
485+
486+
returnfalse;
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_typecount(const CppStringT& sub,const size_type start =0,const size_type end = -1)constnoexcept

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp