Skip to content

Commit

Permalink
🎨 CMake Improvements!
Browse files Browse the repository at this point in the history
- Improved transcode_iterator/__encoding_iterator details
- Fix up some warnings-as-errors
  • Loading branch information
ThePhD committed Apr 1, 2021
1 parent f30977a commit 4178b82
Show file tree
Hide file tree
Showing 11 changed files with 170 additions and 96 deletions.
22 changes: 22 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,27 @@ string(CONCAT --disable-permissive $<
${ztd-text-is-top-level}
>:/permissive-
>)
string(CONCAT --utf8-literal-encoding
$<IF:$<BOOL:${MSVC}>,
/execution-charset:utf-8,
-fexec-charset=utf-8
>
)
string(CONCAT --utf8-source-encoding
$<IF:$<BOOL:${MSVC}>,
/source-charset:utf-8,
-finput-charset=utf-8
>
)
string(CONCAT --extra-constexpr-power
$<IF:$<BOOL:${MSVC}>,
/constexpr:steps2147483647,
$<IF:$<BOOL:${CLANG}>,
-fconstexpr-steps=2147483647,
-fconstexpr-depth=2147483647
>
>
)
string(CONCAT ztd-use-cuneicode $<
$<AND:
$<BOOL:${ZTD_TEXT_USE_CUNEICODE}>,
Expand All @@ -137,6 +158,7 @@ string(CONCAT --warn-default $<
string(CONCAT --deny-errors $<$<BOOL:${ZTD_TEXT_DIAGNOSTIC_ERRORS}>:-Werror>)


# Main library declarations
add_library(ztd.text INTERFACE)
add_library(ztd::text ALIAS ztd.text)
target_include_directories(ztd.text
Expand Down
6 changes: 3 additions & 3 deletions documentation/source/future.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ Right now, all state parameters are assumed to be move-only. This is detrimental



Transcoding Iterators/Transcode View
------------------------------------
Transcoding Iterators/Transcode View
---------------------------------------

Right now these types would not work especially well for input and output ranges. They should be modified just like the internal ``ztd::text::__txt_detail::__encoding_iterator`` class types, so that they work with ``input_iterator`` and ``output_iterator`` types.

- ☑ Improve constructor delegation and make sure to explicitly implement default construction vs. letting it happen with ``=default`` (which does not work for some of the base types present).
- ☑ Modify implementation to cache data and position when an input or output iterator is detected.
- ☑ Return ``const value_type&`` for ``reference`` to enable C++20 ranges to work properly.
- Mark as ``enable_borrowed_range`` when C++20 is detected.
- Mark as ``enable_borrowed_range`` when C++20 is detected.



Expand Down
47 changes: 17 additions & 30 deletions include/ztd/text/detail/encoding_iterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ namespace ztd { namespace text {
template <typename _Encoding, typename _Input, typename _ErrorHandler>
inline constexpr bool __is_encoding_with_error_handler_never_returns_error_v = false;

inline constexpr ::std::size_t _CursorlessSizeSentinel = 1;

using __encoding_sentinel_t = default_sentinel_t;

template <__transaction _EncodeOrDecode, typename _Derived, typename _Encoding, typename _Range,
Expand Down Expand Up @@ -378,12 +376,12 @@ namespace ztd { namespace text {
/// @returns A reference to *this, after incrementing the iterator.
//////
constexpr _Derived& operator++() {
if constexpr (_IsCursorless) {
if constexpr (_IsSingleValueType) {
this->_M_read_one();
}
else {
++this->_M_position;
if (this->_M_position == this->__base_cursor_cache_t::_M_size) {
++this->__base_cursor_cache_t::_M_position;
if (this->__base_cursor_cache_t::_M_position == this->__base_cursor_cache_t::_M_size) {
this->_M_read_one();
}
}
Expand All @@ -399,11 +397,11 @@ namespace ztd { namespace text {
/// writable.
//////
constexpr reference operator*() const noexcept {
if constexpr (_IsCursorless) {
if constexpr (_IsSingleValueType) {
return this->_M_cache[0];
}
else {
return this->_M_cache[this->_M_position];
return this->_M_cache[this->__base_cursor_cache_t::_M_position];
}
}

Expand All @@ -413,15 +411,10 @@ namespace ztd { namespace text {
/// @brief Compares whether or not this iterator has truly reached the end.
///
//////
friend constexpr bool operator==(const _Derived& __it, const __encoding_sentinel_t&) noexcept {
if constexpr (!_IsErrorless) {
if (__it.__base_error_cache_t::_M_error_code == encoding_error::ok) {
return false;
}
}
if constexpr (__it._IsCursorless) {
friend constexpr bool operator==(const _Derived& __it, const __encoding_sentinel_t&) {
if constexpr (__it._IsCursorless || (__it._IsInputOrOutput && __it._IsSingleValueType)) {
return __it._M_base_is_empty()
&& static_cast<__base_cursor_cache_size_t>(_CursorlessSizeSentinel)
&& static_cast<__base_cursor_cache_size_t>(__txt_detail::_CursorlessSizeSentinel)
== __it.__base_cursor_cache_t::_M_size;
}
else {
Expand All @@ -434,23 +427,18 @@ namespace ztd { namespace text {
/// @brief Compares whether or not this iterator has truly reached the end.
///
//////
friend constexpr bool operator==(const __encoding_sentinel_t& __sen, const _Derived& __it) noexcept {
friend constexpr bool operator==(const __encoding_sentinel_t& __sen, const _Derived& __it) {
return __it == __sen;
}

//////
/// @brief Compares whether or not this iterator has truly reached the end.
///
//////
friend constexpr bool operator!=(const _Derived& __it, const __encoding_sentinel_t&) noexcept {
if constexpr (!_IsErrorless) {
if (__it.__base_error_cache_t::_M_error_code != encoding_error::ok) {
return true;
}
}
if constexpr (__it._IsCursorless) {
friend constexpr bool operator!=(const _Derived& __it, const __encoding_sentinel_t&) {
if constexpr (__it._IsCursorless || (__it._IsInputOrOutput && __it._IsSingleValueType)) {
return !__it._M_base_is_empty()
|| static_cast<__base_cursor_cache_size_t>(_CursorlessSizeSentinel)
|| static_cast<__base_cursor_cache_size_t>(__txt_detail::_CursorlessSizeSentinel)
!= __it.__base_cursor_cache_t::_M_size;
}
else {
Expand Down Expand Up @@ -478,9 +466,9 @@ namespace ztd { namespace text {
}
}

constexpr bool _M_read_one() noexcept {
constexpr void _M_read_one() {
if (this->_M_base_is_empty()) {
if constexpr (_IsCursorless) {
if constexpr (_IsCursorless || (_IsSingleValueType && _IsInputOrOutput)) {
this->__base_cursor_cache_t::_M_size
= static_cast<__base_cursor_cache_size_t>(_CursorlessSizeSentinel);
}
Expand All @@ -490,11 +478,11 @@ namespace ztd { namespace text {
this->__base_cursor_cache_t::_M_position
= static_cast<__base_cursor_cache_size_t>(this->_M_cache.size());
}
return false;
return;
}
auto& __this_input_range = this->_M_range();
auto __this_cache_begin = this->_M_cache.data();
decltype(__this_cache_begin) __this_cache_end {};
[[maybe_unused]] decltype(__this_cache_begin) __this_cache_end {};
if constexpr (_IsInputOrOutput) {
auto __result = __basic_encode_or_decode_one<__consume::__no, _EncodeOrDecode>(
::std::move(__this_input_range), this->encoding(), this->_M_cache, this->handler(),
Expand All @@ -515,15 +503,14 @@ namespace ztd { namespace text {
}
this->__base_range_t::__get_value() = ::std::move(__result.input);
}
if constexpr (!_IsCursorless) {
if constexpr (!_IsSingleValueType) {
__base_cursor_cache_size_t __data_size
= static_cast<__base_cursor_cache_size_t>(__this_cache_end - __this_cache_begin);
ZTD_TEXT_ASSERT_MESSAGE_I_("size of produced value can never be bigger thanthe cache",
static_cast<::std::size_t>(__data_size) <= this->_M_cache.size());
this->__base_cursor_cache_t::_M_position = static_cast<__base_cursor_cache_size_t>(0);
this->__base_cursor_cache_t::_M_size = __data_size;
}
return true;
}

constexpr _Derived& _M_derived() noexcept {
Expand Down
14 changes: 12 additions & 2 deletions include/ztd/text/detail/encoding_iterator_storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ namespace ztd { namespace text {
ZTD_TEXT_INLINE_ABI_NAMESPACE_OPEN_I_
namespace __txt_detail {

inline constexpr ::std::size_t _CursorlessSizeSentinel = 1;

template <typename _Encoding, typename _EncodingState, ::std::size_t _Id = 0>
class __state_storage : private __ebco<__remove_cvref_t<__unwrap_t<_EncodingState>>, _Id> {
private:
Expand All @@ -75,11 +77,11 @@ namespace ztd { namespace text {
::std::is_nothrow_default_constructible_v<__state_base_t>)
: __state_base_t() {
}
constexpr __state_storage(_Encoding& __encoding, const _UEncodingState& __state) noexcept(
constexpr __state_storage(_Encoding&, const _UEncodingState& __state) noexcept(
::std::is_nothrow_constructible_v<__state_base_t, const _UEncodingState&>)
: __state_base_t(__state) {
}
constexpr __state_storage(_Encoding& __encoding, _UEncodingState&& __state) noexcept(
constexpr __state_storage(_Encoding&, _UEncodingState&& __state) noexcept(
::std::is_nothrow_constructible_v<__state_base_t, _UEncodingState&&>)
: __state_base_t(::std::move(__state)) {
}
Expand Down Expand Up @@ -117,6 +119,14 @@ namespace ztd { namespace text {
_SizeType _M_position = static_cast<_SizeType>(0);
};

template <>
class __cursor_cache<1, true> {
public:
using _SizeType = unsigned char;

_SizeType _M_size = static_cast<_SizeType>(0);
};

template <>
class __cursor_cache<1, false> {
public:
Expand Down
4 changes: 3 additions & 1 deletion include/ztd/text/is_unicode_encoding.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ namespace ztd { namespace text {
else if constexpr (is_unicode_encoding_v<_Encoding>) {
return true;
}
return false;
else {
return false;
}
}

//////
Expand Down

0 comments on commit 4178b82

Please sign in to comment.