OR-Tools  9.6
numbers.h
Go to the documentation of this file.
1 // Copyright 2010-2022 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // 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
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 // Convert strings to numbers or numbers to strings.
15 
16 #ifndef OR_TOOLS_BASE_NUMBERS_H_
17 #define OR_TOOLS_BASE_NUMBERS_H_
18 
19 #include <functional>
20 #include <limits>
21 #include <string>
22 #include <string_view>
23 
24 #include "absl/strings/numbers.h"
25 
26 namespace strings {
27 // ----------------------------------------------------------------------
28 // ParseLeadingInt32Value
29 // A simple parser for int32_t values. Returns the parsed value
30 // if a valid integer is found; else returns deflt. It does not
31 // check if str is entirely consumed.
32 // This cannot handle decimal numbers with leading 0s, since they will be
33 // treated as octal. If you know it's decimal, use ParseLeadingDec32Value.
34 // --------------------------------------------------------------------
35 int32_t ParseLeadingInt32Value(const char* str, int32_t deflt);
36 inline int32_t ParseLeadingInt32Value(std::string_view str, int32_t deflt) {
37  return ParseLeadingInt32Value(std::string(str).c_str(), deflt);
38 }
39 
40 // ParseLeadingUInt32Value
41 // A simple parser for uint32_t values. Returns the parsed value
42 // if a valid integer is found; else returns deflt. It does not
43 // check if str is entirely consumed.
44 // This cannot handle decimal numbers with leading 0s, since they will be
45 // treated as octal. If you know it's decimal, use ParseLeadingUDec32Value.
46 // --------------------------------------------------------------------
47 uint32_t ParseLeadingUInt32Value(const char* str, uint32_t deflt);
48 inline uint32_t ParseLeadingUInt32Value(std::string_view str, uint32_t deflt) {
49  return ParseLeadingUInt32Value(std::string(str).c_str(), deflt);
50 }
51 
52 // ----------------------------------------------------------------------
53 // ParseLeadingDec32Value
54 // A simple parser for decimal int32_t values. Returns the parsed value
55 // if a valid integer is found; else returns deflt. It does not
56 // check if str is entirely consumed.
57 // The std::string passed in is treated as *10 based*.
58 // This can handle strings with leading 0s.
59 // See also: ParseLeadingDec64Value
60 // --------------------------------------------------------------------
61 int32_t ParseLeadingDec32Value(const char* str, int32_t deflt);
62 inline int32_t ParseLeadingDec32Value(std::string_view str, int32_t deflt) {
63  return ParseLeadingDec32Value(std::string(str).c_str(), deflt);
64 }
65 
66 // ParseLeadingUDec32Value
67 // A simple parser for decimal uint32_t values. Returns the parsed value
68 // if a valid integer is found; else returns deflt. It does not
69 // check if str is entirely consumed.
70 // The std::string passed in is treated as *10 based*.
71 // This can handle strings with leading 0s.
72 // See also: ParseLeadingUDec64Value
73 // --------------------------------------------------------------------
74 uint32_t ParseLeadingUDec32Value(const char* str, uint32_t deflt);
75 inline uint32_t ParseLeadingUDec32Value(std::string_view str, uint32_t deflt) {
76  return ParseLeadingUDec32Value(std::string(str).c_str(), deflt);
77 }
78 
79 // ----------------------------------------------------------------------
80 // ParseLeadingUInt64Value
81 // ParseLeadingInt64Value
82 // ParseLeadingHex64Value
83 // ParseLeadingDec64Value
84 // ParseLeadingUDec64Value
85 // A simple parser for long long values.
86 // Returns the parsed value if a
87 // valid integer is found; else returns deflt
88 // --------------------------------------------------------------------
89 uint64_t ParseLeadingUInt64Value(const char* str, uint64_t deflt);
90 inline uint64_t ParseLeadingUInt64Value(std::string_view str, uint64_t deflt) {
91  return ParseLeadingUInt64Value(std::string(str).c_str(), deflt);
92 }
93 int64_t ParseLeadingInt64Value(const char* str, int64_t deflt);
94 inline int64_t ParseLeadingInt64Value(std::string_view str, int64_t deflt) {
95  return ParseLeadingInt64Value(std::string(str).c_str(), deflt);
96 }
97 uint64_t ParseLeadingHex64Value(const char* str, uint64_t deflt);
98 inline uint64_t ParseLeadingHex64Value(std::string_view str, uint64_t deflt) {
99  return ParseLeadingHex64Value(std::string(str).c_str(), deflt);
100 }
101 int64_t ParseLeadingDec64Value(const char* str, int64_t deflt);
102 inline int64_t ParseLeadingDec64Value(std::string_view str, int64_t deflt) {
103  return ParseLeadingDec64Value(std::string(str).c_str(), deflt);
104 }
105 uint64_t ParseLeadingUDec64Value(const char* str, uint64_t deflt);
106 inline uint64_t ParseLeadingUDec64Value(std::string_view str, uint64_t deflt) {
107  return ParseLeadingUDec64Value(std::string(str).c_str(), deflt);
108 }
109 
110 // ----------------------------------------------------------------------
111 // ParseLeadingDoubleValue
112 // A simple parser for double values. Returns the parsed value
113 // if a valid double is found; else returns deflt. It does not
114 // check if str is entirely consumed.
115 // --------------------------------------------------------------------
116 double ParseLeadingDoubleValue(const char* str, double deflt);
117 inline double ParseLeadingDoubleValue(std::string_view str, double deflt) {
118  return ParseLeadingDoubleValue(std::string(str).c_str(), deflt);
119 }
120 
121 // ----------------------------------------------------------------------
122 // ParseLeadingBoolValue()
123 // A recognizer of boolean std::string values. Returns the parsed value
124 // if a valid value is found; else returns deflt. This skips leading
125 // whitespace, is case insensitive, and recognizes these forms:
126 // 0/1, false/true, no/yes, n/y
127 // --------------------------------------------------------------------
128 bool ParseLeadingBoolValue(const char* str, bool deflt);
129 inline bool ParseLeadingBoolValue(std::string_view str, bool deflt) {
130  return ParseLeadingBoolValue(std::string(str).c_str(), deflt);
131 }
132 } // namespace strings
133 
134 #endif // OR_TOOLS_BASE_NUMBERS_H_
Definition: case.cc:32
uint64_t ParseLeadingUDec64Value(const char *str, uint64_t deflt)
Definition: numbers.cc:188
uint32_t ParseLeadingUDec32Value(const char *str, uint32_t deflt)
Definition: numbers.cc:121
bool ParseLeadingBoolValue(const char *str, bool deflt)
Definition: numbers.cc:219
int64_t ParseLeadingInt64Value(const char *str, int64_t deflt)
Definition: numbers.cc:161
uint64_t ParseLeadingHex64Value(const char *str, uint64_t deflt)
Definition: numbers.cc:167
int32_t ParseLeadingInt32Value(const char *str, int32_t deflt)
Definition: numbers.cc:58
int64_t ParseLeadingDec64Value(const char *str, int64_t deflt)
Definition: numbers.cc:182
int32_t ParseLeadingDec32Value(const char *str, int32_t deflt)
Definition: numbers.cc:107
uint32_t ParseLeadingUInt32Value(const char *str, uint32_t deflt)
Definition: numbers.cc:72
double ParseLeadingDoubleValue(const char *str, double deflt)
Definition: numbers.cc:200
uint64_t ParseLeadingUInt64Value(const char *str, uint64_t deflt)
Definition: numbers.cc:155