Version: SMASH-2.0
stringfunctions.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2014-2020
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 <string>
14 #include <vector>
15 
16 namespace smash {
17 
24 std::string trim(const std::string &s);
25 
32 void remove_substr(std::string &s, const std::string &p);
33 
39 void isoclean(std::string &s);
40 
48 std::vector<std::string> split(const std::string &s, char delim);
49 
50 namespace utf8 {
59 std::string fill_left(const std::string &s, size_t width, char fill = ' ');
60 
69 std::string fill_right(const std::string &s, size_t width, char fill = ' ');
70 
79 std::string fill_both(const std::string &s, size_t width, char fill = ' ');
80 
89 template <typename octet_type>
90 inline uint8_t mask8(octet_type oc) {
91  return static_cast<uint8_t>(0xff & oc);
92 }
93 
101 template <typename octet_iterator>
102 inline typename std::iterator_traits<octet_iterator>::difference_type
103 sequence_length(octet_iterator lead_it) {
104  uint8_t lead = mask8(*lead_it);
105  if (lead < 0x80)
106  return 1;
107  else if ((lead >> 5) == 0x6)
108  return 2;
109  else if ((lead >> 4) == 0xe)
110  return 3;
111  else if ((lead >> 3) == 0x1e)
112  return 4;
113  else
114  return 0;
115 }
116 
117 } // namespace utf8
118 
119 } // namespace smash
120 
121 #endif // SRC_INCLUDE_SMASH_STRINGFUNCTIONS_H_
smash::split
std::vector< std::string > split(const std::string &s, char delim)
Split string by delimiter.
Definition: stringfunctions.cc:120
smash
Definition: action.h:24
smash::utf8::mask8
uint8_t mask8(octet_type oc)
Extract the first byte from a given value.
Definition: stringfunctions.h:90
smash::utf8::fill_left
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: stringfunctions.cc:47
smash::remove_substr
void remove_substr(std::string &s, const std::string &p)
Remove all instances of a substring p in a string s.
Definition: stringfunctions.cc:84
smash::isoclean
void isoclean(std::string &s)
Remove ⁺, ⁻, ⁰ from string.
Definition: stringfunctions.cc:92
smash::utf8::fill_right
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.
Definition: stringfunctions.cc:55
smash::utf8::fill_both
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.
Definition: stringfunctions.cc:63
smash::utf8::sequence_length
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...
Definition: stringfunctions.h:103
smash::trim
std::string trim(const std::string &s)
Strip leading and trailing whitespaces.
Definition: stringfunctions.cc:75
smash::pdg::p
constexpr int p
Proton.
Definition: pdgcode_constants.h:28