/Users/deen/code/yugabyte-db/src/yb/util/stol_utils.cc
Line | Count | Source |
1 | | // Copyright (c) YugaByte, Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
4 | | // in compliance with the License. You may obtain a copy of the License at |
5 | | // |
6 | | // http://www.apache.org/licenses/LICENSE-2.0 |
7 | | // |
8 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
9 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
10 | | // or implied. See the License for the specific language governing permissions and limitations |
11 | | // under the License. |
12 | | // |
13 | | |
14 | | #include "yb/util/stol_utils.h" |
15 | | |
16 | | #include <cstring> |
17 | | |
18 | | using namespace std::placeholders; |
19 | | |
20 | | namespace yb { |
21 | | |
22 | | namespace { |
23 | | |
24 | 135 | CHECKED_STATUS CreateInvalid(Slice input, int err = 0) { |
25 | 135 | auto message = Format("$0 is not a valid number", input.ToDebugString()); |
26 | 135 | if (err != 0) { |
27 | 76 | message += ": "; |
28 | 76 | message += std::strerror(err); |
29 | 76 | } |
30 | 135 | return STATUS(InvalidArgument, message); |
31 | 135 | } |
32 | | |
33 | 9.47M | CHECKED_STATUS CheckNotSpace(Slice slice) { |
34 | 9.47M | if (slice.empty() || isspace(*slice.cdata())9.47M ) { |
35 | | // disable skip of spaces. |
36 | 32 | return CreateInvalid(slice); |
37 | 32 | } |
38 | 9.47M | return Status::OK(); |
39 | 9.47M | } |
40 | | |
41 | | template <typename T, typename StrToT> |
42 | 9.48M | Result<T> CheckedSton(Slice slice, StrToT str_to_t) { |
43 | 9.48M | RETURN_NOT_OK(CheckNotSpace(slice)); |
44 | 9.48M | char* str_end; |
45 | 9.48M | errno = 0; |
46 | 9.48M | T result = str_to_t(slice.cdata(), &str_end); |
47 | | // Check errno. |
48 | 9.48M | if (errno != 0) { |
49 | 76 | return CreateInvalid(slice, errno); |
50 | 76 | } |
51 | | |
52 | | // Check that entire string was processed. |
53 | 9.48M | if (str_end != slice.cend()) { |
54 | 27 | return CreateInvalid(slice); |
55 | 27 | } |
56 | | |
57 | 9.48M | return result; |
58 | 9.48M | } stol_utils.cc:yb::Result<long long> yb::(anonymous namespace)::CheckedSton<long long, std::__1::__bind<long long (*)(char const*, char**, int), std::__1::placeholders::__ph<1> const&, std::__1::placeholders::__ph<2> const&, int> >(yb::Slice, std::__1::__bind<long long (*)(char const*, char**, int), std::__1::placeholders::__ph<1> const&, std::__1::placeholders::__ph<2> const&, int>) Line | Count | Source | 42 | 9.45M | Result<T> CheckedSton(Slice slice, StrToT str_to_t) { | 43 | 9.45M | RETURN_NOT_OK(CheckNotSpace(slice)); | 44 | 9.45M | char* str_end; | 45 | 9.45M | errno = 0; | 46 | 9.45M | T result = str_to_t(slice.cdata(), &str_end); | 47 | | // Check errno. | 48 | 9.45M | if (errno != 0) { | 49 | 60 | return CreateInvalid(slice, errno); | 50 | 60 | } | 51 | | | 52 | | // Check that entire string was processed. | 53 | 9.45M | if (str_end != slice.cend()) { | 54 | 8 | return CreateInvalid(slice); | 55 | 8 | } | 56 | | | 57 | 9.45M | return result; | 58 | 9.45M | } |
stol_utils.cc:yb::Result<long double> yb::(anonymous namespace)::CheckedSton<long double, long double (*)(char const*, char**)>(yb::Slice, long double (*)(char const*, char**)) Line | Count | Source | 42 | 23.0k | Result<T> CheckedSton(Slice slice, StrToT str_to_t) { | 43 | 23.0k | RETURN_NOT_OK(CheckNotSpace(slice)); | 44 | 23.0k | char* str_end; | 45 | 23.0k | errno = 0; | 46 | 23.0k | T result = str_to_t(slice.cdata(), &str_end); | 47 | | // Check errno. | 48 | 23.0k | if (errno != 0) { | 49 | 16 | return CreateInvalid(slice, errno); | 50 | 16 | } | 51 | | | 52 | | // Check that entire string was processed. | 53 | 23.0k | if (str_end != slice.cend()) { | 54 | 19 | return CreateInvalid(slice); | 55 | 19 | } | 56 | | | 57 | 23.0k | return result; | 58 | 23.0k | } |
|
59 | | |
60 | | } // Anonymous namespace |
61 | | |
62 | 9.46M | Result<int64_t> CheckedStoll(Slice slice) { |
63 | 9.46M | return CheckedSton<int64_t>(slice, std::bind(&std::strtoll, _1, _2, 10)); |
64 | 9.46M | } |
65 | | |
66 | 23.0k | Result<long double> CheckedStold(Slice slice) { |
67 | 23.0k | return CheckedSton<long double>(slice, std::strtold); |
68 | 23.0k | } |
69 | | |
70 | | } // namespace yb |