String.h
Go to the documentation of this file.
1 #ifndef _utl_String_h_
2 #define _utl_String_h_
3 
4 #include <boost/lexical_cast.hpp>
5 #include <boost/algorithm/string/trim.hpp>
6 #include <boost/tokenizer.hpp>
7 #include <sstream>
8 #include <iomanip>
9 #include <algorithm>
10 #include <string>
11 #include <vector>
12 #include <set>
13 #include <type_traits>
14 
15 
16 namespace utl {
17 
23  template<typename T, typename U>
25  inline
26  T
27  As(const U& x)
28  {
29  return boost::lexical_cast<T>(x);
30  }
31 
32 
33  template<typename T>
34  inline
35  std::string
36  AsHex(const T& x, const unsigned int width = 2*sizeof(T))
37  {
38  std::ostringstream oss;
39  oss << "0x" << std::setfill('0') << std::setw(width) << std::hex << x;
40  return oss.str();
41  }
42 
43 
44  template<typename T>
45  inline
46  std::string
47  AsBinary(const T& x, const unsigned int bitWidth = 8*sizeof(T))
48  {
49  std::ostringstream oss;
50  oss << "0b";
51  for (int i = bitWidth-1; i >= 0; --i)
52  oss << ((x >> i) & 1);
53  return oss.str();
54  }
55 
56 
58  template<class S, class V>
59  std::string
60  Join(const S& sep, const V& v)
61  {
62  std::ostringstream oss;
63  if (!v.empty()) {
64  auto it = v.begin();
65  oss << *it++;
66  for (const auto end = v.end(); it != end; ++it)
67  oss << sep << *it;
68  }
69  return oss.str();
70  }
71 
72 
73  namespace String {
74 
75  template<class T>
76  inline
77  std::vector<T>
78  AsVectorOf(const std::string& str)
79  {
80  std::istringstream iss(str);
81  return std::vector<T>(std::istream_iterator<T>(iss), std::istream_iterator<T>());
82  }
83 
84 
85  inline
86  std::string
87  Strip(std::string str)
88  {
89  boost::algorithm::trim(str);
90  return str;
91  }
92 
93 
94  std::vector<std::string> Split(const std::string& str, const char* const separators = " ");
95 
96 
97  std::string OfSortedIds(std::vector<int> ids);
98 
99 
100  std::string OfSortedIds(const std::set<int>& ids);
101 
102 
103  template<typename T, typename = typename std::enable_if<std::is_integral<T>::value, T>::type>
104  inline const char* Plural(const T n)
105  { return (n > 1) ? "s" : ""; }
106 
107 
108  template<class Container, typename T = typename Container::value_type>
109  inline const char* Plural(const Container& c)
110  { return (c.size() > 1) ? "s" : ""; }
111 
112 
113  template<typename Map>
114  inline
115  std::string
116  OfMapKeys(const Map& m,
117  const std::string& quote = "'",
118  const std::string& separator = ", ")
119  {
120  std::ostringstream os;
121  auto it = m.begin();
122  const auto end = m.end();
123  if (it != end) {
124  os << quote << it->first << quote;
125  for (++it; it != end; ++it)
126  os << separator << quote << it->first << quote;
127  }
128  return os.str();
129  }
130 
131 
133  template<typename T>
134  std::string
135  AsBinary(const T& number, const int maxBits = 8*sizeof(T),
136  const char separator = ' ', const int stride = 4)
137  {
138  std::ostringstream os;
139  int bit = std::min(maxBits, int(8*sizeof(T)));
140  if (bit) {
141  T mask = (T(1) << --bit);
142  os << bool(number & mask);
143  for (--bit; bit >= 0; --bit) {
144  mask >>= 1;
145  if (!((bit+1) % stride))
146  os << separator;
147  os << bool(number & mask);
148  }
149  }
150  return os.str();
151  }
152 
153 
154  template<typename T>
155  std::pair<double, char>
156  AsHumanReadable(const T& x, const int thousand = 1000)
157  {
158  char m;
159  const double lnt = std::log(thousand);
160  const double r = std::log(x) / lnt;
161  const int i = floor(r);
162  switch (i) {
163  case 8: m = 'Y'; break;
164  case 7: m = 'Z'; break;
165  case 6: m = 'E'; break;
166  case 5: m = 'P'; break;
167  case 4: m = 'T'; break;
168  case 3: m = 'G'; break;
169  case 2: m = 'M'; break;
170  case 1: m = 'k'; break;
171  case 0: m = '\0'; break;
172  case -1: m = 'm'; break;
173  case -2: m = 'u'; break;
174  case -3: m = 'n'; break;
175  case -4: m = 'p'; break;
176  case -5: m = 'f'; break;
177  case -6: m = 'a'; break;
178  case -7: m = 'z'; break;
179  case -8: m = 'y'; break;
180  default: m = '?'; break;
181  }
182  const double rem = std::exp(lnt * (r - i));
183  return std::make_pair(rem, m);
184  }
185 
186 
187  template<typename Geo, typename CS>
188  inline
189  std::string
190  Make(const Geo& g, const CS& cs, const double unit = 1, const std::string& sep = " ")
191  {
192  const auto& x = g.GetCoordinates(cs);
193  std::ostringstream os;
194  os << x.template get<0>()/unit << sep << x.template get<1>()/unit << sep << x.template get<2>();
195  return os.str();
196  }
197 
198  }
199 
200 }
201 
202 
203 #endif
std::string Make(const Geo &g, const CS &cs, const double unit=1, const std::string &sep=" ")
Definition: String.h:190
std::pair< double, char > AsHumanReadable(const T &x, const int thousand=1000)
Definition: String.h:156
constexpr double V
Definition: AugerUnits.h:233
std::string Strip(std::string str)
Definition: String.h:87
#define U
const char * Plural(const T n)
Definition: String.h:104
oss<< "0b";oss<< ((x >> i)&1);return oss.str();}template< class S, class V > std::string Join(const S &sep, const V &v)
Definition: String.h:60
std::string AsBinary(const T &number, const int maxBits=8 *sizeof(T), const char separator= ' ', const int stride=4)
converts integer-type numbers into a string of binary representation
Definition: String.h:135
T As(const U &x)
useful shorthand for boost::lexical_cast
Definition: String.h:27
#define S
constexpr double g
Definition: AugerUnits.h:200
const double unit[npar]
Definition: UnivRec.h:76
std::string OfMapKeys(const Map &m, const std::string &quote="'", const std::string &separator=", ")
Definition: String.h:116
std::vector< T > AsVectorOf(const std::string &str)
Definition: String.h:78
string OfSortedIds(vector< int > ids)
Definition: String.cc:65
constexpr double m
Definition: AugerUnits.h:121
vector< string > Split(const string &str, const char *const separators)
Definition: String.cc:13

, generated on Tue Sep 26 2023.