A toolkit for working with phylogenetic data.
v0.20.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& str,
101  std::string const& delimiters = " ",
102  const bool trim_empty = true
103 );
104 
113 std::vector<std::string> split (
114  std::string const& str,
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& str,
128  std::string const& delimiter,
129  const bool trim_empty = true
130 );
131 
138 std::vector<size_t> split_range_list( std::string const& str );
139 
140 // =================================================================================================
141 // Manipulate
142 // =================================================================================================
143 
151 std::string indent(
152  std::string const& text,
153  std::string const& indentation = " "
154 );
155 
160 std::string replace_all (
161  std::string const& text,
162  std::string const& search,
163  std::string const& replace
164 );
165 
169 std::string replace_all_chars (
170  std::string const& text,
171  std::string const& search_chars,
172  char replace
173 );
174 
178 std::string trim_right (
179  std::string const& s,
180  std::string const& delimiters = " \f\n\r\t\v"
181 );
182 
186 std::string trim_left (
187  std::string const& s,
188  std::string const& delimiters = " \f\n\r\t\v"
189 );
190 
194 std::string trim (
195  std::string const& s,
196  std::string const& delimiters = " \f\n\r\t\v"
197 );
198 
199 // =================================================================================================
200 // Normalize
201 // =================================================================================================
202 
206 inline std::string to_lower( std::string const& str )
207 {
208  auto res = str;
209  for( auto& c : res ){
210  c = tolower(c);
211  }
212  return res;
213 }
214 
218 inline void to_lower_inplace( std::string& str )
219 {
220  for( auto& c : str ){
221  c = tolower(c);
222  }
223 }
224 
228 inline std::string to_upper( std::string const& str )
229 {
230  auto res = str;
231  for( auto& c : res ){
232  c = toupper(c);
233  }
234  return res;
235 }
236 
240 inline void to_upper_inplace( std::string& str )
241 {
242  for( auto& c : str ){
243  c = toupper(c);
244  }
245 }
246 
250 inline std::string to_lower_ascii( std::string const& str )
251 {
252  auto res = str;
253  for( auto& c : res ){
254  c = to_lower_ascii(c);
255  }
256  return res;
257 }
258 
262 inline void to_lower_ascii_inplace( std::string& str )
263 {
264  for( auto& c : str ){
265  c = to_lower_ascii(c);
266  }
267 }
268 
272 inline std::string to_upper_ascii( std::string const& str )
273 {
274  auto res = str;
275  for( auto& c : res ){
276  c = to_upper_ascii(c);
277  }
278  return res;
279 }
280 
284 inline void to_upper_ascii_inplace( std::string& str )
285 {
286  for( auto& c : str ){
287  c = to_upper_ascii(c);
288  }
289 }
290 
298 std::string escape( std::string const& text );
299 
311 std::string deescape( std::string const& text );
312 
321 char deescape( char c );
322 
323 // =================================================================================================
324 // Output
325 // =================================================================================================
326 
330 std::string repeat( std::string const& word, size_t times );
331 
338 std::string to_string_leading_zeros( size_t value, size_t length = 6 );
339 
351 std::string to_string_precise( double value, int precision = 6 );
352 
364 std::string to_string_rounded( double value, int precision = 6 );
365 
380 template <typename T>
381 std::string to_string( T const& v )
382 {
383  std::ostringstream s;
384  s << v;
385  return s.str();
386 }
387 
398 template <typename T>
399 std::string join( T const& v, std::string const& delimiter )
400 {
401  std::ostringstream s;
402  for( auto const& i : v ) {
403  if( &i != &(*v.begin()) ) {
404  s << delimiter;
405  }
406  s << i;
407  }
408  return s.str();
409 }
410 
411 } // namespace utils
412 } // namespace genesis
413 
414 #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:116
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:218
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:446
bool starts_with(std::string const &text, std::string const &start)
Return whether a string starts with another string.
Definition: string.cpp:74
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:352
std::string to_upper(std::string const &str)
Return an all-uppercase copy of the given string, locale-aware.
Definition: string.hpp:228
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:328
std::string deescape(std::string const &text)
Return a string where backslash-escaped characters are transformed into their respective string form...
Definition: string.cpp:375
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:399
std::string head(std::string const &text, size_t lines)
Return the first lines of the text.
Definition: string.cpp:94
std::string repeat(std::string const &word, size_t times)
Take a string and repeat it a given number of times.
Definition: string.cpp:418
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:280
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:137
std::string to_string(T const &v)
Return a string representation of a given value.
Definition: string.hpp:381
std::string to_lower(std::string const &str)
Return an all-lowercase copy of the given string, locale-aware.
Definition: string.hpp:206
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:314
std::vector< size_t > split_range_list(std::string const &str)
Split a string containing positive interger numbers into its parts and resolve ranges.
Definition: string.cpp:228
std::string escape(std::string const &text)
Return a string where special chars are replaces by their escape sequence.
Definition: string.cpp:363
std::string tail(std::string const &text, size_t lines)
Return the last lines of the text.
Definition: string.cpp:103
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:272
bool equals_ci(std::string const &lhs, std::string const &rhs)
Compare two strings case insensitive.
Definition: string.cpp:60
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:438
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:431
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:340
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:82
void to_upper_inplace(std::string &str)
Turn the given string to all-uppercase, locale-aware.
Definition: string.hpp:240
std::vector< std::string > split_at(std::string const &str, std::string const &delimiter, const bool trim_empty)
Spilt a string into parts, given a delimiter string.
Definition: string.cpp:213
void to_upper_ascii_inplace(std::string &str)
Turn the given string to all-uppercase, ascii-only.
Definition: string.hpp:284
void to_lower_ascii_inplace(std::string &str)
Turn the given string to all-lowercase, ascii-only.
Definition: string.hpp:262
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:49