Skip to content

Commit e3d8f11

Browse files
committed
#13 - Implement method CppStringT::endswith()
Completed.
1 parent eab2dab commit e3d8f11

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

cpp-strings/cppstrings.h

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424
#include <algorithm>
2525
#include <cctype>
2626
#include <format>
27+
#include <span>
2728
#include <stdexcept>
28-
#include <string>
29+
#include <string_view>
2930
#include <vector>
3031

3132

@@ -194,6 +195,40 @@ class CppStringT : public std::basic_string<CharT>
194195
}
195196

196197

198+
//--- endswith() --------------------------------------
199+
/** Returns true if the string ends with the specified suffix, otherwise return false. Test begins at start position and stops at end position. */
200+
inline const bool endswith(const CppStringT& suffix, const size_type start, const size_type end) const noexcept
201+
{
202+
return endswith(std::span{ suffix }, start, end);
203+
}
204+
205+
/** Returns true if the string ends with the specified suffix, otherwise return false. Test begins at start position and stops at end of string. */
206+
inline const bool endswith(const CppStringT& suffix, const size_type start) const noexcept
207+
{
208+
return endswith(std::span{ suffix }, start, this->size()-1);
209+
}
210+
211+
/** Returns true if the string ends with the specified suffix, otherwise return false. Test runs on the whole string. */
212+
inline const bool endswith(const CppStringT& suffix) const noexcept
213+
{
214+
return this->ends_with(suffix);
215+
}
216+
217+
/** Returns true if the string ends with any of the specified suffixes, otherwise return false. Test begins at start position and stops at end of string. */
218+
inline const bool endswith(const std::span<CppStringT>& suffixes, const size_type start, const size_type end) const noexcept
219+
{
220+
if (start > end)
221+
return false;
222+
223+
for (auto& suffix : suffixes) {
224+
if (this->substr(start, end).ends_with(suffix))
225+
return true;
226+
}
227+
228+
return false;
229+
}
230+
231+
197232
//--- find_n() ----------------------------------------
198233
/** Returns the lowest index in the string where substring sub is found within the slice str[start:start+count-1], or -1 (i.e. 'npos') if sub is not found.
199234
*

0 commit comments

Comments
 (0)