Skip to content

Commit fc98a07

Browse files
committed
#19 - Implement method CppStringT::index()
Completed.
1 parent f82fc5d commit fc98a07

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

cpp-strings/cppstrings.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
//=============================================================
2424
#include <algorithm>
2525
#include <cctype>
26+
#include <format>
2627
#include <stdexcept>
2728
#include <string>
2829
#include <vector>
29-
//#include <initializer_list>
3030

3131

3232
//=============================================================
@@ -214,6 +214,18 @@ class CppStringT : public std::basic_string<CharT>
214214
}
215215

216216

217+
//--- index() -----------------------------------------
218+
/** Like find(), but raises NotFoundException when the substring is not found. */
219+
inline constexpr size_type index(const CppStringT& sub, const size_type start, const size_type end) const
220+
{
221+
const size_type ret_value = find(sub, start, end);
222+
if (size_type == CppStringT::npos)
223+
throw NotFoundException(std::format("substring \"{}\" not found in string \"{}\"", sub, this->c_str()));
224+
else
225+
return ret_value;
226+
}
227+
228+
217229
//--- is_punctuation() --------------------------------
218230
/** \brief Returns true if the string contains only one character and if this character belongs to the ASCII punctuation set. */
219231
inline const bool is_punctuation() const noexcept

0 commit comments

Comments
 (0)