irccd  3.0.3
Classes | Enumerations | Functions
irccd::string_util Namespace Reference

String utilities. More...

Classes

class  subst
 Used for format() function. More...
 

Enumerations

enum  subst_flags : unsigned {
  subst_flags::date = (1 << 0), subst_flags::keywords = (1 << 1), subst_flags::env = (1 << 2), subst_flags::shell = (1 << 3),
  subst_flags::irc_attrs = (1 << 4), subst_flags::shell_attrs = (1 << 5)
}
 Disable or enable some features. More...
 

Functions

auto format (std::string text, const subst &params={}) -> 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 >
 

Detailed Description

String utilities.

Enumeration Type Documentation

◆ subst_flags

enum irccd::string_util::subst_flags : unsigned
strong

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

Function Documentation

◆ 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
textthe text to format
paramsthe 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
valuethe value
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
namethe identifier name
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
cthe container
delimthe 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
firstthe first iterator
lastthe last iterator
delimthe 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
listthe initializer list
delimthe 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
listthe string to split
delimitersa list of delimiters
maxmax 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
strthe string
Returns
the removed white spaces

◆ to_int()

template<typename T = int>
auto irccd::string_util::to_int ( const std::string &  str,
min = std::numeric_limits<T>::min(),
max = std::numeric_limits<T>::max() 
) -> std::optional<T>
noexcept

Convert the given string into a signed integer.

Parameters
strthe string to convert
minthe minimum value allowed
maxthe 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,
min = std::numeric_limits<T>::min(),
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
strthe string to convert
minthe minimum value allowed
maxthe maximum value allowed
Returns
the value or boost::none if not convertible