SST  12.0.0
StructuralSimulationToolkit
stringize.h
1 // Copyright 2009-2022 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-2022, 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 <cstdarg>
18 #include <cstdio>
19 #include <string>
20 #include <strings.h>
21 
22 namespace SST {
23 
24 __attribute__((deprecated(
25  "SST::to_string() is deprecated and will be removed in SST 13. Please use std::to_string() instead"))) inline std::
26  string
27  to_string(double val)
28 {
29  return std::to_string(val);
30 };
31 
32 __attribute__((deprecated(
33  "SST::to_string() is deprecated and will be removed in SST 13. Please use std::to_string() instead"))) inline std::
34  string
35  to_string(float val)
36 {
37  return std::to_string(val);
38 };
39 
40 __attribute__((deprecated(
41  "SST::to_string() is deprecated and will be removed in SST 13. Please use std::to_string() instead"))) inline std::
42  string
43  to_string(int32_t val)
44 {
45  return std::to_string(val);
46 };
47 
48 __attribute__((deprecated(
49  "SST::to_string() is deprecated and will be removed in SST 13. Please use std::to_string() instead"))) inline std::
50  string
51  to_string(int64_t val)
52 {
53  return std::to_string(val);
54 };
55 
56 __attribute__((deprecated(
57  "SST::to_string() is deprecated and will be removed in SST 13. Please use std::to_string() instead"))) inline std::
58  string
59  to_string(uint32_t val)
60 {
61  return std::to_string(val);
62 };
63 
64 __attribute__((deprecated(
65  "SST::to_string() is deprecated and will be removed in SST 13. Please use std::to_string() instead"))) inline std::
66  string
67  to_string(uint64_t val)
68 {
69  return std::to_string(val);
70 };
71 
72 inline bool
73 strcasecmp(const std::string& s1, const std::string& s2)
74 {
75  return !::strcasecmp(s1.c_str(), s2.c_str());
76 }
77 
78 inline void
79 to_lower(std::string& s)
80 {
81  for ( size_t i = 0; i < s.size(); i++ ) {
82  s[i] = std::tolower(s[i]);
83  }
84 }
85 
86 inline void
87 trim(std::string& s)
88 {
89  auto start = s.find_first_not_of(" \t\n\r\v\f");
90  if ( start != 0 ) { s.replace(s.begin(), s.begin() + (start), ""); }
91  auto end = s.find_last_not_of(" \t\n\r\v\f");
92  if ( end != s.size() - 1 ) { s.replace(s.begin() + end + 1, s.end(), ""); }
93 }
94 
96 {
97  typedef std::string::const_iterator iter;
98  const std::string delim;
99  char_delimiter(const std::string& delim = " \t\v\f\n\r") : delim(delim) {}
100 
101  /**
102  * @return pair<iter, iter> = <tok_end, next_tok>
103  */
104  void operator()(iter& first, iter last, std::string& token)
105  {
106  token.clear();
107 
108  /* Skip any leading separators */
109  while ( first != last && delim.find(*first) != std::string::npos )
110  ++first;
111 
112  while ( first != last && delim.find(*first) == std::string::npos )
113  token += *first++;
114  }
115 };
116 
118 {
119  typedef std::string::const_iterator iter;
120  std::string e;
121  std::string q;
122  std::string s;
123 
125  const std::string& esc = "\\", const std::string& sep = ",", const std::string& quote = "\"") :
126  e(esc),
127  q(quote),
128  s(sep)
129  {}
130 
131  /**
132  * @return pair<iter, iter> = <tok_end, next_tok>
133  */
134  void operator()(iter& first, iter last, std::string& token)
135  {
136  token.clear();
137 
138  bool inside_quotes = false;
139  bool in_escape = false;
140  while ( first != last ) {
141  char c = *first++;
142 
143  if ( in_escape ) {
144  token += c;
145  in_escape = false;
146  }
147  else if ( s.find(c) != std::string::npos && !inside_quotes ) {
148  /* Separator found */
149  break;
150  }
151  else if ( q.find(c) != std::string::npos ) {
152  inside_quotes = !inside_quotes;
153  }
154  else if ( e.find(c) != std::string::npos ) {
155  in_escape = true;
156  }
157  else {
158  token += c;
159  }
160  }
161  }
162 };
163 
164 template <typename TokenizerFunc = char_delimiter>
166 {
167 
168  template <typename Func>
169  struct token_iter : public std::iterator<std::input_iterator_tag, std::string>
170  {
171  Func& f;
172  std::string::const_iterator first;
173  std::string::const_iterator last;
174  std::string token;
175 
176  public:
177  explicit token_iter(Func& f_, std::string::const_iterator& first_, std::string::const_iterator& last_) :
178  f(f_),
179  first(first_),
180  last(last_)
181  {
182  f(first, last, token);
183  }
184  token_iter& operator++()
185  {
186  f(first, last, token);
187  return *this;
188  }
189  token_iter operator++(int)
190  {
191  token_iter retval = *this;
192  ++(*this);
193  return retval;
194  }
195  bool operator==(token_iter other) const
196  {
197  return (first == other.first) && (last == other.last) && (token == other.token);
198  }
199  bool operator!=(token_iter other) const { return !(*this == other); }
200  const std::string& operator*() const { return token; }
201  const std::string& operator->() const { return token; }
202  };
203 
204  typedef token_iter<TokenizerFunc> iter;
205 
206 public:
207  typedef iter iterator;
208  typedef iter const_iterator;
209  typedef std::string value_type;
210 
211  iter begin() { return iter(f, first, last); }
212  iter end() { return iter(f, last, last); }
213 
214  Tokenizer(const std::string& s, const TokenizerFunc& f = TokenizerFunc()) : first(s.begin()), last(s.end()), f(f) {}
215 
216 private:
217  std::string::const_iterator first, last;
218  TokenizerFunc f;
219 };
220 
221 
222 /**
223  Creates a string using a vprintf like function call. This function
224  uses a dynamically allocated char array of size max_length to
225  create the buffer to intialize the string.
226 
227  @param max_length Maximum length of string. Anything past
228  max_length will be truncated (null terminator is included in the
229  length)
230 
231  @param format printf-like format string
232 
233  @param args va_list containing variable length argument list
234 
235  @return formatted string, potentially truncated at length max_length - 1
236  */
237 std::string vformat_string(size_t max_length, const char* format, va_list args);
238 
239 /**
240  Creates a string using a printf like function call. This function
241  uses a compile time allocated char array of length 256 to create
242  the buffer to intialize the string. If this is not long enough, it
243  will dynamically allocate an array that is just big enough to
244  create the buffer to initialize the string. No truncation will
245  occur.
246 
247  @param format printf-like format string
248 
249  @param args va_list containing variable length argument list
250 
251  @return formatted string
252  */
253 std::string vformat_string(const char* format, va_list args);
254 
255 /**
256  Creates a string using a printf like function call. This function
257  uses a dynamically allocated char array of size max_length to
258  create the buffer to intialize the string.
259 
260  @param max_length Maximum length of string. Anything past
261  max_length will be truncated (null terminator is included in the
262  length)
263 
264  @param format printf-like format string
265 
266  @param ... arguments for format string
267 
268  @return formatted string, potentially truncated at length max_length - 1
269  */
270 std::string format_string(size_t max_length, const char* format, ...) __attribute__((format(printf, 2, 3)));
271 
272 /**
273  Creates a string using a printf like function call. This function
274  uses a compile time allocated char array of length 256 to create
275  the buffer to intialize the string. If this is not long enough, it
276  will dynamically allocate an array that is just big enough to
277  create the buffer to initialize the string. No truncation will
278  occur.
279 
280  @param format printf-like format string
281 
282  @param ... arguments for format string
283 
284  @return formatted string
285  */
286 std::string format_string(const char* format, ...) __attribute__((format(printf, 1, 2)));
287 
288 } // namespace SST
289 
290 #endif // SST_CORE_STRINGIZE_H
Definition: stringize.h:117
void operator()(iter &first, iter last, std::string &token)
Definition: stringize.h:134
Definition: stringize.h:165
Definition: stringize.h:95
void operator()(iter &first, iter last, std::string &token)
Definition: stringize.h:104