A toolkit for working with phylogenetic data.
v0.24.0
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-2020 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 <cctype>
37 #include <functional>
38 #include <iostream>
39 #include <sstream>
40 #include <string>
41 #include <vector>
42 
43 namespace genesis {
44 namespace utils {
45 
46 // =================================================================================================
47 // Compare
48 // =================================================================================================
49 
53 bool contains_ci( std::vector<std::string> const& haystack, std::string const& needle );
54 
58 bool equals_ci( std::string const& lhs, std::string const& rhs );
59 
63 bool starts_with( std::string const & text, std::string const & start );
64 
68 bool ends_with( std::string const & text, std::string const & ending );
69 
70 // =================================================================================================
71 // Substrings
72 // =================================================================================================
73 
77 std::string head( std::string const& text, size_t lines = 10 );
78 
82 std::string tail( std::string const& text, size_t lines = 10 );
83 
84 // =================================================================================================
85 // Find and Count
86 // =================================================================================================
87 
91 size_t count_substring_occurrences( std::string const& str, std::string const& sub );
92 
100 std::vector<std::string> split (
101  std::string const& str,
102  std::string const& delimiters = " ",
103  const bool trim_empty = true
104 );
105 
114 std::vector<std::string> split (
115  std::string const& str,
116  std::function<bool (char)> delimiter_predicate,
117  const bool trim_empty = true
118 );
119 
127 std::vector<std::string> split_at (
128  std::string const& str,
129  std::string const& delimiter,
130  const bool trim_empty = true
131 );
132 
139 std::vector<size_t> split_range_list( std::string const& str );
140 
141 // =================================================================================================
142 // Manipulate
143 // =================================================================================================
144 
148 std::string wrap(
149  std::string const& text,
150  size_t line_length = 80
151 );
152 
160 std::string indent(
161  std::string const& text,
162  std::string const& indentation = " "
163 );
164 
169 std::string replace_all (
170  std::string const& text,
171  std::string const& search,
172  std::string const& replace
173 );
174 
178 std::string replace_all_chars (
179  std::string const& text,
180  std::string const& search_chars,
181  char replace
182 );
183 
187 std::string trim_right (
188  std::string const& s,
189  std::string const& delimiters = " \f\n\r\t\v"
190 );
191 
195 std::string trim_left (
196  std::string const& s,
197  std::string const& delimiters = " \f\n\r\t\v"
198 );
199 
203 std::string trim (
204  std::string const& s,
205  std::string const& delimiters = " \f\n\r\t\v"
206 );
207 
208 // =================================================================================================
209 // Case Conversion
210 // =================================================================================================
211 
215 inline std::string to_lower( std::string const& str )
216 {
217  auto res = str;
218  for( auto& c : res ){
219  // Weird C relicts need weird conversions...
220  // See https://en.cppreference.com/w/cpp/string/byte/tolower
221  c = static_cast<char>( std::tolower( static_cast<unsigned char>( c )));
222  }
223  return res;
224 }
225 
229 inline void to_lower_inplace( std::string& str )
230 {
231  for( auto& c : str ){
232  c = static_cast<char>( std::tolower( static_cast<unsigned char>( c )));
233  }
234 }
235 
239 inline std::string to_upper( std::string const& str )
240 {
241  auto res = str;
242  for( auto& c : res ){
243  c = static_cast<char>( std::toupper( static_cast<unsigned char>( c )));
244  }
245  return res;
246 }
247 
251 inline void to_upper_inplace( std::string& str )
252 {
253  for( auto& c : str ){
254  c = static_cast<char>( std::toupper( static_cast<unsigned char>( c )));
255  }
256 }
257 
261 void to_lower_ascii_inplace( std::string& str );
262 
266 std::string to_lower_ascii( std::string const& str );
267 
273 void to_upper_ascii_inplace( std::string& str );
274 
278 std::string to_upper_ascii( std::string const& str );
279 
280 // =================================================================================================
281 // Normalize
282 // =================================================================================================
283 
291 std::string escape( std::string const& text );
292 
304 std::string deescape( std::string const& text );
305 
314 char deescape( char c );
315 
316 // =================================================================================================
317 // Output
318 // =================================================================================================
319 
323 std::string repeat( std::string const& word, size_t times );
324 
331 std::string to_string_leading_zeros( size_t value, size_t length = 6 );
332 
344 std::string to_string_precise( double value, int precision = 6 );
345 
357 std::string to_string_rounded( double value, int precision = 6 );
358 
373 template <typename T>
374 std::string to_string_nice( T const& v )
375 {
376  std::ostringstream s;
377  s << v;
378  return s.str();
379 }
380 
391 template <typename T>
392 std::string join( T const& v, std::string const& delimiter = ", " )
393 {
394  std::ostringstream s;
395  for( auto const& i : v ) {
396  if( &i != &(*v.begin()) ) {
397  s << delimiter;
398  }
399  s << i;
400  }
401  return s.str();
402 }
403 
404 } // namespace utils
405 } // namespace genesis
406 
407 #endif // include guard
std::string escape(std::string const &text)
Return a string where special chars are replaces by their escape sequence.
Definition: string.cpp:495
void to_lower_inplace(std::string &str)
Turn the given string to all-lowercase, locale-aware.
Definition: string.hpp:229
std::string deescape(std::string const &text)
Return a string where backslash-escaped characters are transformed into their respective string form...
Definition: string.cpp:507
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:578
bool starts_with(std::string const &text, std::string const &start)
Return whether a string starts with another string.
Definition: string.cpp:79
std::string repeat(std::string const &word, size_t times)
Take a string and repeat it a given number of times.
Definition: string.cpp:550
void to_upper_ascii_inplace(std::string &str)
Turn the given string to all-uppercase, ASCII-only, inline.
Definition: string.cpp:467
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:231
std::string to_string_nice(T const &v)
Return a string representation of a given value.
Definition: string.hpp:374
Container namespace for all symbols of genesis in order to keep them separate when used as a library...
std::string to_upper_ascii(std::string const &str)
Return an all-uppercase copy of the given string, ASCII-only.
Definition: string.cpp:484
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:392
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:314
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:382
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:322
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:370
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:394
void to_lower_ascii_inplace(std::string &str)
Turn the given string to all-lowercase, ASCII-only.
Definition: string.cpp:443
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:356
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:570
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:563
std::string wrap(std::string const &text, size_t line_length)
Wrap a text at a given line_length.
Definition: string.cpp:275
std::vector< std::string > split(std::string const &str, std::string const &delimiters, const bool trim_empty)
Spilt a string into parts, given a delimiters set of chars.
Definition: string.cpp:178
std::string head(std::string const &text, size_t lines)
Return the first lines of the text.
Definition: string.cpp:99
bool equals_ci(std::string const &lhs, std::string const &rhs)
Compare two strings case insensitive.
Definition: string.cpp:65
bool ends_with(std::string const &text, std::string const &ending)
Return whether a string ends with another string.
Definition: string.cpp:87
std::string tail(std::string const &text, size_t lines)
Return the last lines of the text.
Definition: string.cpp:108
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:121
constexpr char to_upper(char c) noexcept
Return the upper case version of a letter, ASCII-only.
Definition: char.hpp:227
void to_upper_inplace(std::string &str)
Turn the given string to all-uppercase, locale-aware.
Definition: string.hpp:251
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:216
double length(Tree const &tree)
Get the length of the tree, i.e., the sum of all branch lengths.
constexpr char to_lower(char c) noexcept
Return the lower case version of a letter, ASCII-only.
Definition: char.hpp:218
std::string to_lower_ascii(std::string const &str)
Return an all-lowercase copy of the given string, ASCII-only.
Definition: string.cpp:460
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:54