String utilities.
More...
|
auto | format (std::string text, const subst ¶ms={}) -> std::string |
|
auto | strip (std::string str) noexcept -> std::string |
|
auto | split (std::string_view list, const std::string &delimiters, int max=-1) -> std::vector< std::string > |
|
template<typename InputIt , typename DelimType = char> |
auto | join (InputIt first, InputIt last, DelimType delim=':') -> std::string |
|
template<typename Container , typename DelimType = char> |
auto | join (const Container &c, DelimType delim=':') -> std::string |
|
template<typename T , typename DelimType = char> |
auto | join (std::initializer_list< T > list, DelimType delim=':') -> std::string |
|
auto | is_identifier (std::string_view name) noexcept -> bool |
|
auto | is_boolean (std::string value) noexcept -> bool |
|
template<typename T = int> |
auto | to_int (const std::string &str, T min=std::numeric_limits< T >::min(), T max=std::numeric_limits< T >::max()) noexcept -> std::optional< T > |
|
template<typename T = unsigned> |
auto | to_uint (const std::string &str, T min=std::numeric_limits< T >::min(), T max=std::numeric_limits< T >::max()) noexcept -> std::optional< T > |
|
◆ subst_flags
Disable or enable some features.
Enumerator |
---|
date | date templates
|
keywords | keywords
|
env | environment variables
|
shell | command line command
|
irc_attrs | IRC escape codes.
|
shell_attrs | shell attributes
|
◆ format()
auto irccd::string_util::format |
( |
std::string |
text, |
|
|
const subst & |
params = {} |
|
) |
| -> std::string |
Format a string and update all templates.
Syntax
The syntax is ?{} where ? is replaced by one of the token defined below. Braces are mandatory and cannot be ommited.
To write a literal template construct, prepend the token twice.
Availables templates
The following templates are available:
- #{name}: name will be substituted from the keywords in params,
- ${name}: name will be substituted from the environment variable,
- @{attributes}: the attributes will be substituted to IRC or shell colors (see below),
- %, any format accepted by strftime(3).
Attributes
The attribute format is composed of three parts, foreground, background and modifiers, each separated by a comma.
Note: you cannot omit parameters, to specify the background, you must specify the foreground.
Examples
Valid constructs
- #{target}, welcome: if target is set to "irccd", becomes "irccd, welcome",
- @{red}#{target}: if target is specified, it is written in red,
Invalid or literals constructs
- ##{target}: will output "\#{target}",
- ##: will output "\#\#",
- #target: will output "\#target",
- #{target: will throw std::invalid_argument.
Colors & attributes
- @{red,blue}: will write text red on blue background,
- @{default,yellow}: will write default color text on yellow background,
- @{white,black,bold,underline}: will write white text on black in both bold and underline.
- Parameters
-
text | the text to format |
params | the additional options |
- Returns
- the modified text
◆ is_boolean()
auto irccd::string_util::is_boolean |
( |
std::string |
value | ) |
-> bool |
|
noexcept |
Check if the value is a boolean, 1, yes and true are accepted.
- Parameters
-
- Returns
- true if is boolean
- Note
- this function is case-insensitive
◆ is_identifier()
auto irccd::string_util::is_identifier |
( |
std::string_view |
name | ) |
-> bool |
|
noexcept |
Check if a string is a valid irccd identifier.
- Parameters
-
- Returns
- true if is valid
◆ join() [1/3]
template<typename Container , typename DelimType = char>
auto irccd::string_util::join |
( |
const Container & |
c, |
|
|
DelimType |
delim = ':' |
|
) |
| -> std::string
|
Overloaded function that takes a container.
- Parameters
-
c | the container |
delim | the optional delimiter |
- Returns
- the string
◆ join() [2/3]
template<typename InputIt , typename DelimType = char>
auto irccd::string_util::join |
( |
InputIt |
first, |
|
|
InputIt |
last, |
|
|
DelimType |
delim = ':' |
|
) |
| -> std::string
|
Join values by a separator and return a string.
- Parameters
-
first | the first iterator |
last | the last iterator |
delim | the optional delimiter |
- Returns
- the string
◆ join() [3/3]
template<typename T , typename DelimType = char>
auto irccd::string_util::join |
( |
std::initializer_list< T > |
list, |
|
|
DelimType |
delim = ':' |
|
) |
| -> std::string
|
Convenient overload.
- Parameters
-
list | the initializer list |
delim | the delimiter |
- Returns
- the string
◆ split()
auto irccd::string_util::split |
( |
std::string_view |
list, |
|
|
const std::string & |
delimiters, |
|
|
int |
max = -1 |
|
) |
| -> std::vector< std::string > |
Split a string by delimiters.
- Parameters
-
list | the string to split |
delimiters | a list of delimiters |
max | max number of split |
- Returns
- a list of string splitted
◆ strip()
auto irccd::string_util::strip |
( |
std::string |
str | ) |
-> std::string |
|
noexcept |
Remove leading and trailing spaces.
- Parameters
-
- Returns
- the removed white spaces
◆ to_int()
template<typename T = int>
auto irccd::string_util::to_int |
( |
const std::string & |
str, |
|
|
T |
min = std::numeric_limits<T>::min() , |
|
|
T |
max = std::numeric_limits<T>::max() |
|
) |
| -> std::optional<T>
|
|
noexcept |
Convert the given string into a signed integer.
- Parameters
-
str | the string to convert |
min | the minimum value allowed |
max | the maximum value allowed |
- Returns
- the value or boost::none if not convertible
◆ to_uint()
template<typename T = unsigned>
auto irccd::string_util::to_uint |
( |
const std::string & |
str, |
|
|
T |
min = std::numeric_limits<T>::min() , |
|
|
T |
max = std::numeric_limits<T>::max() |
|
) |
| -> std::optional<T>
|
|
noexcept |
Convert the given string into a unsigned integer.
- Note
- invalid numbers are valid as well
- Parameters
-
str | the string to convert |
min | the minimum value allowed |
max | the maximum value allowed |
- Returns
- the value or boost::none if not convertible