Skip to content

Commit

Permalink
✨ Properly use fixed_basic_string and fixed_vector
Browse files Browse the repository at this point in the history
— 💬 This is instead of std::vector/std::string for things of known output sizes that we also know are small, to alleviate much of the burden here.
  • Loading branch information
ThePhD committed Jun 12, 2022
1 parent 993f475 commit 710a476
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 65 deletions.
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ FetchContent_Declare(ztd.idk
GIT_TAG main)
FetchContent_MakeAvailable(ztd.idk)

# ztd.fixed_container
FetchContent_Declare(ztd.fixed_container
GIT_REPOSITORY https://github.com/soasis/fixed_container.git
GIT_TAG main)
FetchContent_MakeAvailable(ztd.fixed_container)

# ztd.cuneicode
if (ZTD_TEXT_USE_CUNEICODE)
FetchContent_Declare(ztd.cuneicode
Expand Down Expand Up @@ -214,6 +220,7 @@ target_link_libraries(ztd.text
INTERFACE
${ztd-text-libiconv-dl}
ztd::idk
ztd::fixed_container
${ztd-text-libiconv}
${ztd-text-cuneicode})
install(DIRECTORY include/
Expand Down
18 changes: 7 additions & 11 deletions include/ztd/text/decode_one.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,9 @@

#include <ztd/ranges/unbounded.hpp>
#include <ztd/idk/span.hpp>
#include <ztd/fixed_container.hpp>
#include <ztd/ranges/detail/insert_bulk.hpp>

#include <string>
#include <vector>

#include <ztd/prologue.hpp>

namespace ztd { namespace text {
Expand Down Expand Up @@ -283,14 +281,13 @@ namespace ztd { namespace text {
constexpr bool _IsStringable
= (is_char_traitable_v<_OutputCodePoint> || is_unicode_code_point_v<_OutputCodePoint>);
if constexpr (_IsVoidContainer && _IsStringable) {
// prevent instantiation errors with basic_string by boxing it inside of an "if constexpr"
using _RealOutputContainer = ::std::basic_string<_OutputCodePoint>;
using _RealOutputContainer = ::ztd::fixed_basic_string<_OutputCodePoint, max_code_points_v<_UEncoding>>;
return __txt_detail::__decode_one_dispatch<_RealOutputContainer>(::std::forward<_Input>(__input),
::std::forward<_Encoding>(__encoding), ::std::forward<_ErrorHandler>(__error_handler), __state);
}
else {
using _RealOutputContainer
= ::std::conditional_t<_IsVoidContainer, ::std::vector<_OutputCodePoint>, _OutputContainer>;
using _RealOutputContainer = ::std::conditional_t<_IsVoidContainer,
::ztd::fixed_vector<_OutputCodePoint, max_code_points_v<_UEncoding>>, _OutputContainer>;
return __txt_detail::__decode_one_dispatch<_RealOutputContainer>(::std::forward<_Input>(__input),
::std::forward<_Encoding>(__encoding), ::std::forward<_ErrorHandler>(__error_handler), __state);
}
Expand Down Expand Up @@ -408,14 +405,13 @@ namespace ztd { namespace text {
constexpr bool _IsStringable
= (is_char_traitable_v<_OutputCodePoint> || is_unicode_code_point_v<_OutputCodePoint>);
if constexpr (_IsVoidContainer && _IsStringable) {
// prevent instantiation errors with basic_string by boxing it inside of an "if constexpr"
using _RealOutputContainer = ::std::basic_string<_OutputCodePoint>;
using _RealOutputContainer = ::ztd::fixed_basic_string<_OutputCodePoint, max_code_points_v<_UEncoding>>;
return __txt_detail::__decode_one_dispatch<_RealOutputContainer>(::std::forward<_Input>(__input),
::std::forward<_Encoding>(__encoding), ::std::forward<_ErrorHandler>(__error_handler), __state);
}
else {
using _RealOutputContainer
= ::std::conditional_t<_IsVoidContainer, ::std::vector<_OutputCodePoint>, _OutputContainer>;
using _RealOutputContainer = ::std::conditional_t<_IsVoidContainer,
::ztd::fixed_vector<_OutputCodePoint, max_code_points_v<_UEncoding>>, _OutputContainer>;
return __txt_detail::__decode_one_dispatch<_RealOutputContainer>(::std::forward<_Input>(__input),
::std::forward<_Encoding>(__encoding), ::std::forward<_ErrorHandler>(__error_handler), __state);
}
Expand Down
22 changes: 9 additions & 13 deletions include/ztd/text/encode_one.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,9 @@

#include <ztd/ranges/unbounded.hpp>
#include <ztd/idk/span.hpp>
#include <ztd/fixed_container.hpp>
#include <ztd/ranges/detail/insert_bulk.hpp>

#include <string>
#include <vector>

#include <ztd/prologue.hpp>

namespace ztd { namespace text {
Expand Down Expand Up @@ -281,14 +279,13 @@ namespace ztd { namespace text {
constexpr bool _IsStringable
= (is_char_traitable_v<_OutputCodeUnit> || is_unicode_code_point_v<_OutputCodeUnit>);
if constexpr (_IsVoidContainer && _IsStringable) {
// prevent instantiation errors with basic_string by boxing it inside of an "if constexpr"
using _RealOutputContainer = ::std::basic_string<_OutputCodeUnit>;
using _RealOutputContainer = ::ztd::fixed_basic_string<_OutputCodeUnit, max_code_units_v<_UEncoding>>;
return __txt_detail::__encode_one_dispatch<false, _RealOutputContainer>(::std::forward<_Input>(__input),
::std::forward<_Encoding>(__encoding), ::std::forward<_ErrorHandler>(__error_handler), __state);
}
else {
using _RealOutputContainer
= ::std::conditional_t<_IsVoidContainer, ::std::vector<_OutputCodeUnit>, _OutputContainer>;
using _RealOutputContainer = ::std::conditional_t<_IsVoidContainer,
::ztd::fixed_vector<_OutputCodeUnit, max_code_units_v<_UEncoding>>, _OutputContainer>;
return __txt_detail::__encode_one_dispatch<false, _RealOutputContainer>(::std::forward<_Input>(__input),
::std::forward<_Encoding>(__encoding), ::std::forward<_ErrorHandler>(__error_handler), __state);
}
Expand Down Expand Up @@ -408,15 +405,14 @@ namespace ztd { namespace text {
constexpr bool _IsStringable
= (is_char_traitable_v<_OutputCodeUnit> || is_unicode_code_point_v<_OutputCodeUnit>);
if constexpr (_IsVoidContainer && _IsStringable) {
// prevent instantiation errors with basic_string by boxing it inside of an "if constexpr"
using _RealOutputContainer = ::std::basic_string<_OutputCodeUnit>;
return __txt_detail::__encode_one_dispatch<_RealOutputContainer>(::std::forward<_Input>(__input),
using _RealOutputContainer = ::ztd::fixed_basic_string<_OutputCodeUnit, max_code_units_v<_UEncoding>>;
return __txt_detail::__encode_one_dispatch<true, _RealOutputContainer>(::std::forward<_Input>(__input),
::std::forward<_Encoding>(__encoding), ::std::forward<_ErrorHandler>(__error_handler), __state);
}
else {
using _RealOutputContainer
= ::std::conditional_t<_IsVoidContainer, ::std::vector<_OutputCodeUnit>, _OutputContainer>;
return __txt_detail::__encode_one_dispatch<_RealOutputContainer>(::std::forward<_Input>(__input),
using _RealOutputContainer = ::std::conditional_t<_IsVoidContainer,
::ztd::fixed_vector<_OutputCodeUnit, max_code_units_v<_UEncoding>>, _OutputContainer>;
return __txt_detail::__encode_one_dispatch<true, _RealOutputContainer>(::std::forward<_Input>(__input),
::std::forward<_Encoding>(__encoding), ::std::forward<_ErrorHandler>(__error_handler), __state);
}
}
Expand Down
64 changes: 31 additions & 33 deletions include/ztd/text/transcode_one.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,9 @@

#include <ztd/ranges/unbounded.hpp>
#include <ztd/idk/span.hpp>
#include <ztd/fixed_container.hpp>
#include <ztd/ranges/detail/insert_bulk.hpp>

#include <string>
#include <vector>

#include <ztd/prologue.hpp>

namespace ztd { namespace text {
Expand Down Expand Up @@ -421,8 +419,8 @@ namespace ztd { namespace text {
/// encoding for the output, which is then returned in a result structure with additional information about
/// success.
///
/// @tparam _OutputContainer The container to default-construct and serialize data into. Typically, a @c
/// std::basic_string or a `std::vector` of some sort.
/// @tparam _OutputContainer The container to default-construct and serialize data into. Typically, a fixed-size
/// container of some sort.
///
/// @param[in] __input An input_view to read code units from and use in the decode operation that will
/// produce intermediate code points.
Expand Down Expand Up @@ -452,15 +450,15 @@ namespace ztd { namespace text {
constexpr bool _IsStringable
= (is_char_traitable_v<_OutputCodeUnit> || is_unicode_code_point_v<_OutputCodeUnit>);
if constexpr (_IsVoidContainer && _IsStringable) {
using _RealOutputContainer = ::std::basic_string<_OutputCodeUnit>;
using _RealOutputContainer = ::ztd::fixed_basic_string<_OutputCodeUnit, max_code_units_v<_UToEncoding>>;
return __txt_detail::__transcode_one_dispatch<false, _RealOutputContainer>(
::std::forward<_Input>(__input), ::std::forward<_FromEncoding>(__from_encoding),
::std::forward<_ToEncoding>(__to_encoding), ::std::forward<_FromErrorHandler>(__from_error_handler),
::std::forward<_ToErrorHandler>(__to_error_handler), __from_state, __to_state);
}
else {
using _RealOutputContainer
= ::std::conditional_t<_IsVoidContainer, ::std::vector<_OutputCodeUnit>, _OutputContainer>;
using _RealOutputContainer = ::std::conditional_t<_IsVoidContainer,
::ztd::fixed_vector<_OutputCodeUnit, max_code_units_v<_UToEncoding>>, _OutputContainer>;
return __txt_detail::__transcode_one_dispatch<false, _RealOutputContainer>(
::std::forward<_Input>(__input), ::std::forward<_FromEncoding>(__from_encoding),
::std::forward<_ToEncoding>(__to_encoding), ::std::forward<_FromErrorHandler>(__from_error_handler),
Expand All @@ -473,8 +471,8 @@ namespace ztd { namespace text {
/// encoding for the output, which is then returned in a result structure with additional information about
/// success.
///
/// @tparam _OutputContainer The container to default-construct and serialize data into. Typically, a @c
/// std::basic_string or a `std::vector` of some sort.
/// @tparam _OutputContainer The container to default-construct and serialize data into. Typically, a fixed-size
/// container of some sort.
///
/// @param[in] __input An input_view to read code units from and use in the decode operation that will
/// produce intermediate code points.
Expand Down Expand Up @@ -513,8 +511,8 @@ namespace ztd { namespace text {
/// encoding for the output, which is then returned in a result structure with additional information about
/// success.
///
/// @tparam _OutputContainer The container to default-construct and serialize data into. Typically, a
/// `std::basic_string` or a `std::vector` of some sort.
/// @tparam _OutputContainer The container to default-construct and serialize data into. Typically, a fixed-size
/// container of some sort.
///
/// @param[in] __input An input_view to read code units from and use in the decode operation that will
/// produce intermediate code points.
Expand Down Expand Up @@ -552,8 +550,8 @@ namespace ztd { namespace text {
/// encoding for the output, which is then returned in a result structure with additional information about
/// success.
///
/// @tparam _OutputContainer The container to default-construct and serialize data into. Typically, a @c
/// std::basic_string or a `std::vector` of some sort.
/// @tparam _OutputContainer The container to default-construct and serialize data into. Typically, a fixed-size
/// container of some sort.
///
/// @param[in] __input An input_view to read code units from and use in the decode operation that will
/// produce intermediate code points.
Expand Down Expand Up @@ -587,8 +585,8 @@ namespace ztd { namespace text {
/// encoding for the output, which is then returned in a result structure with additional information about
/// success.
///
/// @tparam _OutputContainer The container to default-construct and serialize data into. Typically, a @c
/// std::basic_string or a `std::vector` of some sort.
/// @tparam _OutputContainer The container to default-construct and serialize data into. Typically, a fixed-size
/// container of some sort.
///
/// @param[in] __input An input_view to read code units from and use in the decode operation that will
/// produce intermediate code points.
Expand Down Expand Up @@ -618,8 +616,8 @@ namespace ztd { namespace text {
/// encoding for the output, which is then returned in a result structure with additional information about
/// success.
///
/// @tparam _OutputContainer The container to default-construct and serialize data into. Typically, a @c
/// std::basic_string or a `std::vector` of some sort.
/// @tparam _OutputContainer The container to default-construct and serialize data into. Typically, a fixed-size
/// container of some sort.
///
/// @param[in] __input An input_view to read code units from and use in the decode operation that will
/// produce intermediate code points.
Expand Down Expand Up @@ -662,8 +660,8 @@ namespace ztd { namespace text {
/// encoding for the output, which is then returned in a result structure with additional information about
/// success.
///
/// @tparam _OutputContainer The container to default-construct and serialize data into. Typically, a @c
/// std::basic_string or a `std::vector` of some sort.
/// @tparam _OutputContainer The container to default-construct and serialize data into. Typically, a fixed-size
/// container of some sort.
///
/// @param[in] __input An input_view to read code units from and use in the decode operation that will
/// produce intermediate code points.
Expand Down Expand Up @@ -692,15 +690,15 @@ namespace ztd { namespace text {
constexpr bool _IsStringable
= (is_char_traitable_v<_OutputCodeUnit> || is_unicode_code_point_v<_OutputCodeUnit>);
if constexpr (_IsVoidContainer && _IsStringable) {
using _RealOutputContainer = ::std::basic_string<_OutputCodeUnit>;
using _RealOutputContainer = ::ztd::fixed_basic_string<_OutputCodeUnit, max_code_units_v<_UToEncoding>>;
return __txt_detail::__transcode_one_dispatch<true, _RealOutputContainer>(
::std::forward<_Input>(__input), ::std::forward<_FromEncoding>(__from_encoding),
::std::forward<_ToEncoding>(__to_encoding), ::std::forward<_FromErrorHandler>(__from_error_handler),
::std::forward<_ToErrorHandler>(__to_error_handler), __from_state, __to_state);
}
else {
using _RealOutputContainer
= ::std::conditional_t<_IsVoidContainer, ::std::vector<_OutputCodeUnit>, _OutputContainer>;
using _RealOutputContainer = ::std::conditional_t<_IsVoidContainer,
::ztd::fixed_vector<_OutputCodeUnit, max_code_units_v<_UToEncoding>>, _OutputContainer>;
return __txt_detail::__transcode_one_dispatch<true, _RealOutputContainer>(
::std::forward<_Input>(__input), ::std::forward<_FromEncoding>(__from_encoding),
::std::forward<_ToEncoding>(__to_encoding), ::std::forward<_FromErrorHandler>(__from_error_handler),
Expand All @@ -713,8 +711,8 @@ namespace ztd { namespace text {
/// encoding for the output, which is then returned in a result structure with additional information about
/// success.
///
/// @tparam _OutputContainer The container to default-construct and serialize data into. Typically, a @c
/// std::basic_string or a `std::vector` of some sort.
/// @tparam _OutputContainer The container to default-construct and serialize data into. Typically, a fixed-size
/// container of some sort.
///
/// @param[in] __input An input_view to read code units from and use in the decode operation that will
/// produce intermediate code points.
Expand Down Expand Up @@ -753,8 +751,8 @@ namespace ztd { namespace text {
/// encoding for the output, which is then returned in a result structure with additional information about
/// success.
///
/// @tparam _OutputContainer The container to default-construct and serialize data into. Typically, a @c
/// std::basic_string or a `std::vector` of some sort.
/// @tparam _OutputContainer The container to default-construct and serialize data into. Typically, a fixed-size
/// container of some sort.
///
/// @param[in] __input An input_view to read code units from and use in the decode operation that will
/// produce intermediate code points.
Expand Down Expand Up @@ -792,8 +790,8 @@ namespace ztd { namespace text {
/// encoding for the output, which is then returned in a result structure with additional information about
/// success.
///
/// @tparam _OutputContainer The container to default-construct and serialize data into. Typically, a @c
/// std::basic_string or a `std::vector` of some sort.
/// @tparam _OutputContainer The container to default-construct and serialize data into. Typically, a fixed-size
/// container of some sort.
///
/// @param[in] __input An input_view to read code units from and use in the decode operation that will
/// produce intermediate code points.
Expand Down Expand Up @@ -827,8 +825,8 @@ namespace ztd { namespace text {
/// encoding for the output, which is then returned in a result structure with additional information about
/// success.
///
/// @tparam _OutputContainer The container to default-construct and serialize data into. Typically, a @c
/// std::basic_string or a `std::vector` of some sort.
/// @tparam _OutputContainer The container to default-construct and serialize data into. Typically, a fixed-size
/// container of some sort.
///
/// @param[in] __input An input_view to read code units from and use in the decode operation that will
/// produce intermediate code points.
Expand Down Expand Up @@ -858,8 +856,8 @@ namespace ztd { namespace text {
/// encoding for the output, which is then returned in a result structure with additional information about
/// success.
///
/// @tparam _OutputContainer The container to default-construct and serialize data into. Typically, a @c
/// std::basic_string or a `std::vector` of some sort.
/// @tparam _OutputContainer The container to default-construct and serialize data into. Typically, a fixed-size
/// container of some sort.
///
/// @param[in] __input An input_view to read code units from and use in the decode operation that will
/// produce intermediate code points.
Expand Down
16 changes: 8 additions & 8 deletions tests/basic_run_time/source/errors.throw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
inline namespace ztd_text_tests_basic_run_time_errors_throw {
template <typename Encoding, typename Input>
void decode_one_throw_check(Encoding& encoding, Input& container) {
constexpr std::size_t output_max = ztd::text::max_code_points_v<Encoding>;
using code_point = ztd::text::code_point_t<Encoding>;
using code_unit = ztd::text::code_unit_t<Encoding>;
using state = ztd::text::decode_state_t<Encoding>;
[[maybe_unused]] constexpr std::size_t output_max = ztd::text::max_code_points_v<Encoding>;
using code_point = ztd::text::code_point_t<Encoding>;
using code_unit = ztd::text::code_unit_t<Encoding>;
using state = ztd::text::decode_state_t<Encoding>;

auto action = [&]() noexcept(false) {
code_point output_buffer[ztd::text::max_code_points_v<Encoding>] {};
Expand All @@ -55,10 +55,10 @@ inline namespace ztd_text_tests_basic_run_time_errors_throw {

template <typename Encoding, typename Input>
void encode_one_throw_check(Encoding& encoding, Input& container) {
constexpr std::size_t output_max = ztd::text::max_code_units_v<Encoding>;
using code_point = ztd::text::code_point_t<Encoding>;
using code_unit = ztd::text::code_unit_t<Encoding>;
using state = ztd::text::encode_state_t<Encoding>;
[[maybe_unused]] constexpr std::size_t output_max = ztd::text::max_code_units_v<Encoding>;
using code_point = ztd::text::code_point_t<Encoding>;
using code_unit = ztd::text::code_unit_t<Encoding>;
using state = ztd::text::encode_state_t<Encoding>;

auto action = [&]() noexcept(false) {
code_unit output_buffer[ztd::text::max_code_units_v<Encoding>] {};
Expand Down

0 comments on commit 710a476

Please sign in to comment.