A toolkit for working with phylogenetic data.
v0.18.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-2017 Lucas Czech
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 
34 #include <iostream>
35 #include <sstream>
36 #include <string>
37 #include <vector>
38 
39 namespace genesis {
40 namespace utils {
41 
42 // =================================================================================================
43 // Compare
44 // =================================================================================================
45 
49 bool equals_ci( std::string const& lhs, std::string const& rhs );
50 
54 bool starts_with( std::string const & text, std::string const & start );
55 
59 bool ends_with( std::string const & text, std::string const & ending );
60 
61 // =================================================================================================
62 // Substrings
63 // =================================================================================================
64 
68 std::string head( std::string const& text, size_t lines = 10 );
69 
73 std::string tail( std::string const& text, size_t lines = 10 );
74 
75 // =================================================================================================
76 // Find and Count
77 // =================================================================================================
78 
82 size_t count_substring_occurrences( std::string const& str, std::string const& sub );
83 
91 std::vector<std::string> split (
92  std::string const& str,
93  std::string const& delimiters = " ",
94  const bool trim_empty = true
95 );
96 
97 // =================================================================================================
98 // Manipulate
99 // =================================================================================================
100 
108 std::string indent(
109  std::string const& text,
110  std::string const& indentation = " "
111 );
112 
117 std::string replace_all (
118  std::string const& text,
119  std::string const& search,
120  std::string const& replace
121 );
122 
126 std::string replace_all_chars (
127  std::string const& text,
128  std::string const& search_chars,
129  char replace
130 );
131 
135 std::string trim_right (
136  std::string const& s,
137  std::string const& delimiters = " \f\n\r\t\v"
138 );
139 
143 std::string trim_left (
144  std::string const& s,
145  std::string const& delimiters = " \f\n\r\t\v"
146 );
147 
151 std::string trim (
152  std::string const& s,
153  std::string const& delimiters = " \f\n\r\t\v"
154 );
155 
156 // =================================================================================================
157 // Normalize
158 // =================================================================================================
159 
163 std::string to_lower( std::string const& str );
164 
168 void to_lower_inplace( std::string& str );
169 
173 std::string to_upper( std::string const& str );
174 
178 void to_upper_inplace( std::string& str );
179 
183 char to_lower_ascii( char c );
184 
188 char to_upper_ascii( char c );
189 
193 std::string to_lower_ascii( std::string const& str );
194 
198 void to_lower_ascii_inplace( std::string& str );
199 
203 std::string to_upper_ascii( std::string const& str );
204 
208 void to_upper_ascii_inplace( std::string& str );
209 
217 std::string escape ( std::string const& text );
218 
230 std::string deescape( std::string const& text );
231 
240 char deescape( char c );
241 
242 // =================================================================================================
243 // Output
244 // =================================================================================================
245 
249 std::string repeat( std::string const& word, size_t times );
250 
257 std::string to_string_leading_zeros( size_t value, size_t length = 6 );
258 
270 std::string to_string_precise( double value, int precision = 6 );
271 
283 std::string to_string_rounded( double value, int precision = 6 );
284 
299 template <typename T>
300 std::string to_string( T const& v )
301 {
302  std::ostringstream s;
303  s << v;
304  return s.str();
305 }
306 
317 template <typename T>
318 std::string join( T const& v, std::string const& delimiter )
319 {
320  std::ostringstream s;
321  for( auto const& i : v ) {
322  if( &i != &v[0] ) {
323  s << delimiter;
324  }
325  s << i;
326  }
327  return s.str();
328 }
329 
330 } // namespace utils
331 } // namespace genesis
332 
333 #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:103
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:406
bool starts_with(std::string const &text, std::string const &start)
Return whether a string starts with another string.
Definition: string.cpp:61
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:238
void to_upper_ascii_inplace(std::string &str)
Turn the given string to all-uppercase, ascii-only.
Definition: string.cpp:316
std::vector< std::string > split(std::string const &str, std::string const &delimiters, const bool trim_empty)
Spilt a string into parts, given a set of delimiter chars.
Definition: string.cpp:121
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:214
std::string deescape(std::string const &text)
Return a string where backslash-escaped characters are transformed into their respective string form...
Definition: string.cpp:335
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:318
std::string to_upper(std::string const &str)
Return an all-uppercase copy of the given string, locale-aware.
Definition: string.cpp:265
std::string head(std::string const &text, size_t lines)
Return the first lines of the text.
Definition: string.cpp:81
std::string repeat(std::string const &word, size_t times)
Take a string and repeat it a given number of times.
Definition: string.cpp:378
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:166
char to_lower_ascii(char c)
Return the lower case of a given char, ascii-only.
Definition: string.cpp:281
std::string to_string(T const &v)
Return a string representation of a given value.
Definition: string.hpp:300
std::string to_lower(std::string const &str)
Return an all-lowercase copy of the given string, locale-aware.
Definition: string.cpp:249
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:200
std::string escape(std::string const &text)
Return a string where special chars are replaces by their escape sequence.
Definition: string.cpp:323
std::string tail(std::string const &text, size_t lines)
Return the last lines of the text.
Definition: string.cpp:90
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:158
void to_lower_ascii_inplace(std::string &str)
Turn the given string to all-lowercase, ascii-only.
Definition: string.cpp:300
bool equals_ci(std::string const &lhs, std::string const &rhs)
Compare two strings case insensitive.
Definition: string.cpp:47
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:398
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:391
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:226
bool ends_with(std::string const &text, std::string const &ending)
Return whether a string ends with another string.
Definition: string.cpp:69
void to_lower_inplace(std::string &str)
Turn the given string to all-lowercase, locale-aware.
Definition: string.cpp:258
char to_upper_ascii(char c)
Return the upper case of a given char, ascii-only.
Definition: string.cpp:286
double length(Tree const &tree)
Get the length of the tree, i.e., the sum of all branch lengths.
void to_upper_inplace(std::string &str)
Turn the given string to all-uppercase, locale-aware.
Definition: string.cpp:274