SST  11.1.0
StructuralSimulationToolkit
stringize.h
1 // Copyright 2009-2021 NTESS. Under the terms
2 // of Contract DE-NA0003525 with NTESS, the U.S.
3 // Government retains certain rights in this software.
4 //
5 // Copyright (c) 2009-2021, NTESS
6 // All rights reserved.
7 //
8 // This file is part of the SST software package. For license
9 // information, see the LICENSE file in the top level directory of the
10 // distribution.
11 
12 #ifndef SST_CORE_STRINGIZE_H
13 #define SST_CORE_STRINGIZE_H
14 
15 #include <cctype>
16 #include <cinttypes>
17 #include <string>
18 #include <strings.h>
19 
20 namespace SST {
21 
22 inline std::string
23 to_string(double val)
24 {
25  char buffer[256];
26  sprintf(buffer, "%f", val);
27 
28  std::string buffer_str(buffer);
29  return buffer_str;
30 };
31 
32 inline std::string
33 to_string(float val)
34 {
35  char buffer[256];
36  sprintf(buffer, "%f", val);
37 
38  std::string buffer_str(buffer);
39  return buffer_str;
40 };
41 
42 inline std::string
43 to_string(int32_t val)
44 {
45  char buffer[256];
46  sprintf(buffer, "%" PRId32, val);
47 
48  std::string buffer_str(buffer);
49  return buffer_str;
50 };
51 
52 inline std::string
53 to_string(int64_t val)
54 {
55  char buffer[256];
56  sprintf(buffer, "%" PRId64, val);
57 
58  std::string buffer_str(buffer);
59  return buffer_str;
60 };
61 
62 inline std::string
63 to_string(uint32_t val)
64 {
65  char buffer[256];
66  sprintf(buffer, "%" PRIu32, val);
67 
68  std::string buffer_str(buffer);
69  return buffer_str;
70 };
71 
72 inline std::string
73 to_string(uint64_t val)
74 {
75  char buffer[256];
76  sprintf(buffer, "%" PRIu64, val);
77 
78  std::string buffer_str(buffer);
79  return buffer_str;
80 };
81 
82 inline bool
83 strcasecmp(const std::string& s1, const std::string& s2)
84 {
85  return !::strcasecmp(s1.c_str(), s2.c_str());
86 }
87 
88 inline void
89 to_lower(std::string& s)
90 {
91  for ( size_t i = 0; i < s.size(); i++ ) {
92  s[i] = std::tolower(s[i]);
93  }
94 }
95 
96 inline void
97 trim(std::string& s)
98 {
99  auto start = s.find_first_not_of(" \t\n\r\v\f");
100  if ( start != 0 ) { s.replace(s.begin(), s.begin() + (start), ""); }
101  auto end = s.find_last_not_of(" \t\n\r\v\f");
102  if ( end != s.size() - 1 ) { s.replace(s.begin() + end + 1, s.end(), ""); }
103 }
104 
106 {
107  typedef std::string::const_iterator iter;
108  const std::string delim;
109  char_delimiter(const std::string& delim = " \t\v\f\n\r") : delim(delim) {}
110 
111  /**
112  * @return pair<iter, iter> = <tok_end, next_tok>
113  */
114  void operator()(iter& first, iter last, std::string& token)
115  {
116  token.clear();
117 
118  /* Skip any leading separators */
119  while ( first != last && delim.find(*first) != std::string::npos )
120  ++first;
121 
122  while ( first != last && delim.find(*first) == std::string::npos )
123  token += *first++;
124  }
125 };
126 
128 {
129  typedef std::string::const_iterator iter;
130  std::string e;
131  std::string q;
132  std::string s;
133 
135  const std::string& esc = "\\", const std::string& sep = ",", const std::string& quote = "\"") :
136  e(esc),
137  q(quote),
138  s(sep)
139  {}
140 
141  /**
142  * @return pair<iter, iter> = <tok_end, next_tok>
143  */
144  void operator()(iter& first, iter last, std::string& token)
145  {
146  token.clear();
147 
148  bool inside_quotes = false;
149  bool in_escape = false;
150  while ( first != last ) {
151  char c = *first++;
152 
153  if ( in_escape ) {
154  token += c;
155  in_escape = false;
156  }
157  else if ( s.find(c) != std::string::npos && !inside_quotes ) {
158  /* Separator found */
159  break;
160  }
161  else if ( q.find(c) != std::string::npos ) {
162  inside_quotes = !inside_quotes;
163  }
164  else if ( e.find(c) != std::string::npos ) {
165  in_escape = true;
166  }
167  else {
168  token += c;
169  }
170  }
171  }
172 };
173 
174 template <typename TokenizerFunc = char_delimiter>
176 {
177 
178  template <typename Func>
179  struct token_iter : public std::iterator<std::input_iterator_tag, std::string>
180  {
181  Func& f;
182  std::string::const_iterator first;
183  std::string::const_iterator last;
184  std::string token;
185 
186  public:
187  explicit token_iter(Func& f_, std::string::const_iterator& first_, std::string::const_iterator& last_) :
188  f(f_),
189  first(first_),
190  last(last_)
191  {
192  f(first, last, token);
193  }
194  token_iter& operator++()
195  {
196  f(first, last, token);
197  return *this;
198  }
199  token_iter operator++(int)
200  {
201  token_iter retval = *this;
202  ++(*this);
203  return retval;
204  }
205  bool operator==(token_iter other) const
206  {
207  return (first == other.first) && (last == other.last) && (token == other.token);
208  }
209  bool operator!=(token_iter other) const { return !(*this == other); }
210  const std::string& operator*() const { return token; }
211  const std::string& operator->() const { return token; }
212  };
213 
214  typedef token_iter<TokenizerFunc> iter;
215 
216 public:
217  typedef iter iterator;
218  typedef iter const_iterator;
219  typedef std::string value_type;
220 
221  iter begin() { return iter(f, first, last); }
222  iter end() { return iter(f, last, last); }
223 
224  Tokenizer(const std::string& s, const TokenizerFunc& f = TokenizerFunc()) : first(s.begin()), last(s.end()), f(f) {}
225 
226 private:
227  std::string::const_iterator first, last;
228  TokenizerFunc f;
229 };
230 
231 } // namespace SST
232 
233 #endif // SST_CORE_STRINGIZE_H
Definition: stringize.h:127
void operator()(iter &first, iter last, std::string &token)
Definition: stringize.h:144
Definition: stringize.h:175
Definition: stringize.h:105
void operator()(iter &first, iter last, std::string &token)
Definition: stringize.h:114