Version: SMASH-3.4
stringfunctions.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2014-2018,2020,2022,2024,2026
4  * SMASH Team
5  *
6  * GNU General Public License (GPLv3 or later)
7  *
8  */
9 
10 #ifndef SRC_INCLUDE_SMASH_STRINGFUNCTIONS_H_
11 #define SRC_INCLUDE_SMASH_STRINGFUNCTIONS_H_
12 
13 #include <cstdint>
14 #include <set>
15 #include <string>
16 #include <vector>
17 
18 namespace smash {
19 
20 /**
21  * Strip leading and trailing whitespaces.
22  *
23  * \param s String to be trimmed.
24  * \return Trimmed string.
25  */
26 std::string trim(const std::string &s);
27 
28 /**
29  * Remove all instances of a substring p in a string s.
30  *
31  * \param[inout] s String to be searched and modified.
32  * \param[in] p Substring to be removed.
33  */
34 void remove_substr(std::string &s, const std::string &p);
35 
36 /**
37  * Remove ⁺, ⁻, ⁰ from string.
38  *
39  * \param[inout] s String to be cleaned.
40  */
41 void isoclean(std::string &s);
42 
43 /**
44  * Split string by delimiter.
45  *
46  * \param[in] s String to be split.
47  * \param[in] delim Splitting delimiter.
48  * \return Split string.
49  */
50 std::vector<std::string> split(const std::string &s, char delim);
51 
52 /**
53  * Join strings using delimiter.
54  *
55  * \param[in] v Strings to be joint.
56  * \param[in] delim Joining delimiter.
57  * \return Joint string.
58  */
59 std::string join(const std::vector<std::string> &v, std::string_view delim);
60 
61 /**
62  * Join string views using delimiter.
63  *
64  * \param[in] v String views to be joint.
65  * \param[in] delim Joining delimiter.
66  * \return Joint string.
67  */
68 std::string join(const std::vector<std::string_view> &v,
69  std::string_view delim);
70 
71 /**
72  * Join strings using delimiter.
73  *
74  * \param[in] s Strings to be joint.
75  * \param[in] delim Joining delimiter.
76  * \return Joint string.
77  */
78 std::string join(const std::set<std::string> &s, std::string_view delim);
79 
80 /**
81  * Add quotes around string.
82  * This is a simpler version of \c std::quoted that also escapes
83  * e.g. contained quotes and cannot directly be converted to a string.
84  *
85  * \param[in] s Strings to be quoted.
86  * \return Quoted string.
87  */
88 std::string quote(const std::string &s);
89 
90 namespace utf8 {
91 /**
92  * Fill string with characters to the left until the given width is reached.
93  *
94  * \param[in] s Input string.
95  * \param[in] width Total width of output string.
96  * \param[in] fill Filling character.
97  * \return Padded string.
98  */
99 std::string fill_left(const std::string &s, size_t width, char fill = ' ');
100 
101 /**
102  * Fill string with characters to the right until the given width is reached.
103  *
104  * \param[in] s Input string.
105  * \param[in] width Total width of output string.
106  * \param[in] fill Filling character.
107  * \return Padded string.
108  */
109 std::string fill_right(const std::string &s, size_t width, char fill = ' ');
110 
111 /**
112  * Fill string with characters at both sides until the given width is reached.
113  *
114  * \param[in] s Input string.
115  * \param[in] width Total width of output string.
116  * \param[in] fill Filling character.
117  * \return Padded string.
118  */
119 std::string fill_both(const std::string &s, size_t width, char fill = ' ');
120 
121 /**
122  * Extract the first byte from a given value.
123  *
124  * This function was taken from the Boost-licensed library UTF8-CPP.
125  * See http://utfcpp.sourceforge.net/.
126  *
127  * \tparam octet_type Type for one byte
128  */
129 template <typename octet_type>
130 inline uint8_t mask8(octet_type oc) {
131  return static_cast<uint8_t>(0xff & oc);
132 }
133 
134 /**
135  * Given an iterator to the beginning of a UTF-8 sequence, return the
136  * length of the next UTF-8 code point.
137  *
138  * This function was taken from the Boost-licensed library UTF8-CPP.
139  * See http://utfcpp.sourceforge.net/.
140  */
141 template <typename octet_iterator>
142 inline typename std::iterator_traits<octet_iterator>::difference_type
143 sequence_length(octet_iterator lead_it) {
144  uint8_t lead = mask8(*lead_it);
145  if (lead < 0x80)
146  return 1;
147  else if ((lead >> 5) == 0x6)
148  return 2;
149  else if ((lead >> 4) == 0xe)
150  return 3;
151  else if ((lead >> 3) == 0x1e)
152  return 4;
153  else
154  return 0;
155 }
156 
157 } // namespace utf8
158 
159 } // namespace smash
160 
161 #endif // SRC_INCLUDE_SMASH_STRINGFUNCTIONS_H_
constexpr int p
Proton.
std::iterator_traits< octet_iterator >::difference_type sequence_length(octet_iterator lead_it)
Given an iterator to the beginning of a UTF-8 sequence, return the length of the next UTF-8 code poin...
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.
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.
uint8_t mask8(octet_type oc)
Extract the first byte from a given value.
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, std::string_view 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.