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 06:11:17 UTC

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

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


##########
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 &&

Review Comment:
   What if `res` and `T` are signed and unsigned? Guess more careful type check is needed here.



-- 
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