Skip to content

Commit 7eb0631

Browse files
committed
#68 - Fix method rfind()
Completed.
1 parent 8a666e5 commit 7eb0631

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

cpp-strings/cppstrings.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,8 @@ class CppStringT : public std::basic_string<CharT>
345345
//--- rfind() -----------------------------------------
346346
/** Returns the highest index in the string where substring sub is found within the slice str[start:end], or -1 (i.e. 'npos') if sub is not found.
347347
*
348+
* Note that this is an offset from the start of the string, not the end.
349+
*
348350
* Note: this method should be used only if you need to know the position
349351
* of sub. To check if sub is a substring or not, use the method contains().
350352
*
@@ -353,15 +355,16 @@ class CppStringT : public std::basic_string<CharT>
353355
*/
354356
inline constexpr size_type rfind(const CppStringT& sub, const size_type start, const size_type end) const noexcept
355357
{
356-
const size_type length{ this->length() };
357-
if (start >= length || start > end)
358+
if (start > end)
358359
return CppStringT::npos;
359360
else
360-
return this->find_end(this->cbegin()+start, this->cbegin()+end, sub.cbegin(), sub.cend());
361+
return this->substr(start, end - start + 1).rfind(sub);
361362
}
362363

363364
/** 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.
364365
*
366+
* Note that this is an offset from the start of the string, not the end.
367+
*
365368
* Note: this method should be used only if you need to know the position
366369
* of sub. To check if sub is a substring or not, use the method contains().
367370
*
@@ -370,15 +373,13 @@ class CppStringT : public std::basic_string<CharT>
370373
*/
371374
inline constexpr size_type rfind(const CppStringT& sub, const size_type start) const noexcept
372375
{
373-
const size_type length{ this->length() };
374-
if (start >= length)
375-
return CppStringT::npos;
376-
else
377-
return this->find_end(this->cbegin() + start, this->cend(), sub.cbegin(), sub.cend());
376+
return rfind(sub, start, this->size() - start + 1);
378377
}
379378

380379
/** 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.
381380
*
381+
* Note that this is an offset from the start of the string, not the end.
382+
*
382383
* Note: this method should be used only if you need to know the position
383384
* of sub. To check if sub is a substring or not, use the method contains().
384385
*
@@ -387,7 +388,7 @@ class CppStringT : public std::basic_string<CharT>
387388
*/
388389
inline constexpr size_type rfind(const CppStringT& sub) const noexcept
389390
{
390-
return this->find_end(this->cbegin(), this->cend(), sub.cbegin(), sub.cend());
391+
return MyBaseClass::rfind(sub);
391392
}
392393

393394

0 commit comments

Comments
 (0)