NAP
stringutils.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 // std includes
8 #include <list>
9 #include <sstream>
10 #include <string>
11 #include <vector>
12 #include <algorithm>
13 #include <locale>
14 #include <memory>
15 #include <unordered_map>
16 
17 namespace nap
18 {
23  namespace utility
24  {
25  /*
26  * Splits a string based on the given delimiter. Results are stored in ioParts.
27  * @param string the sequence to split
28  * @param delim the character used to split the string
29  * @param ioParts list of individual split parts
30  */
31  void splitString(const std::string& string, const char delim, std::vector<std::string>& ioParts);
32 
33  /*
34  * Splits a string based on the given delimiter.
35  * @param string the sequence to split.
36  * @param delim the delimiter used to split the string.
37  * @return vector of split parts.
38  */
39  std::vector<std::string> splitString(const std::string& string, const char delim);
40 
48  template<typename T>
49  std::string joinString(const T& list, const char* delim);
50 
56  void writeString(std::ostream& stream, const std::string& text);
57 
63  std::string readString(std::istream& stream);
64 
69  void toLower(std::string& ioString);
70 
76  std::string toLower(const std::string& string);
77 
82  void toUpper(std::string& ioString);
83 
89  std::string toUpper(const std::string& string);
90 
96  std::string stripNamespace(const std::string& str);
97 
105  void tokenize(const std::string& str, std::list<std::string>& tokens, const std::string& delims, bool omitTokens = false);
106 
114  bool startsWith(const std::string& string, const std::string& subString, bool caseSensitive = true);
115 
123  bool endsWith(const std::string& string, const std::string& subString, bool caseSensitive = true);
124 
132  bool contains(const std::string& string, const std::string& subString, bool caseSensitive = true);
133 
139  std::string trim(const std::string& string);
140 
146  std::string lTrim(const std::string& string);
147 
153  std::string rTrim(const std::string& string);
154 
160  template <typename T>
161  std::string addresStr(T thing);
162 
170  template <typename... Args>
171  static std::string stringFormat(const char* msg, Args&&... args);
172 
188  void namedFormat(std::string& subject, const std::unordered_map<std::string, std::string>& rep);
189 
204  void namedFormat(std::vector<std::string>& subjects, const std::unordered_map<std::string, std::string>& rep);
205 
212  std::string replaceTemplateType(const std::string& typeName, const std::string& templateTypeName);
213 
220  void replaceAllInstances(std::string& inString, const std::string& find, const std::string& replace);
221 
229  std::string replaceAllInstances(const std::string& inString, const std::string& find, const std::string& replace);
230 
237  int getLine(const std::string& buffer, size_t offset);
238 
239 
241  // Template Definitions
243 
244  template<typename T>
245  std::string joinString(const T& list, const char* delim)
246  {
247  std::stringstream ss;
248  for (size_t i = 0, len = list.size(); i < len; i++)
249  {
250  if (i > 0)
251  ss << delim;
252  ss << list.at(i);
253  }
254  return ss.str();
255  }
256 
257 
258  template <typename... Args>
259  std::string stringFormat(const char* format, Args&&... args)
260  {
261  size_t size = (size_t)(snprintf(nullptr, 0, format, args...) + 1); // Extra space for '\0'
262  std::unique_ptr<char[]> buf(new char[size]);
263  snprintf(buf.get(), size, format, args...);
264  return std::string(buf.get(), buf.get() + size - 1); // We don't want the '\0' inside
265  }
266 
267  template <typename T>
268  std::string addresStr(T thing)
269  {
270  const void* addr = static_cast<const void*>(thing);
271  std::stringstream ss;
272  ss << addr;
273  return ss.str();
274  }
275  }
276 }
nap::utility::endsWith
bool endsWith(const std::string &string, const std::string &subString, bool caseSensitive=true)
nap::utility::contains
bool contains(const std::string &string, const std::string &subString, bool caseSensitive=true)
nap::utility::toUpper
void toUpper(std::string &ioString)
nap::utility::tokenize
void tokenize(const std::string &str, std::list< std::string > &tokens, const std::string &delims, bool omitTokens=false)
nap::utility::toLower
void toLower(std::string &ioString)
nap::utility::replaceTemplateType
std::string replaceTemplateType(const std::string &typeName, const std::string &templateTypeName)
nap::utility::namedFormat
void namedFormat(std::string &subject, const std::unordered_map< std::string, std::string > &rep)
nap::utility::splitString
void splitString(const std::string &string, const char delim, std::vector< std::string > &ioParts)
nap::utility::joinString
std::string joinString(const T &list, const char *delim)
Definition: stringutils.h:245
nap::utility::lTrim
std::string lTrim(const std::string &string)
nap::utility::stripNamespace
std::string stripNamespace(const std::string &str)
nap::utility::rTrim
std::string rTrim(const std::string &string)
nap::utility::readString
std::string readString(std::istream &stream)
nap
Definition: templateapp.h:17
nap::utility::trim
std::string trim(const std::string &string)
nap::utility::startsWith
bool startsWith(const std::string &string, const std::string &subString, bool caseSensitive=true)
nap::utility::getLine
int getLine(const std::string &buffer, size_t offset)
nap::utility::addresStr
std::string addresStr(T thing)
Definition: stringutils.h:268
nap::utility::writeString
void writeString(std::ostream &stream, const std::string &text)
nap::utility::replaceAllInstances
void replaceAllInstances(std::string &inString, const std::string &find, const std::string &replace)