/Users/deen/code/yugabyte-db/src/yb/gutil/strtoint.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2008 Google Inc. All Rights Reserved. |
2 | | // |
3 | | // The following only applies to changes made to this file as part of YugaByte development. |
4 | | // |
5 | | // Portions Copyright (c) YugaByte, Inc. |
6 | | // |
7 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
8 | | // in compliance with the License. You may obtain a copy of the License at |
9 | | // |
10 | | // http://www.apache.org/licenses/LICENSE-2.0 |
11 | | // |
12 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
13 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
14 | | // or implied. See the License for the specific language governing permissions and limitations |
15 | | // under the License. |
16 | | // |
17 | | // Architecture-neutral plug compatible replacements for strtol() friends. |
18 | | // |
19 | | // Long's have different lengths on ILP-32 and LP-64 platforms, and so overflow |
20 | | // behavior across the two varies when strtol() and similar are used to parse |
21 | | // 32-bit integers. Similar problems exist with atoi(), because although it |
22 | | // has an all-integer interface, it uses strtol() internally, and so suffers |
23 | | // from the same narrowing problems on assignments to int. |
24 | | // |
25 | | // Examples: |
26 | | // errno = 0; |
27 | | // i = strtol("3147483647", NULL, 10); |
28 | | // printf("%d, errno %d\n", i, errno); |
29 | | // // 32-bit platform: 2147483647, errno 34 |
30 | | // // 64-bit platform: -1147483649, errno 0 |
31 | | // |
32 | | // printf("%d\n", atoi("3147483647")); |
33 | | // // 32-bit platform: 2147483647 |
34 | | // // 64-bit platform: -1147483649 |
35 | | // |
36 | | // A way round this is to define local replacements for these, and use them |
37 | | // instead of the standard libc functions. |
38 | | // |
39 | | // In most 32-bit cases the replacements can be inlined away to a call to the |
40 | | // libc function. In a couple of 64-bit cases, however, adapters are required, |
41 | | // to provide the right overflow and errno behavior. |
42 | | // |
43 | | |
44 | | #ifndef BASE_STRTOINT_H_ |
45 | | #define BASE_STRTOINT_H_ |
46 | | |
47 | | #include <stdlib.h> // For strtol* functions. |
48 | | #include <string> |
49 | | using std::string; |
50 | | #include "yb/gutil/integral_types.h" |
51 | | #include "yb/gutil/macros.h" |
52 | | #include "yb/gutil/port.h" |
53 | | |
54 | | // Adapter functions for handling overflow and errno. |
55 | | int32 strto32_adapter(const char *nptr, char **endptr, int base); |
56 | | uint32 strtou32_adapter(const char *nptr, char **endptr, int base); |
57 | | |
58 | | // Conversions to a 32-bit integer can pass the call to strto[u]l on 32-bit |
59 | | // platforms, but need a little extra work on 64-bit platforms. |
60 | 1.13M | inline int32 strto32(const char *nptr, char **endptr, int base) { |
61 | 1.13M | if (sizeof(int32) == sizeof(long)) |
62 | 0 | return static_cast<int32>(strtol(nptr, endptr, base)); |
63 | 1.13M | else |
64 | 1.13M | return strto32_adapter(nptr, endptr, base); |
65 | 1.13M | } |
66 | | |
67 | 361k | inline uint32 strtou32(const char *nptr, char **endptr, int base) { |
68 | 361k | if (sizeof(uint32) == sizeof(unsigned long)) |
69 | 0 | return static_cast<uint32>(strtoul(nptr, endptr, base)); |
70 | 361k | else |
71 | 361k | return strtou32_adapter(nptr, endptr, base); |
72 | 361k | } |
73 | | |
74 | | // For now, long long is 64-bit on all the platforms we care about, so these |
75 | | // functions can simply pass the call to strto[u]ll. |
76 | 9 | inline int64 strto64(const char *nptr, char **endptr, int base) { |
77 | 9 | COMPILE_ASSERT(sizeof(int64) == sizeof(long long), |
78 | 9 | sizeof_int64_is_not_sizeof_long_long); |
79 | 9 | return strtoll(nptr, endptr, base); |
80 | 9 | } |
81 | | |
82 | 2 | inline uint64 strtou64(const char *nptr, char **endptr, int base) { |
83 | 2 | COMPILE_ASSERT(sizeof(uint64) == sizeof(unsigned long long), |
84 | 2 | sizeof_uint64_is_not_sizeof_long_long); |
85 | 2 | return strtoull(nptr, endptr, base); |
86 | 2 | } |
87 | | |
88 | | // Although it returns an int, atoi() is implemented in terms of strtol, and |
89 | | // so has differing overflow and underflow behavior. atol is the same. |
90 | 0 | inline int32 atoi32(const char *nptr) { |
91 | 0 | return strto32(nptr, NULL, 10); |
92 | 0 | } |
93 | | |
94 | 0 | inline int64 atoi64(const char *nptr) { |
95 | 0 | return strto64(nptr, NULL, 10); |
96 | 0 | } |
97 | | |
98 | | // Convenience versions of the above that take a string argument. |
99 | 0 | inline int32 atoi32(const string &s) { |
100 | 0 | return atoi32(s.c_str()); |
101 | 0 | } |
102 | | |
103 | 0 | inline int64 atoi64(const string &s) { |
104 | 0 | return atoi64(s.c_str()); |
105 | 0 | } |
106 | | |
107 | | #endif // BASE_STRTOINT_H_ |