1 //
2 // Copyright 2019 The Abseil Authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // https://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15
16 #include "absl/flags/marshalling.h"
17
18 #include <stddef.h>
19
20 #include <cmath>
21 #include <limits>
22 #include <string>
23 #include <type_traits>
24 #include <vector>
25
26 #include "absl/base/config.h"
27 #include "absl/base/log_severity.h"
28 #include "absl/base/macros.h"
29 #include "absl/strings/ascii.h"
30 #include "absl/strings/match.h"
31 #include "absl/strings/numbers.h"
32 #include "absl/strings/str_cat.h"
33 #include "absl/strings/str_format.h"
34 #include "absl/strings/str_join.h"
35 #include "absl/strings/str_split.h"
36 #include "absl/strings/string_view.h"
37
38 namespace absl {
39 ABSL_NAMESPACE_BEGIN
40 namespace flags_internal {
41
42 // --------------------------------------------------------------------
43 // AbslParseFlag specializations for boolean type.
44
AbslParseFlag(absl::string_view text,bool * dst,std::string *)45 bool AbslParseFlag(absl::string_view text, bool* dst, std::string*) {
46 const char* kTrue[] = {"1", "t", "true", "y", "yes"};
47 const char* kFalse[] = {"0", "f", "false", "n", "no"};
48 static_assert(sizeof(kTrue) == sizeof(kFalse), "true_false_equal");
49
50 text = absl::StripAsciiWhitespace(text);
51
52 for (size_t i = 0; i < ABSL_ARRAYSIZE(kTrue); ++i) {
53 if (absl::EqualsIgnoreCase(text, kTrue[i])) {
54 *dst = true;
55 return true;
56 } else if (absl::EqualsIgnoreCase(text, kFalse[i])) {
57 *dst = false;
58 return true;
59 }
60 }
61 return false; // didn't match a legal input
62 }
63
64 // --------------------------------------------------------------------
65 // AbslParseFlag for integral types.
66
67 // Return the base to use for parsing text as an integer. Leading 0x
68 // puts us in base 16. But leading 0 does not put us in base 8. It
69 // caused too many bugs when we had that behavior.
NumericBase(absl::string_view text)70 static int NumericBase(absl::string_view text) {
71 const bool hex = (text.size() >= 2 && text[0] == '0' &&
72 (text[1] == 'x' || text[1] == 'X'));
73 return hex ? 16 : 10;
74 }
75
76 template <typename IntType>
ParseFlagImpl(absl::string_view text,IntType & dst)77 inline bool ParseFlagImpl(absl::string_view text, IntType& dst) {
78 text = absl::StripAsciiWhitespace(text);
79
80 return absl::numbers_internal::safe_strtoi_base(text, &dst,
81 NumericBase(text));
82 }
83
AbslParseFlag(absl::string_view text,short * dst,std::string *)84 bool AbslParseFlag(absl::string_view text, short* dst, std::string*) {
85 int val;
86 if (!ParseFlagImpl(text, val)) return false;
87 if (static_cast<short>(val) != val) // worked, but number out of range
88 return false;
89 *dst = static_cast<short>(val);
90 return true;
91 }
92
AbslParseFlag(absl::string_view text,unsigned short * dst,std::string *)93 bool AbslParseFlag(absl::string_view text, unsigned short* dst, std::string*) {
94 unsigned int val;
95 if (!ParseFlagImpl(text, val)) return false;
96 if (static_cast<unsigned short>(val) !=
97 val) // worked, but number out of range
98 return false;
99 *dst = static_cast<unsigned short>(val);
100 return true;
101 }
102
AbslParseFlag(absl::string_view text,int * dst,std::string *)103 bool AbslParseFlag(absl::string_view text, int* dst, std::string*) {
104 return ParseFlagImpl(text, *dst);
105 }
106
AbslParseFlag(absl::string_view text,unsigned int * dst,std::string *)107 bool AbslParseFlag(absl::string_view text, unsigned int* dst, std::string*) {
108 return ParseFlagImpl(text, *dst);
109 }
110
AbslParseFlag(absl::string_view text,long * dst,std::string *)111 bool AbslParseFlag(absl::string_view text, long* dst, std::string*) {
112 return ParseFlagImpl(text, *dst);
113 }
114
AbslParseFlag(absl::string_view text,unsigned long * dst,std::string *)115 bool AbslParseFlag(absl::string_view text, unsigned long* dst, std::string*) {
116 return ParseFlagImpl(text, *dst);
117 }
118
AbslParseFlag(absl::string_view text,long long * dst,std::string *)119 bool AbslParseFlag(absl::string_view text, long long* dst, std::string*) {
120 return ParseFlagImpl(text, *dst);
121 }
122
AbslParseFlag(absl::string_view text,unsigned long long * dst,std::string *)123 bool AbslParseFlag(absl::string_view text, unsigned long long* dst,
124 std::string*) {
125 return ParseFlagImpl(text, *dst);
126 }
127
128 // --------------------------------------------------------------------
129 // AbslParseFlag for floating point types.
130
AbslParseFlag(absl::string_view text,float * dst,std::string *)131 bool AbslParseFlag(absl::string_view text, float* dst, std::string*) {
132 return absl::SimpleAtof(text, dst);
133 }
134
AbslParseFlag(absl::string_view text,double * dst,std::string *)135 bool AbslParseFlag(absl::string_view text, double* dst, std::string*) {
136 return absl::SimpleAtod(text, dst);
137 }
138
139 // --------------------------------------------------------------------
140 // AbslParseFlag for strings.
141
AbslParseFlag(absl::string_view text,std::string * dst,std::string *)142 bool AbslParseFlag(absl::string_view text, std::string* dst, std::string*) {
143 dst->assign(text.data(), text.size());
144 return true;
145 }
146
147 // --------------------------------------------------------------------
148 // AbslParseFlag for vector of strings.
149
AbslParseFlag(absl::string_view text,std::vector<std::string> * dst,std::string *)150 bool AbslParseFlag(absl::string_view text, std::vector<std::string>* dst,
151 std::string*) {
152 // An empty flag value corresponds to an empty vector, not a vector
153 // with a single, empty std::string.
154 if (text.empty()) {
155 dst->clear();
156 return true;
157 }
158 *dst = absl::StrSplit(text, ',', absl::AllowEmpty());
159 return true;
160 }
161
162 // --------------------------------------------------------------------
163 // AbslUnparseFlag specializations for various builtin flag types.
164
Unparse(bool v)165 std::string Unparse(bool v) { return v ? "true" : "false"; }
Unparse(short v)166 std::string Unparse(short v) { return absl::StrCat(v); }
Unparse(unsigned short v)167 std::string Unparse(unsigned short v) { return absl::StrCat(v); }
Unparse(int v)168 std::string Unparse(int v) { return absl::StrCat(v); }
Unparse(unsigned int v)169 std::string Unparse(unsigned int v) { return absl::StrCat(v); }
Unparse(long v)170 std::string Unparse(long v) { return absl::StrCat(v); }
Unparse(unsigned long v)171 std::string Unparse(unsigned long v) { return absl::StrCat(v); }
Unparse(long long v)172 std::string Unparse(long long v) { return absl::StrCat(v); }
Unparse(unsigned long long v)173 std::string Unparse(unsigned long long v) { return absl::StrCat(v); }
174 template <typename T>
UnparseFloatingPointVal(T v)175 std::string UnparseFloatingPointVal(T v) {
176 // digits10 is guaranteed to roundtrip correctly in string -> value -> string
177 // conversions, but may not be enough to represent all the values correctly.
178 std::string digit10_str =
179 absl::StrFormat("%.*g", std::numeric_limits<T>::digits10, v);
180 if (std::isnan(v) || std::isinf(v)) return digit10_str;
181
182 T roundtrip_val = 0;
183 std::string err;
184 if (absl::ParseFlag(digit10_str, &roundtrip_val, &err) &&
185 roundtrip_val == v) {
186 return digit10_str;
187 }
188
189 // max_digits10 is the number of base-10 digits that are necessary to uniquely
190 // represent all distinct values.
191 return absl::StrFormat("%.*g", std::numeric_limits<T>::max_digits10, v);
192 }
Unparse(float v)193 std::string Unparse(float v) { return UnparseFloatingPointVal(v); }
Unparse(double v)194 std::string Unparse(double v) { return UnparseFloatingPointVal(v); }
AbslUnparseFlag(absl::string_view v)195 std::string AbslUnparseFlag(absl::string_view v) { return std::string(v); }
AbslUnparseFlag(const std::vector<std::string> & v)196 std::string AbslUnparseFlag(const std::vector<std::string>& v) {
197 return absl::StrJoin(v, ",");
198 }
199
200 } // namespace flags_internal
201
AbslParseFlag(absl::string_view text,absl::LogSeverity * dst,std::string * err)202 bool AbslParseFlag(absl::string_view text, absl::LogSeverity* dst,
203 std::string* err) {
204 text = absl::StripAsciiWhitespace(text);
205 if (text.empty()) {
206 *err = "no value provided";
207 return false;
208 }
209 if (text.front() == 'k' || text.front() == 'K') text.remove_prefix(1);
210 if (absl::EqualsIgnoreCase(text, "info")) {
211 *dst = absl::LogSeverity::kInfo;
212 return true;
213 }
214 if (absl::EqualsIgnoreCase(text, "warning")) {
215 *dst = absl::LogSeverity::kWarning;
216 return true;
217 }
218 if (absl::EqualsIgnoreCase(text, "error")) {
219 *dst = absl::LogSeverity::kError;
220 return true;
221 }
222 if (absl::EqualsIgnoreCase(text, "fatal")) {
223 *dst = absl::LogSeverity::kFatal;
224 return true;
225 }
226 std::underlying_type<absl::LogSeverity>::type numeric_value;
227 if (absl::ParseFlag(text, &numeric_value, err)) {
228 *dst = static_cast<absl::LogSeverity>(numeric_value);
229 return true;
230 }
231 *err = "only integers and absl::LogSeverity enumerators are accepted";
232 return false;
233 }
234
AbslUnparseFlag(absl::LogSeverity v)235 std::string AbslUnparseFlag(absl::LogSeverity v) {
236 if (v == absl::NormalizeLogSeverity(v)) return absl::LogSeverityName(v);
237 return absl::UnparseFlag(static_cast<int>(v));
238 }
239
240 ABSL_NAMESPACE_END
241 } // namespace absl
242