OGS 6.1.0-1721-g6382411ad
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  strList.push_back(item);
45  return strList;
46 }
47 
48 std::string replaceString(const std::string &searchString,
49  const std::string &replaceString,
50  std::string stringToReplace)
51 {
52  boost::replace_all(stringToReplace, searchString, replaceString);
53  return stringToReplace;
54 }
55 
56 void trim(std::string &str, char ch)
57 {
58  std::string::size_type pos = str.find_last_not_of(ch);
59  if(pos != std::string::npos)
60  {
61  str.erase(pos + 1);
62  pos = str.find_first_not_of(ch);
63  if(pos != std::string::npos)
64  str.erase(0, pos);
65  }
66  else
67  str.erase(str.begin(), str.end());
68 }
69 
70 void simplify(std::string &str)
71 {
72  trim (str);
73  str.erase(
74  std::unique(str.begin(), str.end(), [](char a, char b) { return a == ' ' && b == ' '; }),
75  str.end()
76  );
77 }
78 
79 std::string padLeft(std::string const& str, int maxlen, char ch)
80 {
81  std::stringstream ss(str);
82  ss << std::right << std::setw(maxlen) << std::setfill(ch) << str;
83  return ss.str();
84 }
85 
86 std::string const& tostring(std::string const& value)
87 {
88  return value;
89 }
90 
91 std::string format(const char* format_str, ... )
92 {
93  va_list args;
94  va_start(args, format_str);
95  // get the number of chars to write
96  va_list args_tmp;
97  va_copy(args_tmp, args);
98  int char_length = std::vsnprintf(nullptr, 0, format_str, args_tmp);
99  va_end(args_tmp);
100  // allocate buffer and store formatted output there
101  std::vector<char> buffer(char_length + 1); // note +1 for null terminator
102  vsnprintf(buffer.data(), buffer.size(), format_str, args);
103  va_end(args);
104 
105  return std::string(buffer.data());
106 }
107 
108 } // end namespace BaseLib
Definition of string helper functions.
std::string padLeft(std::string const &str, int maxlen, char ch)
Definition: StringTools.cpp:79
std::string format(const char *format_str,...)
returns printf-like formatted string
Definition: StringTools.cpp:91
std::string const & tostring(std::string const &value)
Definition: StringTools.cpp:86
Build information.
void simplify(std::string &str)
Definition: StringTools.cpp:70
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:48
void trim(std::string &str, char ch)
Definition: StringTools.cpp:56