ShlubluLib  v0.5
ShlubluLib is a lightweight, modular, general purpose, open-source C++ library for Linux and Windows.
String.h
Go to the documentation of this file.
1 #pragma once
2 
9 #include <algorithm>
10 #include <iomanip>
11 #include <sstream>
12 #include <vector>
13 
14 namespace shlublu
15 {
16 
20 namespace String
21 {
34  template <typename T>
35  inline std::string xtos(T arg)
36  {
37  static_assert(std::is_arithmetic<T>::value || std::is_pointer<T>::value, "Type should be either arithmetic or pointer.");
38 
39  std::ostringstream buffer;
40  buffer << arg;
41 
42  return buffer.str();
43  }
44 
45 
57  template <typename T>
58  inline std::string xtofs(T arg)
59  {
60  static_assert(std::is_floating_point<T>::value, "Type should be floating point.");
61 
62  std::ostringstream buffer;
63  buffer << std::fixed;
64  buffer << arg;
65 
66  return buffer.str();
67  }
68 
69 
84  std::vector<std::string> const & split(std::string const& s, char delim, std::vector<std::string>& elems);
85 
86 
98  std::vector<std::string> split(std::string const& s, char delim);
99 
100 
113  std::string& ltrim(std::string& s);
114 
115 
128  std::string& rtrim(std::string& s); //
129 
130 
143  std::string& trim(std::string& s);
144 
145 
160  std::string& replace(std::string& source, std::string const& find, std::string const& replaceBy);
161 
162 
174  std::string& lower(std::string& s);
175 
176 
188  std::string lower(std::string const& s);
189 
190 
202  std::string& upper(std::string& s);
203 
204 
216  std::string upper(std::string const& s);
217 
218 
230  std::wstring toWString(std::string const& str);
231 
232 
244  std::string fromWString(std::wstring const& wstr);
245 
246 
258  size_t levenshteinDistance(std::string const & s, std::string const & t);
259 }
260 
261 }
shlublu::String::fromWString
std::string fromWString(std::wstring const &wstr)
Returns a string version of the given UTF-8 wstring.
shlublu::String::xtofs
std::string xtofs(T arg)
Converts floating point values to std::string in fixed notation.
Definition: String.h:58
shlublu::String::toWString
std::wstring toWString(std::string const &str)
Returns an UTF-8 wstring version of the given string.
shlublu::String::rtrim
std::string & rtrim(std::string &s)
Trims the trailing blank characters of a string.
shlublu::String::trim
std::string & trim(std::string &s)
Trims the leading and trailing blank characters of a string.
shlublu
shlublu::String::replace
std::string & replace(std::string &source, std::string const &find, std::string const &replaceBy)
Replaces all occurences of a substring in a string.
shlublu::String::lower
std::string & lower(std::string &s)
Converts a string to lowercase.
shlublu::String::xtos
std::string xtos(T arg)
Converts arithmetic values or pointers to std::string.
Definition: String.h:35
shlublu::String::split
std::vector< std::string > const & split(std::string const &s, char delim, std::vector< std::string > &elems)
Splits a string delimited by a given character and stores the substrings in the given target vector.
shlublu::String::levenshteinDistance
size_t levenshteinDistance(std::string const &s, std::string const &t)
Computes the Levenshtein distance between two strings.
shlublu::String::ltrim
std::string & ltrim(std::string &s)
Trims the leading blank characters of a string.
shlublu::String::upper
std::string & upper(std::string &s)
Converts a string to uppercase.