SST  9.0.0
StructuralSimulationToolkit
stringize.h
1 // Copyright 2009-2019 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-2019, 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 _H_SST_CORE_STRINGIZE
13 #define _H_SST_CORE_STRINGIZE
14 
15 #include <string>
16 #include <strings.h>
17 #include <cinttypes>
18 #include <cctype>
19 
20 namespace SST {
21 
22 inline std::string to_string(double val) {
23  char buffer[256];
24  sprintf(buffer, "%f", val);
25 
26  std::string buffer_str(buffer);
27  return buffer_str;
28 };
29 
30 inline std::string to_string(float val) {
31  char buffer[256];
32  sprintf(buffer, "%f", val);
33 
34  std::string buffer_str(buffer);
35  return buffer_str;
36 };
37 
38 inline std::string to_string(int32_t val) {
39  char buffer[256];
40  sprintf(buffer, "%" PRId32, val);
41 
42  std::string buffer_str(buffer);
43  return buffer_str;
44 };
45 
46 inline std::string to_string(int64_t val) {
47  char buffer[256];
48  sprintf(buffer, "%" PRId64, val);
49 
50  std::string buffer_str(buffer);
51  return buffer_str;
52 };
53 
54 inline std::string to_string(uint32_t val) {
55  char buffer[256];
56  sprintf(buffer, "%" PRIu32, val);
57 
58  std::string buffer_str(buffer);
59  return buffer_str;
60 };
61 
62 inline std::string to_string(uint64_t val) {
63  char buffer[256];
64  sprintf(buffer, "%" PRIu64, val);
65 
66  std::string buffer_str(buffer);
67  return buffer_str;
68 };
69 
70 
71 inline bool strcasecmp(const std::string &s1, const std::string &s2) {
72  return !::strcasecmp(s1.c_str(), s2.c_str());
73 }
74 
75 inline void to_lower(std::string &s) {
76  for ( size_t i = 0 ; i < s.size() ; i++ ) {
77  s[i] = std::tolower(s[i]);
78  }
79 }
80 
81 
82 inline void trim(std::string &s) {
83  auto start = s.find_first_not_of(" \t\n\r\v\f");
84  if ( start != 0 ) {
85  s.replace(s.begin(), s.begin() + (start), "");
86  }
87  auto end = s.find_last_not_of(" \t\n\r\v\f");
88  if ( end != s.size() -1 ) {
89  s.replace(s.begin() + end + 1, s.end(), "");
90  }
91 }
92 
93 
94 
96  typedef std::string::const_iterator iter;
97  const std::string delim;
98  char_delimiter(const std::string& delim = " \t\v\f\n\r")
99  : 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  token.clear();
106 
107  /* Skip any leading separators */
108  while ( first != last && delim.find(*first) != std::string::npos )
109  ++first;
110 
111  while ( first != last && delim.find(*first) == std::string::npos )
112  token += *first++;
113  }
114 
115 };
116 
117 
118 
120  typedef std::string::const_iterator iter;
121  std::string e;
122  std::string q;
123  std::string s;
124 
125  escaped_list_separator(const std::string &esc="\\", const std::string &sep=",", const std::string &quote="\"")
126  : e(esc), q(quote), s(sep) { }
127 
128 
129  /**
130  * @return pair<iter, iter> = <tok_end, next_tok>
131  */
132  void operator()(iter &first, iter last, std::string &token) {
133  token.clear();
134 
135  bool inside_quotes = false;
136  bool in_escape = false;
137  while ( first != last ) {
138  char c = *first++;
139 
140  if ( in_escape ) {
141  token += c;
142  in_escape = false;
143  } else if ( s.find(c) != std::string::npos && !inside_quotes ) {
144  /* Separator found */
145  break;
146  } else if ( q.find(c) != std::string::npos ) {
147  inside_quotes = !inside_quotes;
148  } else if ( e.find(c) != std::string::npos ) {
149  in_escape = true;
150  } else {
151  token += c;
152  }
153  }
154  }
155 
156 };
157 
158 
159 
160 template< typename TokenizerFunc = char_delimiter >
161 class Tokenizer {
162 
163  template <typename Func>
164  struct token_iter
165  : public std::iterator< std::input_iterator_tag, std::string >
166  {
167  Func &f;
168  std::string::const_iterator first;
169  std::string::const_iterator last;
170  std::string token;
171  public:
172  explicit token_iter(Func& f_, std::string::const_iterator& first_, std::string::const_iterator& last_)
173  : f(f_), first(first_), last(last_)
174  {
175  f(first, last, token);
176  }
177  token_iter& operator++() { f(first, last, token); return *this; }
178  token_iter operator++(int) {token_iter retval = *this; ++(*this); return retval;}
179  bool operator==(token_iter other) const { return (first == other.first) && (last == other.last) && (token == other.token); }
180  bool operator!=(token_iter other) const { return !(*this == other); }
181  const std::string& operator*() const { return token; }
182  const std::string& operator->() const { return token; }
183  };
184 
185 
186 
187  typedef token_iter<TokenizerFunc> iter;
188 public:
189  typedef iter iterator;
190  typedef iter const_iterator;
191  typedef std::string value_type;
192 
193  iter begin() { return iter(f, first, last); }
194  iter end() { return iter(f, last, last); }
195 
196  Tokenizer(const std::string &s, const TokenizerFunc& f = TokenizerFunc())
197  : first(s.begin()), last(s.end()), f(f) { }
198 
199 private:
200  std::string::const_iterator first, last;
201  TokenizerFunc f;
202 };
203 
204 }
205 
206 #endif
Definition: stringize.h:119
void operator()(iter &first, iter last, std::string &token)
Definition: stringize.h:132
Definition: stringize.h:161
Definition: stringize.h:95
void operator()(iter &first, iter last, std::string &token)
Definition: stringize.h:104