Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 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 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 18 | #include <stddef.h> |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 19 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 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" |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 28 | #include "absl/base/macros.h" |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 29 | #include "absl/strings/ascii.h" |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 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" |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 36 | #include "absl/strings/string_view.h" |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 37 | |
| 38 | namespace absl { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 39 | ABSL_NAMESPACE_BEGIN |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 40 | namespace flags_internal { |
| 41 | |
| 42 | // -------------------------------------------------------------------- |
| 43 | // AbslParseFlag specializations for boolean type. |
| 44 | |
| 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. |
| 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> |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 77 | inline bool ParseFlagImpl(absl::string_view text, IntType& dst) { |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 78 | text = absl::StripAsciiWhitespace(text); |
| 79 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 80 | return absl::numbers_internal::safe_strtoi_base(text, &dst, |
| 81 | NumericBase(text)); |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | bool AbslParseFlag(absl::string_view text, short* dst, std::string*) { |
| 85 | int val; |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 86 | if (!ParseFlagImpl(text, val)) return false; |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 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 | |
| 93 | bool AbslParseFlag(absl::string_view text, unsigned short* dst, std::string*) { |
| 94 | unsigned int val; |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 95 | if (!ParseFlagImpl(text, val)) return false; |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 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 | |
| 103 | bool AbslParseFlag(absl::string_view text, int* dst, std::string*) { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 104 | return ParseFlagImpl(text, *dst); |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | bool AbslParseFlag(absl::string_view text, unsigned int* dst, std::string*) { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 108 | return ParseFlagImpl(text, *dst); |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | bool AbslParseFlag(absl::string_view text, long* dst, std::string*) { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 112 | return ParseFlagImpl(text, *dst); |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | bool AbslParseFlag(absl::string_view text, unsigned long* dst, std::string*) { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 116 | return ParseFlagImpl(text, *dst); |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | bool AbslParseFlag(absl::string_view text, long long* dst, std::string*) { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 120 | return ParseFlagImpl(text, *dst); |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | bool AbslParseFlag(absl::string_view text, unsigned long long* dst, |
| 124 | std::string*) { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 125 | return ParseFlagImpl(text, *dst); |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | // -------------------------------------------------------------------- |
| 129 | // AbslParseFlag for floating point types. |
| 130 | |
| 131 | bool AbslParseFlag(absl::string_view text, float* dst, std::string*) { |
| 132 | return absl::SimpleAtof(text, dst); |
| 133 | } |
| 134 | |
| 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 | |
| 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 | |
| 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 | |
| 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> |
| 175 | std::string UnparseFloatingPointVal(T v) { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 176 | // digits10 is guaranteed to roundtrip correctly in string -> value -> string |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 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 | } |
| 193 | std::string Unparse(float v) { return UnparseFloatingPointVal(v); } |
| 194 | std::string Unparse(double v) { return UnparseFloatingPointVal(v); } |
| 195 | std::string AbslUnparseFlag(absl::string_view v) { return 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 | |
| 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 | |
| 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 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 240 | ABSL_NAMESPACE_END |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 241 | } // namespace absl |