You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@kvrocks.apache.org by GitBox <gi...@apache.org> on 2022/08/23 01:43:06 UTC

[GitHub] [incubator-kvrocks] caipengbo commented on a diff in pull request #787: Add newly designed `ParseInt`

caipengbo commented on code in PR #787:
URL: https://github.com/apache/incubator-kvrocks/pull/787#discussion_r952062710


##########
src/parse_util.h:
##########
@@ -0,0 +1,130 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+#pragma once
+
+#include <cstdlib>
+#include <limits>
+#include <string>
+#include <tuple>
+#include <status.h>
+
+namespace details {
+
+template <typename>
+struct ParseIntFunc;
+
+template <>
+struct ParseIntFunc<short> { // NOLINT
+  constexpr static const auto value = std::strtol;
+};
+
+template <>
+struct ParseIntFunc<int> {
+  constexpr static const auto value = std::strtol;
+};
+
+template <>
+struct ParseIntFunc<long> { // NOLINT
+  constexpr static const auto value = std::strtol;
+};
+
+template <>
+struct ParseIntFunc<long long> { // NOLINT
+  constexpr static const auto value = std::strtoll;
+};
+
+template <>
+struct ParseIntFunc<unsigned short> { // NOLINT
+  constexpr static const auto value = std::strtoul;
+};
+
+template <>
+struct ParseIntFunc<unsigned> {
+  constexpr static const auto value = std::strtoul;
+};
+
+template <>
+struct ParseIntFunc<unsigned long> { // NOLINT
+  constexpr static const auto value = std::strtoul;
+};
+
+template <>
+struct ParseIntFunc<unsigned long long> { // NOLINT
+  constexpr static const auto value = std::strtoull;
+};
+
+}  // namespace details
+
+template <typename T>
+using ParseResultAndPos = std::tuple<T, const char *>;
+
+template <typename T = long long> // NOLINT
+StatusOr<ParseResultAndPos<T>> TryParseInt(const char *v, int base = 0) {

Review Comment:
   Should we put TryParseInt under ParseInt?  Normal first, then special, which I think is logical.
   
   And write a few simple comments on TryParseInt to explain the difference between TryParseInt and ParseInt?
   It was in the test cases that I learned the difference.



##########
src/parse_util.h:
##########
@@ -0,0 +1,130 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+#pragma once
+
+#include <cstdlib>
+#include <limits>
+#include <string>
+#include <tuple>
+#include <status.h>
+
+namespace details {
+
+template <typename>
+struct ParseIntFunc;
+
+template <>
+struct ParseIntFunc<short> { // NOLINT
+  constexpr static const auto value = std::strtol;
+};
+
+template <>
+struct ParseIntFunc<int> {
+  constexpr static const auto value = std::strtol;
+};
+
+template <>
+struct ParseIntFunc<long> { // NOLINT
+  constexpr static const auto value = std::strtol;
+};
+
+template <>
+struct ParseIntFunc<long long> { // NOLINT
+  constexpr static const auto value = std::strtoll;
+};
+
+template <>
+struct ParseIntFunc<unsigned short> { // NOLINT
+  constexpr static const auto value = std::strtoul;
+};
+
+template <>
+struct ParseIntFunc<unsigned> {
+  constexpr static const auto value = std::strtoul;
+};
+
+template <>
+struct ParseIntFunc<unsigned long> { // NOLINT
+  constexpr static const auto value = std::strtoul;
+};
+
+template <>
+struct ParseIntFunc<unsigned long long> { // NOLINT
+  constexpr static const auto value = std::strtoull;
+};
+
+}  // namespace details
+
+template <typename T>
+using ParseResultAndPos = std::tuple<T, const char *>;
+
+template <typename T = long long> // NOLINT
+StatusOr<ParseResultAndPos<T>> TryParseInt(const char *v, int base = 0) {
+  char *end;
+
+  errno = 0;
+  auto res = details::ParseIntFunc<T>::value(v, &end, base);
+
+  if (v == end) {
+    return {Status::NotOK, "TryParseInt: not started as an integer"};
+  }
+
+  if (errno) {
+    return {Status::NotOK, std::string {"TryParseInt: "} + std::strerror(errno)};
+  }
+
+  if (!std::is_same<T, decltype(res)>::value &&
+    (res < std::numeric_limits<T>::min() || res > std::numeric_limits<T>::max())) {
+    return {Status::NotOK, "TryParseInt: out of range of integer type"};

Review Comment:
   Personally, I don't think you need to prefix the function name in the Status information returned, which is probably a little redundant. 
   This is what the caller needs to add, he knows what function he is using.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org