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