Skip to content

Commit 895404b

Browse files
committed
#66 - Fix methods find() and find_n()
Completed.
1 parent 5d49ab1 commit 895404b

File tree

1 file changed

+36
-17
lines changed

1 file changed

+36
-17
lines changed

cpp-strings/cppstrings.h

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -179,47 +179,51 @@ class CppStringT : public std::basic_string<CharT>
179179
//--- find() ------------------------------------------
180180
/** Returns the lowest 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.
181181
*
182-
* Note: this method should be used only if you need to know the position
183-
* of sub. To check if sub is a substring or not, use the method contains().
184-
*
182+
* Note: this method should be used only if you need to know the position of
183+
* sub. To check if sub is a substring or not, use the method contains_n().
184+
*
185185
* \see find_n(), rfind() and rfind_n().
186186
* \see index(), index_n(), rindex() and rindex_n().
187187
*/
188188
inline constexpr size_type find(const CppStringT& sub, const size_type start, const size_type end) const noexcept
189189
{
190-
const size_type length{ this->length() };
191-
if (start >= length || start > end)
190+
if (start > end)
192191
return CppStringT::npos;
193192
else
194-
return MyBaseClass(this->cbegin() + start, this->cbegin() + std::min(end, length - 1)).find(sub);
193+
return find_n(sub, start, end - start + 1);
195194
}
196195

197196

198197
//--- find_n() ----------------------------------------
199-
/** Returns the lowest index in the string where substring sub is found within the slice str[start:start+length-1], or -1 (i.e. 'npos') if sub is not found.
198+
/** 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.
200199
*
201-
* Note: this method should be used only if you need to know the position
202-
* of sub. To check if sub is a substring or not, use the method contains_n().
200+
* Note: this method should be used only if you need to know the position of
201+
* sub. To check if sub is a substring or not, use the method contains_n().
203202
*
204203
* \see find(), rfind() and rfind_n().
205204
* \see index(), index_n(), rindex() and rindex_n().
206205
*/
207-
inline constexpr size_type find_n(const CppStringT& sub, const size_type start, const size_type length) const noexcept
206+
inline constexpr size_type find_n(const CppStringT& sub, const size_type start, const size_type count) const noexcept
208207
{
209-
return find(sub, start, start + length - 1);
208+
try {
209+
return this->substr(start, count).find(sub);
210+
}
211+
catch (...) {
212+
return CppStringT::npos;
213+
}
210214
}
211215

212-
/** Returns the lowest index in the string where substring sub is found within the slice str[0:length-1], or -1 (i.e. 'npos') if sub is not found.
216+
/** Returns the lowest index in the string where substring sub is found within the slice str[0:count-1], or -1 (i.e. 'npos') if sub is not found.
213217
*
214-
* Note: this method should be used only if you need to know the position
215-
* of sub. To check if sub is a substring or not, use the method contains_n().
218+
* Note: this method should be used only if you need to know the position of
219+
* sub. To check if sub is a substring or not, use the method contains_n().
216220
*
217221
* \see find(), rfind() and rfind_n().
218222
* \see index(), index_n(), rindex() and rindex_n().
219223
*/
220-
inline constexpr size_type find_n(const CppStringT& sub, const size_type length) const noexcept
224+
inline constexpr size_type find_n(const CppStringT& sub, const size_type count) const noexcept
221225
{
222-
return find(sub, 0, length - 1);
226+
return find_n(sub, 0, count);
223227
}
224228

225229

@@ -250,7 +254,7 @@ class CppStringT : public std::basic_string<CharT>
250254
return index_n(sub, start, start + length - 1);
251255
}
252256

253-
/** Like find_n(sub, length), but raises NotFoundException when the substring is not found.
257+
/** Like find_n(sub, start), but raises NotFoundException when the substring is not found.
254258
*
255259
* \see index_n(), rindex() and rindex_n().
256260
* \see find(), find_n(), rfind() and rfind_n().
@@ -388,6 +392,21 @@ class CppStringT : public std::basic_string<CharT>
388392
}
389393

390394

395+
//--- rfind_n() ---------------------------------------
396+
/** Returns the highest index in the string where substring sub is found within the slice str[start:start+length-1], or -1 (i.e. 'npos') if sub is not found.
397+
*
398+
* Note: this method should be used only if you need to know the position
399+
* of sub. To check if sub is a substring or not, use the method contains_n().
400+
*
401+
* \see find(), find_n() and rfind().
402+
* \see index(), index_n(), rindex() and rindex_n().
403+
*/
404+
inline constexpr size_type rfind_n(const CppStringT& sub, const size_type start, const size_type length) const noexcept
405+
{
406+
return rfind(sub, start, start + length - 1);
407+
}
408+
409+
391410
//--- rindex() ----------------------------------------
392411
/** Like rfind(sub, start, end), but raises NotFoundException when the substring is not found.
393412
*

0 commit comments

Comments
 (0)