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