A library for working with phylogenetic and population genetic data.
v0.32.0
string.hpp File Reference

Detailed Description

Provides some commonly used string utility functions.

Definition in file string.hpp.

#include "genesis/utils/text/char.hpp"
#include <algorithm>
#include <cctype>
#include <functional>
#include <iostream>
#include <sstream>
#include <string>
#include <type_traits>
#include <vector>

Go to the source code of this file.

Classes

struct  NaturalGreater< T >
 Functor class to compare to strings with natural "human" sorting, see compare_natural(). More...
 
struct  NaturalLess< T >
 Functor class to compare to strings with natural "human" sorting, see compare_natural(). More...
 

Namespaces

 genesis
 Container namespace for all symbols of genesis in order to keep them separate when used as a library.
 
 genesis::utils
 

Functions

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 sort by ASCII value "A1", "A100", "A2". More...
 
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. More...
 
bool contains_ci_alnum (std::vector< std::string > const &haystack, std::string const &needle)
 Return whether a vector of strings contains a given string, case insensitive, and ignoring all non-alphanumerical characters. More...
 
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. More...
 
char deescape (char c)
 Return the de-escaped char for a backslash-escaped char. More...
 
std::string deescape (std::string const &text)
 Return a string where backslash-escaped characters are transformed into their respective string form. More...
 
bool ends_with (std::string const &text, std::string const &suffix)
 Return whether a string ends with another string, i.e., check for a suffix. More...
 
bool ends_with (std::string const &text, std::string const &suffix, std::string &prefix)
 Return whether a string ends with another string, i.e., check for a suffix. More...
 
bool ends_with_ci (std::string const &text, std::string const &suffix)
 Return whether a string ends with another string, i.e., check for a suffix, case insensitive. More...
 
bool ends_with_ci (std::string const &text, std::string const &suffix, std::string &prefix)
 Return whether a string ends with another string, i.e., check for a suffix, case insensitive. More...
 
bool ends_with_ci_alnum (std::string const &text, std::string const &suffix)
 Return whether a string ends with another string (suffix), comparing case-independent, and ignoring all non-alphanumerical characters. More...
 
bool ends_with_ci_alnum (std::string const &text, std::string const &suffix, std::string &prefix, bool trim_prefix=false)
 Return whether a string ends with another string (suffix), comparing case-independent, and ignoring all non-alphanumerical characters. More...
 
bool equals_ci (std::string const &lhs, std::string const &rhs)
 Compare two strings, case insensitive. More...
 
bool equals_ci_alnum (std::string const &lhs, std::string const &rhs)
 Compare two strings, case insensitive, and ignoring all non-alphanumerical characters. More...
 
std::string escape (std::string const &text)
 Return a string where special chars are replaces by their escape sequence. More...
 
std::string head (std::string const &text, size_t lines=10)
 Return the first lines of the text. More...
 
std::string indent (std::string const &text, std::string const &indentation=" ")
 Indent each line of text with indentation and return the result. More...
 
template<typename C >
std::string join (C const &container, std::string const &delimiter=", ")
 Return a string where the elements of a container v are joined using the string delimiter in between them. More...
 
template<typename C , typename std::enable_if< ! std::is_same< typename C::value_type, unsigned char >::value &&! std::is_same< typename C::value_type, signed char >::value >::type * = nullptr>
std::ostream & join (std::ostream &stream, C const &container, std::string const &delimiter=", ")
 Print elements of the given container to the output stream, joining them with the delimiter. More...
 
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 (0 or more) wildcard characters, respectively. More...
 
std::string remove_all (std::string const &text, std::string const &search)
 Return a copy of a string, where all occurrences of a search string are removed. More...
 
std::string remove_all_chars (std::string const &text, std::string const &search_chars)
 Remove all occurrences of the search_chars in text. More...
 
template<class UnaryPredicate >
std::string remove_all_chars_pred (std::string const &text, UnaryPredicate predicate)
 Remove all occurrences characters for which predicate is true in text. More...
 
std::string remove_all_non_alnum (std::string const &text)
 Remove all non-alphanumerical characters from a string. More...
 
std::string repeat (std::string const &word, size_t times)
 Take a string and repeat it a given number of times. More...
 
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. More...
 
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. More...
 
template<class UnaryPredicate >
std::string replace_all_chars_pred (std::string const &text, UnaryPredicate predicate, char replace)
 Replace all occurrences of characters for which predicate is true in text by the replace char. More...
 
template<typename RandomAccessIterator >
void sort_natural (RandomAccessIterator first, RandomAccessIterator last, bool reverse=false)
 Sort a range of std::string (or convertible to std::string) elements, using natural sorting; see compare_natural(). More...
 
std::vector< std::string > split (std::string const &str, char delimiter='\t', const bool trim_empty=true)
 Spilt a string into parts, given a delimiter char. More...
 
std::vector< std::string > split (std::string const &str, std::function< bool(char)> delimiter_predicate, const bool trim_empty=true)
 Spilt a string into parts, given a delimiter_predicate that returns true for delimiters chars. More...
 
std::vector< std::string > split (std::string const &str, std::string const &delimiters, const bool trim_empty=true)
 Spilt a string into parts, given a delimiters set of chars. More...
 
std::vector< std::string > split_at (std::string const &str, std::string const &delimiter, const bool trim_empty=true)
 Spilt a string into parts, given a delimiter string. More...
 
std::vector< size_t > split_range_list (std::string const &str)
 Split a string containing positive interger numbers into its parts and resolve ranges. More...
 
bool starts_with (std::string const &text, std::string const &prefix)
 Return whether a string starts with another string, i.e., check for a prefix. More...
 
bool starts_with (std::string const &text, std::string const &prefix, std::string &suffix)
 Return whether a string starts with another string, i.e., check for a prefix. More...
 
bool starts_with_ci (std::string const &text, std::string const &prefix)
 Return whether a string starts with another string, i.e., check for a prefix, case insensitive. More...
 
bool starts_with_ci (std::string const &text, std::string const &prefix, std::string &suffix)
 Return whether a string starts with another string, i.e., check for a prefix, case insensitive. More...
 
bool starts_with_ci_alnum (std::string const &text, std::string const &prefix)
 Return whether a string starts with another string (prefix), comparing case-independent, and ignoring all non-alphanumerical characters. More...
 
bool starts_with_ci_alnum (std::string const &text, std::string const &prefix, std::string &suffix, bool trim_suffix=false)
 Return whether a string starts with another string (prefix), comparing case-independent, and ignoring all non-alphanumerical characters. More...
 
int strcasecmp (char const *s1, char const *s2)
 Compares two strings, ignoring case differences. More...
 
int strncasecmp (char const *s1, char const *s2, size_t n)
 Compares up to n chars of two strings, ignoring case differences. More...
 
std::string tail (std::string const &text, size_t lines=10)
 Return the last lines of the text. More...
 
template<typename T >
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. More...
 
std::string to_lower (std::string const &str)
 Return an all-lowercase copy of the given string, locale-aware. More...
 
std::string to_lower_ascii (std::string const &str)
 Return an all-lowercase copy of the given string, ASCII-only. More...
 
void to_lower_ascii_inplace (std::string &str)
 Turn the given string to all-lowercase, ASCII-only. More...
 
void to_lower_inplace (std::string &str)
 Turn the given string to all-lowercase, locale-aware. More...
 
std::string to_string_byte_format (size_t value)
 Produce a human readable formatting of a size in bytes, using the appropriate suffix. More...
 
std::string to_string_leading_zeros (size_t value, size_t length=6)
 Return a string representation of a size_t value with a fixed length, that is, by adding leading zeros. More...
 
template<typename T >
std::string to_string_nice (T const &v)
 Return a string representation of a given value. More...
 
std::string to_string_precise (double value, int precision=6)
 Return a precise string representation of the input value, using the provided precision value (determining its decimal places). More...
 
std::string to_string_rounded (double value, int precision=6)
 Return a string representation of the input value, using the provided precision value (determining its decimal places) to round, and truncate trailing zeros. More...
 
std::string to_upper (std::string const &str)
 Return an all-uppercase copy of the given string, locale-aware. More...
 
std::string to_upper_ascii (std::string const &str)
 Return an all-uppercase copy of the given string, ASCII-only. More...
 
void to_upper_ascii_inplace (std::string &str)
 Turn the given string to all-uppercase, ASCII-only, inline. More...
 
void to_upper_inplace (std::string &str)
 Turn the given string to all-uppercase, locale-aware. More...
 
std::string trim (std::string const &s, std::string const &delimiters=" \f\n\r\t\v")
 Return a copy of the input string, with trimmed white spaces (or any other delimiters). More...
 
std::string trim_left (std::string const &s, std::string const &delimiters=" \f\n\r\t\v")
 Return a copy of the input string, with right trimmed white spaces (or any other delimiters). More...
 
std::string trim_right (std::string const &s, std::string const &delimiters=" \f\n\r\t\v")
 Return a copy of the input string, with left trimmed white spaces (or any other delimiters). More...
 
std::string wrap (std::string const &text, size_t line_length=80)
 Wrap a text at a given line_length. More...