diff --git a/include/upa/str_arg.h b/include/upa/str_arg.h index 9e6e643..829b6fb 100644 --- a/include/upa/str_arg.h +++ b/include/upa/str_arg.h @@ -6,9 +6,9 @@ /************************************************************** // Usage example: -template = 0> -inline void procfn(Args&&... args) { - const auto inp = make_str_arg(std::forward(args)...); +template = 0> +inline void procfn(StrT&& str) { + const auto inp = make_str_arg(std::forward(str)); const auto* first = inp.begin(); const auto* last = inp.end(); // do something with first ... last @@ -76,18 +76,17 @@ class str_arg { : first_(s) , last_(s + traits_type::length(s)) {} - str_arg(const CharT* s, std::size_t length) - : first_(s) - , last_(s + length) - {} - str_arg(const CharT* s, std::ptrdiff_t length) + + template , int> = 0> + str_arg(const CharT* s, SizeT length) : first_(s) , last_(s + length) - {} + { assert(length >= 0); } + str_arg(const CharT* first, const CharT* last) : first_(first) , last_(last) - {} + { assert(first <= last); } // destructor ~str_arg() noexcept = default; @@ -150,33 +149,10 @@ namespace detail { // Requirements for string arguments -template +template struct str_arg_char {}; -// two pointers -template -struct str_arg_char : std::remove_cv { - - template - static str_arg to_str_arg(const T* first, const T* last) { - assert(first <= last); - return { first, last }; - } -}; - -// pointer and size -template -struct str_arg_char : std::enable_if< - is_size_type_v, - std::remove_cv_t> { - - template - static str_arg to_str_arg(const T* s, std::size_t length) { - return { s, length }; - } -}; - -// one pointer (null terminated string) +// Null terminated string template struct str_arg_char : std::remove_cv { @@ -186,7 +162,7 @@ struct str_arg_char : std::remove_cv { } }; -// one string class argument +// String that has data() and length() members template struct str_arg_char : std::enable_if< std::is_pointer_v> && @@ -202,24 +178,24 @@ struct str_arg_char : std::enable_if< // String arguments helper types -template -using str_arg_char_s = str_arg_char...>; +template +using str_arg_char_s = str_arg_char>; -template -using str_arg_char_t = typename str_arg_char_s::type; +template +using str_arg_char_t = typename str_arg_char_s::type; -template +template using enable_if_str_arg_t = std::enable_if_t< - is_char_type_v>, + is_char_type_v>, int>; // String arguments helper function -template -inline auto make_str_arg(Args&&... args) -> str_arg> { - return str_arg_char_s::to_str_arg(std::forward(args)...); +template +inline auto make_str_arg(StrT&& str) -> str_arg> { + return str_arg_char_s::to_str_arg(std::forward(str)); } @@ -233,9 +209,9 @@ constexpr bool is_char8_type_v = #endif ; -template +template using enable_if_str_arg_to_char8_t = std::enable_if_t< - is_char8_type_v>, + is_char8_type_v>, int>; template @@ -244,9 +220,9 @@ constexpr bool is_charW_type_v = std::is_same_v || std::is_same_v; -template +template using enable_if_str_arg_to_charW_t = std::enable_if_t< - is_charW_type_v>, + is_charW_type_v>, int>; @@ -254,15 +230,15 @@ inline std::string&& make_string(std::string&& str) { return std::move(str); } -template = 0> -inline string_view make_string(Args&&... args) { - const auto inp = make_str_arg(std::forward(args)...); +template = 0> +inline string_view make_string(StrT&& str) { + const auto inp = make_str_arg(std::forward(str)); return { inp.data(), inp.length() }; } -template = 0> -inline std::string make_string(Args&&... args) { - const auto inp = make_str_arg(std::forward(args)...); +template = 0> +inline std::string make_string(StrT&& str) { + const auto inp = make_str_arg(std::forward(str)); return url_utf::to_utf8_string(inp.begin(), inp.end()); } diff --git a/include/upa/url_for_atl.h b/include/upa/url_for_atl.h index bd19e4e..9b08dbf 100644 --- a/include/upa/url_for_atl.h +++ b/include/upa/url_for_atl.h @@ -24,7 +24,7 @@ struct str_arg_char_for_atl { using type = typename StrT::XCHAR; static str_arg to_str_arg(const StrT& str) { - return { str.GetString(), static_cast(str.GetLength()) }; + return { str.GetString(), str.GetLength() }; } }; diff --git a/include/upa/url_for_qt.h b/include/upa/url_for_qt.h index 28ea476..f461252 100644 --- a/include/upa/url_for_qt.h +++ b/include/upa/url_for_qt.h @@ -22,7 +22,6 @@ #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) # include #endif -#include // std::ptrdiff_t namespace upa { @@ -33,10 +32,7 @@ struct str_arg_char_for_qt { using type = CharT; static str_arg to_str_arg(const StrT& str) { - return { - reinterpret_cast(str.data()), - static_cast(str.length()) - }; + return { reinterpret_cast(str.data()), str.length() }; } }; diff --git a/test/test-str_arg.cpp b/test/test-str_arg.cpp index 623a3a1..76fbd8e 100644 --- a/test/test-str_arg.cpp +++ b/test/test-str_arg.cpp @@ -13,11 +13,9 @@ // Function to test -using namespace upa; - -template = 0> -inline std::size_t procfn(Args&&... args) { - const auto inp = make_str_arg(std::forward(args)...); +template = 0> +inline std::size_t procfn(StrT&& str) { + const auto inp = upa::make_str_arg(std::forward(str)); return std::distance(inp.begin(), inp.end()); } @@ -54,7 +52,7 @@ struct str_arg_char> { template inline void test_char() { - const size_t N = 3; + const std::size_t N = 3; CharT arr[] = { '1', '2', '3', 0 }; const CharT carr[] = { '1', '2', '3', 0 }; CharT* ptr = arr; @@ -67,30 +65,33 @@ inline void test_char() { procfn(cptr); procfn(vptr); - procfn(arr, N); - procfn(carr, N); - procfn(ptr, N); - procfn(cptr, N); - procfn(vptr, N); - + // upa::str_arg + upa::str_arg arg(arr); + procfn(arg); + const upa::str_arg carg(arr); + procfn(carg); + + procfn(upa::str_arg{arr, N}); + procfn(upa::str_arg{carr, N}); + procfn(upa::str_arg{ptr, N}); + procfn(upa::str_arg{cptr, N}); + procfn(upa::str_arg{vptr, N}); // int size - procfn(arr, int(N)); - procfn(cptr, int(N)); + procfn(upa::str_arg(arr, static_cast(N))); + procfn(upa::str_arg(cptr, static_cast(N))); - procfn(arr, arr + N); - procfn(carr, carr + N); - procfn(ptr, ptr + N); - procfn(cptr, cptr + N); - procfn(vptr, vptr + N); + procfn(upa::str_arg{arr, arr + N}); + procfn(upa::str_arg{carr, carr + N}); + procfn(upa::str_arg{ptr, ptr + N}); + procfn(upa::str_arg{cptr, cptr + N}); + procfn(upa::str_arg{vptr, vptr + N}); + // std::basic_string const std::basic_string str(arr); procfn(str); procfn(std::basic_string(arr)); - upa::str_arg arg(arr); - procfn(arg); - - // test custom string + // custom string procfn(CustomString{cptr, N}); }