Skip to content

Commit

Permalink
Merge pull request cppfastio#860 from trcrsired/master
Browse files Browse the repository at this point in the history
fix os_c_str(ptr, n) since it does not guarantee null termination
  • Loading branch information
trcrsired authored Sep 30, 2024
2 parents 504ad38 + 4ec9830 commit 5f3bc69
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
33 changes: 26 additions & 7 deletions include/fast_io_core_impl/integers/pointer.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,38 @@ print_alias_define(io_alias_t, basic_os_str_known_size_without_null_terminated<c
return {bas.ptr, bas.n};
}

template <::std::integral T>
inline constexpr basic_os_c_str_with_known_size<T> os_c_str(T const *ch, ::std::size_t n) noexcept
template <::std::integral char_type>
inline constexpr basic_os_str_known_size_without_null_terminated<char_type> os_c_str(char_type const *ch, ::std::size_t n) noexcept
{
return {ch, ::fast_io::cstr_nlen(ch, n)};
}

template <::std::integral char_type, ::std::size_t n>
requires(n != 0)
inline constexpr basic_os_c_str_with_known_size<char_type> os_c_str_arr(char_type const (&cstr)[n]) noexcept
inline constexpr basic_os_str_known_size_without_null_terminated<char_type> os_c_str_carr(char_type const (&cstr)[n]) noexcept
{
constexpr ::std::size_t nm1{static_cast<::std::size_t>(n - 1u)};
return os_c_str(cstr, n);
}

template <::std::integral char_type>
inline constexpr basic_os_c_str_with_known_size<char_type> os_c_str_null_terminated(char_type const *ch, ::std::size_t n) noexcept
{
if (ch[n] != 0) [[unlikely]]
{
::fast_io::fast_terminate();
}
return {ch, n};
}

template <::std::integral char_type, ::std::size_t n>
requires(n != 0)
inline constexpr basic_os_str_known_size_without_null_terminated<char_type> os_c_str_null_terminated_carr(char_type const (&cstr)[n]) noexcept
{
constexpr ::std::size_t nm1{static_cast<::std::size_t>(n - 1u)};
return os_c_str_null_terminated(cstr, nm1);
}

template <::std::integral char_type, ::std::size_t n>
requires(n != 0)
inline constexpr auto small_scatter(char_type const (&s)[n]) noexcept
Expand Down Expand Up @@ -121,7 +140,7 @@ inline constexpr basic_io_scatter_t<::std::remove_cvref_t<::std::ranges::range_v

template <::std::ranges::contiguous_range rg>
requires(::std::integral<::std::ranges::range_value_t<rg>>)
inline constexpr basic_os_c_str_with_known_size<::std::remove_cvref_t<::std::ranges::range_value_t<rg>>>
inline constexpr basic_os_str_known_size_without_null_terminated<::std::remove_cvref_t<::std::ranges::range_value_t<rg>>>
os_c_str(rg &&r) noexcept
{
auto p{::std::ranges::data(r)};
Expand Down Expand Up @@ -161,10 +180,10 @@ inline consteval my_constant_passer<char_type, n> compute_char_literal_array_typ
return {};
}

template<typename T>
template <typename T>
concept printaliascarray = ::std::is_array_v<::std::remove_reference_t<T>> &&
::std::integral<::std::remove_extent_t<::std::remove_cvref_t<T>>> &&
requires(T const &s) { ::fast_io::details::compute_char_literal_array_type(s); };
::std::integral<::std::remove_extent_t<::std::remove_cvref_t<T>>> &&
requires(T const &s) { ::fast_io::details::compute_char_literal_array_type(s); };

} // namespace details

Expand Down
3 changes: 3 additions & 0 deletions include/fast_io_dsal/impl/string_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ class basic_string_view
constexpr basic_string_view(::fast_io::manipulators::basic_os_c_str_with_known_size<char_type> osstr) noexcept
: ptr(osstr.ptr), n(osstr.n)
{}
constexpr basic_string_view(::fast_io::manipulators::basic_os_str_known_size_without_null_terminated<char_type> osstr) noexcept
: ptr(osstr.ptr), n(osstr.n)
{}

constexpr basic_string_view(basic_string_view const &) noexcept = default;
constexpr basic_string_view &operator=(basic_string_view const &) noexcept = default;
Expand Down

0 comments on commit 5f3bc69

Please sign in to comment.