Skip to content

Commit

Permalink
fix #3 (#4)
Browse files Browse the repository at this point in the history
- fix using views as range arguments to functions

Co-authored-by: Artur Bać <[email protected]>
  • Loading branch information
arturbac and Artur Bać authored Aug 14, 2024
1 parent a090506 commit 2da246e
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 38 deletions.
4 changes: 2 additions & 2 deletions cmake/CPM.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
36 changes: 18 additions & 18 deletions include/stralgo/detail/stralgo.h
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ struct find_first_of_t
requires concepts::same_range_type<string_view_type, string_view_type2>
[[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(
Expand All @@ -301,7 +301,7 @@ struct trim_left_with_pred_t
{
template<concepts::char_range string_view_type, typename predicate_type>
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)};
Expand All @@ -319,7 +319,7 @@ struct trim_right_with_pred_t
{
template<concepts::char_range string_view_type, typename predicate_type>
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
Expand All @@ -344,7 +344,7 @@ struct trim_pred_t
{
template<concepts::char_range string_view_type, typename predicate_type>
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);
}
Expand Down Expand Up @@ -374,7 +374,7 @@ struct substr_t
template<concepts::char_range string_view_type>
[[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);
Expand All @@ -401,7 +401,7 @@ struct left_t
template<concepts::char_range string_view_type>
[[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);
}
Expand Down Expand Up @@ -430,7 +430,7 @@ struct right_t
template<concepts::char_range string_view_type>
[[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);
}
Expand All @@ -448,13 +448,13 @@ constexpr size_t view_size(maybe_char_type) noexcept

template<concepts::char_range string_view_type>
requires(!concepts::char_type<string_view_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<typename string_view_type, typename... args_type>
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...);
Expand All @@ -472,7 +472,7 @@ struct copy_view_data_t<char_type>
{
template<concepts::char_output_iterator<char_type> 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;
Expand All @@ -493,7 +493,7 @@ struct copy_view_data_t<char_range>

template<concepts::char_output_iterator<char_type> 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;
Expand Down Expand Up @@ -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<size_type>(ranges::size(view)); }
)};
return copy_views_t<string_type, supports_resize_and_overwrite>{
Expand All @@ -640,7 +640,7 @@ struct merge_range_t
template<ranges::forward_range forward_range>
[[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));
}
Expand All @@ -660,7 +660,7 @@ struct ends_with_t
requires concepts::same_range_type<string_view_type, string_view_type2>
[[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);
Expand All @@ -686,7 +686,7 @@ struct starts_with_t
requires concepts::same_range_type<string_view_type, string_view_type2>
[[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;
Expand Down Expand Up @@ -714,7 +714,7 @@ struct compare_no_case_t
requires concepts::same_range_type<string_view_type, string_view_type2>
[[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<string_view_type>;
using char_type = ranges::range_value_t<string_view_type>;
Expand Down Expand Up @@ -752,7 +752,7 @@ struct is_number_t
template<concepts::char_range string_view_type>
[[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<string_view_type>;
return ranges::end(str) == ranges::find_if(str, [](char_type c) noexcept { return !isdigit(c); });
Expand All @@ -767,7 +767,7 @@ struct is_hexnumber_t
template<concepts::char_range string_view_type>
[[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<string_view_type>;
return ranges::end(str) == ranges::find_if(str, [](char_type c) noexcept { return !isxdigit(c); });
Expand Down
18 changes: 9 additions & 9 deletions include/stralgo/stralgo.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -42,7 +42,7 @@ struct trim_left_t
requires std::same_as<ranges::range_value_t<string_view_type>, 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<char_type>{value});
}
Expand All @@ -53,7 +53,7 @@ struct trim_left_t
requires concepts::same_range_type<string_view_type, string_view_type2>
[[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)});
Expand All @@ -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);
}
Expand All @@ -82,7 +82,7 @@ struct trim_right_t
requires std::same_as<ranges::range_value_t<string_view_type>, 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<char_type>{value});
}
Expand All @@ -93,7 +93,7 @@ struct trim_right_t
requires concepts::same_range_type<string_view_type, string_view_type2>
[[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)});
Expand All @@ -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);
}
Expand All @@ -125,7 +125,7 @@ struct trim_t
requires std::same_as<ranges::range_value_t<string_view_type>, 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<char_type>{value});
}
Expand All @@ -135,7 +135,7 @@ struct trim_t
requires concepts::same_range_type<string_view_type, string_view_type2>
[[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)});
Expand Down
4 changes: 2 additions & 2 deletions include/stralgo/strconv_numeric.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<integral_type, input_format>(str_number);
}
Expand All @@ -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<std::floating_point float_type, stralgo::concepts::char_range string_view_type>
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<float_type>(str_number);
}
Expand Down
14 changes: 7 additions & 7 deletions include/stralgo/utf/utf.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ struct utf_input_view_t : public std::ranges::view_interface<utf_input_view_t<So
inline constexpr utf_input_view_t(iterator b, sentinel e) noexcept : begin_{b}, end_{e} {}

template<concepts::char_range forward_range>
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))}
{
Expand All @@ -267,7 +267,7 @@ template<class T>
using const_iterator_t = decltype(std::ranges::begin(std::declval<T const &>()));

template<concepts::char_range forward_range>
utf_input_view_t(forward_range const & r) -> utf_input_view_t<const_iterator_t<forward_range>>;
utf_input_view_t(forward_range && r) -> utf_input_view_t<const_iterator_t<forward_range>>;

///\brief returns code point length of utf sequence
struct length_t
Expand All @@ -293,7 +293,7 @@ struct length_t

template<concepts::char_range forward_range>
[[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));
Expand Down Expand Up @@ -334,7 +334,7 @@ struct capacity_t

template<concepts::char_range forward_range>
[[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));
Expand Down Expand Up @@ -364,7 +364,7 @@ struct convert_t

template<concepts::char_range forward_range, concepts::char_iterator target_iterator>
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);
}
Expand Down Expand Up @@ -418,7 +418,7 @@ struct to_string_t

template<concepts::char_range forward_range>
[[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));
Expand Down Expand Up @@ -483,7 +483,7 @@ struct verify_t

template<concepts::char_range forward_range>
[[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));
Expand Down
14 changes: 14 additions & 0 deletions unittests/utf_.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<decltype(inview)>);
auto res{utf::to_string(inview)};
constexpr_test("бклвр"sv == res);
return {};
};
result |= run_constexpr_test(fn_tmpl);
result |= run_consteval_test(fn_tmpl);
};
}

0 comments on commit 2da246e

Please sign in to comment.