diff --git a/doc/src/Developer_utils.rst b/doc/src/Developer_utils.rst index b92d817d87..9c2c6f1d0c 100644 --- a/doc/src/Developer_utils.rst +++ b/doc/src/Developer_utils.rst @@ -133,6 +133,9 @@ and parsing files or arguments. .. doxygenfunction:: trim_comment :project: progguide +.. doxygenfunction:: strip_style_suffix + :project: progguide + .. doxygenfunction:: star_subst :project: progguide diff --git a/src/utils.cpp b/src/utils.cpp index ab9791c715..30a47f3263 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -866,6 +866,30 @@ std::string utils::star_subst(const std::string &name, bigint step, int pad) return fmt::format("{}{:0{}}{}", name.substr(0, star), step, pad, name.substr(star + 1)); } + +/* ---------------------------------------------------------------------- + Remove accelerator style suffix from string +------------------------------------------------------------------------- */ +std::string utils::strip_style_suffix(const std::string &style, LAMMPS *lmp) +{ + std::string newstyle = style; + if (lmp->suffix_enable) { + if (lmp->suffix) { + if (utils::strmatch(style, fmt::format("/{}$", lmp->suffix))) { + newstyle.resize(style.size() - strlen(lmp->suffix) - 1); + return newstyle; + } + } + if (lmp->suffix2) { + if (utils::strmatch(style, fmt::format("/{}$", lmp->suffix2))) { + newstyle.resize(style.size() - strlen(lmp->suffix2) - 1); + return newstyle; + } + } + } + return newstyle; +} + /* ---------------------------------------------------------------------- Replace UTF-8 encoded chars with known ASCII equivalents ------------------------------------------------------------------------- */ diff --git a/src/utils.h b/src/utils.h index 52ada7c8cc..048de17998 100644 --- a/src/utils.h +++ b/src/utils.h @@ -430,7 +430,23 @@ namespace utils { std::string star_subst(const std::string &name, bigint step, int pad); - /*! Check if a string will likely have UTF-8 encoded characters + /*! Remove style suffix from string if suffix flag is active + * + * +\verbatim embed:rst + +This will try to undo the effect from using the :doc:`suffix command ` +or the *-suffix/-sf* command line flag and return correspondingly modified string. + +\endverbatim + * + * \param style string of style name + * \param lmp pointer to the LAMMPS class (has suffix_flag and suffix strings) + * \return processed string */ + + std::string strip_style_suffix(const std::string &style, LAMMPS *lmp); + +/*! 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 * all other characters are encoded as multiple bytes. For the multi-byte diff --git a/unittest/commands/test_simple_commands.cpp b/unittest/commands/test_simple_commands.cpp index ade8f85275..db6ee3c1e7 100644 --- a/unittest/commands/test_simple_commands.cpp +++ b/unittest/commands/test_simple_commands.cpp @@ -20,6 +20,7 @@ #include "input.h" #include "output.h" #include "update.h" +#include "utils.h" #include "variable.h" #include "../testing/core.h" @@ -265,14 +266,22 @@ TEST_F(SimpleCommandsTest, Suffix) BEGIN_HIDE_OUTPUT(); command("suffix one"); + command("suffix yes"); END_HIDE_OUTPUT(); ASSERT_THAT(lmp->suffix, StrEq("one")); + ASSERT_EQ(lmp->suffix_enable, 1); + ASSERT_THAT(utils::strip_style_suffix("one/four", lmp), StrEq("one/four")); + ASSERT_THAT(utils::strip_style_suffix("four/one", lmp), StrEq("four")); BEGIN_HIDE_OUTPUT(); command("suffix hybrid two three"); END_HIDE_OUTPUT(); ASSERT_THAT(lmp->suffix, StrEq("two")); ASSERT_THAT(lmp->suffix2, StrEq("three")); + ASSERT_THAT(utils::strip_style_suffix("one/four", lmp), StrEq("one/four")); + ASSERT_THAT(utils::strip_style_suffix("one/two", lmp), StrEq("one")); + ASSERT_THAT(utils::strip_style_suffix("one/three", lmp), StrEq("one")); + ASSERT_THAT(utils::strip_style_suffix("four/one", lmp), StrEq("four/one")); BEGIN_HIDE_OUTPUT(); command("suffix four"); @@ -284,11 +293,13 @@ TEST_F(SimpleCommandsTest, Suffix) command("suffix off"); END_HIDE_OUTPUT(); ASSERT_EQ(lmp->suffix_enable, 0); + ASSERT_THAT(utils::strip_style_suffix("one/four", lmp), StrEq("one/four")); BEGIN_HIDE_OUTPUT(); command("suffix yes"); END_HIDE_OUTPUT(); ASSERT_EQ(lmp->suffix_enable, 1); + ASSERT_THAT(utils::strip_style_suffix("one/four", lmp), StrEq("one")); BEGIN_HIDE_OUTPUT(); command("suffix no");