Version: SMASH-3.1
stringfunctions.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2014-2018,2022
4  * SMASH Team
5  *
6  * GNU General Public License (GPLv3 or later)
7  *
8  */
9 
10 #include "smash/stringfunctions.h"
11 
12 #include <numeric>
13 #include <sstream>
14 
15 namespace smash {
16 
17 namespace utf8 {
18 
28 inline static size_t adjust(const std::string &s, size_t width) {
29  for (unsigned char c : s) {
30  if (c >= 0xFC) {
31  width += 5;
32  } else if (c >= 0xF8) {
33  width += 4;
34  } else if (c >= 0xF0) {
35  width += 3;
36  } else if (c >= 0xE0) {
37  width += 2;
38  } else if (c == 0xCC || c == 0xCD) {
39  // combining character (2 Bytes) - doesn't appear at all
40  width += 2;
41  } else if (c >= 0xC0) {
42  width += 1;
43  }
44  }
45  return width;
46 }
47 
48 std::string fill_left(const std::string &s, size_t width, char fill) {
49  width = adjust(s, width - s.size());
50  if (width > 0) {
51  return std::string(width, fill) + s;
52  }
53  return s;
54 }
55 
56 std::string fill_right(const std::string &s, size_t width, char fill) {
57  width = adjust(s, width - s.size());
58  if (width > 0) {
59  return s + std::string(width, fill);
60  }
61  return s;
62 }
63 
64 std::string fill_both(const std::string &s, size_t width, char fill) {
65  width = adjust(s, width - s.size());
66  if (width > 0) {
67  const int l = width / 2;
68  const int r = width - l;
69  return std::string(l, fill) + s + std::string(r, fill);
70  }
71  return s;
72 }
73 
74 } // namespace utf8
75 
76 std::string trim(const std::string &s) {
77  const auto begin = s.find_first_not_of(" \t\n\r");
78  if (begin == std::string::npos) {
79  return {};
80  }
81  const auto end = s.find_last_not_of(" \t\n\r");
82  return s.substr(begin, end - begin + 1);
83 }
84 
85 void remove_substr(std::string &s, const std::string &p) {
86  using str = std::string;
87  str::size_type n = p.length();
88  for (str::size_type i = s.find(p); i != str::npos; i = s.find(p)) {
89  s.erase(i, n);
90  }
91 }
92 
93 void isoclean(std::string &s) {
94  remove_substr(s, "⁺");
95  remove_substr(s, "⁻");
96  remove_substr(s, "⁰");
97 }
98 
108 template <typename Out>
109 void split(const std::string &s, char delim, Out result);
110 
111 template <typename Out>
112 void split(const std::string &s, char delim, Out result) {
113  std::stringstream ss;
114  ss.str(s);
115  std::string item;
116  while (std::getline(ss, item, delim)) {
117  *(result++) = item;
118  }
119 }
120 
121 std::vector<std::string> split(const std::string &s, char delim) {
122  std::vector<std::string> elems;
123  split(s, delim, std::back_inserter(elems));
124  return elems;
125 }
126 
127 std::string join(const std::vector<std::string> &v, const std::string &delim) {
128  return std::accumulate(std::begin(v), std::end(v), std::string{},
129  [&delim](const std::string &ss, const std::string &s) {
130  return ss.empty() ? s : ss + delim + s;
131  });
132 }
133 
134 std::string quote(const std::string &s) { return "\"" + s + "\""; }
135 
136 } // namespace smash
constexpr int p
Proton.
constexpr int n
Neutron.
std::string fill_right(const std::string &s, size_t width, char fill=' ')
Fill string with characters to the right until the given width is reached.
static size_t adjust(const std::string &s, size_t width)
Adjust filling width by taking the size of unicode characters into account.
std::string fill_both(const std::string &s, size_t width, char fill=' ')
Fill string with characters at both sides until the given width is reached.
std::string fill_left(const std::string &s, size_t width, char fill=' ')
Fill string with characters to the left until the given width is reached.
Definition: action.h:24
std::vector< std::string > split(const std::string &s, char delim)
Split string by delimiter.
std::string quote(const std::string &s)
Add quotes around string.
std::string trim(const std::string &s)
Strip leading and trailing whitespaces.
std::string join(const std::vector< std::string > &v, const std::string &delim)
Join strings using delimiter.
void isoclean(std::string &s)
Remove ⁺, ⁻, ⁰ from string.
void remove_substr(std::string &s, const std::string &p)
Remove all instances of a substring p in a string s.