OGS 6.2.2-330-gf48c72f61.dirty.20200225212913
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 const& tostring(std::string const& value)
86 {
87  return value;
88 }
89 
90 std::string format(const char* format_str, ... )
91 {
92  va_list args;
93  va_start(args, format_str);
94  // get the number of chars to write
95  va_list args_tmp;
96  va_copy(args_tmp, args);
97  int char_length = std::vsnprintf(nullptr, 0, format_str, args_tmp);
98  va_end(args_tmp);
99  // allocate buffer and store formatted output there
100  std::vector<char> buffer(char_length + 1); // note +1 for null terminator
101  vsnprintf(buffer.data(), buffer.size(), format_str, args);
102  va_end(args);
103 
104  return std::string(buffer.data());
105 }
106 
107 } // end namespace BaseLib
Definition of string helper functions.
std::string format(const char *format_str,...)
returns printf-like formatted string
Definition: StringTools.cpp:90
std::string const & tostring(std::string const &value)
Definition: StringTools.cpp:85
Functionality to build different search length algorithm objects from given config.
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:37
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