add star_subst() utility function that replaces a '*' in a string with a number
This commit is contained in:
@ -133,6 +133,9 @@ and parsing files or arguments.
|
|||||||
.. doxygenfunction:: trim_comment
|
.. doxygenfunction:: trim_comment
|
||||||
:project: progguide
|
:project: progguide
|
||||||
|
|
||||||
|
.. doxygenfunction:: star_subst
|
||||||
|
:project: progguide
|
||||||
|
|
||||||
.. doxygenfunction:: has_utf8
|
.. doxygenfunction:: has_utf8
|
||||||
:project: progguide
|
:project: progguide
|
||||||
|
|
||||||
|
|||||||
@ -791,11 +791,23 @@ std::string utils::trim(const std::string &line)
|
|||||||
|
|
||||||
std::string utils::trim_comment(const std::string &line)
|
std::string utils::trim_comment(const std::string &line)
|
||||||
{
|
{
|
||||||
auto end = line.find_first_of('#');
|
auto end = line.find('#');
|
||||||
if (end != std::string::npos) { return line.substr(0, end); }
|
if (end != std::string::npos) { return line.substr(0, end); }
|
||||||
return {line};
|
return {line};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ----------------------------------------------------------------------
|
||||||
|
Replace '*' with number and optional zero-padding
|
||||||
|
------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
std::string utils::star_subst(const std::string &name, bigint step, int pad)
|
||||||
|
{
|
||||||
|
auto star = name.find('*');
|
||||||
|
if (star == std::string::npos) return name;
|
||||||
|
|
||||||
|
return fmt::format("{}{:0{}}{}",name.substr(0,star),step,pad,name.substr(star+1));
|
||||||
|
}
|
||||||
|
|
||||||
/* ----------------------------------------------------------------------
|
/* ----------------------------------------------------------------------
|
||||||
Replace UTF-8 encoded chars with known ASCII equivalents
|
Replace UTF-8 encoded chars with known ASCII equivalents
|
||||||
------------------------------------------------------------------------- */
|
------------------------------------------------------------------------- */
|
||||||
|
|||||||
16
src/utils.h
16
src/utils.h
@ -368,13 +368,27 @@ namespace utils {
|
|||||||
|
|
||||||
std::string trim(const std::string &line);
|
std::string trim(const std::string &line);
|
||||||
|
|
||||||
/*! Return string with anything from '#' onward removed
|
/*! Return string with anything from the first '#' character onward removed
|
||||||
*
|
*
|
||||||
* \param line string that should be trimmed
|
* \param line string that should be trimmed
|
||||||
* \return new string without comment (string) */
|
* \return new string without comment (string) */
|
||||||
|
|
||||||
std::string trim_comment(const std::string &line);
|
std::string trim_comment(const std::string &line);
|
||||||
|
|
||||||
|
/*! Replace first '*' character in a string with a number, optionally zero-padded
|
||||||
|
*
|
||||||
|
* If there is no '*' character in the string, return the original string.
|
||||||
|
* If the number requires more characters than the value of the *pad*
|
||||||
|
* argument, do not add zeros; otherwise add as many zeroes as needed to
|
||||||
|
* the left to make the the number representation *pad* characters wide.
|
||||||
|
*
|
||||||
|
* \param name string with file containing a '*' (or not)
|
||||||
|
* \param step step number to replace the (first) '*'
|
||||||
|
* \param pad zero-padding (may be zero)
|
||||||
|
* \return processed string */
|
||||||
|
|
||||||
|
std::string star_subst(const std::string &name, bigint step, int pad);
|
||||||
|
|
||||||
/*! Check if a string will likely have UTF-8 encoded characters
|
/*! Check if a string will likely have UTF-8 encoded characters
|
||||||
*
|
*
|
||||||
* UTF-8 uses the 7-bit standard ASCII table for the first 127 characters and
|
* UTF-8 uses the 7-bit standard ASCII table for the first 127 characters and
|
||||||
|
|||||||
@ -78,6 +78,25 @@ TEST(Utils, trim_comment)
|
|||||||
ASSERT_THAT(trimmed, StrEq("some text "));
|
ASSERT_THAT(trimmed, StrEq("some text "));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(Utils, star_subst)
|
||||||
|
{
|
||||||
|
std::string starred = "beforeafter";
|
||||||
|
std::string subst = utils::star_subst(starred, 1234, 0);
|
||||||
|
ASSERT_THAT(subst, StrEq("beforeafter"));
|
||||||
|
|
||||||
|
starred = "before*after";
|
||||||
|
subst = utils::star_subst(starred, 1234, 6);
|
||||||
|
ASSERT_THAT(subst, StrEq("before001234after"));
|
||||||
|
|
||||||
|
starred = "before*";
|
||||||
|
subst = utils::star_subst(starred, 1234, 0);
|
||||||
|
ASSERT_THAT(subst, StrEq("before1234"));
|
||||||
|
|
||||||
|
starred = "*after";
|
||||||
|
subst = utils::star_subst(starred, 1234, 2);
|
||||||
|
ASSERT_THAT(subst, StrEq("1234after"));
|
||||||
|
}
|
||||||
|
|
||||||
TEST(Utils, has_utf8)
|
TEST(Utils, has_utf8)
|
||||||
{
|
{
|
||||||
const char ascii_string[] = " -2";
|
const char ascii_string[] = " -2";
|
||||||
|
|||||||
Reference in New Issue
Block a user