OGS 6.2.0-97-g4a610c866
StringTools.cpp
Go to the documentation of this file.
1 
15 #include "StringTools.h"
16 
17 #include <algorithm>
18 #include <cctype>
19 #include <cstdarg>
20 #include <cstdio>
21 #include <iomanip>
22 
23 #include <logog/include/logog.hpp>
24 #include <boost/algorithm/string/replace.hpp>
25 
26 namespace BaseLib
27 {
28 std::vector<std::string> splitString(std::string const& str)
29 {
30  std::istringstream str_stream(str);
31  std::vector<std::string> items;
32  std::copy(std::istream_iterator<std::string>(str_stream),
33  std::istream_iterator<std::string>(),
34  std::back_inserter(items));
35  return items;
36 }
37 
38 std::list<std::string> splitString(const std::string &str, char delim)
39 {
40  std::list<std::string> strList;
41  std::stringstream ss(str);
42  std::string item;
43  while (getline(ss, item, delim))
44  {
45  strList.push_back(item);
46  }
47  return strList;
48 }
49 
50 std::string replaceString(const std::string &searchString,
51  const std::string &replaceString,
52  std::string stringToReplace)
53 {
54  boost::replace_all(stringToReplace, searchString, replaceString);
55  return stringToReplace;
56 }
57 
58 void trim(std::string &str, char ch)
59 {
60  std::string::size_type pos = str.find_last_not_of(ch);
61  if(pos != std::string::npos)
62  {
63  str.erase(pos + 1);
64  pos = str.find_first_not_of(ch);
65  if (pos != std::string::npos)
66  {
67  str.erase(0, pos);
68  }
69  }
70  else
71  {
72  str.erase(str.begin(), str.end());
73  }
74 }
75 
76 void simplify(std::string &str)
77 {
78  trim (str);
79  str.erase(
80  std::unique(str.begin(), str.end(), [](char a, char b) { return a == ' ' && b == ' '; }),
81  str.end()
82  );
83 }
84 
85 std::string padLeft(std::string const& str, int maxlen, char ch)
86 {
87  std::stringstream ss(str);
88  ss << std::right << std::setw(maxlen) << std::setfill(ch) << str;
89  return ss.str();
90 }
91 
92 std::string const& tostring(std::string const& value)
93 {
94  return value;
95 }
96 
97 std::string format(const char* format_str, ... )
98 {
99  va_list args;
100  va_start(args, format_str);
101  // get the number of chars to write
102  va_list args_tmp;
103  va_copy(args_tmp, args);
104  int char_length = std::vsnprintf(nullptr, 0, format_str, args_tmp);
105  va_end(args_tmp);
106  // allocate buffer and store formatted output there
107  std::vector<char> buffer(char_length + 1); // note +1 for null terminator
108  vsnprintf(buffer.data(), buffer.size(), format_str, args);
109  va_end(args);
110 
111  return std::string(buffer.data());
112 }
113 
114 } // end namespace BaseLib
Definition of string helper functions.
std::string padLeft(std::string const &str, int maxlen, char ch)
Definition: StringTools.cpp:85
std::string format(const char *format_str,...)
returns printf-like formatted string
Definition: StringTools.cpp:97
std::string const & tostring(std::string const &value)
Definition: StringTools.cpp:92
Build information.
void simplify(std::string &str)
Definition: StringTools.cpp:76
std::vector< std::string > splitString(std::string const &str)
Definition: StringTools.cpp:28
void copy(MatrixOrVector const &x, MatrixOrVector &y)
Copies x to y.
Definition: LinAlg.h:36
std::string replaceString(const std::string &searchString, const std::string &replaceString, std::string stringToReplace)
Definition: StringTools.cpp:50
void trim(std::string &str, char ch)
Definition: StringTools.cpp:58