Skip to content

Commit c368826

Browse files
committed
#42 - Implement method CppStringT::rfind()
Completed.
1 parent b428966 commit c368826

File tree

1 file changed

+74
-3
lines changed

1 file changed

+74
-3
lines changed

cpp-strings/cppstrings.h

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ class CppStringT : public std::basic_string<CharT>
181181
*
182182
* Note: this method should be used only if you need to know the position
183183
* of sub. To check if sub is a substring or not, use the method contains().
184+
*
185+
* \see find_n(), rfind() and rfind_n().
186+
* \see index(), index_n(), rindex() and rindex_n().
184187
*/
185188
inline constexpr size_type find(const CppStringT& sub, const size_type start, const size_type end) const noexcept
186189
{
@@ -197,6 +200,9 @@ class CppStringT : public std::basic_string<CharT>
197200
*
198201
* Note: this method should be used only if you need to know the position
199202
* of sub. To check if sub is a substring or not, use the method contains_n().
203+
*
204+
* \see find(), rfind() and rfind_n().
205+
* \see index(), index_n(), rindex() and rindex_n().
200206
*/
201207
inline constexpr size_type find_n(const CppStringT& sub, const size_type start, const size_type length) const noexcept
202208
{
@@ -207,6 +213,9 @@ class CppStringT : public std::basic_string<CharT>
207213
*
208214
* Note: this method should be used only if you need to know the position
209215
* of sub. To check if sub is a substring or not, use the method contains_n().
216+
*
217+
* \see find(), rfind() and rfind_n().
218+
* \see index(), index_n(), rindex() and rindex_n().
210219
*/
211220
inline constexpr size_type find_n(const CppStringT& sub, const size_type length) const noexcept
212221
{
@@ -215,7 +224,11 @@ class CppStringT : public std::basic_string<CharT>
215224

216225

217226
//--- index() -----------------------------------------
218-
/** Like find(), but raises NotFoundException when the substring is not found. */
227+
/** Like find(), but raises NotFoundException when the substring is not found.
228+
*
229+
* \see index_n(), rindex() and rindex_n().
230+
* \see find(), find_n(), rfind() and rfind_n().
231+
*/
219232
inline constexpr size_type index(const CppStringT& sub, const size_type start, const size_type end) const
220233
{
221234
const size_type ret_value = find(sub, start, end);
@@ -227,13 +240,21 @@ class CppStringT : public std::basic_string<CharT>
227240

228241

229242
//--- index_n() ---------------------------------------
230-
/** Like find_n(sub, start, length), but raises NotFoundException when the substring is not found. */
243+
/** Like find_n(sub, start, length), but raises NotFoundException when the substring is not found.
244+
*
245+
* \see index_n(), rindex() and rindex_n().
246+
* \see find(), find_n(), rfind() and rfind_n().
247+
*/
231248
inline constexpr size_type index_n(const CppStringT& sub, const size_type start, const size_type length) const
232249
{
233250
return index_n(sub, start, start + length - 1);
234251
}
235252

236-
/** Like find_n(sub, length), but raises NotFoundException when the substring is not found. */
253+
/** Like find_n(sub, length), but raises NotFoundException when the substring is not found.
254+
*
255+
* \see index_n(), rindex() and rindex_n().
256+
* \see find(), find_n(), rfind() and rfind_n().
257+
*/
237258
inline constexpr size_type index_n(const CppStringT& sub, const size_type length) const
238259
{
239260
return index_n(sub, 0, length - 1);
@@ -318,6 +339,56 @@ class CppStringT : public std::basic_string<CharT>
318339
}
319340

320341

342+
//--- rfind() -----------------------------------------
343+
/** 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.
344+
*
345+
* Note: this method should be used only if you need to know the position
346+
* of sub. To check if sub is a substring or not, use the method contains().
347+
*
348+
* \see find(), find_n() and rfind_n().
349+
* \see index(), index_n(), rindex() and rindex_n().
350+
*/
351+
inline constexpr size_type rfind(const CppStringT& sub, const size_type start, const size_type end) const noexcept
352+
{
353+
const size_type length{ this->length() };
354+
if (start >= length || start > end)
355+
return CppStringT::npos;
356+
else
357+
return this->find_end(this->cbegin()+start, this->cbegin()+end, sub.cbegin(), sub.cend());
358+
}
359+
360+
/** 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.
361+
*
362+
* Note: this method should be used only if you need to know the position
363+
* of sub. To check if sub is a substring or not, use the method contains().
364+
*
365+
* \see find(), find_n() and rfind_n().
366+
* \see index(), index_n(), rindex() and rindex_n().
367+
*/
368+
inline constexpr size_type rfind(const CppStringT& sub, const size_type start) const noexcept
369+
{
370+
const size_type length{ this->length() };
371+
if (start >= length)
372+
return CppStringT::npos;
373+
else
374+
return this->find_end(this->cbegin() + start, this->cend(), sub.cbegin(), sub.cend());
375+
}
376+
377+
/** 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.
378+
*
379+
* Note: this method should be used only if you need to know the position
380+
* of sub. To check if sub is a substring or not, use the method contains().
381+
*
382+
* \see find(), find_n() and rfind_n().
383+
* \see index(), index_n(), rindex() and rindex_n().
384+
*/
385+
inline constexpr size_type rfind(const CppStringT& sub) const noexcept
386+
{
387+
return this->find_end(this->cbegin(), this->cend(), sub.cbegin(), sub.cend());
388+
}
389+
390+
391+
321392
//--- upper () -----------------------------------------
322393
/** \brief In-place replaces all characters of the string with their uppercase conversion. Returns a reference to string.
323394
*

0 commit comments

Comments
 (0)