Skip to content

Commit 2c30532

Browse files
committed
#160 - Test CppStringT::rfind() with char and wchar_t
Tests completed.
1 parent 56b7326 commit 2c30532

File tree

2 files changed

+140
-11
lines changed

2 files changed

+140
-11
lines changed

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

Lines changed: 113 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,6 @@ namespace cppstringstests
967967
Assert::AreEqual(L"cdab", L"abcdab"cs.removeprefix(L"ab").c_str());
968968
}
969969

970-
971970
TEST_METHOD(removesuffix)
972971
{
973972
pcs::CppString s("abcd");
@@ -985,7 +984,6 @@ namespace cppstringstests
985984
Assert::AreEqual(L"abcd", L"abcdab"cs.removesuffix(L"ab").c_str());
986985
}
987986

988-
989987
TEST_METHOD(replace)
990988
{
991989
pcs::CppString s("abbaa");
@@ -1022,5 +1020,118 @@ namespace cppstringstests
10221020
Assert::AreEqual(L"AAbbAAAA", ws.replace(L"a", L"AA", 3).c_str());
10231021
Assert::AreEqual(L"aBBaa", ws.replace(L"b", L"B", 5).c_str());
10241022
}
1023+
1024+
TEST_METHOD(rfind)
1025+
{
1026+
size_t found_pos;
1027+
1028+
pcs::CppString test{ "ABC0123456789." };
1029+
for (int c = 0; c <= 255; ++c) {
1030+
char ch{ char(c) };
1031+
Assert::AreEqual(test.MyBaseClass::rfind(ch), test.rfind(ch));
1032+
1033+
found_pos = test.substr(2).MyBaseClass::rfind(ch);
1034+
if (found_pos == pcs::CppString::npos)
1035+
Assert::AreEqual(found_pos, test.rfind(ch, 2));
1036+
else
1037+
Assert::AreEqual(found_pos, test.rfind(ch, 2) - 2);
1038+
1039+
found_pos = test.substr(2, 5).MyBaseClass::rfind(ch);
1040+
if (found_pos == pcs::CppString::npos)
1041+
Assert::AreEqual(found_pos, test.rfind(ch, 2, pcs::CppString::size_type(5 + 2 - 1)));
1042+
else
1043+
Assert::AreEqual(found_pos, test.rfind(ch, 2, pcs::CppString::size_type(5 + 2 - 1)) - 2);
1044+
1045+
CppString s(ch);
1046+
Assert::AreEqual(test.MyBaseClass::rfind(s), test.rfind(s));
1047+
found_pos = test.substr(2).MyBaseClass::rfind(s);
1048+
if (found_pos == pcs::CppString::npos)
1049+
Assert::AreEqual(found_pos, test.rfind(s, 2));
1050+
else
1051+
Assert::AreEqual(found_pos, test.rfind(s, 2) - 2);
1052+
1053+
found_pos = test.substr(2, 5).MyBaseClass::rfind(s);
1054+
if (found_pos == pcs::CppString::npos)
1055+
Assert::AreEqual(found_pos, test.rfind(s, 2, pcs::CppString::size_type(5 + 2 - 1)));
1056+
else
1057+
Assert::AreEqual(found_pos, test.rfind(s, 2, pcs::CppString::size_type(5 + 2 - 1)) - 2);
1058+
1059+
if (c > 0) {
1060+
char str[2]{ ch, 0 };
1061+
Assert::AreEqual(test.MyBaseClass::rfind(str), test.rfind(str));
1062+
1063+
found_pos = test.substr(2).MyBaseClass::rfind(str);
1064+
if (found_pos == pcs::CppString::npos)
1065+
Assert::AreEqual(found_pos, test.rfind(str, 2));
1066+
else
1067+
Assert::AreEqual(found_pos, test.rfind(str, 2) - 2);
1068+
1069+
found_pos = test.substr(2, 5).MyBaseClass::rfind(str);
1070+
if (found_pos == pcs::CppString::npos)
1071+
Assert::AreEqual(found_pos, test.rfind(str, 2, pcs::CppString::size_type(5 + 2 - 1)));
1072+
else
1073+
Assert::AreEqual(found_pos, test.rfind(str, 2, pcs::CppString::size_type(5 + 2 - 1)) - 2);
1074+
}
1075+
}
1076+
Assert::AreEqual(pcs::CppString::npos, test.rfind("A", 1));
1077+
Assert::AreEqual(test.size(), test.rfind(""));
1078+
Assert::AreEqual(pcs::CppString::npos, test.rfind(".", 14));
1079+
Assert::AreEqual(size_t(13), test.rfind(".", 13));
1080+
1081+
pcs::CppWString wtest{ L"ABC0123456789." };
1082+
for (int wc = 0; wc <= 0xffff; ++wc) {
1083+
wchar_t wch{ wchar_t(wc) };
1084+
1085+
found_pos = wtest.substr(2).MyBaseClass::rfind(wch);
1086+
if (found_pos == pcs::CppString::npos)
1087+
Assert::AreEqual(found_pos, wtest.rfind(wch, 2));
1088+
else
1089+
Assert::AreEqual(found_pos, wtest.rfind(wch, 2) - 2);
1090+
1091+
found_pos = wtest.substr(2, 5).MyBaseClass::rfind(wch);
1092+
if (found_pos == pcs::CppString::npos)
1093+
Assert::AreEqual(found_pos, wtest.rfind(wch, 2, pcs::CppString::size_type(5 + 2 - 1)));
1094+
else
1095+
Assert::AreEqual(found_pos, wtest.rfind(wch, 2, pcs::CppString::size_type(5 + 2 - 1)) - 2);
1096+
1097+
CppWString ws(wch);
1098+
Assert::AreEqual(wtest.MyBaseClass::rfind(ws), wtest.rfind(ws));
1099+
1100+
found_pos = wtest.substr(2).MyBaseClass::rfind(ws);
1101+
if (found_pos == pcs::CppString::npos)
1102+
Assert::AreEqual(found_pos, wtest.rfind(ws, 2));
1103+
else
1104+
Assert::AreEqual(found_pos, wtest.rfind(ws, 2) - 2);
1105+
1106+
found_pos = wtest.substr(2, 5).MyBaseClass::rfind(ws);
1107+
if (found_pos == pcs::CppString::npos)
1108+
Assert::AreEqual(found_pos, wtest.rfind(ws, 2, pcs::CppString::size_type(5 + 2 - 1)));
1109+
else
1110+
Assert::AreEqual(found_pos, wtest.rfind(ws, 2, pcs::CppString::size_type(5 + 2 - 1)) - 2);
1111+
1112+
1113+
if (wc > 0) {
1114+
wchar_t wstr[2]{ wch, 0 };
1115+
Assert::AreEqual(wtest.MyBaseClass::rfind(wstr), wtest.rfind(wstr));
1116+
1117+
found_pos = wtest.substr(2).MyBaseClass::rfind(wstr);
1118+
if (found_pos == pcs::CppString::npos)
1119+
Assert::AreEqual(found_pos, wtest.rfind(wstr, 2));
1120+
else
1121+
Assert::AreEqual(found_pos, wtest.rfind(wstr, 2) - 2);
1122+
1123+
found_pos = wtest.substr(2, 5).MyBaseClass::rfind(wstr);
1124+
if (found_pos == pcs::CppString::npos)
1125+
Assert::AreEqual(found_pos, wtest.rfind(wstr, 2, pcs::CppString::size_type(5 + 2 - 1)));
1126+
else
1127+
Assert::AreEqual(found_pos, wtest.rfind(wstr, 2, pcs::CppString::size_type(5 + 2 - 1)) - 2);
1128+
}
1129+
}
1130+
Assert::AreEqual(pcs::CppString::npos, wtest.rfind(L"A", 1));
1131+
Assert::AreEqual(wtest.size(), wtest.rfind(L""));
1132+
Assert::AreEqual(pcs::CppString::npos, wtest.rfind(L".", 14));
1133+
Assert::AreEqual(size_t(13), wtest.rfind(L".", 13));
1134+
}
1135+
10251136
};
10261137
}

cpp-strings/cppstrings.h

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ namespace pcs // i.e. "pythonic c++ strings"
576576
* sub. To check if sub is a substring or not, use the method contains().
577577
*
578578
* CAUTION: empty substrings are considered to be in the string if start and
579-
* end positions are both less then the string size and if start <= end.
579+
* end positions are both less than the string size and if start <= end.
580580
*
581581
* \see find_n(), rfind() and rfind_n().
582582
* \see index(), index_n(), rindex() and rindex_n().
@@ -597,7 +597,7 @@ namespace pcs // i.e. "pythonic c++ strings"
597597
* sub. To check if sub is a substring or not, use the method contains().
598598
*
599599
* CAUTION: empty substrings are considered to be in the string if start and
600-
* end positions are both less then the string size and if start <= end.
600+
* end positions are both less than the string size and if start <= end.
601601
*
602602
* \see find_n(), rfind() and rfind_n().
603603
* \see index(), index_n(), rindex() and rindex_n().
@@ -613,7 +613,8 @@ namespace pcs // i.e. "pythonic c++ strings"
613613
* sub. To check if sub is a substring or not, use the method contains().
614614
*
615615
* CAUTION: empty substrings are considered to be in the string if start and
616-
* end positions are both less then the string size and if start <= end.
616+
* end positions are both less than the string size and if start <= end. The
617+
* returned position is 0.
617618
*
618619
* \see find_n(), rfind() and rfind_n().
619620
* \see index(), index_n(), rindex() and rindex_n().
@@ -634,7 +635,8 @@ namespace pcs // i.e. "pythonic c++ strings"
634635
* sub. To check if sub is a substring or not, use the method contains_n().
635636
*
636637
* CAUTION: empty substrings are considered to be in the string if start and
637-
* end positions are both less then the string size and if start <= end.
638+
* end positions are both less than the string size and if start <= end. The
639+
* returned position is 0.
638640
*
639641
* \see find(), rfind() and rfind_n().
640642
* \see index(), index_n(), rindex() and rindex_n().
@@ -660,7 +662,8 @@ namespace pcs // i.e. "pythonic c++ strings"
660662
* sub. To check if sub is a substring or not, use the method contains_n().
661663
*
662664
* CAUTION: empty substrings are considered to be in the string if start and
663-
* end positions are both less then the string size and if start <= end.
665+
* end positions are both less than the string size and if start <= end. The
666+
* returned position is 0.
664667
*
665668
* \see find(), rfind() and rfind_n().
666669
* \see index(), index_n(), rindex() and rindex_n().
@@ -676,7 +679,7 @@ namespace pcs // i.e. "pythonic c++ strings"
676679
* sub. To check if sub is a substring or not, use the method contains_n().
677680
*
678681
* CAUTION: empty substrings are considered to be in the string if start and
679-
* end positions are both less then the string size and if start <= end.
682+
* end positions are both less than the string size and if start <= end.
680683
*
681684
* \see find(), rfind() and rfind_n().
682685
* \see index(), index_n(), rindex() and rindex_n().
@@ -1215,30 +1218,41 @@ namespace pcs // i.e. "pythonic c++ strings"
12151218
* Note: this method should be used only if you need to know the position
12161219
* of sub. To check if sub is a substring or not, use the method contains().
12171220
*
1221+
* CAUTION: empty substrings are considered to be in the string if start and
1222+
* end positions are both less than the string size and if start <= end. The
1223+
* returned position is the size of the string.
1224+
*
12181225
* \see find(), find_n() and rfind_n().
12191226
* \see index(), index_n(), rindex() and rindex_n().
12201227
*/
12211228
inline constexpr size_type rfind(const CppStringT& sub, const size_type start, const size_type end) const noexcept
12221229
{
12231230
if (start > end)
12241231
return CppStringT::npos;
1225-
else
1226-
return this->substr(start, end - start + 1).rfind(sub);
1232+
else {
1233+
const size_type found_pos{ this->substr(start, end - start + 1).rfind(sub) };
1234+
return (found_pos == CppStringT::npos) ? CppStringT::npos : found_pos + start;
1235+
}
12271236
}
12281237

1238+
12291239
/** Returns the highest index in the string where substring sub is found starting at start position in string, or -1 (i.e. 'npos') if sub is not found.
12301240
*
12311241
* Note that this is an offset from the start of the string, not the end.
12321242
*
12331243
* Note: this method should be used only if you need to know the position
12341244
* of sub. To check if sub is a substring or not, use the method contains().
12351245
*
1246+
* CAUTION: empty substrings are considered to be in the string if start and
1247+
* end positions are both less than the string size and if start <= end. The
1248+
* returned position is the size of the string.
1249+
*
12361250
* \see find(), find_n() and rfind_n().
12371251
* \see index(), index_n(), rindex() and rindex_n().
12381252
*/
12391253
inline constexpr size_type rfind(const CppStringT& sub, const size_type start) const noexcept
12401254
{
1241-
return rfind(sub, start, this->size() - start + 1);
1255+
return rfind(sub, start, this->size() - 1);
12421256
}
12431257

12441258
/** Returns the highest index in the string where substring sub is found in the whole string, or -1 (i.e. 'npos') if sub is not found.
@@ -1248,6 +1262,10 @@ namespace pcs // i.e. "pythonic c++ strings"
12481262
* Note: this method should be used only if you need to know the position
12491263
* of sub. To check if sub is a substring or not, use the method contains().
12501264
*
1265+
* CAUTION: empty substrings are considered to be in the string if start and
1266+
* end positions are both less than the string size and if start <= end. The
1267+
* returned position is the size of the string.
1268+
*
12511269
* \see find(), find_n() and rfind_n().
12521270
* \see index(), index_n(), rindex() and rindex_n().
12531271
*/

0 commit comments

Comments
 (0)