From: Kefu Chai Date: Mon, 7 Jun 2021 09:18:19 +0000 (+0800) Subject: common/strtol: do not check for existence of X-Git-Tag: v17.1.0~1694^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=refs%2Fpull%2F41729%2Fhead;p=ceph.git common/strtol: do not check for existence of is available since GCC-8, see https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.2017 > Elementary string conversions P0067R5 11.1 (integral types supported since 8.1) __has_include(), __cpp_lib_to_chars >= 201611 since we always have the access to GCC-8.1 and up, there is no need to detect the existence of anymore. also, because GCC-11 introduced the support of float types support, update the comment to reflect the change. Signed-off-by: Kefu Chai --- diff --git a/src/common/strtol.h b/src/common/strtol.h index 5ebfc2a46b2..1e243c33c3e 100644 --- a/src/common/strtol.h +++ b/src/common/strtol.h @@ -15,9 +15,7 @@ #ifndef CEPH_COMMON_STRTOL_H #define CEPH_COMMON_STRTOL_H -#if __has_include() #include -#endif // __has_include() #include #include #include @@ -28,7 +26,6 @@ namespace ceph { -#if __has_include() // Wrappers around std::from_chars. // // Why do we want this instead of strtol and friends? Because the @@ -38,6 +35,7 @@ namespace ceph { // // Returns the found number on success. Returns an empty optional on // failure OR on trailing characters. +// Sadly GCC < 11 is missing the floating point versions. template auto parse(std::string_view s, int base = 10) -> std::enable_if_t, std::optional> @@ -69,67 +67,6 @@ auto consume(std::string_view& s, int base = 10) } return t; } -// Sadly GCC is missing the floating point versions. -#else // __has_include() -template -auto parse(std::string_view sv, int base = 10) - -> std::enable_if_t, std::optional> -{ - std::string s(sv); - char* end = nullptr; - std::conditional_t, std::intmax_t, std::uintmax_t> v; - errno = 0; - - if (s.size() > 0 && std::isspace(s[0])) - return std::nullopt; - - if constexpr (std::is_signed_v) { - v = std::strtoimax(s.data(), &end, base); - } else { - if (s.size() > 0 && s[0] == '-') - return std::nullopt; - v = std::strtoumax(s.data(), &end, base); - } - if (errno != 0 || - end != s.data() + s.size() || - v > std::numeric_limits::max() || - v < std::numeric_limits::min()) - return std::nullopt; - return static_cast(v); -} - -template -auto consume(std::string_view& sv, int base = 10) - -> std::enable_if_t, std::optional> -{ - std::string s(sv); - char* end = nullptr; - std::conditional_t, std::intmax_t, std::uintmax_t> v; - errno = 0; - - if (s.size() > 0 && std::isspace(s[0])) - return std::nullopt; - - if constexpr (std::is_signed_v) { - v = std::strtoimax(s.data(), &end, base); - } else { - if (s.size() > 0 && s[0] == '-') - return std::nullopt; - v = std::strtoumax(s.data(), &end, base); - } - if (errno != 0 || - end == s.data() || - v > std::numeric_limits::max() || - v < std::numeric_limits::min()) - return std::nullopt; - if (end == s.data() + s.size()) { - sv = std::string_view{}; - } else { - sv.remove_prefix(end - s.data()); - } - return static_cast(v); -} -#endif // __has_include() } // namespace ceph bool strict_strtob(const char* str, std::string *err);