|
A library for working with phylogenetic and population genetic data.
v0.27.0
|
|
Go to the documentation of this file. 1 #ifndef GENESIS_UTILS_TEXT_STRING_H_
2 #define GENESIS_UTILS_TEXT_STRING_H_
54 bool contains_ci( std::vector<std::string>
const& haystack, std::string
const& needle );
59 bool equals_ci( std::string
const& lhs, std::string
const& rhs );
64 bool starts_with( std::string
const & text, std::string
const & start );
69 bool ends_with( std::string
const & text, std::string
const & ending );
75 bool match_wildcards( std::string
const& str, std::string
const& pattern );
88 template <
class T = std::
string>
89 struct NaturalLess :
public std::binary_function<T, T, bool> {
99 template <
typename RandomAccessIterator>
101 RandomAccessIterator first,
102 RandomAccessIterator last
104 using T =
typename RandomAccessIterator::value_type;
115 std::string
head( std::string
const& text,
size_t lines = 10 );
120 std::string
tail( std::string
const& text,
size_t lines = 10 );
138 std::vector<std::string>
split (
139 std::string
const& str,
140 std::string
const& delimiters =
" ",
141 const bool trim_empty =
true
152 std::vector<std::string>
split (
153 std::string
const& str,
154 std::function<
bool (
char)> delimiter_predicate,
155 const bool trim_empty =
true
166 std::string
const& str,
167 std::string
const& delimiter,
168 const bool trim_empty =
true
187 std::string
const& text,
188 size_t line_length = 80
199 std::string
const& text,
200 std::string
const& indentation =
" "
208 std::string
const& text,
209 std::string
const& search,
210 std::string
const& replace
217 std::string
const& text,
218 std::string
const& search_chars,
226 std::string
const& s,
227 std::string
const& delimiters =
" \f\n\r\t\v"
234 std::string
const& s,
235 std::string
const& delimiters =
" \f\n\r\t\v"
242 std::string
const& s,
243 std::string
const& delimiters =
" \f\n\r\t\v"
253 inline std::string
to_lower( std::string
const& str )
256 for(
auto& c : res ){
259 c =
static_cast<char>( std::tolower(
static_cast<unsigned char>( c )));
269 for(
auto& c : str ){
270 c =
static_cast<char>( std::tolower(
static_cast<unsigned char>( c )));
277 inline std::string
to_upper( std::string
const& str )
280 for(
auto& c : res ){
281 c =
static_cast<char>( std::toupper(
static_cast<unsigned char>( c )));
291 for(
auto& c : str ){
292 c =
static_cast<char>( std::toupper(
static_cast<unsigned char>( c )));
329 std::string
escape( std::string
const& text );
342 std::string
deescape( std::string
const& text );
361 std::string
repeat( std::string
const& word,
size_t times );
411 template <
typename T>
414 std::ostringstream s;
429 template <
typename T>
430 std::string
join( T
const& v, std::string
const& delimiter =
", " )
432 std::ostringstream s;
433 for(
auto const& i : v ) {
434 if( &i != &(*v.begin()) ) {
449 inline std::string join<std::vector<unsigned char>>(
450 std::vector<unsigned char>
const& v, std::string
const& delimiter
452 std::ostringstream s;
453 for(
auto const& i : v ) {
454 if( &i != &(*v.begin()) ) {
457 s << static_cast<int>( i );
471 T
const x,
char const zero =
'0',
char const one =
'1',
bool const byte_space =
true
474 std::is_unsigned<T>::value,
475 "Can only use to_bit_string() with unsigned types."
478 std::string binary =
"";
480 for(
size_t i = 0; i <
sizeof(T) * 8; ++i ) {
481 if( byte_space && i > 0 && i % 8 == 0 ) {
482 binary =
' ' + binary;
485 binary = one + binary;
487 binary = zero + binary;
497 #endif // include guard
std::string deescape(std::string const &text)
Return a string where backslash-escaped characters are transformed into their respective string form.
std::string indent(std::string const &text, std::string const &indentation)
Indent each line of text with indentation and return the result.
std::string trim_right(std::string const &s, std::string const &delimiters)
Return a copy of the input string, with left trimmed white spaces.
std::string tail(std::string const &text, size_t lines)
Return the last lines of the text.
void to_upper_ascii_inplace(std::string &str)
Turn the given string to all-uppercase, ASCII-only, inline.
bool equals_ci(std::string const &lhs, std::string const &rhs)
Compare two strings case insensitive.
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.
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.
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...
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.
std::string trim(std::string const &s, std::string const &delimiters)
Return a copy of the input string, with trimmed white spaces.
void to_lower_ascii_inplace(std::string &str)
Turn the given string to all-lowercase, ASCII-only.
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.
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...
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...
constexpr char to_upper(char c) noexcept
Return the upper case version of a letter, ASCII-only.
std::string head(std::string const &text, size_t lines)
Return the first lines of the text.
bool starts_with(std::string const &text, std::string const &start)
Return whether a string starts with another string.
int compare_natural(std::string const &lhs, std::string const &rhs)
Compare two strings with natural human sorting, that is "A1", "A2", "A100", instead of the standard s...
std::string to_upper_ascii(std::string const &str)
Return an all-uppercase copy of the given string, ASCII-only.
Interval< DataType, NumericalType, IntervalKind > join(Interval< DataType, NumericalType, IntervalKind > const &a, Interval< DataType, NumericalType, IntervalKind > const &b)
Creates a new Interval that contains both intervals and whatever is between.
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
bool ends_with(std::string const &text, std::string const &ending)
Return whether a string ends with another string.
std::string to_string_nice(T const &v)
Return a string representation of a given value.
std::string to_lower_ascii(std::string const &str)
Return an all-lowercase copy of the given string, ASCII-only.
std::string repeat(std::string const &word, size_t times)
Take a string and repeat it a given number of times.
void sort_natural(RandomAccessIterator first, RandomAccessIterator last)
Sort a range of std::string (or convertible to std::string) elements, using natural sorting; see comp...
std::string wrap(std::string const &text, size_t line_length)
Wrap a text at a given line_length.
bool operator()(T const &lhs, T const &rhs) const
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.
constexpr char to_lower(char c) noexcept
Return the lower case version of a letter, ASCII-only.
bool match_wildcards(std::string const &str, std::string const &pattern)
Return whether a string is matched by a wildcard pattern containing ? and * for single and mutliple (...
std::string to_bit_string(T const x, char const zero='0', char const one='1', bool const byte_space=true)
Return the bit representation of an unsigned int.
std::string trim_left(std::string const &s, std::string const &delimiters)
Return a copy of the input string, with right trimmed white spaces.
void to_lower_inplace(std::string &str)
Turn the given string to all-lowercase, locale-aware.
Functor class to compare to strings with natural "human" sorting, see compare_natural().
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.
std::vector< size_t > split_range_list(std::string const &str)
Split a string containing positive interger numbers into its parts and resolve ranges.
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.
std::string escape(std::string const &text)
Return a string where special chars are replaces by their escape sequence.