Compare View
Commits (2)
Showing
1 changed file
Show diff stats
include/cbutils.h
... | ... | @@ -39,16 +39,17 @@ namespace cbutils |
39 | 39 | namespace string |
40 | 40 | { |
41 | 41 | // Splits a string using any of a list of separators |
42 | -std::vector<std::string> split(const std::string& str, | |
42 | +[[nodiscard]] std::vector<std::string> split(const std::string& str, | |
43 | 43 | const char* separators = "\t ") |
44 | -{ | |
44 | +{ | |
45 | 45 | std::vector< std::string > tokens; |
46 | - boost::split(tokens, str, boost::is_any_of(separators), | |
46 | + boost::split(tokens, str, boost::is_any_of(separators), | |
47 | 47 | boost::token_compress_on); |
48 | 48 | return tokens; |
49 | 49 | } |
50 | 50 | // Joins a vector of strings into a string |
51 | -std::string join(const std::vector<std::string>& vos, char sep=' ') | |
51 | +[[nodiscard]] std::string join(const std::vector<std::string>& vos, | |
52 | + char sep=' ') | |
52 | 53 | { |
53 | 54 | std::stringstream ss; |
54 | 55 | ss << vos[0]; |
... | ... | @@ -59,7 +60,7 @@ std::string join(const std::vector<std::string>& vos, char sep=' ') |
59 | 60 | return ss.str(); |
60 | 61 | } |
61 | 62 | // True if the string contains one of the patterns |
62 | -bool contains(std::string s, std::vector<std::string> patterns) | |
63 | +[[nodiscard]] bool contains(std::string s, std::vector<std::string> patterns) | |
63 | 64 | { |
64 | 65 | bool result{false}; |
65 | 66 | for(const auto& pat : patterns) |
... | ... | @@ -75,10 +76,9 @@ bool contains(std::string s, std::vector<std::string> patterns) |
75 | 76 | // ----------------- System calls ---------------------------------------------- |
76 | 77 | namespace system{ |
77 | 78 | // Runs the command sending it to the OS and returns a string wit its output |
78 | - std::string call(const std::string& command) | |
79 | - { | |
79 | + std::string call(const std::string &command) { | |
80 | 80 | std::string tmp_filename{std::tmpnam(nullptr)}; |
81 | - std::stringstream command_redirect{command+" > "+tmp_filename}; | |
81 | + std::stringstream command_redirect{command + " > " + tmp_filename}; | |
82 | 82 | std::system(command_redirect.str().c_str()); |
83 | 83 | std::stringstream command_output; |
84 | 84 | command_output << std::ifstream(tmp_filename).rdbuf(); |
... | ... | @@ -90,7 +90,7 @@ bool contains(std::string s, std::vector<std::string> patterns) |
90 | 90 | namespace file |
91 | 91 | { |
92 | 92 | //Reads a text file and returns a vector of lines |
93 | -std::vector<std::string> read_lines(const std::string& fname) | |
93 | +[[nodiscard]] std::vector<std::string> read_lines(const std::string& fname) | |
94 | 94 | { |
95 | 95 | std::ifstream ifs(fname); |
96 | 96 | std::vector<std::string> lines{}; |
... | ... | @@ -103,7 +103,7 @@ std::vector<std::string> read_lines(const std::string& fname) |
103 | 103 | return lines; |
104 | 104 | } |
105 | 105 | //Count the number of columns of a CSV file |
106 | -std::size_t count_columns(const std::string& fname, | |
106 | +[[nodiscard]] std::size_t count_columns(const std::string& fname, | |
107 | 107 | const char* separators = "\t ") |
108 | 108 | { |
109 | 109 | std::ifstream ifile(fname); |
... | ... | @@ -124,7 +124,7 @@ std::size_t count_columns(const std::string& fname, |
124 | 124 | #ifdef USE_FILESYSEM |
125 | 125 | // List the files in a directory eventually matching a pattern |
126 | 126 | // needs to link with -lboost_system -lboost_filesystem |
127 | -std::vector<std::string> list_files(const std::string& dir, | |
127 | +[[nodiscard]] std::vector<std::string> list_files(const std::string& dir, | |
128 | 128 | const std::string& pattern = ".*") |
129 | 129 | { |
130 | 130 | std::regex regex_pattern(pattern); |
... | ... | @@ -149,8 +149,8 @@ std::vector<std::string> list_files(const std::string& dir, |
149 | 149 | namespace seq |
150 | 150 | { |
151 | 151 | //Chops off the head of a sequence and returns its tail |
152 | -template <typename Seq> | |
153 | -Seq tail(const Seq& s) | |
152 | +template <typename Seq> | |
153 | +[[nodiscard]] Seq tail(const Seq& s) | |
154 | 154 | { |
155 | 155 | Seq result{}; |
156 | 156 | auto head = std::begin(s); |
... | ... | @@ -159,8 +159,8 @@ Seq tail(const Seq& s) |
159 | 159 | return result; |
160 | 160 | } |
161 | 161 | //Returns the last element of a sequence |
162 | -template <typename Seq> | |
163 | -auto last(const Seq& s) | |
162 | +template <typename Seq> | |
163 | +[[nodiscard]] auto last(const Seq& s) | |
164 | 164 | { |
165 | 165 | auto element = s.cend(); |
166 | 166 | --element; |
... | ... | @@ -168,7 +168,7 @@ auto last(const Seq& s) |
168 | 168 | } |
169 | 169 | //python-like range function |
170 | 170 | template <typename T> |
171 | -std::vector<T> range(T minvalue, T maxvalue, T step=1) | |
171 | +[[nodiscard]] std::vector<T> range(T minvalue, T maxvalue, T step=1) | |
172 | 172 | { |
173 | 173 | assert(step>0); |
174 | 174 | auto nb_elements = (maxvalue-minvalue)/step+1; |
... | ... | @@ -184,19 +184,21 @@ std::vector<T> range(T minvalue, T maxvalue, T step=1) |
184 | 184 | namespace rand |
185 | 185 | { |
186 | 186 | template <typename T> |
187 | -T uniform(T minvalue, T maxvalue, std::mt19937& engine, std::true_type) | |
187 | +[[nodiscard]] T uniform(T minvalue, T maxvalue, std::mt19937& engine, | |
188 | + std::true_type) | |
188 | 189 | { |
189 | 190 | static std::uniform_int_distribution<T> dist(minvalue, maxvalue); |
190 | 191 | return dist(engine); |
191 | 192 | } |
192 | 193 | template <typename T> |
193 | -T uniform(T minvalue, T maxvalue, std::mt19937& engine, std::false_type) | |
194 | +[[nodiscard]] T uniform(T minvalue, T maxvalue, std::mt19937& engine, | |
195 | + std::false_type) | |
194 | 196 | { |
195 | 197 | static std::uniform_real_distribution<T> dist(minvalue, maxvalue); |
196 | 198 | return dist(engine); |
197 | 199 | } |
198 | 200 | template <typename T> |
199 | -T uniform(T minvalue, T maxvalue) | |
201 | +[[nodiscard]] T uniform(T minvalue, T maxvalue) | |
200 | 202 | { |
201 | 203 | static std::random_device random_device; |
202 | 204 | static std::mt19937 engine{random_device()}; |
... | ... | @@ -206,9 +208,10 @@ T uniform(T minvalue, T maxvalue) |
206 | 208 | // ----------------- Miscellaneous --------------------------------------------- |
207 | 209 | namespace misc |
208 | 210 | { |
209 | -std::string now_str() | |
211 | +[[nodiscard]] std::string now_str() | |
210 | 212 | { |
211 | - auto now_t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); | |
213 | + auto now_t = | |
214 | + std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); | |
212 | 215 | std::stringstream result; |
213 | 216 | result << std::put_time(std::localtime(&now_t), "%F %T") << " -- "; |
214 | 217 | return result.str(); | ... | ... |