YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/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
127
CHECKED_STATUS CreateInvalid(Slice input, int err = 0) {
25
127
  auto message = Format("$0 is not a valid number", input.ToDebugString());
26
127
  if (err != 0) {
27
68
    message += ": ";
28
68
    message += std::strerror(err);
29
68
  }
30
127
  return STATUS(InvalidArgument, message);
31
127
}
32
33
4.88M
CHECKED_STATUS CheckNotSpace(Slice slice) {
34
4.88M
  if (slice.empty() || isspace(*slice.cdata())) {
35
    // disable skip of spaces.
36
32
    return CreateInvalid(slice);
37
32
  }
38
4.88M
  return Status::OK();
39
4.88M
}
40
41
template <typename T, typename StrToT>
42
4.88M
Result<T> CheckedSton(Slice slice, StrToT str_to_t) {
43
4.88M
  RETURN_NOT_OK(CheckNotSpace(slice));
44
4.88M
  char* str_end;
45
4.88M
  errno = 0;
46
4.88M
  T result = str_to_t(slice.cdata(), &str_end);
47
  // Check errno.
48
4.88M
  if (errno != 0) {
49
68
    return CreateInvalid(slice, errno);
50
68
  }
51
52
  // Check that entire string was processed.
53
4.88M
  if (str_end != slice.cend()) {
54
27
    return CreateInvalid(slice);
55
27
  }
56
57
4.88M
  return result;
58
4.88M
}
stol_utils.cc:_ZN2yb12_GLOBAL__N_111CheckedStonIxNSt3__16__bindIPFxPKcPPciEJRKNS2_12placeholders4__phILi1EEERKNSB_ILi2EEEiEEEEENS_6ResultIT_EENS_5SliceET0_
Line
Count
Source
42
4.86M
Result<T> CheckedSton(Slice slice, StrToT str_to_t) {
43
4.86M
  RETURN_NOT_OK(CheckNotSpace(slice));
44
4.86M
  char* str_end;
45
4.86M
  errno = 0;
46
4.86M
  T result = str_to_t(slice.cdata(), &str_end);
47
  // Check errno.
48
4.86M
  if (errno != 0) {
49
52
    return CreateInvalid(slice, errno);
50
52
  }
51
52
  // Check that entire string was processed.
53
4.86M
  if (str_end != slice.cend()) {
54
8
    return CreateInvalid(slice);
55
8
  }
56
57
4.86M
  return result;
58
4.86M
}
stol_utils.cc:_ZN2yb12_GLOBAL__N_111CheckedStonIePFePKcPPcEEENS_6ResultIT_EENS_5SliceET0_
Line
Count
Source
42
12.3k
Result<T> CheckedSton(Slice slice, StrToT str_to_t) {
43
12.3k
  RETURN_NOT_OK(CheckNotSpace(slice));
44
12.3k
  char* str_end;
45
12.3k
  errno = 0;
46
12.3k
  T result = str_to_t(slice.cdata(), &str_end);
47
  // Check errno.
48
12.3k
  if (errno != 0) {
49
16
    return CreateInvalid(slice, errno);
50
16
  }
51
52
  // Check that entire string was processed.
53
12.3k
  if (str_end != slice.cend()) {
54
19
    return CreateInvalid(slice);
55
19
  }
56
57
12.3k
  return result;
58
12.3k
}
59
60
} // Anonymous namespace
61
62
4.87M
Result<int64_t> CheckedStoll(Slice slice) {
63
4.87M
  return CheckedSton<int64_t>(slice, std::bind(&std::strtoll, _1, _2, 10));
64
4.87M
}
65
66
12.3k
Result<long double> CheckedStold(Slice slice) {
67
12.3k
  return CheckedSton<long double>(slice, std::strtold);
68
12.3k
}
69
70
} // namespace yb