String Utils: formatting and documentation cleanup

This commit is contained in:
Charles Dang 2016-10-30 19:58:01 +11:00
parent e3c249fd88
commit 6e021ba746
2 changed files with 130 additions and 104 deletions

View file

@ -91,9 +91,9 @@ std::vector<std::string> split(const std::string& val, const char c, const int f
return res;
}
std::vector< std::string > square_parenthetical_split(std::string const &val,
const char separator, std::string const &left,
std::string const &right,const int flags)
std::vector<std::string> square_parenthetical_split(const std::string& val,
const char separator, const std::string& left,
const std::string& right,const int flags)
{
std::vector< std::string > res;
std::vector<char> part;
@ -260,12 +260,12 @@ std::vector< std::string > square_parenthetical_split(std::string const &val,
return res;
}
std::map< std::string, std::string > map_split(
std::string const &val
std::map<std::string, std::string> map_split(
const std::string& val
, char major
, char minor
, int flags
, std::string const& default_value)
, const std::string& default_value)
{
//first split by major so that we get a vector with the key-value pairs
std::vector< std::string > v = split(val, major, flags);
@ -291,9 +291,9 @@ std::map< std::string, std::string > map_split(
return res;
}
std::vector< std::string > parenthetical_split(std::string const &val,
const char separator, std::string const &left,
std::string const &right,const int flags)
std::vector<std::string> parenthetical_split(const std::string& val,
const char separator, const std::string& left,
const std::string& right,const int flags)
{
std::vector< std::string > res;
std::vector<char> part;
@ -731,7 +731,7 @@ std::string indent(const std::string& string, size_t indent_size)
return res;
}
std::vector< std::string > quoted_split(std::string const &val, char c, int flags, char quote)
std::vector<std::string> quoted_split(const std::string& val, char c, int flags, char quote)
{
std::vector<std::string> res;
@ -770,7 +770,7 @@ std::vector< std::string > quoted_split(std::string const &val, char c, int flag
return res;
}
std::pair< int, int > parse_range(std::string const &str)
std::pair<int, int> parse_range(std::string const &str)
{
const std::string::const_iterator dash = std::find(str.begin(), str.end(), '-');
const std::string a(str.begin(), dash);
@ -786,7 +786,7 @@ std::pair< int, int > parse_range(std::string const &str)
return res;
}
std::vector< std::pair< int, int > > parse_ranges(std::string const &str)
std::vector<std::pair<int, int>> parse_ranges(std::string const &str)
{
std::vector< std::pair< int, int > > to_return;
std::vector<std::string> strs = utils::split(str);

View file

@ -29,91 +29,121 @@ class t_string;
namespace utils {
using string_map = std::map<std::string, t_string>;
bool isnewline(const char c);
bool portable_isspace(const char c);
bool notspace(char c);
enum { REMOVE_EMPTY = 0x01, /**< REMOVE_EMPTY : remove empty elements. */
STRIP_SPACES = 0x02 /**< STRIP_SPACES : strips leading and trailing blank spaces. */
enum {
REMOVE_EMPTY = 0x01, /** REMOVE_EMPTY: remove empty elements. */
STRIP_SPACES = 0x02 /** STRIP_SPACES: strips leading and trailing blank spaces. */
};
/// Splits a (comma-)separated string into a vector of pieces.
/** Splits a (comma-)separated string into a vector of pieces. */
std::vector<std::string> split(const std::string& val, const char c = ',', const int flags = REMOVE_EMPTY | STRIP_SPACES);
/// Splits a (comma-)separated string into a set of pieces.
/// See split() for the meanings of the parameters.
inline std::set< std::string > set_split(std::string const &val, const char c = ',', const int flags = REMOVE_EMPTY | STRIP_SPACES)
/**
* This function is identical to split(), except it does not split when it otherwise would if the
* previous character was identical to the parameter 'quote' (i.e. it does not split quoted commas).
* This method was added to make it possible to quote user input, particularly so commas in user input
* would not cause visual problems in menus.
*
* @todo Why not change split()? That would change the methods post condition.
*/
std::vector<std::string> quoted_split(const std::string& val, char c= ',', int flags = REMOVE_EMPTY | STRIP_SPACES, char quote = '\\');
/**
* Splits a (comma-)separated string into a set of pieces.
* See split() for the meanings of the parameters.
*/
inline std::set<std::string> set_split(std::string const &val, const char c = ',', const int flags = REMOVE_EMPTY | STRIP_SPACES)
{
std::vector< std::string > vec_split = split(val, c, flags);
std::vector<std::string> vec_split = split(val, c, flags);
return std::set< std::string >(vec_split.begin(), vec_split.end());
}
/**
* Splits a string based on two separators into a map.
* major: the separator between elements of the map
* minor: the separator between keys and values in one element
*
* Major: the separator between elements of the map
* Minor: the separator between keys and values in one element
*
* For example, the string 'a:b,c:d,e:f' would be parsed into:
* a => b
* c => d
* e => f
*/
std::map< std::string, std::string > map_split(
std::string const &val
, char major = ','
, char minor = ':'
, int flags = REMOVE_EMPTY | STRIP_SPACES
, std::string const& default_value = "");
*/
std::map<std::string, std::string> map_split(
const std::string& val,
char major = ',',
char minor = ':',
int flags = REMOVE_EMPTY | STRIP_SPACES,
const std::string& default_value = "");
/**
* Splits a string based either on a separator where text within parenthesis
* is protected from splitting (Note that one can use the same character for
* both the left and right parenthesis. In this mode it usually makes only
* sense to have one character for the left and right parenthesis.)
* or if the separator == 0 it splits a string into an odd number of parts:
* Splits a string based either on a separator, except then the text appears within specified parenthesis.
*
* If the separator is "0" (default), it splits a string into an odd number of parts:
* - The part before the first '(',
* - the part between the first '('
* - and the matching right ')', etc ...
* and the remainder of the string.
* Note that this will find the first matching char in the left string
* and match against the corresponding char in the right string.
* In this mode, a correctly processed string should return with
* an odd number of elements to the vector and
* an empty elements are never removed as they are placeholders.
* hence REMOVE EMPTY only works for the separator split.
*
* parenthetical_split("a(b)c{d}e(f{g})h",0,"({",")}") should return
* a vector of <"a","b","c","d","e","f{g}","h">
* Note that one can use the same character for both the left and right parenthesis, which usually makes
* the most sense for this function.
*
* Note that this will find the first matching char in the left string and match against the corresponding
* char in the right string. A correctly processed string should return a vector with an odd number of
* elements. Empty elements are never removed as they are placeholders, hence REMOVE EMPTY only works for
* the separator split.
*
* INPUT: ("a(b)c{d}e(f{g})h", 0, "({", ")}")
* RETURNS: {"a", "b", "c", "d", "e", "f{g}", "h"}
*/
std::vector< std::string > parenthetical_split(std::string const &val,
const char separator = 0 , std::string const &left="(",
std::string const &right=")",const int flags = REMOVE_EMPTY | STRIP_SPACES);
std::vector< std::string > parenthetical_split(
const std::string& val,
const char separator = 0,
const std::string& left = "(",
const std::string& right = ")",
const int flags = REMOVE_EMPTY | STRIP_SPACES);
/**
* Similar to parenthetical_split, but also expands embedded square brackets.
* Separator must be specified and number of entries in each square bracket
* must match in each section.
* Leading zeros are preserved if specified between square brackets.
* An asterisk as in [a*n] indicates to expand 'a' n times
*
* This is useful to expand animation WML code.
* Notes:
* - The Separator must be specified and number of entries in each square bracket must match in each section.
* - Leading zeros are preserved if specified between square brackets.
* - An asterisk as in [a*n] indicates to expand 'a' n times
*
* This is useful for expanding animation WML code.
*
* Examples:
* square_parenthetical_split("a[1-3](1,[5,6,7]),b[8,9]",",") should return
* <"a1(1,5)","a2(1,6)","a3(1,7)","b8","b9">
* square_parenthetical_split("abc[07-10]") should return
* <"abc07","abc08","abc09","abc10">
* square_parenthetical_split("a[1,2]b[3-4]:c[5,6]") should return
* <"a1b3:c5","a2b4:c6">
* square_parenthetical_split("abc[3,1].png") should return
* <"abc3.png","abc2.png","abc1.png">
* square_parenthetical_split("abc[de,xyz]") should return
* <"abcde","abcxyz">
* square_parenthetical_split("abc[1*3]") should return
* <"abc1","abc1","abc1">
*
* INPUT: ("a[1-3](1,[5,6,7]),b[8,9]", ",")
* RETURNS: {"a1(1,5)", "a2(1,6)", "a3(1,7)", "b8", "b9"}
*
* INPUT: ("abc[07-10]")
* RETURNS: {"abc07", "abc08", "abc09", "abc10"}
*
* INPUT: ("a[1,2]b[3-4]:c[5,6]")
* RETURNS: {"a1b3:c5", "a2b4:c6"}
*
* INPUT: ("abc[3,1].png")
* RETURNS: {"abc3.png", "abc2.png", "abc1.png"}
*
* INPUT: ("abc[de,xyz]")
* RETURNS: {"abcde", "abcxyz"}
*
* INPUT: ("abc[1*3]")
* RETURNS: {"abc1", "abc1", "abc1"}
*/
std::vector< std::string > square_parenthetical_split(std::string const &val,
const char separator = ',' , std::string const &left="([",
std::string const &right=")]",const int flags = REMOVE_EMPTY | STRIP_SPACES);
std::vector<std::string> square_parenthetical_split(
const std::string& val,
const char separator = ',',
const std::string& left = "([",
const std::string& right = ")]",
const int flags = REMOVE_EMPTY | STRIP_SPACES);
/**
* Generates a new string joining container items in a list.
@ -122,32 +152,36 @@ std::vector< std::string > square_parenthetical_split(std::string const &val,
* @param s List delimiter.
*/
template <typename T>
std::string join(T const &v, const std::string& s = ",")
std::string join(const T& v, const std::string& s = ",")
{
std::stringstream str;
for(typename T::const_iterator i = v.begin(); i != v.end(); ++i) {
str << *i;
if (std::next(i) != v.end())
str << s;
}
std::stringstream str;
return str.str();
for(typename T::const_iterator i = v.begin(); i != v.end(); ++i) {
str << *i;
if(std::next(i) != v.end()) {
str << s;
}
}
return str.str();
}
template <typename T>
std::string join_map(
const T& v
, const std::string& major = ","
, const std::string& minor = ":")
const T& v,
const std::string& major = ",",
const std::string& minor = ":")
{
std::stringstream str;
for(typename T::const_iterator i = v.begin(); i != v.end(); ++i) {
str << i->first << minor << i->second;
if (std::next(i) != v.end())
str << major;
}
std::stringstream str;
return str.str();
for(typename T::const_iterator i = v.begin(); i != v.end(); ++i) {
str << i->first << minor << i->second;
if(std::next(i) != v.end()) {
str << major;
}
}
return str.str();
}
/**
@ -188,25 +222,17 @@ std::string bullet_list(const T& v, size_t indent = 4, const std::string& bullet
*/
std::string indent(const std::string& string, size_t indent_size = 4);
/**
* This function is identical to split(), except it does not split
* when it otherwise would if the previous character was identical to the parameter 'quote'.
* i.e. it does not split quoted commas.
* This method was added to make it possible to quote user input,
* particularly so commas in user input will not cause visual problems in menus.
*
* @todo Why not change split()? That would change the methods post condition.
*/
std::vector< std::string > quoted_split(std::string const &val, char c= ',',
int flags = REMOVE_EMPTY | STRIP_SPACES, char quote = '\\');
std::pair< int, int > parse_range(std::string const &str);
std::vector< std::pair< int, int > > parse_ranges(std::string const &str);
int apply_modifier( const int number, const std::string &amount, const int minimum = 0);
std::pair<int, int> parse_range(std::string const &str);
/* add a "+" or replace the "-" par Unicode minus */
std::vector<std::pair<int, int>> parse_ranges(std::string const &str);
int apply_modifier(const int number, const std::string &amount, const int minimum = 0);
/** Add a "+" or replace the "-" par Unicode minus */
inline std::string print_modifier(const std::string &mod)
{ return mod[0] == '-' ?
(font::unicode_minus + std::string(mod.begin()+1, mod.end())) : ("+" + mod);}
{
return mod[0] == '-' ? (font::unicode_minus + std::string(mod.begin() + 1, mod.end())) : ("+" + mod);
}
/** Prepends a configurable set of characters with a backslash */
std::string escape(const std::string &str, const char *special_chars);
@ -218,7 +244,9 @@ std::string escape(const std::string &str, const char *special_chars);
* #@{}+-,\*=
*/
inline std::string escape(const std::string &str)
{ return escape(str, "#@{}+-,\\*="); }
{
return escape(str, "#@{}+-,\\*=");
}
/** Remove all escape characters (backslash) */
std::string unescape(const std::string &str);
@ -285,7 +313,7 @@ bool wildcard_string_match(const std::string& str, const std::string& match);
*
* (all alpha-numeric characters plus underscore and hyphen)
*/
bool isvalid_username(const std::string &login);
bool isvalid_username(const std::string& login);
/**
* Check if the username pattern contains only valid characters.
@ -293,9 +321,7 @@ bool isvalid_username(const std::string &login);
* (all alpha-numeric characters plus underscore, hyphen,
* question mark and asterisk)
*/
bool isvalid_wildcard(const std::string &login);
typedef std::map< std::string, t_string > string_map;
bool isvalid_wildcard(const std::string& login);
/**
* Truncates a string to a given utf-8 character count and then appends an ellipsis.