@@ -127,27 +127,42 @@ class CppStringT : public std::basic_string<CharT>
127127 */
128128 inline CppStringT center (const size_type width, const value_type fillch = value_type(' ' )) const noexcept
129129 {
130- const size_type l = this ->length ();
130+ const size_type l{ this ->length () } ;
131131 if (l <= width)
132132 return CppStringT (*this );
133- const size_type half = (width - l) / 2 ;
133+ const size_type half{ (width - l) / 2 } ;
134134 return CppStringT (fillch, half) + *this + CppStringT (fillch, width - half - l);
135135 }
136136
137137
138+ // --- count() -----------------------------------------
139+ /* * \brief Returns the number of non-overlapping occurrences of substring sub in the range [start, end]. */
140+ inline constexpr size_type count (const CppStringT& sub, const size_type start = 0 , const size_type end = 0 ) const noexcept
141+ {
142+ const size_type length{ this ->length () };
143+ const size_type end_{ (end == 0 ) ? length : end };
144+
145+ size_type n = 0 ;
146+ size_type start_ = start;
147+ while ((start_ = find (sub, start_, end_)) != CppStringT::npos)
148+ n++;
149+
150+ return n;
151+ }
152+
138153 // --- 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.
154+ /* * 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.
140155 *
141156 * Note: this method should be used only if you need to know the position
142157 * of sub. To check if sub is a substring or not, use the method contains().
143158 */
144159 inline constexpr size_type find (const CppStringT& sub, const size_type start, const size_type end) const noexcept
145160 {
146- const size_type l = this ->length ();
147- if (start >= l || end >= l || start > end)
148- return - 1 ;
161+ const size_type length{ this ->length () } ;
162+ if (start >= length || start > end)
163+ return CppStringT::npos ;
149164 else
150- return MyBaseClass (this ->cbegin () + start, this ->cbegin () + end ( )).find (sub);
165+ return MyBaseClass (this ->cbegin () + start, this ->cbegin () + std::min (end, length - 1 )).find (sub);
151166 }
152167
153168
@@ -237,8 +252,8 @@ class CppStringT : public std::basic_string<CharT>
237252 inline CppStringT& upper () noexcept
238253 {
239254 std::transform (this ->begin (), this ->end (),
240- this ->begin (),
241- [](value_type ch) { return this ->upper (ch); });
255+ this ->begin (),
256+ [](value_type ch) { return this ->upper (ch); });
242257 return *this ;
243258 }
244259
@@ -259,7 +274,7 @@ class CppStringT : public std::basic_string<CharT>
259274
260275private:
261276 // === DATA ============================================
262- static inline constexpr std::vector<value_type> _ASCII_PUNCT_DATA{ ' !' , ' ,' , ' .' , ' :' , ' ;' , ' ?' };
263- static inline constexpr std::vector<value_type> _ASCII_SPACES{ ' ' , ' \t ' , ' \n ' , ' r' , ' \f ' };
277+ static inline constexpr std::vector<value_type> _ASCII_PUNCT_DATA { ' !' , ' ,' , ' .' , ' :' , ' ;' , ' ?' };
278+ static inline constexpr std::vector<value_type> _ASCII_SPACES { ' ' , ' \t ' , ' \n ' , ' r' , ' \f ' };
264279
265280};
0 commit comments