Skip to content

Commit de3e8e0

Browse files
committed
#40 - Implement method CppStringT::removesuffix()
Completed. Fixed wrong ordering on methods definitions.
1 parent a9733c0 commit de3e8e0

File tree

1 file changed

+33
-20
lines changed

1 file changed

+33
-20
lines changed

cpp-strings/cppstrings.h

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -216,24 +216,6 @@ namespace pcs // i.e. "pythonic c++ strings"
216216
}
217217

218218

219-
//--- find() ------------------------------------------
220-
/** 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.
221-
*
222-
* Note: this method should be used only if you need to know the position of
223-
* sub. To check if sub is a substring or not, use the method contains_n().
224-
*
225-
* \see find_n(), rfind() and rfind_n().
226-
* \see index(), index_n(), rindex() and rindex_n().
227-
*/
228-
inline constexpr size_type find(const CppStringT& sub, const size_type start, const size_type end) const noexcept
229-
{
230-
if (start > end)
231-
return CppStringT::npos;
232-
else
233-
return find_n(sub, start, end - start + 1);
234-
}
235-
236-
237219
//--- endswith() --------------------------------------
238220
/** Returns true if the string ends with the specified suffix, otherwise returns false. Test begins at start position and stops at end position. */
239221
inline const bool endswith(const CppStringT& suffix, const size_type start, const size_type end) const noexcept
@@ -269,7 +251,7 @@ namespace pcs // i.e. "pythonic c++ strings"
269251

270252

271253
//--- endswith_n() ------------------------------------
272-
/** Returns true if the string ends with the specified suffix, otherwise returns false. Test begins at start position and stops after count positions. */
254+
/** Returns true if the string ends with the specified suffix, otherwise returns false. Test begins at start position and stops after count positions. */
273255
inline const bool endswith_n(const CppStringT& suffix, const size_type start, const size_type count) const noexcept
274256
{
275257
return endswith(std::span{ suffix }, start, start + count - 1);
@@ -317,6 +299,24 @@ namespace pcs // i.e. "pythonic c++ strings"
317299
}
318300

319301

302+
//--- find() ------------------------------------------
303+
/** 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.
304+
*
305+
* Note: this method should be used only if you need to know the position of
306+
* sub. To check if sub is a substring or not, use the method contains_n().
307+
*
308+
* \see find_n(), rfind() and rfind_n().
309+
* \see index(), index_n(), rindex() and rindex_n().
310+
*/
311+
inline constexpr size_type find(const CppStringT& sub, const size_type start, const size_type end) const noexcept
312+
{
313+
if (start > end)
314+
return CppStringT::npos;
315+
else
316+
return find_n(sub, start, end - start + 1);
317+
}
318+
319+
320320
//--- find_n() ----------------------------------------
321321
/** 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.
322322
*
@@ -673,7 +673,20 @@ namespace pcs // i.e. "pythonic c++ strings"
673673
{
674674
if (this->startswith(prefix)) {
675675
const size_type prefix_length = prefix.size();
676-
return this->substr(prefix_length, this->size - prefix_length + 1);
676+
return this->substr(prefix_length, this->size() - prefix_length + 1);
677+
}
678+
else
679+
return *this;
680+
}
681+
682+
683+
//--- removesuffix() ----------------------------------
684+
/** \brief If the string ends with the suffix string, returns a new string with the suffix removed. Otherwise, returns a copy of the original string. */
685+
inline CppStringT removesuffix(const CppStringT& suffix) const noexcept
686+
{
687+
if (this->endswith(suffix)) {
688+
const size_type suffix_length = suffix.size();
689+
return this->substr(0, this->size() - suffix_length + 1);
677690
}
678691
else
679692
return *this;

0 commit comments

Comments
 (0)