From 2da246edce43a521963f03da812feca946b7b67d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Artur=20Ba=C4=87?= Date: Wed, 14 Aug 2024 18:59:55 +0200 Subject: [PATCH] fix #3 (#4) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - fix using views as range arguments to functions Co-authored-by: Artur Bać --- cmake/CPM.cmake | 4 ++-- include/stralgo/detail/stralgo.h | 36 +++++++++++++++---------------- include/stralgo/stralgo.h | 18 ++++++++-------- include/stralgo/strconv_numeric.h | 4 ++-- include/stralgo/utf/utf.h | 14 ++++++------ unittests/utf_.cc | 14 ++++++++++++ 6 files changed, 52 insertions(+), 38 deletions(-) diff --git a/cmake/CPM.cmake b/cmake/CPM.cmake index f2c1205..baf2d8c 100644 --- a/cmake/CPM.cmake +++ b/cmake/CPM.cmake @@ -2,8 +2,8 @@ # # SPDX-FileCopyrightText: Copyright (c) 2019-2023 Lars Melchior and contributors -set(CPM_DOWNLOAD_VERSION 0.40.0) -set(CPM_HASH_SUM "7b354f3a5976c4626c876850c93944e52c83ec59a159ae5de5be7983f0e17a2a") +set(CPM_DOWNLOAD_VERSION 0.40.2) +set(CPM_HASH_SUM "c8cdc32c03816538ce22781ed72964dc864b2a34a310d3b7104812a5ca2d835d") if(CPM_SOURCE_CACHE) set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake") diff --git a/include/stralgo/detail/stralgo.h b/include/stralgo/detail/stralgo.h index 9c507e6..1617110 100644 --- a/include/stralgo/detail/stralgo.h +++ b/include/stralgo/detail/stralgo.h @@ -285,7 +285,7 @@ struct find_first_of_t requires concepts::same_range_type [[nodiscard]] stralgo_static_call_operator constexpr auto - operator()(string_view_type const & view, string_view_type2 const & one_of) + operator()(string_view_type && view, string_view_type2 && one_of) stralgo_static_call_operator_const noexcept { return ranges::find_if( @@ -301,7 +301,7 @@ struct trim_left_with_pred_t { template stralgo_static_call_operator constexpr auto - operator()(string_view_type const & view, predicate_type const & pred) stralgo_static_call_operator_const noexcept + operator()(string_view_type && view, predicate_type && pred) stralgo_static_call_operator_const noexcept { auto first{ranges::find_if(view, pred)}; auto last{ranges::end(view)}; @@ -319,7 +319,7 @@ struct trim_right_with_pred_t { template stralgo_static_call_operator constexpr auto - operator()(string_view_type const & view, predicate_type const & pred) stralgo_static_call_operator_const noexcept + operator()(string_view_type && view, predicate_type && pred) stralgo_static_call_operator_const noexcept { #if defined(__clang__) && !defined(_LIBCPP_STD_VER) && __clang_major__ <= 15 // clang 15 with libstdc++ has broken unusable views @@ -344,7 +344,7 @@ struct trim_pred_t { template stralgo_static_call_operator constexpr auto - operator()(string_view_type const & view, predicate_type const & pred) stralgo_static_call_operator_const noexcept + operator()(string_view_type && view, predicate_type && pred) stralgo_static_call_operator_const noexcept { return trim_left_with_pred(trim_right_with_pred(view, pred), pred); } @@ -374,7 +374,7 @@ struct substr_t template [[nodiscard]] stralgo_static_call_operator constexpr auto - operator()(string_view_type const & view, std::size_t pos, std::size_t count = npos) + operator()(string_view_type && view, std::size_t pos, std::size_t count = npos) stralgo_static_call_operator_const noexcept { return operator()(ranges::begin(view), ranges::end(view), pos, count); @@ -401,7 +401,7 @@ struct left_t template [[nodiscard]] stralgo_static_call_operator constexpr auto - operator()(string_view_type const & view, std::size_t count) stralgo_static_call_operator_const noexcept + operator()(string_view_type && view, std::size_t count) stralgo_static_call_operator_const noexcept { return operator()(ranges::begin(view), ranges::end(view), count); } @@ -430,7 +430,7 @@ struct right_t template [[nodiscard]] stralgo_static_call_operator constexpr auto - operator()(string_view_type const & view, std::size_t count) stralgo_static_call_operator_const noexcept + operator()(string_view_type && view, std::size_t count) stralgo_static_call_operator_const noexcept { return operator()(ranges::begin(view), ranges::end(view), count); } @@ -448,13 +448,13 @@ constexpr size_t view_size(maybe_char_type) noexcept template requires(!concepts::char_type) -constexpr size_t view_size(string_view_type const & view) noexcept +constexpr size_t view_size(string_view_type && view) noexcept { return ranges::size(view); } template -constexpr auto count_size(string_view_type const & viewl, args_type const &... args) noexcept +constexpr auto count_size(string_view_type && viewl, args_type &&... args) noexcept { if constexpr(sizeof...(args_type) != 0) return view_size(viewl) + count_size(args...); @@ -472,7 +472,7 @@ struct copy_view_data_t { template iterator, typename... args_type> stralgo_static_call_operator constexpr iterator operator()( - iterator it, char_type const & char_value, args_type const &... args + iterator it, char_type const & char_value, args_type &&... args ) stralgo_static_call_operator_const noexcept { *it = char_value; @@ -493,7 +493,7 @@ struct copy_view_data_t template iterator, typename... args_type> stralgo_static_call_operator constexpr iterator operator()( - iterator it, char_range const & view, args_type const &... args + iterator it, char_range const & view, args_type &&... args ) stralgo_static_call_operator_const noexcept { it = ranges::copy(view, it).out; @@ -624,7 +624,7 @@ struct merge_range_t itbeg, itend, size_type{}, - [](size_type init, auto const & view) noexcept -> size_type + [](size_type init, auto && view) noexcept -> size_type { return init + static_cast(ranges::size(view)); } )}; return copy_views_t{ @@ -640,7 +640,7 @@ struct merge_range_t template [[nodiscard]] stralgo_static_call_operator constexpr auto - operator()(forward_range const & range) stralgo_static_call_operator_const + operator()(forward_range && range) stralgo_static_call_operator_const { return operator()(ranges::begin(range), ranges::end(range)); } @@ -660,7 +660,7 @@ struct ends_with_t requires concepts::same_range_type [[nodiscard]] stralgo_static_call_operator constexpr bool - operator()(string_view_type const & str, string_view_type2 const & other) + operator()(string_view_type && str, string_view_type2 && other) stralgo_static_call_operator_const noexcept { auto const str0_sz = ranges::size(str); @@ -686,7 +686,7 @@ struct starts_with_t requires concepts::same_range_type [[nodiscard]] stralgo_static_call_operator constexpr bool - operator()(string_view_type const & str, string_view_type2 const & other) + operator()(string_view_type && str, string_view_type2 && other) stralgo_static_call_operator_const noexcept { using ranges::begin; @@ -714,7 +714,7 @@ struct compare_no_case_t requires concepts::same_range_type [[nodiscard]] stralgo_static_call_operator constexpr int - operator()(string_view_type const & s1, string_view_type2 const & s2) stralgo_static_call_operator_const noexcept + operator()(string_view_type && s1, string_view_type2 && s2) stralgo_static_call_operator_const noexcept { using difference_type = ranges::range_difference_t; using char_type = ranges::range_value_t; @@ -752,7 +752,7 @@ struct is_number_t template [[nodiscard]] stralgo_static_call_operator constexpr bool - operator()(string_view_type const & str) stralgo_static_call_operator_const noexcept + operator()(string_view_type && str) stralgo_static_call_operator_const noexcept { using char_type = ranges::range_value_t; return ranges::end(str) == ranges::find_if(str, [](char_type c) noexcept { return !isdigit(c); }); @@ -767,7 +767,7 @@ struct is_hexnumber_t template [[nodiscard]] stralgo_static_call_operator constexpr bool - operator()(string_view_type const & str) stralgo_static_call_operator_const noexcept + operator()(string_view_type && str) stralgo_static_call_operator_const noexcept { using char_type = ranges::range_value_t; return ranges::end(str) == ranges::find_if(str, [](char_type c) noexcept { return !isxdigit(c); }); diff --git a/include/stralgo/stralgo.h b/include/stralgo/stralgo.h index fcb4abf..b4756bc 100644 --- a/include/stralgo/stralgo.h +++ b/include/stralgo/stralgo.h @@ -31,7 +31,7 @@ struct trim_left_t ///ranges::contiguous_range concept then return type is basic_string_view and ranges::subrange if not. [[nodiscard]] stralgo_static_call_operator constexpr auto - operator()(concepts::char_range auto const & view) stralgo_static_call_operator_const noexcept + operator()(concepts::char_range auto && view) stralgo_static_call_operator_const noexcept { return detail::trim_left_with_pred(view, detail::not_is_space); } @@ -42,7 +42,7 @@ struct trim_left_t requires std::same_as, char_type> [[nodiscard]] stralgo_static_call_operator constexpr auto - operator()(string_view_type const & view, char_type value) stralgo_static_call_operator_const noexcept + operator()(string_view_type && view, char_type value) stralgo_static_call_operator_const noexcept { return detail::trim_left_with_pred(view, detail::not_is_char_pred_t{value}); } @@ -53,7 +53,7 @@ struct trim_left_t requires concepts::same_range_type [[nodiscard]] stralgo_static_call_operator constexpr auto - operator()(string_view_type const & view, string_view_type2 const & any_of) + operator()(string_view_type && view, string_view_type2 && any_of) stralgo_static_call_operator_const noexcept { return detail::trim_left_with_pred(view, detail::not_is_any_of{ranges::begin(any_of), ranges::end(any_of)}); @@ -71,7 +71,7 @@ struct trim_right_t ///ranges::contiguous_range concept then return type is basic_string_view and ranges::subrange if not. [[nodiscard]] stralgo_static_call_operator constexpr auto - operator()(concepts::char_range auto const & view) stralgo_static_call_operator_const noexcept + operator()(concepts::char_range auto && view) stralgo_static_call_operator_const noexcept { return detail::trim_right_with_pred(view, detail::not_is_space); } @@ -82,7 +82,7 @@ struct trim_right_t requires std::same_as, char_type> [[nodiscard]] stralgo_static_call_operator constexpr auto - operator()(string_view_type const & view, char_type value) stralgo_static_call_operator_const noexcept + operator()(string_view_type && view, char_type value) stralgo_static_call_operator_const noexcept { return detail::trim_right_with_pred(view, detail::not_is_char_pred_t{value}); } @@ -93,7 +93,7 @@ struct trim_right_t requires concepts::same_range_type [[nodiscard]] stralgo_static_call_operator constexpr auto - operator()(string_view_type const & view, string_view_type2 const & any_of) + operator()(string_view_type && view, string_view_type2 && any_of) stralgo_static_call_operator_const noexcept { return detail::trim_right_with_pred(view, detail::not_is_any_of{ranges::begin(any_of), ranges::end(any_of)}); @@ -115,7 +115,7 @@ struct trim_t ///\returns trimmed view of \param view from values that match isspace [[nodiscard]] stralgo_static_call_operator constexpr auto - operator()(concepts::char_range auto const & view) stralgo_static_call_operator_const noexcept + operator()(concepts::char_range auto && view) stralgo_static_call_operator_const noexcept { return detail::trim_pred(view, detail::not_is_space); } @@ -125,7 +125,7 @@ struct trim_t requires std::same_as, char_type> [[nodiscard]] stralgo_static_call_operator constexpr auto - operator()(string_view_type const & view, char_type value) stralgo_static_call_operator_const noexcept + operator()(string_view_type && view, char_type value) stralgo_static_call_operator_const noexcept { return detail::trim_pred(view, detail::not_is_char_pred_t{value}); } @@ -135,7 +135,7 @@ struct trim_t requires concepts::same_range_type [[nodiscard]] stralgo_static_call_operator constexpr auto - operator()(string_view_type const & view, string_view_type2 const & any_of) + operator()(string_view_type && view, string_view_type2 && any_of) stralgo_static_call_operator_const noexcept { return detail::trim_pred(view, detail::not_is_any_of{ranges::begin(any_of), ranges::end(any_of)}); diff --git a/include/stralgo/strconv_numeric.h b/include/stralgo/strconv_numeric.h index 539fbdc..06fafc7 100644 --- a/include/stralgo/strconv_numeric.h +++ b/include/stralgo/strconv_numeric.h @@ -138,7 +138,7 @@ template< std::integral integral_type, input_format_e input_format = input_format_e::undetermined, stralgo::concepts::char_range string_view_type> -inline constexpr auto str2int(string_view_type const & str_number) noexcept +inline constexpr auto str2int(string_view_type && str_number) noexcept { return string_to_integral(str_number); } @@ -147,7 +147,7 @@ inline constexpr auto str2int(string_view_type const & str_number) noexcept ///\brief signed float conversion from string supports untrimmed strings of decimal notation [+/-]d[n] and hexadecimal ///lower and uppercase [+/-]0xh[n] numbers template -inline constexpr auto str2f(string_view_type const & str_number) noexcept +inline constexpr auto str2f(string_view_type && str_number) noexcept { return string_to_float(str_number); } diff --git a/include/stralgo/utf/utf.h b/include/stralgo/utf/utf.h index 3dabd1e..c6afecf 100644 --- a/include/stralgo/utf/utf.h +++ b/include/stralgo/utf/utf.h @@ -244,7 +244,7 @@ struct utf_input_view_t : public std::ranges::view_interface - explicit inline constexpr utf_input_view_t(forward_range const & range) : + explicit inline constexpr utf_input_view_t(forward_range && range) : begin_{utf_forward_iterator_t(std::ranges::begin(range))}, end_{utf_forward_iterator_t(std::ranges::end(range))} { @@ -267,7 +267,7 @@ template using const_iterator_t = decltype(std::ranges::begin(std::declval())); template -utf_input_view_t(forward_range const & r) -> utf_input_view_t>; +utf_input_view_t(forward_range && r) -> utf_input_view_t>; ///\brief returns code point length of utf sequence struct length_t @@ -293,7 +293,7 @@ struct length_t template [[nodiscard]] - stralgo_static_call_operator constexpr auto operator()(forward_range const & range + stralgo_static_call_operator constexpr auto operator()(forward_range && range ) stralgo_static_call_operator_const noexcept { return operator()(std::ranges::begin(range), std::ranges::end(range)); @@ -334,7 +334,7 @@ struct capacity_t template [[nodiscard]] - stralgo_static_call_operator constexpr auto operator()(forward_range const & range + stralgo_static_call_operator constexpr auto operator()(forward_range && range ) stralgo_static_call_operator_const noexcept { return operator()(std::ranges::begin(range), std::ranges::end(range)); @@ -364,7 +364,7 @@ struct convert_t template stralgo_static_call_operator constexpr auto - operator()(forward_range const & range, target_iterator out) stralgo_static_call_operator_const noexcept + operator()(forward_range && range, target_iterator out) stralgo_static_call_operator_const noexcept { return operator()(std::ranges::begin(range), std::ranges::end(range), out); } @@ -418,7 +418,7 @@ struct to_string_t template [[nodiscard]] - stralgo_static_call_operator constexpr auto operator()(forward_range const & range + stralgo_static_call_operator constexpr auto operator()(forward_range && range ) stralgo_static_call_operator_const->string_type { return operator()(std::ranges::begin(range), std::ranges::end(range)); @@ -483,7 +483,7 @@ struct verify_t template [[nodiscard]] - stralgo_static_call_operator constexpr auto operator()(forward_range const & range + stralgo_static_call_operator constexpr auto operator()(forward_range && range ) noexcept stralgo_static_call_operator_const->verify_status_e { return operator()(std::ranges::begin(range), std::ranges::end(range)); diff --git a/unittests/utf_.cc b/unittests/utf_.cc index f63f38e..cd19352 100644 --- a/unittests/utf_.cc +++ b/unittests/utf_.cc @@ -542,4 +542,18 @@ int main() expect(eq(res, "бакалавра-томатами-бакалавра томатами-бакалавра-томатами"sv)); } }; + "std::ranges::filter_view as argument to to_string_t #3"_test = [&] + { + auto fn_tmpl = []() -> metatests::test_result + { + using namespace std::string_view_literals; + auto inview{U"бакалавра"sv | std::views::filter([](char32_t c) noexcept -> bool { return c != U'а'; })}; + static_assert(stralgo::concepts::char_range); + auto res{utf::to_string(inview)}; + constexpr_test("бклвр"sv == res); + return {}; + }; + result |= run_constexpr_test(fn_tmpl); + result |= run_consteval_test(fn_tmpl); + }; }