]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common/strtol: do not check for existence of <charconv> 41729/head
authorKefu Chai <kchai@redhat.com>
Mon, 7 Jun 2021 09:18:19 +0000 (17:18 +0800)
committerKefu Chai <kchai@redhat.com>
Wed, 9 Jun 2021 15:26:00 +0000 (23:26 +0800)
<charconv> 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(<charconv>), __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 <charconv> anymore.

also, because GCC-11 introduced the support of float types support,
update the comment to reflect the change.

Signed-off-by: Kefu Chai <kchai@redhat.com>
src/common/strtol.h

index 5ebfc2a46b2b0068deabc7b441d246018fbcd9c0..1e243c33c3e851908c39550d2455441d6b7842ba 100644 (file)
@@ -15,9 +15,7 @@
 #ifndef CEPH_COMMON_STRTOL_H
 #define CEPH_COMMON_STRTOL_H
 
-#if __has_include(<charconv>)
 #include <charconv>
-#endif // __has_include(<charconv>)
 #include <cinttypes>
 #include <cstdlib>
 #include <optional>
@@ -28,7 +26,6 @@
 
 
 namespace ceph {
-#if __has_include(<charconv>)
 // 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<typename T>
 auto parse(std::string_view s, int base = 10)
   -> std::enable_if_t<std::is_integral_v<T>, std::optional<T>>
@@ -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(<charconv>)
-template<typename T>
-auto parse(std::string_view sv, int base = 10)
-  -> std::enable_if_t<std::is_integral_v<T>, std::optional<T>>
-{
-  std::string s(sv);
-  char* end = nullptr;
-  std::conditional_t<std::is_signed_v<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<T>) {
-    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<T>::max() ||
-      v < std::numeric_limits<T>::min())
-    return std::nullopt;
-  return static_cast<T>(v);
-}
-
-template<typename T>
-auto consume(std::string_view& sv, int base = 10)
-  -> std::enable_if_t<std::is_integral_v<T>, std::optional<T>>
-{
-  std::string s(sv);
-  char* end = nullptr;
-  std::conditional_t<std::is_signed_v<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<T>) {
-    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<T>::max() ||
-      v < std::numeric_limits<T>::min())
-    return std::nullopt;
-  if (end == s.data() + s.size()) {
-    sv = std::string_view{};
-  } else {
-    sv.remove_prefix(end - s.data());
-  }
-  return static_cast<T>(v);
-}
-#endif // __has_include(<charconv>)
 } // namespace ceph
 
 bool strict_strtob(const char* str, std::string *err);