You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nifi.apache.org by ma...@apache.org on 2023/11/06 15:33:14 UTC
(nifi-minifi-cpp) 02/03: MINIFICPP-2248 Refactor string::join_pack Closes #1680
This is an automated email from the ASF dual-hosted git repository.
martinzink pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/nifi-minifi-cpp.git
commit 14c6c40e35357bd962b85179459e3a8e155b9ed3
Author: Marton Szasz <sz...@apache.org>
AuthorDate: Mon Nov 6 13:57:30 2023 +0100
MINIFICPP-2248 Refactor string::join_pack
Closes #1680
Signed-off-by: Martin Zink <ma...@apache.org>
---
CPPLINT.cfg | 2 +-
cmake/RangeV3.cmake | 3 --
libminifi/include/utils/StringUtils.h | 56 ++++++++++++++------------------
libminifi/test/unit/StringUtilsTests.cpp | 27 ++++++++++-----
4 files changed, 44 insertions(+), 44 deletions(-)
diff --git a/CPPLINT.cfg b/CPPLINT.cfg
index 3119e3131..6e6e951c9 100644
--- a/CPPLINT.cfg
+++ b/CPPLINT.cfg
@@ -1,2 +1,2 @@
set noparent
-filter=-runtime/reference,-runtime/string,-build/c++11,-build/include_subdir,-whitespace/forcolon,-build/namespaces_literals,-readability/check,-build/include_what_you_use,-readability/nolint
+filter=-runtime/reference,-runtime/string,-build/c++11,-build/include_subdir,-whitespace/forcolon,-build/namespaces_literals,-readability/check,-build/include_what_you_use,-readability/nolint,-readability/braces
diff --git a/cmake/RangeV3.cmake b/cmake/RangeV3.cmake
index 6def37776..ff26be3a1 100644
--- a/cmake/RangeV3.cmake
+++ b/cmake/RangeV3.cmake
@@ -22,6 +22,3 @@ FetchContent_Declare(range-v3_src
URL_HASH SHA256=015adb2300a98edfceaf0725beec3337f542af4915cec4d0b89fa0886f4ba9cb
)
FetchContent_MakeAvailable(range-v3_src)
-
-# for better error messages
-target_compile_options(range-v3 INTERFACE $<$<COMPILE_LANG_AND_ID:CXX,GNU>:-fconcepts>)
diff --git a/libminifi/include/utils/StringUtils.h b/libminifi/include/utils/StringUtils.h
index 51fbc3a44..5245d4297 100644
--- a/libminifi/include/utils/StringUtils.h
+++ b/libminifi/include/utils/StringUtils.h
@@ -187,48 +187,40 @@ constexpr size_t size(const std::basic_string_view<CharT>& str) noexcept { retur
namespace detail {
// implementation detail of join_pack
-template<typename CharT>
-struct str_detector {
- template<typename Str>
- using valid_string_t = decltype(std::declval<std::basic_string<CharT>>().append(std::declval<Str>()));
+template<typename Str, typename CharT>
+concept valid_string = requires(std::basic_string<CharT> left, Str appended) {
+ left.append(appended);
};
-template<typename ResultT, typename CharT, typename... Strs>
-using valid_string_pack_t = std::enable_if_t<(meta::is_detected_v<str_detector<CharT>::template valid_string_t, Strs> && ...), ResultT>;
+// normally CharT would be at the end, because we're checking that Strs are valid strings for CharT,
+// but the syntax needs the parameter pack at the end
+template<typename CharT, typename... Strs>
+concept valid_string_pack = (valid_string<Strs, CharT> && ...);
-template<typename CharT, typename... Strs, valid_string_pack_t<void, CharT, Strs...>* = nullptr>
-static std::basic_string<CharT> join_pack(const Strs&... strs) {
- std::basic_string<CharT> result;
- result.reserve((size(strs) + ...));
- (result.append(strs), ...);
- return result;
-}
+template<typename Str> struct char_type_of_impl {};
+template<typename CharT> struct char_type_of_impl<std::basic_string<CharT>> : std::type_identity<CharT> {};
+template<typename CharT> struct char_type_of_impl<CharT*> : std::type_identity<std::remove_cv_t<CharT>> {};
+template<typename CharT> struct char_type_of_impl<std::basic_string_view<CharT>> : std::type_identity<CharT> {};
+template<typename Str> using char_type_of = typename char_type_of_impl<std::decay_t<Str>>::type;
} // namespace detail
/**
* Join all arguments
- * @tparam CharT Deduced character type
- * @tparam Strs Deduced string types
+ * @tparam HeadStr Deduced type of the first string
+ * @tparam Strs Rest of deduced string types
* @param head First string, used for CharT deduction
* @param tail Rest of the strings
- * @return std::basic_string<CharT> containing the resulting string
+ * @return A specialization of std::basic_string containing the resulting string
+ * @pre All strings must have the same character type
*/
-template<typename CharT, typename... Strs>
-inline detail::valid_string_pack_t<std::basic_string<CharT>, CharT, Strs...>
-join_pack(const std::basic_string<CharT>& head, const Strs&... tail) {
- return detail::join_pack<CharT>(head, tail...);
-}
-
-template<typename CharT, typename... Strs>
-inline detail::valid_string_pack_t<std::basic_string<CharT>, CharT, Strs...>
-join_pack(const CharT* head, const Strs&... tail) {
- return detail::join_pack<CharT>(head, tail...);
-}
-
-template<typename CharT, typename... Strs>
-inline detail::valid_string_pack_t<std::basic_string<CharT>, CharT, Strs...>
-join_pack(const std::basic_string_view<CharT>& head, const Strs&... tail) {
- return detail::join_pack<CharT>(head, tail...);
+template<typename HeadStr, typename... Strs>
+requires detail::valid_string_pack<detail::char_type_of<HeadStr>, Strs...>
+inline constexpr auto join_pack(const HeadStr& head, const Strs&... tail) {
+ std::basic_string<detail::char_type_of<HeadStr>> result;
+ result.reserve(size(head) + (size(tail) + ...));
+ result.append(head);
+ (result.append(tail), ...);
+ return result;
}
/**
diff --git a/libminifi/test/unit/StringUtilsTests.cpp b/libminifi/test/unit/StringUtilsTests.cpp
index a7a3b9b7f..c3ad68fdb 100644
--- a/libminifi/test/unit/StringUtilsTests.cpp
+++ b/libminifi/test/unit/StringUtilsTests.cpp
@@ -458,6 +458,13 @@ TEST_CASE("test string::testJoinPack", "[test join_pack]") {
const char carr[] = "char array"; // NOLINT(cppcoreguidelines-avoid-c-arrays): testing const char[] on purpose
REQUIRE(string::join_pack("rvalue c string, ", cstr, std::string{", rval std::string, "}, stdstr, ", ", strview, ", ", carr)
== "rvalue c string, c string, rval std::string, std::string, std::string_view, char array");
+
+ // clang can't use the constexpr string implementation of libstdc++
+#if defined(__cpp_lib_constexpr_string) && __cpp_lib_constexpr_string >= 201907L && (!defined(__clang__) || defined(_LIBCPP_VERSION))
+ STATIC_REQUIRE(string::join_pack(std::string{"a"}, std::string_view{"b"}, "c") == "abc");
+#else
+ REQUIRE(string::join_pack(std::string{"a"}, std::string_view{"b"}, "c") == "abc");
+#endif
}
TEST_CASE("test string::testJoinPackWstring", "[test join_pack wstring]") {
@@ -469,15 +476,19 @@ TEST_CASE("test string::testJoinPackWstring", "[test join_pack wstring]") {
== L"rvalue c string, c string, rval std::string, std::string, std::string_view, char array");
}
-/* doesn't and shouldn't compile
-TEST_CASE("test string::testJoinPackNegative", "[test join_pack negative]") {
- std::wstring stdstr = L"std::string";
- const wchar_t* cstr = L"c string";
- const wchar_t carr[] = L"char array";
- REQUIRE(string::join_pack("rvalue c string, ", cstr, std::string{ ", rval std::string, " }, stdstr, ", ", carr)
- == "rvalue c string, c string, rval std::string, std::string, char array");
+namespace detail {
+template<typename... Strs>
+concept join_pack_works_with_args = requires(Strs... strs) {
+ string::join_pack(strs...);
+};
+} // namespace detail
+
+TEST_CASE("test string::join_pack can't combine different char types", "[test join_pack negative][different char types]") {
+ STATIC_CHECK(!detail::join_pack_works_with_args<const char*&&, const wchar_t*&, std::string, std::wstring, const char*, const wchar_t[]>); // NOLINT: testing C array
+ STATIC_CHECK(!detail::join_pack_works_with_args<std::string, std::wstring>);
+ STATIC_CHECK(!detail::join_pack_works_with_args<std::wstring_view, std::string_view>);
+ STATIC_CHECK(!detail::join_pack_works_with_args<const char[], std::string, std::wstring>); // NOLINT: testing C array
}
- */
TEST_CASE("string::replaceOne works correctly", "[replaceOne]") {
REQUIRE(string::replaceOne("", "x", "y") == "");