A toolkit for working with phylogenetic data.
v0.19.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
string.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_TEXT_STRING_H_
2 #define GENESIS_UTILS_TEXT_STRING_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2018 Lucas Czech and HITS gGmbH
7 
8  This program is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 
21  Contact:
22  Lucas Czech <lucas.czech@h-its.org>
23  Exelixis Lab, Heidelberg Institute for Theoretical Studies
24  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
25 */
26 
35 
36 #include <functional>
37 #include <iostream>
38 #include <sstream>
39 #include <string>
40 #include <vector>
41 
42 namespace genesis {
43 namespace utils {
44 
45 // =================================================================================================
46 // Compare
47 // =================================================================================================
48 
52 bool contains_ci( std::vector<std::string> const& haystack, std::string const& needle );
53 
57 bool equals_ci( std::string const& lhs, std::string const& rhs );
58 
62 bool starts_with( std::string const & text, std::string const & start );
63 
67 bool ends_with( std::string const & text, std::string const & ending );
68 
69 // =================================================================================================
70 // Substrings
71 // =================================================================================================
72 
76 std::string head( std::string const& text, size_t lines = 10 );
77 
81 std::string tail( std::string const& text, size_t lines = 10 );
82 
83 // =================================================================================================
84 // Find and Count
85 // =================================================================================================
86 
90 size_t count_substring_occurrences( std::string const& str, std::string const& sub );
91 
99 std::vector<std::string> split (
100  std::string const& string,
101  std::string const& delimiters = " ",
102  const bool trim_empty = true
103 );
104 
113 std::vector<std::string> split (
114  std::string const& string,
115  std::function<bool (char)> delimiter_predicate,
116  const bool trim_empty = true
117 );
118 
126 std::vector<std::string> split_at (
127  std::string const& string,
128  std::string const& delimiter,
129  const bool trim_empty = true
130 );
131 
132 // =================================================================================================
133 // Manipulate
134 // =================================================================================================
135 
143 std::string indent(
144  std::string const& text,
145  std::string const& indentation = " "
146 );
147 
152 std::string replace_all (
153  std::string const& text,
154  std::string const& search,
155  std::string const& replace
156 );
157 
161 std::string replace_all_chars (
162  std::string const& text,
163  std::string const& search_chars,
164  char replace
165 );
166 
170 std::string trim_right (
171  std::string const& s,
172  std::string const& delimiters = " \f\n\r\t\v"
173 );
174 
178 std::string trim_left (
179  std::string const& s,
180  std::string const& delimiters = " \f\n\r\t\v"
181 );
182 
186 std::string trim (
187  std::string const& s,
188  std::string const& delimiters = " \f\n\r\t\v"
189 );
190 
191 // =================================================================================================
192 // Normalize
193 // =================================================================================================
194 
198 inline std::string to_lower( std::string const& str )
199 {
200  auto res = str;
201  for( auto& c : res ){
202  c = tolower(c);
203  }
204  return res;
205 }
206 
210 inline void to_lower_inplace( std::string& str )
211 {
212  for( auto& c : str ){
213  c = tolower(c);
214  }
215 }
216 
220 inline std::string to_upper( std::string const& str )
221 {
222  auto res = str;
223  for( auto& c : res ){
224  c = toupper(c);
225  }
226  return res;
227 }
228 
232 inline void to_upper_inplace( std::string& str )
233 {
234  for( auto& c : str ){
235  c = toupper(c);
236  }
237 }
238 
242 inline std::string to_lower_ascii( std::string const& str )
243 {
244  auto res = str;
245  for( auto& c : res ){
246  c = to_lower_ascii(c);
247  }
248  return res;
249 }
250 
254 inline void to_lower_ascii_inplace( std::string& str )
255 {
256  for( auto& c : str ){
257  c = to_lower_ascii(c);
258  }
259 }
260 
264 inline std::string to_upper_ascii( std::string const& str )
265 {
266  auto res = str;
267  for( auto& c : res ){
268  c = to_upper_ascii(c);
269  }
270  return res;
271 }
272 
276 inline void to_upper_ascii_inplace( std::string& str )
277 {
278  for( auto& c : str ){
279  c = to_upper_ascii(c);
280  }
281 }
282 
290 std::string escape( std::string const& text );
291 
303 std::string deescape( std::string const& text );
304 
313 char deescape( char c );
314 
315 // =================================================================================================
316 // Output
317 // =================================================================================================
318 
322 std::string repeat( std::string const& word, size_t times );
323 
330 std::string to_string_leading_zeros( size_t value, size_t length = 6 );
331 
343 std::string to_string_precise( double value, int precision = 6 );
344 
356 std::string to_string_rounded( double value, int precision = 6 );
357 
372 template <typename T>
373 std::string to_string( T const& v )
374 {
375  std::ostringstream s;
376  s << v;
377  return s.str();
378 }
379 
390 template <typename T>
391 std::string join( T const& v, std::string const& delimiter )
392 {
393  std::ostringstream s;
394  for( auto const& i : v ) {
395  if( &i != &v[0] ) {
396  s << delimiter;
397  }
398  s << i;
399  }
400  return s.str();
401 }
402 
403 } // namespace utils
404 } // namespace genesis
405 
406 #endif // include guard
size_t count_substring_occurrences(std::string const &str, std::string const &sub)
Return the number of (possibly overlapping) occurrences of a substring in a string.
Definition: string.cpp:115
char to_lower_ascii(char c)
Return the lower case of a given char, ascii-only.
Definition: char.hpp:81
void to_lower_inplace(std::string &str)
Turn the given string to all-lowercase, locale-aware.
Definition: string.hpp:210
std::string to_string_rounded(double const value, int const precision)
Return a string representation of the input value, using the provided precision value (determining it...
Definition: string.cpp:405
std::vector< std::string > split_at(std::string const &string, std::string const &delimiter, const bool trim_empty)
Spilt a string into parts, given a delimiter string.
Definition: string.cpp:212
bool starts_with(std::string const &text, std::string const &start)
Return whether a string starts with another string.
Definition: string.cpp:73
std::string trim(std::string const &s, std::string const &delimiters)
Return a copy of the input string, with trimmed white spaces.
Definition: string.cpp:311
std::string to_upper(std::string const &str)
Return an all-uppercase copy of the given string, locale-aware.
Definition: string.hpp:220
std::string trim_right(std::string const &s, std::string const &delimiters)
Return a copy of the input string, with left trimmed white spaces.
Definition: string.cpp:287
std::string deescape(std::string const &text)
Return a string where backslash-escaped characters are transformed into their respective string form...
Definition: string.cpp:334
std::string join(T const &v, std::string const &delimiter)
Return a string where the elements of a container v are joined using the string delimiter in between ...
Definition: string.hpp:391
std::string head(std::string const &text, size_t lines)
Return the first lines of the text.
Definition: string.cpp:93
std::string repeat(std::string const &word, size_t times)
Take a string and repeat it a given number of times.
Definition: string.cpp:377
std::string replace_all(std::string const &text, std::string const &search, std::string const &replace)
Return a copy of a string, where all occurrences of a search string are replaced by a replace string...
Definition: string.cpp:239
std::vector< std::string > split(std::string const &string, std::function< size_t(std::string const &, size_t)> find_pos, size_t advance_by, const bool trim_empty)
Local function that does the work for the split cuntions.
Definition: string.cpp:136
std::string to_string(T const &v)
Return a string representation of a given value.
Definition: string.hpp:373
std::string to_lower(std::string const &str)
Return an all-lowercase copy of the given string, locale-aware.
Definition: string.hpp:198
std::string replace_all_chars(std::string const &text, std::string const &search_chars, char replace)
Replace all occurrences of the search_chars in text by the replace char.
Definition: string.cpp:273
std::string escape(std::string const &text)
Return a string where special chars are replaces by their escape sequence.
Definition: string.cpp:322
std::string tail(std::string const &text, size_t lines)
Return the last lines of the text.
Definition: string.cpp:102
std::string indent(std::string const &text, std::string const &indentation)
Indent each line of text with indentation and return the result.
Definition: string.cpp:231
bool equals_ci(std::string const &lhs, std::string const &rhs)
Compare two strings case insensitive.
Definition: string.cpp:59
std::string to_string_precise(double const value, int const precision)
Return a precise string representation of the input value, using the provided precision value (determ...
Definition: string.cpp:397
std::string to_string_leading_zeros(size_t value, size_t length)
Return a string representation of a size_t value with a fixed length, that is, by adding leading zero...
Definition: string.cpp:390
std::string trim_left(std::string const &s, std::string const &delimiters)
Return a copy of the input string, with right trimmed white spaces.
Definition: string.cpp:299
char to_upper_ascii(char c)
Return the upper case of a given char, ascii-only.
Definition: char.hpp:89
bool ends_with(std::string const &text, std::string const &ending)
Return whether a string ends with another string.
Definition: string.cpp:81
void to_upper_inplace(std::string &str)
Turn the given string to all-uppercase, locale-aware.
Definition: string.hpp:232
void to_upper_ascii_inplace(std::string &str)
Turn the given string to all-uppercase, ascii-only.
Definition: string.hpp:276
void to_lower_ascii_inplace(std::string &str)
Turn the given string to all-lowercase, ascii-only.
Definition: string.hpp:254
double length(Tree const &tree)
Get the length of the tree, i.e., the sum of all branch lengths.
bool contains_ci(std::vector< std::string > const &haystack, std::string const &needle)
Return whether a vector of strings contains a given string, case insensitive.
Definition: string.cpp:48