KSSUtil  v14.2.0
C++ general utility library
stringutil.hpp
Go to the documentation of this file.
1 //
2 // stringutil.hpp
3 // kssutil
4 //
5 // Created by Steven W. Klassen on 2012-12-21.
6 // Copyright (c) 2012 Klassen Software Solutions. All rights reserved.
7 // Licensing follows the MIT License.
8 //
9 
15 #ifndef kssutil_stringutil_hpp
16 #define kssutil_stringutil_hpp
17 
18 #include <string>
19 
20 namespace kss { namespace util { namespace strings {
21 
27  std::string format(std::string pattern, ...);
28  std::string vformat(const std::string& pattern, va_list ap);
29 
36  std::string& ltrim(std::string& s) noexcept;
37  std::string& ltrim(std::string& s, char c) noexcept;
38  std::string& rtrim(std::string& s) noexcept;
39  std::string& rtrim(std::string& s, char c) noexcept;
40  inline std::string& trim(std::string& s) noexcept { return ltrim(rtrim(s)); }
41  inline std::string& trim(std::string& s, char c) noexcept { return ltrim(rtrim(s, c), c); }
42 
46  bool startsWith(const std::string& str, const std::string& prefix) noexcept;
47  bool endsWith(const std::string& str, const std::string& suffix) noexcept;
48 
55  std::string& toUpper(std::string& s) noexcept;
56  inline std::string toUpper(const std::string& s) {
57  std::string s2(s);
58  return toUpper(s2);
59  }
60 
61  std::wstring& toUpper(std::wstring& s) noexcept;
62  inline std::wstring toUpper(const std::wstring& s) {
63  std::wstring s2(s);
64  return toUpper(s2);
65  }
66 
67  std::string& toLower(std::string& s) noexcept;
68  inline std::string toLower(const std::string& s) {
69  std::string s2(s);
70  return toLower(s2);
71  }
72 
73  std::wstring& toLower(std::wstring& s) noexcept;
74  inline std::wstring toLower(const std::wstring& s) {
75  std::wstring s2(s);
76  return toLower(s2);
77  }
78 
79 
85  bool iequal(const std::string& a, const std::string& b) noexcept;
86  bool iequal(const std::wstring& a, const std::wstring& b) noexcept;
87  int icompare(const std::string& a, const std::string& b) noexcept;
88  int icompare(const std::wstring& a, const std::wstring& b) noexcept;
89 
90 
103  template <class Char, class traits = std::char_traits<Char>, class Alloc = std::allocator<Char>>
104  unsigned countOccurrencesOf(const std::basic_string<Char, traits, Alloc>& source,
105  const std::basic_string<Char, traits, Alloc>& substr,
106  bool allowOverlaps = false) noexcept
107  {
108  using string_t = std::basic_string<Char, traits, Alloc>;
109  const typename string_t::size_type step = (allowOverlaps ? 1 : substr.size());
110  unsigned count = 0;
111  typename string_t::size_type pos = 0;
112 
113  while ((pos = source.find(substr, pos)) != string_t::npos) {
114  pos += step;
115  ++count;
116  }
117 
118  return count;
119  }
120 
121  template <class Char, class Traits = std::char_traits<Char>, class Alloc = std::allocator<Char>>
122  inline unsigned countOccurrencesOf(const std::basic_string<Char, Traits, Alloc>& source,
123  const Char* substr,
124  bool allowOverlaps = false) noexcept
125  {
126  using string_t = std::basic_string<Char, Traits, Alloc>;
127  return countOccurrencesOf(source, string_t(substr), allowOverlaps);
128  }
129 }}}
130 
131 #endif
kss::util::strings::vformat
std::string vformat(const std::string &pattern, va_list ap)
kss::util::strings::rtrim
std::string & rtrim(std::string &s) noexcept
kss::util::strings::toUpper
std::string & toUpper(std::string &s) noexcept
kss::util::strings::countOccurrencesOf
unsigned countOccurrencesOf(const std::basic_string< Char, traits, Alloc > &source, const std::basic_string< Char, traits, Alloc > &substr, bool allowOverlaps=false) noexcept
Definition: stringutil.hpp:104
kss::util::strings::iequal
bool iequal(const std::string &a, const std::string &b) noexcept
kss::util::strings::startsWith
bool startsWith(const std::string &str, const std::string &prefix) noexcept
kss::util::strings::endsWith
bool endsWith(const std::string &str, const std::string &suffix) noexcept
kss::util::strings::toLower
std::string & toLower(std::string &s) noexcept
kss::util::strings::trim
std::string & trim(std::string &s) noexcept
Definition: stringutil.hpp:40
kss::util::strings::ltrim
std::string & ltrim(std::string &s) noexcept
kss
All Klassen Software Solutions libraries begin with this namespace.
Definition: add_rel_ops.hpp:19
kss::util::strings::icompare
int icompare(const std::string &a, const std::string &b) noexcept
kss::util::strings::format
std::string format(std::string pattern,...)