diff options
Diffstat (limited to 'src/libutil/util.hh')
-rw-r--r-- | src/libutil/util.hh | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/src/libutil/util.hh b/src/libutil/util.hh index 9c2385e84..ac4aa1d3a 100644 --- a/src/libutil/util.hh +++ b/src/libutil/util.hh @@ -604,7 +604,33 @@ std::string replaceStrings( std::string_view to); -std::string rewriteStrings(std::string s, const StringMap & rewrites); +/** + * Rewrites a string given a map of replacements, applying the replacements in + * sorted order, only once, considering only the strings appearing in the input + * string in performing replacement. + * + * - Replacements are not performed on intermediate strings. That is, for an input + * `"abb"` with replacements `{"ab" -> "ba"}`, the result is `"bab"`. + * - Transitive replacements are not performed. For example, for the input `"abcde"` + * with replacements `{"a" -> "b", "b" -> "c", "e" -> "b"}`, the result is + * `"bccdb"`. + */ +class Rewriter +{ +private: + std::string initials; + std::map<std::string, std::string> rewrites; + +public: + explicit Rewriter(std::map<std::string, std::string> rewrites); + + std::string operator()(std::string s); +}; + +inline std::string rewriteStrings(std::string s, const StringMap & rewrites) +{ + return Rewriter(rewrites)(s); +} /** |