Version: SMASH-3.1
stringfunctions.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2014-2018,2020,2022,2024
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 <string>
15 #include <vector>
16 
17 namespace smash {
18 
25 std::string trim(const std::string &s);
26 
33 void remove_substr(std::string &s, const std::string &p);
34 
40 void isoclean(std::string &s);
41 
49 std::vector<std::string> split(const std::string &s, char delim);
50 
58 std::string join(const std::vector<std::string> &v, const std::string &delim);
59 
68 std::string quote(const std::string &s);
69 
70 namespace utf8 {
79 std::string fill_left(const std::string &s, size_t width, char fill = ' ');
80 
89 std::string fill_right(const std::string &s, size_t width, char fill = ' ');
90 
99 std::string fill_both(const std::string &s, size_t width, char fill = ' ');
100 
109 template <typename octet_type>
110 inline uint8_t mask8(octet_type oc) {
111  return static_cast<uint8_t>(0xff & oc);
112 }
113 
121 template <typename octet_iterator>
122 inline typename std::iterator_traits<octet_iterator>::difference_type
123 sequence_length(octet_iterator lead_it) {
124  uint8_t lead = mask8(*lead_it);
125  if (lead < 0x80)
126  return 1;
127  else if ((lead >> 5) == 0x6)
128  return 2;
129  else if ((lead >> 4) == 0xe)
130  return 3;
131  else if ((lead >> 3) == 0x1e)
132  return 4;
133  else
134  return 0;
135 }
136 
137 } // namespace utf8
138 
139 } // namespace smash
140 
141 #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, 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.