|
A library for working with phylogenetic and population genetic data.
v0.32.0
|
|
Go to the documentation of this file. 1 #ifndef GENESIS_UTILS_TEXT_STRING_H_
2 #define GENESIS_UTILS_TEXT_STRING_H_
42 #include <type_traits>
55 bool contains_ci( std::vector<std::string>
const& haystack, std::string
const& needle );
61 bool contains_ci_alnum( std::vector<std::string>
const& haystack, std::string
const& needle );
73 int strcasecmp(
char const* s1,
char const* s2 );
85 int strncasecmp(
char const* s1,
char const* s2,
size_t n );
90 bool equals_ci( std::string
const& lhs, std::string
const& rhs );
100 bool starts_with( std::string
const& text, std::string
const& prefix );
109 bool starts_with( std::string
const& text, std::string
const& prefix, std::string& suffix );
115 bool starts_with_ci( std::string
const& text, std::string
const& prefix );
123 bool starts_with_ci( std::string
const& text, std::string
const& prefix, std::string& suffix );
146 std::string
const& text,
147 std::string
const& prefix,
149 bool trim_suffix =
false
155 bool ends_with( std::string
const& text, std::string
const& suffix );
164 bool ends_with( std::string
const& text, std::string
const& suffix, std::string& prefix );
170 bool ends_with_ci( std::string
const& text, std::string
const& suffix );
178 bool ends_with_ci( std::string
const& text, std::string
const& suffix, std::string& prefix );
201 std::string
const& text,
202 std::string
const& suffix,
204 bool trim_prefix =
false
211 bool match_wildcards( std::string
const& str, std::string
const& pattern );
224 template <
class T = std::
string>
234 template <
class T = std::
string>
245 template <
typename RandomAccessIterator>
247 RandomAccessIterator first,
248 RandomAccessIterator last,
256 using T =
typename RandomAccessIterator::value_type;
271 std::string
head( std::string
const& text,
size_t lines = 10 );
276 std::string
tail( std::string
const& text,
size_t lines = 10 );
294 std::vector<std::string>
split (
295 std::string
const& str,
296 char delimiter =
'\t',
297 const bool trim_empty =
true
307 std::vector<std::string>
split (
308 std::string
const& str,
309 std::string
const& delimiters,
310 const bool trim_empty =
true
321 std::vector<std::string>
split (
322 std::string
const& str,
323 std::function<
bool (
char)> delimiter_predicate,
324 const bool trim_empty =
true
335 std::string
const& str,
336 std::string
const& delimiter,
337 const bool trim_empty =
true
356 std::string
const& text,
357 size_t line_length = 80
368 std::string
const& text,
369 std::string
const& indentation =
" "
377 std::string
const& text,
378 std::string
const& search,
379 std::string
const& replace
386 std::string
const& text,
387 std::string
const& search
394 std::string
const& text,
395 std::string
const& search_chars,
403 template<
class UnaryPredicate >
405 std::string
const& text,
406 UnaryPredicate predicate,
410 for(
auto& c : result ) {
411 if( predicate( c ) ) {
422 std::string
const& text,
423 std::string
const& search_chars
429 template<
class UnaryPredicate >
431 std::string
const& text,
432 UnaryPredicate predicate
435 result.erase( std::remove_if( result.begin(), result.end(), predicate ), result.end() );
449 std::string
const& s,
450 std::string
const& delimiters =
" \f\n\r\t\v"
458 std::string
const& s,
459 std::string
const& delimiters =
" \f\n\r\t\v"
467 std::string
const& s,
468 std::string
const& delimiters =
" \f\n\r\t\v"
478 inline std::string
to_lower( std::string
const& str )
481 for(
auto& c : res ){
484 c =
static_cast<char>( std::tolower(
static_cast<unsigned char>( c )));
494 for(
auto& c : str ){
495 c =
static_cast<char>( std::tolower(
static_cast<unsigned char>( c )));
502 inline std::string
to_upper( std::string
const& str )
505 for(
auto& c : res ){
506 c =
static_cast<char>( std::toupper(
static_cast<unsigned char>( c )));
516 for(
auto& c : str ){
517 c =
static_cast<char>( std::toupper(
static_cast<unsigned char>( c )));
554 std::string
escape( std::string
const& text );
567 std::string
deescape( std::string
const& text );
586 std::string
repeat( std::string
const& word,
size_t times );
636 template <
typename T>
639 std::ostringstream s;
660 T
const x,
char const zero =
'0',
char const one =
'1',
bool const byte_space =
true
663 std::is_unsigned<T>::value,
664 "Can only use to_bit_string() with unsigned types."
667 std::string binary =
"";
669 for(
size_t i = 0; i <
sizeof(T) * 8; ++i ) {
670 if( byte_space && i > 0 && i % 8 == 0 ) {
671 binary =
' ' + binary;
674 binary = one + binary;
676 binary = zero + binary;
694 typename std::enable_if<
695 ! std::is_same<typename C::value_type, unsigned char>::value &&
696 ! std::is_same<typename C::value_type, signed char>::value
699 std::ostream&
join( std::ostream& stream, C
const& container, std::string
const& delimiter =
", " )
701 for(
auto const& element : container ) {
702 if( &element != &(*container.begin()) ) {
719 typename std::enable_if<
720 std::is_same<typename C::value_type, unsigned char>::value ||
721 std::is_same<typename C::value_type, signed char>::value
724 std::ostream&
join( std::ostream& stream, C
const& container, std::string
const& delimiter =
", " )
726 for(
auto const& element : container ) {
727 if( &element != &(*container.begin()) ) {
730 stream << static_cast<int>( element );
739 template <
typename C>
740 std::string
join( C
const& container, std::string
const& delimiter =
", " )
742 std::ostringstream s;
743 join( s, container, delimiter );
750 #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 (or any other delimiters).
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_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.
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 (or any other delimiters).
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 comp...
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...
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.
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...
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.
bool operator()(T const &lhs, T const &rhs) const
std::string remove_all_chars(std::string const &text, std::string const &search_chars)
Remove all occurrences of the search_chars in text.
std::vector< std::string > split(std::string const &str, char delimiter, const bool trim_empty)
Spilt a string into parts, given a delimiter char.
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-al...
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.
int strncasecmp(char const *s1, char const *s2, size_t n)
Compares up to n chars of two strings, ignoring case differences.
int strcasecmp(char const *s1, char const *s2)
Compares two strings, ignoring case differences.
bool equals_ci_alnum(std::string const &lhs, std::string const &rhs)
Compare two strings, case insensitive, and ignoring all non-alphanumerical characters.
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.
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.
std::string remove_all_chars_pred(std::string const &text, UnaryPredicate predicate)
Remove all occurrences characters for which predicate is true in text.
Functor class to compare to strings with natural "human" sorting, see compare_natural().
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 starts_with_ci_alnum(std::string const &text, std::string const &prefix)
Return whether a string starts with another string (prefix), comparing case-independent,...
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.
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,...
std::string to_string_byte_format(size_t value)
Produce a human readable formatting of a size in bytes, using the appropriate suffix.
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.
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 (or any other delimiters).
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::string remove_all_non_alnum(std::string const &text)
Remove all non-alphanumerical characters from 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.
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.
std::string escape(std::string const &text)
Return a string where special chars are replaces by their escape sequence.