Skip to content

Commit

Permalink
➡ Move surrogate usage to other functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ThePhD committed Oct 27, 2021
1 parent 940f282 commit 14e8a92
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 44 deletions.
27 changes: 3 additions & 24 deletions include/ztd/text/detail/unicode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <ztd/text/version.hpp>

#include <ztd/idk/charN_t.hpp>
#include <ztd/idk/detail/unicode.h>

#include <cstddef>

Expand All @@ -45,28 +46,6 @@ namespace ztd { namespace text {
ZTD_TEXT_INLINE_ABI_NAMESPACE_OPEN_I_
namespace __txt_detail {

// codepoint related
inline constexpr char32_t __last_code_point = 0x10FFFF;

inline constexpr char32_t __first_lead_surrogate = 0xD800;
inline constexpr char32_t __last_lead_surrogate = 0xDBFF;

inline constexpr char32_t __first_trail_surrogate = 0xDC00;
inline constexpr char32_t __last_trail_surrogate = 0xDFFF;

inline constexpr char32_t __first_surrogate = __first_lead_surrogate;
inline constexpr char32_t __last_surrogate = __last_trail_surrogate;

inline constexpr bool __is_lead_surrogate(char32_t __value) noexcept {
return __value >= __first_lead_surrogate && __value <= __last_lead_surrogate;
}
inline constexpr bool __is_trail_surrogate(char32_t __value) noexcept {
return __value >= __first_trail_surrogate && __value <= __last_trail_surrogate;
}
inline constexpr bool __is_surrogate(char32_t __value) noexcept {
return __value >= __first_surrogate && __value <= __last_surrogate;
}

// utf8_t related
inline constexpr char32_t __last_1byte_value = 0x7F;
inline constexpr char32_t __last_2byte_value = 0x7FF;
Expand Down Expand Up @@ -184,8 +163,8 @@ namespace ztd { namespace text {
inline constexpr char32_t __ascii_replacement = 0x003F;

inline constexpr char32_t __utf16_combine_surrogates(char16_t __lead, char16_t __trail) noexcept {
auto __hibits = __lead - __first_lead_surrogate;
auto __lobits = __trail - __first_trail_surrogate;
auto __hibits = __lead - __ztd_idk_detail_first_lead_surrogate;
auto __lobits = __trail - __ztd_idk_detail_first_trail_surrogate;
return __normalizing_value + ((__hibits << __lead_shifted_bits) | __lobits);
}

Expand Down
6 changes: 3 additions & 3 deletions include/ztd/text/unicode_code_point.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ namespace ztd { namespace text {
//////
constexpr __unicode_code_point(char32_t __code_point) noexcept : _M_scalar(__code_point) {
#if ZTD_IS_ON(ZTD_TEXT_UNICODE_CODE_POINT_INVARIANT_ABORT_I_)
if (__txt_detail::__is_surrogate(this->_M_scalar)
|| (this->_M_scalar > __txt_detail::__last_code_point)) {
if (__ztd_idk_detail_is_surrogate(this->_M_scalar)
|| (this->_M_scalar > __ztd_idk_detail_last_unicode_code_point)) {
::std::abort();
}
#else
ZTD_TEXT_ASSERT_MESSAGE_I_("The code point value must be a valid code point.",
(this->_M_scalar <= __txt_detail::__last_code_point));
(this->_M_scalar <= __ztd_idk_detail_last_unicode_code_point));
#endif
}

Expand Down
8 changes: 4 additions & 4 deletions include/ztd/text/unicode_scalar_value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ namespace ztd { namespace text {
//////
constexpr __unicode_scalar_value(char32_t __code_point) noexcept : _M_scalar(__code_point) {
#if ZTD_IS_ON(ZTD_TEXT_UNICODE_SCALAR_VALUE_INVARIANT_ABORT_I_)
if (__txt_detail::__is_surrogate(this->_M_scalar)
|| (this->_M_scalar > __txt_detail::__last_code_point)) {
if (__ztd_idk_detail_is_surrogate(this->_M_scalar)
|| (this->_M_scalar > __ztd_idk_detail_last_unicode_code_point)) {
::std::abort();
}
#else
ZTD_TEXT_ASSERT_MESSAGE_I_(
"The code point value must be a valid code point and must not be a surrogate value.",
!__txt_detail::__is_surrogate(this->_M_scalar)
&& (this->_M_scalar <= __txt_detail::__last_code_point));
!__ztd_idk_detail_is_surrogate(this->_M_scalar)
&& (this->_M_scalar <= __ztd_idk_detail_last_unicode_code_point));
#endif
}

Expand Down
12 changes: 6 additions & 6 deletions include/ztd/text/utf16.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ namespace ztd { namespace text {
const code_unit& __lead = __units[0];
ranges::advance(__init);

if (!__txt_detail::__is_surrogate(__lead)) {
if (!__ztd_idk_detail_is_surrogate(__lead)) {
*__outit = static_cast<code_point>(__lead);
ranges::advance(::std::move(__outit));
return _Result(ranges::reconstruct(::std::in_place_type<_UInputRange>, ::std::move(__init),
Expand All @@ -191,7 +191,7 @@ namespace ztd { namespace text {
__s, encoding_error::ok);
}
if constexpr (__call_error_handler) {
if (!__txt_detail::__is_lead_surrogate(__lead)) {
if (!__ztd_idk_detail_is_lead_surrogate(__lead)) {
__self_t __self {};
return __error_handler(__self,
_Result(ranges::reconstruct(::std::in_place_type<_UInputRange>, ::std::move(__init),
Expand Down Expand Up @@ -219,7 +219,7 @@ namespace ztd { namespace text {
const code_unit& __trail = __units[1];
ranges::advance(__init);
if constexpr (__call_error_handler) {
if (!__txt_detail::__is_trail_surrogate(__trail)) {
if (!__ztd_idk_detail_is_trail_surrogate(__trail)) {
__self_t __self {};
return __error_handler(__self,
_Result(ranges::reconstruct(::std::in_place_type<_UInputRange>, ::std::move(__init),
Expand Down Expand Up @@ -305,7 +305,7 @@ namespace ztd { namespace text {
ranges::advance(__init);

if constexpr (__call_error_handler) {
if (__point > __txt_detail::__last_code_point) {
if (__point > __ztd_idk_detail_last_unicode_code_point) {
__self_t __self {};
return __error_handler(__self,
_Result(ranges::reconstruct(::std::in_place_type<_UInputRange>, ::std::move(__init),
Expand All @@ -324,10 +324,10 @@ namespace ztd { namespace text {
}
else {
auto __normal = __point - __txt_detail::__normalizing_value;
auto __lead = __txt_detail::__first_lead_surrogate
auto __lead = __ztd_idk_detail_first_lead_surrogate
+ ((__normal & __txt_detail::__lead_surrogate_bitmask)
>> __txt_detail::__lead_shifted_bits);
auto __trail = __txt_detail::__first_trail_surrogate
auto __trail = __ztd_idk_detail_first_trail_surrogate
+ (__normal & __txt_detail::__trail_surrogate_bitmask);

code_unit __lead16 = static_cast<code_unit>(static_cast<char16_t>(__lead));
Expand Down
6 changes: 3 additions & 3 deletions include/ztd/text/utf32.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ namespace ztd { namespace text {
ranges::advance(__init);

if constexpr (__validate_decodable_as && __call_error_handler) {
if (static_cast<char32_t>(__unit) > __txt_detail::__last_code_point
|| __txt_detail::__is_surrogate(static_cast<char32_t>(__unit))) {
if (static_cast<char32_t>(__unit) > __ztd_idk_detail_last_unicode_code_point
|| __ztd_idk_detail_is_surrogate(static_cast<char32_t>(__unit))) {
__self_t __self {};
return __error_handler(__self,
_Result(ranges::reconstruct(::std::in_place_type<_UInputRange>, ::std::move(__init),
Expand Down Expand Up @@ -261,7 +261,7 @@ namespace ztd { namespace text {
ranges::advance(__init);

if constexpr (__validate_decodable_as && __call_error_handler) {
if (__point > __txt_detail::__last_code_point || __txt_detail::__is_surrogate(__point)) {
if (__point > __ztd_idk_detail_last_unicode_code_point || __ztd_idk_detail_is_surrogate(__point)) {
__self_t __self {};
return __error_handler(__self,
_Result(ranges::reconstruct(::std::in_place_type<_UInputRange>, ::std::move(__init),
Expand Down
8 changes: 4 additions & 4 deletions include/ztd/text/utf8.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ namespace ztd { namespace text {
ranges::advance(__init);

if constexpr (__call_error_handler) {
if (__point > __txt_detail::__last_code_point) {
if (__point > __ztd_idk_detail_last_unicode_code_point) {
__self_t __self {};
return __error_handler(__self,
_Result(ranges::reconstruct(::std::in_place_type<_UInputRange>, ::std::move(__init),
Expand All @@ -188,7 +188,7 @@ namespace ztd { namespace text {
::ztd::span<code_unit, 0>());
}
if constexpr (!__surrogates_allowed) {
if (__txt_detail::__is_surrogate(__point)) {
if (__ztd_idk_detail_is_surrogate(__point)) {
__self_t __self {};
return __error_handler(__self,
_Result(ranges::reconstruct(::std::in_place_type<_UInputRange>,
Expand Down Expand Up @@ -472,7 +472,7 @@ namespace ztd { namespace text {
}
}
if constexpr (!__surrogates_allowed) {
if (__txt_detail::__is_surrogate(__decoded)) {
if (__ztd_idk_detail_is_surrogate(__decoded)) {
__self_t __self {};
return __error_handler(__self,
_Result(ranges::reconstruct(::std::in_place_type<_UInputRange>,
Expand All @@ -483,7 +483,7 @@ namespace ztd { namespace text {
::ztd::span<code_unit>(__units.data(), __length), ::ztd::span<code_point, 0>());
}
}
if (static_cast<char32_t>(__decoded) > __txt_detail::__last_code_point) {
if (static_cast<char32_t>(__decoded) > __ztd_idk_detail_last_unicode_code_point) {
__self_t __self {};
return __error_handler(__self,
_Result(ranges::reconstruct(::std::in_place_type<_UInputRange>, ::std::move(__init),
Expand Down

0 comments on commit 14e8a92

Please sign in to comment.