Skip to content

Commit 1dee9cf

Browse files
committed
#16 - Implement method CppStringT::find()
Completed.
1 parent a3c86ef commit 1dee9cf

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

cpp-strings/cppstrings.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ class CppStringT : public std::basic_string<CharT>
119119

120120

121121
//--- center() ----------------------------------------
122-
/** Returns the string centered in a string of length width.
123-
* *
122+
/** \brief Returns the string centered in a string of length width.
123+
*
124124
* Padding is done using the specified fillchar (default is an ASCII space).
125125
* A copy of the original string is returned if width is less than or equal
126126
* to the length of the string. The original string remains unchanged.
@@ -135,6 +135,22 @@ class CppStringT : public std::basic_string<CharT>
135135
}
136136

137137

138+
//--- find() ------------------------------------------
139+
/** Returns the lowest index in the string where substring sub is found within the slice string[start:end], or -1 if sub is not found.
140+
*
141+
* Note: this method should be used only if you need to know the position
142+
* of sub. To check if sub is a substring or not, use the method contains().
143+
*/
144+
inline constexpr size_type find(const CppStringT& sub, const size_type start, const size_type end) const noexcept
145+
{
146+
const size_type l = this->length();
147+
if (start >= l || end >= l || start > end)
148+
return -1;
149+
else
150+
return MyBaseClass(this->cbegin() + start, this->cbegin() + end()).find(sub);
151+
}
152+
153+
138154
//--- is_punctuation() --------------------------------
139155
/** \brief Returns true if the string contains only one character and if this character belongs to the ASCII punctuation set. */
140156
inline const bool is_punctuation() const noexcept

0 commit comments

Comments
 (0)