stringFunc.hh

Go to the documentation of this file.
1 
6 #ifndef StringFuns_hh
7 #define StringFuns_hh
8 
9 #include <string>
10 #include <vector>
11 #include <iomanip>
12 #include "basics/outputOperator.hh"
13 #include "basics/typedefs.hh"
14 #include "toolbox/sequence.hh"
15 #include "toolbox/set.hh"
16 
17 namespace concepts {
18 
19  // *************************************************************** tolower **
20 
21  char tolower(const char ch);
22 
23  // ********************************************************* stringtolower **
24 
25  std::string stringtolower(const std::string s);
26 
27  // ******************************************************** removeAllWhite **
28 
30  std::string removeAllWhite(const std::string str);
31 
32  // *********************************************************** getFilename **
33 
35  std::string getFilename(const std::string str);
36 
37  // ***************************************************** getFilenamePrefix **
38 
42  std::string getFilenamePrefix(const std::string str);
43 
44  // ********************************************************** getDirectory **
45 
47  std::string getDirectory(const std::string str);
48 
49  // *********************************************************** splitString **
50 
54  std::vector<std::string> splitString(const std::string text,
55  const std::string separators);
56 
57 
58  // ************************************************* splitStringNameParams **
59 
62  std::vector<std::string> splitStringNameParams(const std::string text);
63 
64  // **************************************************** splitStringByComma **
65 
72  std::vector<std::string> splitStringByComma(const std::string text);
73 
74  // ************************************************************ stringSubs **
75 
80  template<class F>
81  std::string stringSubs(const std::string str,
82  const std::string var, F value) {
83  std::stringstream s;
84  s << std::setprecision(16) << value;
85  std::string sNew = str;
86 
87  std::string::size_type i;
88  while((i = sNew.find(var)) != std::string::npos)
89  sNew.replace(i, var.size(), s.str());
90  return sNew;
91  }
92 
93 
94  // ************************************************* ParseObjectFromString **
95 
101  template<class F>
102  class ParseObjectFromString : public virtual OutputOperator {
103  public:
105  ParseObjectFromString(const char* name = "",
106  const Sequence<F> data = Sequence<F>())
107  : name_(name), data_ (data) {}
109  ParseObjectFromString(const char* name, const F data)
110  : name_(name), data_(1) { data_[0] = data; }
112  ParseObjectFromString(const char* name, const F data1, const F data2)
113  : name_(name), data_(2) { data_[0] = data1; data_[1] = data2; }
116  const F data1, const F data2, const F data3)
117  : name_(name), data_(3) {
118  data_[0] = data1;
119  data_[1] = data2;
120  data_[2] = data3;
121  }
122 
124  std::string& name() { return name_; }
126  Sequence<F>& data() { return data_; }
127 
129  bool parse(const std::string s);
130  protected:
131  std::ostream& info(std::ostream& os) const;
132  private:
134  std::string name_;
137  };
138 
139  template<class F>
140  bool ParseObjectFromString<F>::parse(const std::string s) {
142  if (seq.size() == 0) return false;
143 
144  // remove white spaces
145  for(Sequence<std::string>::iterator i = seq.begin(); i != seq.end(); ++i) {
146  std::string t = removeAllWhite(*i); *i = t;
147  }
148  std::stringstream st; st << seq[0];
149  st >> name_;
150  data_.resize(seq.size()-1);
151  for(uint i = 0; i < data_.size(); ++i) {
152  std::stringstream st; st << seq[i+1];
153  st >> data_[i];
154  }
155 
156  return true;
157  }
158 
159  template<class F>
160  std::ostream& ParseObjectFromString<F>::info(std::ostream& os) const {
161  os << concepts::typeOf(*this)<< name_ << '(';
162  for(typename Sequence<F>::const_iterator i = data_.begin();
163  i != data_.end();) {
164  os << *i++;
165  if (i != data_.end())
166  os << ", ";
167  }
168  return os << ')';
169  }
170 
171  // ******************************************** realSeqFromStringWithPower **
172 
182 
183  // ************************************************************ uintSeqSets **
184 
196 
197 } // namespace concepts
198 
199 #endif // StringFuns_hh
bool parse(const std::string s)
Parser from a string.
Definition: stringFunc.hh:140
Sequence< Real > realSeqFromStringWithPower(const std::string s)
Converts a string to a sequence of real numbers, where a power may be allowed to express,...
std::string stringSubs(const std::string str, const std::string var, F value)
Substitute all occurances of a substring var of a string str by value which may be for example a real...
Definition: stringFunc.hh:81
std::vector< std::string > splitString(const std::string text, const std::string separators)
Split the string text string into words, where the separation token are included in separators.
Sequence< F > & data()
Returns the data.
Definition: stringFunc.hh:126
std::string stringtolower(const std::string s)
ParseObjectFromString(const char *name="", const Sequence< F > data=Sequence< F >())
Constructor with default name and default data.
Definition: stringFunc.hh:105
std::vector< std::string > splitStringByComma(const std::string text)
Split a strings in words separated by commas while respecting the bracket hierachies.
ParseObjectFromString(const char *name, const F data1, const F data2)
Constructor with default name and default first two data entries.
Definition: stringFunc.hh:112
ParseObjectFromString(const char *name, const F data)
Constructor with default name and default first data entry.
Definition: stringFunc.hh:109
Sequence< concepts::Set< uint > > uintSeqSets(const std::string s)
Converts a string to a sequence of sets of uint.
std::string getFilenamePrefix(const std::string str)
Returns the prefix of a given full filename, e.g.
Sequence< F > data_
Data of the object.
Definition: stringFunc.hh:136
Class for parsing objects like "Circle(1.0)" or "Edge(1,2)" from a string.
Definition: stringFunc.hh:102
Sequence with operations, output operator, and method of the particular element types.
Definition: sequence.hh:39
std::ostream & info(std::ostream &os) const
Returns information in an output stream.
Definition: stringFunc.hh:160
std::vector< std::string > splitStringNameParams(const std::string text)
Split a string like "Ellipse(1.0, 4)" into "Ellipse", "1.0", "4".
ParseObjectFromString(const char *name, const F data1, const F data2, const F data3)
Constructor with default name and default first three data entries.
Definition: stringFunc.hh:115
std::string getDirectory(const std::string str)
Returns the directory of a given full filename.
std::string & name()
Returns the name of the object.
Definition: stringFunc.hh:124
std::string getFilename(const std::string str)
Returns the filename (with ending) of a given full filename.
std::string removeAllWhite(const std::string str)
Removes all white space in the string str.
char tolower(const char ch)
std::string name_
Name of the object.
Definition: stringFunc.hh:134
std::string typeOf(const T &t)
Return the typeid name of a class object.
Definition: output.hh:43
Class providing an output operator.
Basic namespace for Concepts-2.
Definition: pml_formula.h:16
Page URL: http://wiki.math.ethz.ch/bin/view/Concepts/WebHome
21 August 2020
© 2020 Eidgenössische Technische Hochschule Zürich