From b72b9fd66a55320d38cf508366eeb40a40206651 Mon Sep 17 00:00:00 2001 From: LIU Hao Date: Mon, 6 Sep 2021 00:23:23 +0800 Subject: [PATCH] library/numeric: Cleanup --- asteria/src/library/numeric.cpp | 64 +++++++++++++++++++++++---------- 1 file changed, 46 insertions(+), 18 deletions(-) diff --git a/asteria/src/library/numeric.cpp b/asteria/src/library/numeric.cpp index bf133af22..20cb7100e 100644 --- a/asteria/src/library/numeric.cpp +++ b/asteria/src/library/numeric.cpp @@ -83,39 +83,65 @@ do_append_exponent(V_string& text, ::rocket::ascii_numput& nump, char delim, int return text; } -constexpr auto s_spaces = sref(" \f\n\r\t\v"); - -ROCKET_CONST inline int64_t -bswap_be(int64_t value) noexcept - { return static_cast(be64toh(static_cast(value))); } +ROCKET_CONST inline int16_t +bswap_be(int16_t value) noexcept + { + auto& buf = reinterpret_cast(value); + buf = be16toh(buf); + return value; + } ROCKET_CONST inline int32_t bswap_be(int32_t value) noexcept - { return static_cast(be32toh(static_cast(value))); } + { + auto& buf = reinterpret_cast(value); + buf = be32toh(buf); + return value; + } -ROCKET_CONST inline int16_t -bswap_be(int16_t value) noexcept - { return static_cast(be16toh(static_cast(value))); } +ROCKET_CONST inline int64_t +bswap_be(int64_t value) noexcept + { + auto& buf = reinterpret_cast(value); + buf = be64toh(buf); + return value; + } ROCKET_CONST inline int8_t bswap_be(int8_t value) noexcept - { return value; } + { + return value; + } -ROCKET_CONST inline int64_t -bswap_le(int64_t value) noexcept - { return static_cast(le64toh(static_cast(value))); } +ROCKET_CONST inline int16_t +bswap_le(int16_t value) noexcept + { + auto& buf = reinterpret_cast(value); + buf = le16toh(buf); + return value; + } ROCKET_CONST inline int32_t bswap_le(int32_t value) noexcept - { return static_cast(le32toh(static_cast(value))); } + { + auto& buf = reinterpret_cast(value); + buf = le32toh(buf); + return value; + } -ROCKET_CONST inline int16_t -bswap_le(int16_t value) noexcept - { return static_cast(le16toh(static_cast(value))); } +ROCKET_CONST inline int64_t +bswap_le(int64_t value) noexcept + { + auto& buf = reinterpret_cast(value); + buf = le64toh(buf); + return value; + } ROCKET_CONST inline int8_t bswap_le(int8_t value) noexcept - { return value; } + { + return value; + } template V_string @@ -656,6 +682,7 @@ std_numeric_format(V_real value, optV_integer base, optV_integer ebase) V_integer std_numeric_parse_integer(V_string text) { + static constexpr char s_spaces[] = " \f\n\r\t\v"; auto tpos = text.find_first_not_of(s_spaces); if(tpos == V_string::npos) ASTERIA_THROW_RUNTIME_ERROR("blank string"); @@ -683,6 +710,7 @@ std_numeric_parse_integer(V_string text) V_real std_numeric_parse_real(V_string text, optV_boolean saturating) { + static constexpr char s_spaces[] = " \f\n\r\t\v"; auto tpos = text.find_first_not_of(s_spaces); if(tpos == V_string::npos) ASTERIA_THROW_RUNTIME_ERROR("blank string");