blob: 81f9cebd6f4108d4329219d4507cb5475ccd9105 [file] [log] [blame]
Austin Schuh36244a12019-09-21 17:52:38 -07001//
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
Austin Schuhb4691e92020-12-31 12:37:18 -080018#include <stddef.h>
Austin Schuh36244a12019-09-21 17:52:38 -070019
Austin Schuhb4691e92020-12-31 12:37:18 -080020#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"
Austin Schuh36244a12019-09-21 17:52:38 -070028#include "absl/base/macros.h"
Austin Schuhb4691e92020-12-31 12:37:18 -080029#include "absl/strings/ascii.h"
Austin Schuh36244a12019-09-21 17:52:38 -070030#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"
Austin Schuhb4691e92020-12-31 12:37:18 -080036#include "absl/strings/string_view.h"
Austin Schuh36244a12019-09-21 17:52:38 -070037
38namespace absl {
Austin Schuhb4691e92020-12-31 12:37:18 -080039ABSL_NAMESPACE_BEGIN
Austin Schuh36244a12019-09-21 17:52:38 -070040namespace flags_internal {
41
42// --------------------------------------------------------------------
43// AbslParseFlag specializations for boolean type.
44
45bool 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.
70static 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
76template <typename IntType>
Austin Schuhb4691e92020-12-31 12:37:18 -080077inline bool ParseFlagImpl(absl::string_view text, IntType& dst) {
Austin Schuh36244a12019-09-21 17:52:38 -070078 text = absl::StripAsciiWhitespace(text);
79
Austin Schuhb4691e92020-12-31 12:37:18 -080080 return absl::numbers_internal::safe_strtoi_base(text, &dst,
81 NumericBase(text));
Austin Schuh36244a12019-09-21 17:52:38 -070082}
83
84bool AbslParseFlag(absl::string_view text, short* dst, std::string*) {
85 int val;
Austin Schuhb4691e92020-12-31 12:37:18 -080086 if (!ParseFlagImpl(text, val)) return false;
Austin Schuh36244a12019-09-21 17:52:38 -070087 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
93bool AbslParseFlag(absl::string_view text, unsigned short* dst, std::string*) {
94 unsigned int val;
Austin Schuhb4691e92020-12-31 12:37:18 -080095 if (!ParseFlagImpl(text, val)) return false;
Austin Schuh36244a12019-09-21 17:52:38 -070096 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
103bool AbslParseFlag(absl::string_view text, int* dst, std::string*) {
Austin Schuhb4691e92020-12-31 12:37:18 -0800104 return ParseFlagImpl(text, *dst);
Austin Schuh36244a12019-09-21 17:52:38 -0700105}
106
107bool AbslParseFlag(absl::string_view text, unsigned int* dst, std::string*) {
Austin Schuhb4691e92020-12-31 12:37:18 -0800108 return ParseFlagImpl(text, *dst);
Austin Schuh36244a12019-09-21 17:52:38 -0700109}
110
111bool AbslParseFlag(absl::string_view text, long* dst, std::string*) {
Austin Schuhb4691e92020-12-31 12:37:18 -0800112 return ParseFlagImpl(text, *dst);
Austin Schuh36244a12019-09-21 17:52:38 -0700113}
114
115bool AbslParseFlag(absl::string_view text, unsigned long* dst, std::string*) {
Austin Schuhb4691e92020-12-31 12:37:18 -0800116 return ParseFlagImpl(text, *dst);
Austin Schuh36244a12019-09-21 17:52:38 -0700117}
118
119bool AbslParseFlag(absl::string_view text, long long* dst, std::string*) {
Austin Schuhb4691e92020-12-31 12:37:18 -0800120 return ParseFlagImpl(text, *dst);
Austin Schuh36244a12019-09-21 17:52:38 -0700121}
122
123bool AbslParseFlag(absl::string_view text, unsigned long long* dst,
124 std::string*) {
Austin Schuhb4691e92020-12-31 12:37:18 -0800125 return ParseFlagImpl(text, *dst);
Austin Schuh36244a12019-09-21 17:52:38 -0700126}
127
128// --------------------------------------------------------------------
129// AbslParseFlag for floating point types.
130
131bool AbslParseFlag(absl::string_view text, float* dst, std::string*) {
132 return absl::SimpleAtof(text, dst);
133}
134
135bool AbslParseFlag(absl::string_view text, double* dst, std::string*) {
136 return absl::SimpleAtod(text, dst);
137}
138
139// --------------------------------------------------------------------
140// AbslParseFlag for strings.
141
142bool 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
150bool 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
165std::string Unparse(bool v) { return v ? "true" : "false"; }
166std::string Unparse(short v) { return absl::StrCat(v); }
167std::string Unparse(unsigned short v) { return absl::StrCat(v); }
168std::string Unparse(int v) { return absl::StrCat(v); }
169std::string Unparse(unsigned int v) { return absl::StrCat(v); }
170std::string Unparse(long v) { return absl::StrCat(v); }
171std::string Unparse(unsigned long v) { return absl::StrCat(v); }
172std::string Unparse(long long v) { return absl::StrCat(v); }
173std::string Unparse(unsigned long long v) { return absl::StrCat(v); }
174template <typename T>
175std::string UnparseFloatingPointVal(T v) {
Austin Schuhb4691e92020-12-31 12:37:18 -0800176 // digits10 is guaranteed to roundtrip correctly in string -> value -> string
Austin Schuh36244a12019-09-21 17:52:38 -0700177 // 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}
193std::string Unparse(float v) { return UnparseFloatingPointVal(v); }
194std::string Unparse(double v) { return UnparseFloatingPointVal(v); }
195std::string AbslUnparseFlag(absl::string_view v) { return std::string(v); }
196std::string AbslUnparseFlag(const std::vector<std::string>& v) {
197 return absl::StrJoin(v, ",");
198}
199
200} // namespace flags_internal
201
202bool 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
235std::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
Austin Schuhb4691e92020-12-31 12:37:18 -0800240ABSL_NAMESPACE_END
Austin Schuh36244a12019-09-21 17:52:38 -0700241} // namespace absl