From a26f3d7e1a5a8ef3ce901e9e47412bc764f05fbc Mon Sep 17 00:00:00 2001 From: Martchus Date: Sat, 19 Sep 2020 15:40:32 +0200 Subject: [PATCH] Avoid instantiating an std::string for find and replace parameters This overloads should allow passing an std::string_view or C-string to findAndReplace() without instantiating an std::string. --- CMakeLists.txt | 4 ++-- conversion/stringconversion.h | 34 ++++++++++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9ac3f9a..6b059e0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -113,8 +113,8 @@ set(META_APP_URL "https://github.com/${META_APP_AUTHOR}/${META_PROJECT_NAME}") set(META_APP_DESCRIPTION "Useful C++ classes and routines such as argument parser, IO and conversion utilities") set(META_FEATURES_FOR_COMPILER_DETECTION_HEADER cxx_thread_local) set(META_VERSION_MAJOR 5) -set(META_VERSION_MINOR 6) -set(META_VERSION_PATCH 1) +set(META_VERSION_MINOR 7) +set(META_VERSION_PATCH 0) # find required 3rd party libraries include(3rdParty) diff --git a/conversion/stringconversion.h b/conversion/stringconversion.h index f059a5f..d6357b0 100644 --- a/conversion/stringconversion.h +++ b/conversion/stringconversion.h @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -312,13 +313,42 @@ bool containsSubstrings(const StringType &str, std::initializer_list void findAndReplace(StringType &str, const StringType &find, const StringType &replace) +template +void findAndReplace(StringType1 &str, const StringType2 &find, const StringType3 &replace) { - for (typename StringType::size_type i = 0; (i = str.find(find, i)) != StringType::npos; i += replace.size()) { + for (typename StringType1::size_type i = 0; (i = str.find(find, i)) != StringType1::npos; i += replace.size()) { str.replace(i, find.size(), replace); } } +/*! + * \brief Replaces all occurences of \a find with \a relpace in the specified \a str. + */ +template +inline void findAndReplace(StringType &str, const typename StringType::value_type *find, const typename StringType::value_type *replace) +{ + findAndReplace( + str, std::basic_string_view(find), std::basic_string_view(replace)); +} + +/*! + * \brief Replaces all occurences of \a find with \a relpace in the specified \a str. + */ +template +inline void findAndReplace(StringType1 &str, const StringType2 &find, const typename StringType1::value_type *replace) +{ + findAndReplace(str, find, std::basic_string_view(replace)); +} + +/*! + * \brief Replaces all occurences of \a find with \a relpace in the specified \a str. + */ +template +inline void findAndReplace(StringType1 &str, const typename StringType1::value_type *find, const StringType2 &replace) +{ + findAndReplace(str, std::basic_string_view(find), replace); +} + /*! * \brief Returns the character representation of the specified \a digit. * \remarks