32#ifndef STRINGMAPPER_H_
33#define STRINGMAPPER_H_
39#include <drain/Enum.h>
40#include <drain/RegExp.h>
41#include <drain/Sprinter.h>
45#include "VariableFormatter.h"
59 Stringlet(
const std::string & s =
"",
bool isVariable =
false) : std::string(s), isVar(isVariable) {
67 bool isVariable()
const {
return isVar; };
70 void setVariable(
bool isVariable=
true) { isVar = isVariable; };
73 void setLiteral(
const std::string &s) { assign(s); isVar =
false; };
87std::ostream & operator<<(std::ostream & ostr,
const Stringlet & s) {
89 return ostr <<
"${" << (
const std::string &) s <<
"}";
91 return ostr << (
const std::string &) s;
126 const std::string & format =
"",
127 const std::string & validChars =
"[a-zA-Z0-9_]+",
129 ): formatting(formatting)
131 setValidChars(validChars);
132 regExp.setFlags(REG_EXTENDED);
150 StringMapper & setValidChars(
const std::string & chars){
151 this->validChars = chars;
168 if (this->formatting != formatting){
169 this->formatting = formatting;
173 this->formatting = formatting;
191 std::string & convertEscaped(std::string &s);
204 std::ostream &
toStream(std::ostream & ostr)
const {
214 REMOVE_MISSING_VARIABLE,
215 KEEP_MISSING_VARIABLE
225 std::ostream &
toStream(std::ostream & ostr,
const std::map<std::string,T> & variables,
int replace = REMOVE_MISSING_VARIABLE,
228 for (
const Stringlet & stringlet: *
this){
230 if (stringlet.isVariable()){
232 if (formatter.handle(stringlet, variables, ostr)){
236 else if (replace == KEEP_MISSING_VARIABLE){
239 else if (replace != REMOVE_MISSING_VARIABLE){
240 ostr << (char)replace;
258 toStream(s, m, replaceChar, formatter);
269 for (
auto & entry: *
this){
270 if (entry.isVariable()){
272 if (formatter.handle(entry, m, s)){
273 entry.setLiteral(s.str());
276 entry.setLiteral(
"");
285 std::ostream &
debug(std::ostream & ostr,
const std::map<std::string,T> &m )
const {
289 ostr <<
"StringMapper '"<<
"', RegExp='" << regExp <<
"', " << size() <<
" segments:\n";
291 for (
const auto & entry: *
this){
293 if (entry.isVariable()){
294 ostr << entry <<
"=";
295 if (!formatter.
handle(entry, m, ostr)){
301 ostr <<
"'" << entry <<
"'";
314 std::stringstream sstr;
315 sstr <<
"^(.*)\\$\\{(" << validChars;
317 sstr <<
"(\\|[^}]*)?";
321 regExp.setExpression(sstr.str());
326 std::string validChars;
335std::ostream & operator<<(std::ostream & ostr,
const StringMapper & strmap){
336 return strmap.toStream(ostr);
343DRAIN_TYPENAME(StringMapper::handleMissing);
345DRAIN_ENUM_DICT(StringMapper::handleMissing);
static std::ostream & sequenceToStream(std::ostream &ostr, const T &x, const SprinterLayout &layout)
Convenience: if sequence type (array, list, set, map) not given, assume array.
Definition Sprinter.h:324
static const SprinterLayout emptyLayout
Simply concatenate values without punctuation.
Definition Sprinter.h:209
Definition StringMapper.h:114
bool isLiteral() const
Return true, if all the elements are literal.
Definition StringMapper.cpp:135
StringMapper(const StringMapper &mapper)
Copy constructor copies the parsed string and the regExp.
Definition StringMapper.h:145
StringMapper(const std::string &format="", const std::string &validChars="[a-zA-Z0-9_]+", bool formatting=true)
Default constructor.
Definition StringMapper.h:125
StringMapper & parse(const std::string &s, bool convertEscaped=false)
Converts a std::string containing variables like in "Hello, ${NAME}!" to a list of StringLet's.
Definition StringMapper.cpp:68
std::ostream & toStream(std::ostream &ostr, const std::map< std::string, T > &variables, int replace=REMOVE_MISSING_VARIABLE, const VariableFormatter< T > &formatter=VariableFormatter< T >()) const
Expands the variables in the last.
Definition StringMapper.h:225
void expand(const std::map< std::string, T > &m, const VariableFormatter< T > &formatter=VariableFormatter< T >(), bool clear=false)
Expands the variables in StringMapper, turning expanded variables to constants.
Definition StringMapper.h:268
std::ostream & debug(std::ostream &ostr, const std::map< std::string, T > &m) const
Dumps the list of StringLet's.
Definition StringMapper.h:285
StringMapper(const RegExp ®exp, bool formatting=true)
Initialize with the given RegExp // REMOVE!
Definition StringMapper.h:139
StringMapper & enableFormatting(bool formatting)
Enable variable formatting, followed by pipe '|'.
Definition StringMapper.h:167
std::ostream & toStream(std::ostream &ostr) const
Output a concatenated chain of stringlets: literals as such and variables surrounded with "${" and "}...
Definition StringMapper.h:204
std::string toStr(const std::map< std::string, T > &m, int replaceChar=-1, const VariableFormatter< T > &formatter=VariableFormatter< T >()) const
Expands the variables in the last parsed std::string to a std::string.
Definition StringMapper.h:256
A helper class for StringMapper.
Definition StringMapper.h:55
Stringlet(const Stringlet &s)
Copy constructor.
Definition StringMapper.h:63
Definition DataSelector.cpp:1277