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.
This commit is contained in:
Martchus 2020-09-19 15:40:32 +02:00
parent 26b6ef0486
commit a26f3d7e1a
2 changed files with 34 additions and 4 deletions

View File

@ -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)

View File

@ -14,6 +14,7 @@
#include <memory>
#include <sstream>
#include <string>
#include <string_view>
#include <system_error>
#include <vector>
@ -312,13 +313,42 @@ bool containsSubstrings(const StringType &str, std::initializer_list<const typen
/*!
* \brief Replaces all occurences of \a find with \a relpace in the specified \a str.
*/
template <typename StringType> void findAndReplace(StringType &str, const StringType &find, const StringType &replace)
template <typename StringType1, typename StringType2, typename StringType3>
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 <typename StringType>
inline void findAndReplace(StringType &str, const typename StringType::value_type *find, const typename StringType::value_type *replace)
{
findAndReplace(
str, std::basic_string_view<typename StringType::value_type>(find), std::basic_string_view<typename StringType::value_type>(replace));
}
/*!
* \brief Replaces all occurences of \a find with \a relpace in the specified \a str.
*/
template <typename StringType1, typename StringType2>
inline void findAndReplace(StringType1 &str, const StringType2 &find, const typename StringType1::value_type *replace)
{
findAndReplace(str, find, std::basic_string_view<typename StringType1::value_type>(replace));
}
/*!
* \brief Replaces all occurences of \a find with \a relpace in the specified \a str.
*/
template <typename StringType1, typename StringType2>
inline void findAndReplace(StringType1 &str, const typename StringType1::value_type *find, const StringType2 &replace)
{
findAndReplace(str, std::basic_string_view<typename StringType1::value_type>(find), replace);
}
/*!
* \brief Returns the character representation of the specified \a digit.
* \remarks