From b6f29f4c1b9fac9e46e6be049642d83f6f4c5f9a Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Mon, 26 Jul 2021 14:50:47 +0800 Subject: [PATCH] common/strtol: expose strict_si_cast(std::string_view, ..) this variant is better than strict_si_cast(const char*), because: * we can just pass std::string to it, as std::string_view can be constructed from a std::string implicitly * strict_si_cast(std::string_view, ..) is the underlying implementation of strict_si_cast(const char*,..), so less indirection helps with the readability. Signed-off-by: Kefu Chai --- src/common/options/rbd.yaml.in | 2 +- src/common/strtol.cc | 12 ------------ src/common/strtol.h | 2 +- 3 files changed, 2 insertions(+), 14 deletions(-) diff --git a/src/common/options/rbd.yaml.in b/src/common/options/rbd.yaml.in index e99cab3d2cf27..e8f5d0d2555a6 100644 --- a/src/common/options/rbd.yaml.in +++ b/src/common/options/rbd.yaml.in @@ -310,7 +310,7 @@ options: max: 32_M validator: | [](std::string *value, std::string *error_message) { - uint64_t f = strict_si_cast(value->c_str(), error_message); + uint64_t f = strict_si_cast(*value, error_message); if (!error_message->empty()) { return -EINVAL; } else if (!isp2(f)) { diff --git a/src/common/strtol.cc b/src/common/strtol.cc index ad011b2097dcc..bba31f2a58767 100644 --- a/src/common/strtol.cc +++ b/src/common/strtol.cc @@ -307,15 +307,3 @@ uint64_t strict_sistrtoll(const char *str, std::string *err) { return strict_si_cast(str, err); } - -template -T strict_si_cast(const char *str, std::string *err) -{ - return strict_si_cast(std::string_view(str), err); -} - -template int strict_si_cast(const char *str, std::string *err); -template long strict_si_cast(const char *str, std::string *err); -template long long strict_si_cast(const char *str, std::string *err); -template uint64_t strict_si_cast(const char *str, std::string *err); -template uint32_t strict_si_cast(const char *str, std::string *err); diff --git a/src/common/strtol.h b/src/common/strtol.h index 6af506a6ddcd1..91d2fac9f075f 100644 --- a/src/common/strtol.h +++ b/src/common/strtol.h @@ -89,7 +89,7 @@ T strict_iec_cast(std::string_view str, std::string *err); uint64_t strict_sistrtoll(const char *str, std::string *err); template -T strict_si_cast(const char *str, std::string *err); +T strict_si_cast(std::string_view str, std::string *err); /* On enter buf points to the end of the buffer, e.g. where the least * significant digit of the input number will be printed. Returns pointer to -- 2.39.5