abseil-cpp/absl/flags/marshalling.cc
Go to the documentation of this file.
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 {
40 namespace flags_internal {
41 
42 // --------------------------------------------------------------------
43 // AbslParseFlag specializations for boolean type.
44 
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 
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.
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>
77 inline bool ParseFlagImpl(absl::string_view text, IntType& dst) {
79 
81  NumericBase(text));
82 }
83 
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 
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 
104  return ParseFlagImpl(text, *dst);
105 }
106 
108  return ParseFlagImpl(text, *dst);
109 }
110 
112  return ParseFlagImpl(text, *dst);
113 }
114 
116  return ParseFlagImpl(text, *dst);
117 }
118 
120  return ParseFlagImpl(text, *dst);
121 }
122 
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 
132  return absl::SimpleAtof(text, dst);
133 }
134 
136  return absl::SimpleAtod(text, dst);
137 }
138 
139 // --------------------------------------------------------------------
140 // AbslParseFlag for strings.
141 
143  dst->assign(text.data(), text.size());
144  return true;
145 }
146 
147 // --------------------------------------------------------------------
148 // AbslParseFlag for vector of strings.
149 
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  }
159  return true;
160 }
161 
162 // --------------------------------------------------------------------
163 // AbslUnparseFlag specializations for various builtin flag types.
164 
165 std::string Unparse(bool v) { return v ? "true" : "false"; }
166 std::string Unparse(short v) { return absl::StrCat(v); }
167 std::string Unparse(unsigned short v) { return absl::StrCat(v); }
168 std::string Unparse(int v) { return absl::StrCat(v); }
169 std::string Unparse(unsigned int v) { return absl::StrCat(v); }
170 std::string Unparse(long v) { return absl::StrCat(v); }
171 std::string Unparse(unsigned long v) { return absl::StrCat(v); }
172 std::string Unparse(long long v) { return absl::StrCat(v); }
173 std::string Unparse(unsigned long long v) { return absl::StrCat(v); }
174 template <typename T>
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;
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 }
196 std::string AbslUnparseFlag(const std::vector<std::string>& v) {
197  return absl::StrJoin(v, ",");
198 }
199 
200 } // namespace flags_internal
201 
203  std::string* err) {
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")) {
212  return true;
213  }
214  if (absl::EqualsIgnoreCase(text, "warning")) {
216  return true;
217  }
218  if (absl::EqualsIgnoreCase(text, "error")) {
220  return true;
221  }
222  if (absl::EqualsIgnoreCase(text, "fatal")) {
224  return true;
225  }
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 
237  return absl::UnparseFlag(static_cast<int>(v));
238 }
239 
241 } // namespace absl
absl::StripAsciiWhitespace
ABSL_MUST_USE_RESULT absl::string_view StripAsciiWhitespace(absl::string_view str)
Definition: abseil-cpp/absl/strings/ascii.h:225
absl::StrSplit
strings_internal::Splitter< typename strings_internal::SelectDelimiter< Delimiter >::type, AllowEmpty, absl::string_view > StrSplit(strings_internal::ConvertibleToStringView text, Delimiter d)
Definition: abseil-cpp/absl/strings/str_split.h:499
absl::flags_internal::ParseFlagImpl
bool ParseFlagImpl(absl::string_view text, IntType &dst)
Definition: abseil-cpp/absl/flags/marshalling.cc:77
dst
static const char dst[]
Definition: test-fs-copyfile.c:37
absl::ParseFlag
ABSL_NAMESPACE_BEGIN bool ParseFlag(absl::string_view input, T *dst, std::string *error)
Definition: abseil-cpp/absl/flags/marshalling.h:328
absl::StrCat
std::string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: abseil-cpp/absl/strings/str_cat.cc:98
absl::StrFormat
ABSL_MUST_USE_RESULT std::string StrFormat(const FormatSpec< Args... > &format, const Args &... args)
Definition: abseil-cpp/absl/strings/str_format.h:338
tests.google.protobuf.internal.message_test.isnan
def isnan(val)
Definition: bloaty/third_party/protobuf/python/compatibility_tests/v2.5.0/tests/google/protobuf/internal/message_test.py:65
absl::SimpleAtof
ABSL_NAMESPACE_BEGIN bool SimpleAtof(absl::string_view str, float *out)
Definition: abseil-cpp/absl/strings/numbers.cc:46
absl::flags_internal::NumericBase
static int NumericBase(absl::string_view text)
Definition: abseil-cpp/absl/flags/marshalling.cc:70
absl::flags_internal::Unparse
std::string Unparse(FlagOpFn op, const void *val)
Definition: abseil-cpp/absl/flags/internal/flag.h:130
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
error_ref_leak.err
err
Definition: error_ref_leak.py:35
absl::UnparseFlag
std::string UnparseFlag(const T &v)
Definition: abseil-cpp/absl/flags/marshalling.h:342
absl::LogSeverityName
constexpr const char * LogSeverityName(absl::LogSeverity s)
Definition: abseil-cpp/absl/base/log_severity.h:89
ABSL_ARRAYSIZE
#define ABSL_ARRAYSIZE(array)
Definition: abseil-cpp/absl/base/macros.h:44
ABSL_NAMESPACE_END
#define ABSL_NAMESPACE_END
Definition: third_party/abseil-cpp/absl/base/config.h:171
T
#define T(upbtypeconst, upbtype, ctype, default_value)
absl::AbslParseFlag
bool AbslParseFlag(absl::string_view text, absl::LogSeverity *dst, std::string *err)
Definition: abseil-cpp/absl/flags/marshalling.cc:202
absl::LogSeverity::kFatal
@ kFatal
absl::LogSeverity
LogSeverity
Definition: abseil-cpp/absl/base/log_severity.h:69
ABSL_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition: third_party/abseil-cpp/absl/base/config.h:170
gen_server_registered_method_bad_client_test_body.text
def text
Definition: gen_server_registered_method_bad_client_test_body.py:50
absl::StrJoin
std::string StrJoin(Iterator start, Iterator end, absl::string_view sep, Formatter &&fmt)
Definition: abseil-cpp/absl/strings/str_join.h:239
setup.v
v
Definition: third_party/bloaty/third_party/capstone/bindings/python/setup.py:42
absl::LogSeverity::kError
@ kError
absl::NormalizeLogSeverity
constexpr absl::LogSeverity NormalizeLogSeverity(absl::LogSeverity s)
Definition: abseil-cpp/absl/base/log_severity.h:103
absl::EqualsIgnoreCase
ABSL_NAMESPACE_BEGIN bool EqualsIgnoreCase(absl::string_view piece1, absl::string_view piece2) noexcept
Definition: abseil-cpp/absl/strings/match.cc:22
absl::AbslUnparseFlag
std::string AbslUnparseFlag(absl::LogSeverity v)
Definition: abseil-cpp/absl/flags/marshalling.cc:235
absl::SimpleAtod
bool SimpleAtod(absl::string_view str, double *out)
Definition: abseil-cpp/absl/strings/numbers.cc:77
absl::flags_internal::AbslParseFlag
bool AbslParseFlag(absl::string_view text, bool *dst, std::string *)
Definition: abseil-cpp/absl/flags/marshalling.cc:45
absl::flags_internal::AbslUnparseFlag
std::string AbslUnparseFlag(absl::string_view v)
Definition: abseil-cpp/absl/flags/marshalling.cc:195
absl::chars_format::hex
@ hex
absl::numbers_internal::safe_strtoi_base
ABSL_MUST_USE_RESULT bool safe_strtoi_base(absl::string_view s, int_type *out, int base)
Definition: abseil-cpp/absl/strings/numbers.h:202
tests.google.protobuf.internal.message_test.isinf
def isinf(val)
Definition: bloaty/third_party/protobuf/python/compatibility_tests/v2.5.0/tests/google/protobuf/internal/message_test.py:68
absl::LogSeverity::kWarning
@ kWarning
absl::flags_internal::UnparseFloatingPointVal
std::string UnparseFloatingPointVal(T v)
Definition: abseil-cpp/absl/flags/marshalling.cc:175
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
absl::LogSeverity::kInfo
@ kInfo
absl::AllowEmpty
Definition: abseil-cpp/absl/strings/str_split.h:329
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:21