The Sparta Modeling Framework
Loading...
Searching...
No Matches
StringUtils.hpp
Go to the documentation of this file.
1// <CommandLineSimulator> -*- C++ -*-
2
3
9#pragma once
10
11#include <cinttypes>
12#include <cstring>
13#include <string>
14#include <cstring>
15#include <sstream>
16#include <string>
17#include <iomanip>
18#include <vector>
19#include <algorithm>
20#include <memory>
21
22namespace sparta {
23namespace utils {
24
29//
30inline std::string uint64_to_hexstr (uint64_t val)
31{
32 std::ostringstream oss;
33 oss << std::hex << std::setw(8) << std::setfill('0') << (val >> 32)
34 << "_" << std::setw(8) << (val & 0xffffffff);
35 return oss.str();
36}
37
38
39
43//
44inline std::string uint32_to_hexstr (uint32_t val)
45{
46 std::ostringstream oss;
47 oss << std::hex << std::setw(8) << std::setfill('0') << val;
48 return oss.str();
49}
50
51
55//
56inline std::string uint64_to_str (uint64_t val)
57{
58 std::ostringstream ss;
59 return (static_cast<std::ostringstream*>( &(ss << val))->str());
60}
61
62
66//
67inline std::string uint32_to_str (uint32_t val)
68{
69 std::ostringstream ss;
70 return (static_cast<std::ostringstream*>( &(ss << val))->str());
71}
72
76//
77inline std::string int64_to_str (int64_t val)
78{
79 std::ostringstream ss;
80 return (static_cast<std::ostringstream*>( &(ss << val))->str());
81}
82
83
87//
88inline std::string int32_to_str (int32_t val)
89{
90 std::ostringstream ss;
91 return (static_cast<std::ostringstream*>( &(ss << val))->str());
92}
93
97//
98inline std::string bool_to_str (bool val)
99{
100 std::ostringstream ss;
101 return (static_cast<std::ostringstream*>( &(ss << std::boolalpha << val))->str());
102}
103
107//
108inline std::string bin_to_hexstr(const uint8_t *data, size_t size, const std::string &sep=" ")
109{
110 std::stringstream ss;
111 ss << std::hex << std::setfill('0');
112 for (size_t i = size; i > 0; --i) {
113 ss << std::setw(2) << (uint32_t)data[i - 1];
114 if (i - 1 != 0) {
115 ss << sep;
116 }
117 }
118 return ss.str();
119}
120
124//
125inline std::string bin_to_bitstr(const uint8_t *data, size_t size, const std::string &sep=" ")
126{
127 std::stringstream ss;
128
129 for (size_t i = size; i > 0; --i) {
130 const auto byte = data[i - 1];
131 for (size_t b = 8; b > 0; --b) {
132 const auto mask = 1 << (b - 1);
133
134 ss << ((byte & mask) ? '1' : '0');
135 }
136 if (i > 1) {
137 ss << sep;
138 }
139 }
140
141 return ss.str();
142}
143
147//
148inline std::string eliminate_whitespace(const std::string &str)
149{
150 std::string strip_str = str;
151 strip_str.erase(std::remove_if(strip_str.begin(), strip_str.end(), ::isspace),
152 strip_str.end());
153 return strip_str;
154}
155
159//
160inline std::string strip_whitespace(const std::string &str)
161{
162 const std::string STR_WHITESPACE = " \t\n\r";
163
164 size_t start_pos = str.find_first_not_of(STR_WHITESPACE);
165 size_t end_pos = str.find_last_not_of(STR_WHITESPACE);
166
167 return str.substr(start_pos, end_pos-start_pos + 1);
168}
169
176//
177inline std::string strip_string_pattern(const std::string & pat,
178 const std::string & str)
179{
181 size_t start_pos = str.find(pat);
182 if(start_pos == 0) {
184 start_pos += pat.length();
185 }
186 else {
187 start_pos = 0;
188 }
189
191 size_t end_pos = str.rfind(pat);
192 if(end_pos == 0 || (end_pos + pat.length()) != str.length()) {
194 end_pos = std::string::npos;
195 }
196 else {
197 end_pos -= start_pos;
198 }
199
200 return str.substr(start_pos, end_pos);
201}
202
210//
211inline void tokenize(const std::string &in_str, std::vector<std::string> & str_vector, const std::string &delimiters = " ")
212{
213 size_t prev_pos = 0;
214 size_t cur_pos = 0;
215 while ((cur_pos = in_str.find_first_of(delimiters, prev_pos)) != std::string::npos) {
216 str_vector.push_back(in_str.substr(prev_pos, cur_pos - prev_pos));
217 prev_pos = cur_pos + 1;
218 }
219
220 str_vector.push_back(in_str.substr(prev_pos));
221}
222
229//
231//
234//
236//
239//
241//
246//
248inline void split_lines_around_tokens(std::istream & in_stream,
249 std::vector<std::vector<std::string>> & str_vectors,
250 const std::string & delimiters = " ",
251 const char line_separator = '\n')
252{
253 std::string line;
254 while (std::getline(in_stream, line, line_separator)) {
255 str_vectors.emplace_back(std::vector<std::string>());
256 tokenize(line, str_vectors.back(), delimiters);
257 }
258}
259
260
268//
269inline void tokenize(const char *in_char_str, std::vector<std::string> & str_vector, const std::string &delimiters = " ")
270{
272 std::string in_str = in_char_str;
273
274 tokenize(in_str, str_vector, delimiters);
275}
276
277
281//
282inline void tokenize_strip_whitespace(const std::string &in_str, std::vector<std::string> & str_vector, const std::string &delimiters = " ")
283{
284 size_t prev_pos = 0;
285 size_t cur_pos = 0;
286 while ((cur_pos = in_str.find_first_of(delimiters, prev_pos)) != std::string::npos) {
287 str_vector.push_back(strip_whitespace(in_str.substr(prev_pos, cur_pos - prev_pos)));
288 prev_pos = cur_pos + 1;
289 }
290
291 str_vector.push_back(strip_whitespace(in_str.substr(prev_pos)));
292}
293
294
302//
303inline void tokenize_strip_whitespace(const char *in_char_str, std::vector<std::string> & str_vector, const std::string &delimiters = " ")
304{
306 std::string in_str = in_char_str;
307
308 tokenize_strip_whitespace(in_str, str_vector, delimiters);
309}
310
311
317//
318inline void tokenize_on_whitespace(const std::string &in_str, std::vector<std::string> & str_vector)
319{
320 const std::string DELIMITER = " \t\n\r";
321
322 size_t prev_pos = 0;
323 size_t cur_pos = 0;
324 while ((cur_pos = in_str.find_first_of(DELIMITER, prev_pos)) != std::string::npos) {
325 if (cur_pos != prev_pos) {
326 str_vector.push_back(in_str.substr(prev_pos, cur_pos - prev_pos));
327 }
328 prev_pos = cur_pos + 1;
329 }
330
331 if (prev_pos != in_str.size()) {
332 str_vector.push_back(in_str.substr(prev_pos));
333 }
334}
335
336
342//
343inline void tokenize_on_whitespace(const char *in_char_str, std::vector<std::string> & str_vector)
344{
346 std::string in_str = in_char_str;
347
348 tokenize_on_whitespace(in_str, str_vector);
349}
350
355inline bool strcmp_with_null(const char *s1, const char *s2)
356{
357 if (s1 != nullptr && s2 != nullptr) {
358 return !std::strcmp(s1, s2);
359 }
360 if (s1 == nullptr && s2 == nullptr) {
361 return true;
362 }
363 return false;
364}
365
368//
373//
376//
377inline uint32_t LevenshteinDistance(const char * s, size_t n,
378 const char * t, size_t m)
379{
380 ++n;
381 ++m;
382 std::unique_ptr<size_t[]> dptr(new size_t[n * m]);
383 size_t * d = dptr.get();
384
385 memset(d, 0, sizeof(size_t) * n * m);
386 for (size_t i = 1, im = 0; i < m; ++i, ++im) {
387 for (size_t j = 1, jn = 0; j < n; ++j, ++jn) {
388 if (s[jn] == t[im]) {
389 d[(i * n) + j] = d[((i - 1) * n) + (j - 1)];
390 } else {
391 d[(i * n) + j] = std::min({
392 d[(i - 1) * n + j] + 1,
393 d[(i - 1) * n + j] + 1,
394 d[(i - 1) * n + (j - 1)] + 1
395 });
396 }
397 }
398 }
399
400 return d[n * m - 1];
401}
402
407template <class AppliedTransform>
409{
410public:
411 TransformedString() = default;
412
413 TransformedString(const char * str) :
414 TransformedString(std::string(str))
415 {}
416
417 TransformedString(const std::string & str) :
418 str_(str)
419 {
420 applyTransform_();
421 }
422
423 TransformedString & operator=(const std::string & str) {
424 str_ = str;
425 applyTransform_();
426 return *this;
427 }
428
430 str_(rhs.str_)
431 {}
432
433 TransformedString & operator=(const TransformedString & rhs) {
434 str_ = rhs.str_;
435 return *this;
436 }
437
438 inline bool operator==(const TransformedString & rhs) const {
439 return str_ == rhs.str_;
440 }
441
442 inline bool operator!=(const TransformedString & rhs) const {
443 return str_ != rhs.str_;
444 }
445
446 inline bool operator==(const std::string & rhs) const {
447 return str_ == rhs;
448 }
449
450 inline bool operator!=(const std::string & rhs) const {
451 return str_ != rhs;
452 }
453
454 inline bool operator==(const char * rhs) const {
455 return strcmp(str_.c_str(), rhs) == 0;
456 }
457
458 inline bool operator!=(const char * rhs) const {
459 return strcmp(str_.c_str(), rhs) != 0;
460 }
461
462 inline const std::string & getString() const {
463 return str_;
464 }
465
466 inline operator std::string() const {
467 return str_;
468 }
469
470private:
471 void applyTransform_() {
472 std::transform(str_.begin(), str_.end(), str_.begin(), transformer_);
473 }
474
475 std::string str_;
476 AppliedTransform transformer_;
477};
478
481 int operator()(const int ch) const {
482 return std::tolower(ch);
483 }
484};
485
488 int operator()(const int ch) const {
489 return std::toupper(ch);
490 }
491};
492
495
498
501template <class AppliedTransform>
504{
505 return one.getString() < two.getString();
506}
507
509//
515template <class AppliedTransform>
516inline bool operator==(const std::string & one,
518{
519 return one == two.getString();
520}
521
522template <class AppliedTransform>
523inline bool operator!=(const std::string & one,
524 const TransformedString<AppliedTransform> & two)
525{
526 return one != two.getString();
527}
528
529}
530}
531
void split_lines_around_tokens(std::istream &in_stream, std::vector< std::vector< std::string > > &str_vectors, const std::string &delimiters=" ", const char line_separator='\n')
function tokenize()
bool strcmp_with_null(const char *s1, const char *s2)
function tokenize()
std::string uint64_to_hexstr(uint64_t val)
std::string bool_to_str(bool val)
void tokenize_strip_whitespace(const std::string &in_str, std::vector< std::string > &str_vector, const std::string &delimiters=" ")
function tokenize()
void tokenize(const std::string &in_str, std::vector< std::string > &str_vector, const std::string &delimiters=" ")
function strip_string_pattern()
std::string uint32_to_str(uint32_t val)
uint32_t LevenshteinDistance(const char *s, size_t n, const char *t, size_t m)
std::string bin_to_hexstr(const uint8_t *data, size_t size, const std::string &sep=" ")
std::string int32_to_str(int32_t val)
std::string int64_to_str(int64_t val)
std::string strip_string_pattern(const std::string &pat, const std::string &str)
function strip_whitespace()
std::string strip_whitespace(const std::string &str)
function eliminate_whitespace()
std::string uint64_to_str(uint64_t val)
std::string bin_to_bitstr(const uint8_t *data, size_t size, const std::string &sep=" ")
void tokenize_on_whitespace(const std::string &in_str, std::vector< std::string > &str_vector)
function tokenize_strip_whitespace()
std::string eliminate_whitespace(const std::string &str)
std::string uint32_to_hexstr(uint32_t val)
bool operator<(const TransformedString< AppliedTransform > &one, const TransformedString< AppliedTransform > &two)
function LevenshteinDistance()
Macros for handling exponential backoff.
Functor for ::tolower.
Functor for ::toupper.