KSS Utility
C++ general utilities
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends
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 
25  std::string format(std::string pattern, ...);
26  std::string vformat(const std::string& pattern, va_list ap);
27 
34  std::string& ltrim(std::string& s) noexcept;
35  std::string& ltrim(std::string& s, char c) noexcept;
36  std::string& rtrim(std::string& s) noexcept;
37  std::string& rtrim(std::string& s, char c) noexcept;
38  inline std::string& trim(std::string& s) noexcept { return ltrim(rtrim(s)); }
39  inline std::string& trim(std::string& s, char c) noexcept { return ltrim(rtrim(s, c), c); }
40 
44  bool startsWith(const std::string& str, const std::string& prefix) noexcept;
45  bool endsWith(const std::string& str, const std::string& suffix) noexcept;
46 
53  std::string& toUpper(std::string& s) noexcept;
54  inline std::string toUpper(const std::string& s) {
55  std::string s2(s);
56  return toUpper(s2);
57  }
58 
59  std::wstring& toUpper(std::wstring& s) noexcept;
60  inline std::wstring toUpper(const std::wstring& s) {
61  std::wstring s2(s);
62  return toUpper(s2);
63  }
64 
65  std::string& toLower(std::string& s) noexcept;
66  inline std::string toLower(const std::string& s) {
67  std::string s2(s);
68  return toLower(s2);
69  }
70 
71  std::wstring& toLower(std::wstring& s) noexcept;
72  inline std::wstring toLower(const std::wstring& s) {
73  std::wstring s2(s);
74  return toLower(s2);
75  }
76 
77 
83  bool iequal(const std::string& a, const std::string& b) noexcept;
84  bool iequal(const std::wstring& a, const std::wstring& b) noexcept;
85  int icompare(const std::string& a, const std::string& b) noexcept;
86  int icompare(const std::wstring& a, const std::wstring& b) noexcept;
87 
88 
101  template <class Char, class traits = std::char_traits<Char>, class Alloc = std::allocator<Char>>
102  unsigned countOccurrencesOf(const std::basic_string<Char, traits, Alloc>& source,
103  const std::basic_string<Char, traits, Alloc>& substr,
104  bool allowOverlaps = false) noexcept
105  {
106  using string_t = std::basic_string<Char, traits, Alloc>;
107  const typename string_t::size_type step = (allowOverlaps ? 1 : substr.size());
108  unsigned count = 0;
109  typename string_t::size_type pos = 0;
110 
111  while ((pos = source.find(substr, pos)) != string_t::npos) {
112  pos += step;
113  ++count;
114  }
115 
116  return count;
117  }
118 
119  template <class Char, class Traits = std::char_traits<Char>, class Alloc = std::allocator<Char>>
120  inline unsigned countOccurrencesOf(const std::basic_string<Char, Traits, Alloc>& source,
121  const Char* substr,
122  bool allowOverlaps = false) noexcept
123  {
124  using string_t = std::basic_string<Char, Traits, Alloc>;
125  return countOccurrencesOf(source, string_t(substr), allowOverlaps);
126  }
127 }}}
128 
129 #endif
std::string & toUpper(std::string &s) noexcept
int icompare(const std::string &a, const std::string &b) noexcept
std::string vformat(const std::string &pattern, va_list ap)
std::string & toLower(std::string &s) noexcept
bool endsWith(const std::string &str, const std::string &suffix) noexcept
std::string & trim(std::string &s) noexcept
Definition: stringutil.hpp:38
bool iequal(const std::string &a, const std::string &b) noexcept
std::string & rtrim(std::string &s) noexcept
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:102
std::string format(std::string pattern,...)
bool startsWith(const std::string &str, const std::string &prefix) noexcept
std::string & ltrim(std::string &s) noexcept