Skip to content

Commit 9005fb0

Browse files
committed
#171-Test CppStringT::startswith_n() with char and wchar_t
Completed. Validated. Led to simplification of code.
1 parent a11502e commit 9005fb0

File tree

2 files changed

+127
-7
lines changed

2 files changed

+127
-7
lines changed

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

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3616,5 +3616,126 @@ namespace cppstringstests
36163616
Assert::IsFalse(wtext.startswith({ L"def", L"ghi", L"Abcd" }, 0, len - 4));
36173617
}
36183618

3619+
TEST_METHOD(startswith_n)
3620+
{
3621+
CppString text("Abcdef");
3622+
const size_t len{ text.size() };
3623+
3624+
Assert::IsTrue(text.startswith_n("A", 2));
3625+
Assert::IsTrue(text.startswith_n("Ab", 2));
3626+
Assert::IsTrue(text.startswith_n("Abc", 3));
3627+
Assert::IsTrue(text.startswith_n("Abcd", 5));
3628+
Assert::IsTrue(text.startswith_n("Abcde", 5));
3629+
Assert::IsTrue(text.startswith_n("Abcdef", 7));
3630+
Assert::IsFalse(text.startswith_n("Abcdefg", 11));
3631+
Assert::IsFalse(text.startswith_n("a", 2));
3632+
Assert::IsFalse(text.startswith_n("def", len));
3633+
3634+
Assert::IsTrue(text.startswith_n("b", 1, 1));
3635+
Assert::IsTrue(text.startswith_n("bc", 1, 3));
3636+
Assert::IsTrue(text.startswith_n("bcd", 1, 3));
3637+
Assert::IsFalse(text.startswith_n("bcdefg", 1, 5));
3638+
Assert::IsFalse(text.startswith_n("A", 1, 8));
3639+
Assert::IsFalse(text.startswith_n("def", 1, 2));
3640+
3641+
Assert::IsTrue(text.startswith_n("c", 2, 1));
3642+
Assert::IsTrue(text.startswith_n("cd", 2, 2));
3643+
Assert::IsTrue(text.startswith_n("cde", 2, 4));
3644+
Assert::IsFalse(text.startswith_n("cdefg", 2, 6));
3645+
Assert::IsFalse(text.startswith_n("Ab", 2, 2));
3646+
Assert::IsFalse(text.startswith_n("def", 2, 5));
3647+
3648+
Assert::IsTrue(text.startswith_n("d", 3, 2));
3649+
Assert::IsTrue(text.startswith_n("de", 3, 2));
3650+
Assert::IsTrue(text.startswith_n("def", 3, 4));
3651+
Assert::IsFalse(text.startswith_n("defg", 3, 5));
3652+
Assert::IsFalse(text.startswith_n("Abc", 3, 1));
3653+
Assert::IsFalse(text.startswith_n("ef", 3, 2));
3654+
3655+
Assert::IsTrue(text.startswith_n("e", 4, 1));
3656+
Assert::IsTrue(text.startswith_n("ef", 4, 3));
3657+
Assert::IsFalse(text.startswith_n("efg", 4, 5));
3658+
Assert::IsFalse(text.startswith_n("Abcd", 4, 7));
3659+
Assert::IsFalse(text.startswith_n("f", 4, 9));
3660+
3661+
Assert::IsTrue(text.startswith_n("f", 5, 2));
3662+
Assert::IsFalse(text.startswith_n("fg", 5, 1));
3663+
Assert::IsFalse(text.startswith_n("Abcde", 5, 8));
3664+
Assert::IsFalse(text.startswith_n("g", 5, 11));
3665+
3666+
Assert::IsTrue(text.startswith_n("A", 0, 1));
3667+
Assert::IsFalse(text.startswith_n("b", 0, 2));
3668+
Assert::IsTrue(text.startswith_n("b", 1, 3));
3669+
Assert::IsTrue(text.startswith_n("bc", 1, 3));
3670+
Assert::IsTrue(text.startswith_n("bcd", 1, 3));
3671+
Assert::IsFalse(text.startswith_n("bcde", 1, 3));
3672+
3673+
Assert::IsTrue(text.startswith_n("", 5, 2));
3674+
Assert::IsTrue(text.startswith_n("", 15, 16));
3675+
3676+
Assert::IsTrue(text.startswith_n({ "ghi", "abca", "Abcd" }, 0, len - 2));
3677+
Assert::IsFalse(text.startswith_n({ "def", "ghi" }, 0, len - 4));
3678+
Assert::IsFalse(text.startswith_n({ "def", "ghi", "Abcd" }, 0, len - 4));
3679+
3680+
3681+
CppWString wtext(L"Abcdef");
3682+
const size_t wlen{ wtext.size() };
3683+
3684+
Assert::IsTrue(wtext.startswith_n(L"A", 2));
3685+
Assert::IsTrue(wtext.startswith_n(L"Ab", 2));
3686+
Assert::IsTrue(wtext.startswith_n(L"Abc", 3));
3687+
Assert::IsTrue(wtext.startswith_n(L"Abcd", 5));
3688+
Assert::IsTrue(wtext.startswith_n(L"Abcde", 5));
3689+
Assert::IsTrue(wtext.startswith_n(L"Abcdef", 7));
3690+
Assert::IsFalse(wtext.startswith_n(L"Abcdefg", 11));
3691+
Assert::IsFalse(wtext.startswith_n(L"a", 2));
3692+
Assert::IsFalse(wtext.startswith_n(L"def", wlen));
3693+
3694+
Assert::IsTrue(wtext.startswith_n(L"b", 1, 1));
3695+
Assert::IsTrue(wtext.startswith_n(L"bc", 1, 3));
3696+
Assert::IsTrue(wtext.startswith_n(L"bcd", 1, 3));
3697+
Assert::IsFalse(wtext.startswith_n(L"bcdefg", 1, 5));
3698+
Assert::IsFalse(wtext.startswith_n(L"A", 1, 8));
3699+
Assert::IsFalse(wtext.startswith_n(L"def", 1, 2));
3700+
3701+
Assert::IsTrue(wtext.startswith_n(L"c", 2, 1));
3702+
Assert::IsTrue(wtext.startswith_n(L"cd", 2, 2));
3703+
Assert::IsTrue(wtext.startswith_n(L"cde", 2, 4));
3704+
Assert::IsFalse(wtext.startswith_n(L"cdefg", 2, 6));
3705+
Assert::IsFalse(wtext.startswith_n(L"Ab", 2, 2));
3706+
Assert::IsFalse(wtext.startswith_n(L"def", 2, 5));
3707+
3708+
Assert::IsTrue(wtext.startswith_n(L"d", 3, 2));
3709+
Assert::IsTrue(wtext.startswith_n(L"de", 3, 2));
3710+
Assert::IsTrue(wtext.startswith_n(L"def", 3, 4));
3711+
Assert::IsFalse(wtext.startswith_n(L"defg", 3, 5));
3712+
Assert::IsFalse(wtext.startswith_n(L"Abc", 3, 1));
3713+
Assert::IsFalse(wtext.startswith_n(L"ef", 3, 2));
3714+
3715+
Assert::IsTrue(wtext.startswith_n(L"e", 4, 1));
3716+
Assert::IsTrue(wtext.startswith_n(L"ef", 4, 3));
3717+
Assert::IsFalse(wtext.startswith_n(L"efg", 4, 5));
3718+
Assert::IsFalse(wtext.startswith_n(L"Abcd", 4, 7));
3719+
Assert::IsFalse(wtext.startswith_n(L"f", 4, 9));
3720+
3721+
Assert::IsTrue(wtext.startswith_n(L"f", 5, 2));
3722+
Assert::IsFalse(wtext.startswith_n(L"fg", 5, 1));
3723+
Assert::IsFalse(wtext.startswith_n(L"Abcde", 5, 8));
3724+
Assert::IsFalse(wtext.startswith_n(L"g", 5, 11));
3725+
3726+
Assert::IsTrue(wtext.startswith_n(L"A", 0, 1));
3727+
Assert::IsFalse(wtext.startswith_n(L"b", 0, 2));
3728+
Assert::IsTrue(wtext.startswith_n(L"b", 1, 3));
3729+
Assert::IsTrue(wtext.startswith_n(L"bc", 1, 3));
3730+
Assert::IsTrue(wtext.startswith_n(L"bcd", 1, 3));
3731+
Assert::IsFalse(wtext.startswith_n(L"bcde", 1, 3));
3732+
3733+
Assert::IsTrue(wtext.startswith_n(L"", 5, 2));
3734+
Assert::IsTrue(wtext.startswith_n(L"", 15, 16));
3735+
3736+
Assert::IsTrue(wtext.startswith_n({ L"ghi", L"abca", L"Abcd" }, 0, wlen - 2));
3737+
Assert::IsFalse(wtext.startswith_n({ L"def", L"ghi" }, 0, wlen - 4));
3738+
Assert::IsFalse(wtext.startswith_n({ L"def", L"ghi", L"Abcd" }, 0, wlen - 4));
3739+
}
36193740
};
36203741
}

cpp-strings/cppstrings.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,22 +1690,21 @@ namespace pcs // i.e. "pythonic c++ strings"
16901690

16911691
//--- startswith_n() ----------------------------------
16921692
/** Returns true if the string starts with the specified suffix, otherwise returns false. Test begins at start position and stops after count positions. */
1693-
inline const bool startswith_n(const CppStringT& suffix, const size_type start, const size_type count) const noexcept
1693+
inline const bool startswith_n(const CppStringT& prefix, const size_type start, const size_type count) const noexcept
16941694
{
1695-
//return startswith(std::span{ suffix }, start, start + count - 1);
1696-
return startswith(std::initializer_list<CppStringT>(suffix), start, start + count - 1);
1695+
return this->substr(start, count).MyBaseClass::starts_with(prefix);
16971696
}
16981697

16991698
/** Returns true if the string starts with the specified suffix, otherwise returns false. Test begins at position 0 and stops after count positions. */
1700-
inline const bool startswith_n(const CppStringT& suffix, const size_type count) const noexcept
1699+
inline const bool startswith_n(const CppStringT& prefix, const size_type count) const noexcept
17011700
{
1702-
return startswith(std::span{ suffix }, 0, count - 1);
1701+
return this->substr(0, count).MyBaseClass::starts_with(prefix);
17031702
}
17041703

17051704
/** Returns true if the string starts with any of the specified suffixes, otherwise returns false. Test begins at start position and stops after count positions. */
1706-
inline const bool startswith_n(const std::span<CppStringT>& suffixes, const size_type start, const size_type count) const noexcept
1705+
inline const bool startswith_n(const std::initializer_list<CppStringT>& prefix, const size_type start, const size_type count) const noexcept
17071706
{
1708-
return startswith(suffixes, start, start + count - 1);
1707+
return startswith(prefix, start, count);
17091708
}
17101709

17111710

0 commit comments

Comments
 (0)