String.h
Go to the documentation of this file.
1 #ifndef WIRECELLUTIL_STRING
2 #define WIRECELLUTIL_STRING
3 
4 #include <boost/algorithm/string.hpp>
5 #include <boost/format.hpp>
6 
7 #include <vector>
8 #include <string>
9 #include <map>
10 #include <sstream>
11 
12 namespace WireCell {
13 
14  namespace String {
15 
16  std::vector<std::string> split(const std::string& in, const std::string& delim=":");
17 
18  std::pair<std::string,std::string> parse_pair(const std::string& in, const std::string& delim=":");
19 
20  // format_flatten converts from "%"-list to variadic function call.
21  template<typename TYPE>
23  return f % obj;
24  }
25  template<typename TYPE, typename... MORE>
26  boost::format format_flatten(boost::format start, TYPE o, MORE... objs) {
27  auto next = start % o;
28  return format_flatten(next, objs...);
29  }
31  return f;
32  }
33 
34  /** The format() function provides an sprintf() like function.
35  It's a wrapper on boost::format() but returns a string
36  instead of a stream and has function calling semantics
37  instead of the "%" list. Use like:
38 
39  int a=42;
40  std::string foo = "bar";
41  std::string msg = format("the answer to %s is %d", foo, a);
42  */
43 
44  template<typename... TYPES>
45  std::string format(const std::string& form, TYPES... objs) {
46  auto final = format_flatten(boost::format(form), objs...);
47  std::stringstream ss;
48  ss << final;
49  return ss.str();
50  }
51 
52 
53  // Turn some type into a string via streaming.
54  template<typename T>
56  std::stringstream ss;
57  ss << obj;
58  return ss.str();
59  }
60 
61  }
62 }
63 
64 #endif
string delim()
Definition: fcldump.cxx:41
std::string string
Definition: nybbler.cc:12
boost::format format_flatten(boost::format f, TYPE obj)
Definition: String.h:22
std::pair< std::string, std::string > parse_pair(const std::string &in, const std::string &delim=":")
Definition: String.cxx:15
std::string format(const std::string &form, TYPES...objs)
Definition: String.h:45
Definition: Main.h:22
std::vector< std::string > split(const std::string &in, const std::string &delim=":")
Definition: String.cxx:5
std::string stringify(T obj)
Definition: String.h:55