Support string builder operator '%' for std::string_view

charconv
Martchus 3 years ago
parent 8e3aa84780
commit b8a60a0cd4
  1. 44
      conversion/stringbuilder.h
  2. 2
      tests/conversiontests.cpp

@ -22,6 +22,13 @@ template <class StringType, Traits::EnableIf<std::is_class<StringType>> * = null
return str.size();
}
template <class StringType, class ViewType,
Traits::EnableIf<std::is_same<ViewType, std::basic_string_view<typename StringType::value_type>>> * = nullptr>
inline std::size_t computeTupleElementSize(const ViewType *str)
{
return str->size();
}
template <class StringType, class ViewType,
Traits::EnableIf<std::is_same<ViewType, std::basic_string_view<typename StringType::value_type>>> * = nullptr>
inline std::size_t computeTupleElementSize(ViewType str)
@ -73,6 +80,13 @@ template <class StringType, Traits::EnableIf<std::is_class<StringType>> * = null
target.append(str);
}
template <class StringType, class ViewType,
Traits::EnableIf<std::is_same<ViewType, std::basic_string_view<typename StringType::value_type>>> * = nullptr>
inline void append(StringType &target, const ViewType *str)
{
target.append(*str);
}
template <class StringType, class ViewType,
Traits::EnableIf<std::is_same<ViewType, std::basic_string_view<typename StringType::value_type>>> * = nullptr>
inline void append(StringType &target, ViewType str)
@ -166,7 +180,10 @@ template <class StringType = std::string, class... Args> inline StringType argsT
/*!
* \brief Allows construction of string-tuples via %-operator, eg. string1 % "string2" % string3.
*/
template <class Tuple> constexpr auto operator%(const Tuple &lhs, const std::string &rhs) -> decltype(std::tuple_cat(lhs, std::make_tuple(&rhs)))
template <class Tuple, class StringType,
Traits::EnableIfAny<Traits::IsSpecializationOf<StringType, std::basic_string>, Traits::IsSpecializationOf<StringType, std::basic_string_view>>
* = nullptr>
constexpr auto operator%(const Tuple &lhs, const StringType &rhs) -> decltype(std::tuple_cat(lhs, std::make_tuple(&rhs)))
{
return std::tuple_cat(lhs, std::make_tuple(&rhs));
}
@ -191,7 +208,10 @@ constexpr auto operator%(const Tuple &lhs, IntegralType rhs) -> decltype(std::tu
/*!
* \brief Allows construction of string-tuples via %-operator, eg. string1 % "string2" % string3.
*/
constexpr auto operator%(const std::string &lhs, const std::string &rhs) -> decltype(std::make_tuple(&lhs, &rhs))
template <class StringType,
Traits::EnableIfAny<Traits::IsSpecializationOf<StringType, std::basic_string>, Traits::IsSpecializationOf<StringType, std::basic_string_view>>
* = nullptr>
constexpr auto operator%(const StringType &lhs, const StringType &rhs) -> decltype(std::make_tuple(&lhs, &rhs))
{
return std::make_tuple(&lhs, &rhs);
}
@ -199,7 +219,10 @@ constexpr auto operator%(const std::string &lhs, const std::string &rhs) -> decl
/*!
* \brief Allows construction of string-tuples via %-operator, eg. string1 % "string2" % string3.
*/
constexpr auto operator%(const char *lhs, const std::string &rhs) -> decltype(std::make_tuple(lhs, &rhs))
template <class StringType,
Traits::EnableIfAny<Traits::IsSpecializationOf<StringType, std::basic_string>, Traits::IsSpecializationOf<StringType, std::basic_string_view>>
* = nullptr>
constexpr auto operator%(const char *lhs, const StringType &rhs) -> decltype(std::make_tuple(lhs, &rhs))
{
return std::make_tuple(lhs, &rhs);
}
@ -207,7 +230,10 @@ constexpr auto operator%(const char *lhs, const std::string &rhs) -> decltype(st
/*!
* \brief Allows construction of string-tuples via %-operator, eg. string1 % "string2" % string3.
*/
constexpr auto operator%(const std::string &lhs, const char *rhs) -> decltype(std::make_tuple(&lhs, rhs))
template <class StringType,
Traits::EnableIfAny<Traits::IsSpecializationOf<StringType, std::basic_string>, Traits::IsSpecializationOf<StringType, std::basic_string_view>>
* = nullptr>
constexpr auto operator%(const StringType &lhs, const char *rhs) -> decltype(std::make_tuple(&lhs, rhs))
{
return std::make_tuple(&lhs, rhs);
}
@ -215,7 +241,10 @@ constexpr auto operator%(const std::string &lhs, const char *rhs) -> decltype(st
/*!
* \brief Allows construction of string-tuples via %-operator, eg. string1 % "string2" % string3.
*/
constexpr auto operator%(const std::string &lhs, char rhs) -> decltype(std::make_tuple(&lhs, rhs))
template <class StringType,
Traits::EnableIfAny<Traits::IsSpecializationOf<StringType, std::basic_string>, Traits::IsSpecializationOf<StringType, std::basic_string_view>>
* = nullptr>
constexpr auto operator%(const StringType &lhs, char rhs) -> decltype(std::make_tuple(&lhs, rhs))
{
return std::make_tuple(&lhs, rhs);
}
@ -223,7 +252,10 @@ constexpr auto operator%(const std::string &lhs, char rhs) -> decltype(std::make
/*!
* \brief Allows construction of string-tuples via %-operator, eg. string1 % "string2" % string3.
*/
constexpr auto operator%(char lhs, const std::string &rhs) -> decltype(std::make_tuple(lhs, &rhs))
template <class StringType,
Traits::EnableIfAny<Traits::IsSpecializationOf<StringType, std::basic_string>, Traits::IsSpecializationOf<StringType, std::basic_string_view>>
* = nullptr>
constexpr auto operator%(char lhs, const StringType &rhs) -> decltype(std::make_tuple(lhs, &rhs))
{
return std::make_tuple(lhs, &rhs);
}

@ -394,5 +394,5 @@ void ConversionTests::testStringBuilder()
"velocity: " % numberToString(velocityExample) % " km/h (" % numberToString(velocityExample / 3.6) + " m/s)");
CPPUNIT_ASSERT_EQUAL_MESSAGE(
"regular + operator still works (no problems with ambiguity)"s, "regular + still works"s, "regular"s + " + still works");
CPPUNIT_ASSERT_EQUAL_MESSAGE("using string_view", "foobar123"s, argsToString("foo"sv, "bar"sv, 123));
CPPUNIT_ASSERT_EQUAL_MESSAGE("using string_view", "foobar123"s, "foo"sv % "bar"sv + 123);
}

Loading…
Cancel
Save