marshalling.cc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. #include "absl/flags/marshalling.h"
  16. #include <stddef.h>
  17. #include <cmath>
  18. #include <limits>
  19. #include <string>
  20. #include <type_traits>
  21. #include <vector>
  22. #include "absl/base/config.h"
  23. #include "absl/base/log_severity.h"
  24. #include "absl/base/macros.h"
  25. #include "absl/strings/ascii.h"
  26. #include "absl/strings/match.h"
  27. #include "absl/strings/numbers.h"
  28. #include "absl/strings/str_cat.h"
  29. #include "absl/strings/str_format.h"
  30. #include "absl/strings/str_join.h"
  31. #include "absl/strings/str_split.h"
  32. #include "absl/strings/string_view.h"
  33. namespace absl {
  34. ABSL_NAMESPACE_BEGIN
  35. namespace flags_internal {
  36. // --------------------------------------------------------------------
  37. // AbslParseFlag specializations for boolean type.
  38. bool AbslParseFlag(absl::string_view text, bool* dst, std::string*) {
  39. const char* kTrue[] = {"1", "t", "true", "y", "yes"};
  40. const char* kFalse[] = {"0", "f", "false", "n", "no"};
  41. static_assert(sizeof(kTrue) == sizeof(kFalse), "true_false_equal");
  42. text = absl::StripAsciiWhitespace(text);
  43. for (size_t i = 0; i < ABSL_ARRAYSIZE(kTrue); ++i) {
  44. if (absl::EqualsIgnoreCase(text, kTrue[i])) {
  45. *dst = true;
  46. return true;
  47. } else if (absl::EqualsIgnoreCase(text, kFalse[i])) {
  48. *dst = false;
  49. return true;
  50. }
  51. }
  52. return false; // didn't match a legal input
  53. }
  54. // --------------------------------------------------------------------
  55. // AbslParseFlag for integral types.
  56. // Return the base to use for parsing text as an integer. Leading 0x
  57. // puts us in base 16. But leading 0 does not put us in base 8. It
  58. // caused too many bugs when we had that behavior.
  59. static int NumericBase(absl::string_view text) {
  60. const bool hex = (text.size() >= 2 && text[0] == '0' &&
  61. (text[1] == 'x' || text[1] == 'X'));
  62. return hex ? 16 : 10;
  63. }
  64. template <typename IntType>
  65. inline bool ParseFlagImpl(absl::string_view text, IntType& dst) {
  66. text = absl::StripAsciiWhitespace(text);
  67. return absl::numbers_internal::safe_strtoi_base(text, &dst,
  68. NumericBase(text));
  69. }
  70. bool AbslParseFlag(absl::string_view text, short* dst, std::string*) {
  71. int val;
  72. if (!ParseFlagImpl(text, val)) return false;
  73. if (static_cast<short>(val) != val) // worked, but number out of range
  74. return false;
  75. *dst = static_cast<short>(val);
  76. return true;
  77. }
  78. bool AbslParseFlag(absl::string_view text, unsigned short* dst, std::string*) {
  79. unsigned int val;
  80. if (!ParseFlagImpl(text, val)) return false;
  81. if (static_cast<unsigned short>(val) !=
  82. val) // worked, but number out of range
  83. return false;
  84. *dst = static_cast<unsigned short>(val);
  85. return true;
  86. }
  87. bool AbslParseFlag(absl::string_view text, int* dst, std::string*) {
  88. return ParseFlagImpl(text, *dst);
  89. }
  90. bool AbslParseFlag(absl::string_view text, unsigned int* dst, std::string*) {
  91. return ParseFlagImpl(text, *dst);
  92. }
  93. bool AbslParseFlag(absl::string_view text, long* dst, std::string*) {
  94. return ParseFlagImpl(text, *dst);
  95. }
  96. bool AbslParseFlag(absl::string_view text, unsigned long* dst, std::string*) {
  97. return ParseFlagImpl(text, *dst);
  98. }
  99. bool AbslParseFlag(absl::string_view text, long long* dst, std::string*) {
  100. return ParseFlagImpl(text, *dst);
  101. }
  102. bool AbslParseFlag(absl::string_view text, unsigned long long* dst,
  103. std::string*) {
  104. return ParseFlagImpl(text, *dst);
  105. }
  106. // --------------------------------------------------------------------
  107. // AbslParseFlag for floating point types.
  108. bool AbslParseFlag(absl::string_view text, float* dst, std::string*) {
  109. return absl::SimpleAtof(text, dst);
  110. }
  111. bool AbslParseFlag(absl::string_view text, double* dst, std::string*) {
  112. return absl::SimpleAtod(text, dst);
  113. }
  114. // --------------------------------------------------------------------
  115. // AbslParseFlag for strings.
  116. bool AbslParseFlag(absl::string_view text, std::string* dst, std::string*) {
  117. dst->assign(text.data(), text.size());
  118. return true;
  119. }
  120. // --------------------------------------------------------------------
  121. // AbslParseFlag for vector of strings.
  122. bool AbslParseFlag(absl::string_view text, std::vector<std::string>* dst,
  123. std::string*) {
  124. // An empty flag value corresponds to an empty vector, not a vector
  125. // with a single, empty std::string.
  126. if (text.empty()) {
  127. dst->clear();
  128. return true;
  129. }
  130. *dst = absl::StrSplit(text, ',', absl::AllowEmpty());
  131. return true;
  132. }
  133. // --------------------------------------------------------------------
  134. // AbslUnparseFlag specializations for various builtin flag types.
  135. std::string Unparse(bool v) { return v ? "true" : "false"; }
  136. std::string Unparse(short v) { return absl::StrCat(v); }
  137. std::string Unparse(unsigned short v) { return absl::StrCat(v); }
  138. std::string Unparse(int v) { return absl::StrCat(v); }
  139. std::string Unparse(unsigned int v) { return absl::StrCat(v); }
  140. std::string Unparse(long v) { return absl::StrCat(v); }
  141. std::string Unparse(unsigned long v) { return absl::StrCat(v); }
  142. std::string Unparse(long long v) { return absl::StrCat(v); }
  143. std::string Unparse(unsigned long long v) { return absl::StrCat(v); }
  144. template <typename T>
  145. std::string UnparseFloatingPointVal(T v) {
  146. // digits10 is guaranteed to roundtrip correctly in string -> value -> string
  147. // conversions, but may not be enough to represent all the values correctly.
  148. std::string digit10_str =
  149. absl::StrFormat("%.*g", std::numeric_limits<T>::digits10, v);
  150. if (std::isnan(v) || std::isinf(v)) return digit10_str;
  151. T roundtrip_val = 0;
  152. std::string err;
  153. if (absl::ParseFlag(digit10_str, &roundtrip_val, &err) &&
  154. roundtrip_val == v) {
  155. return digit10_str;
  156. }
  157. // max_digits10 is the number of base-10 digits that are necessary to uniquely
  158. // represent all distinct values.
  159. return absl::StrFormat("%.*g", std::numeric_limits<T>::max_digits10, v);
  160. }
  161. std::string Unparse(float v) { return UnparseFloatingPointVal(v); }
  162. std::string Unparse(double v) { return UnparseFloatingPointVal(v); }
  163. std::string AbslUnparseFlag(absl::string_view v) { return std::string(v); }
  164. std::string AbslUnparseFlag(const std::vector<std::string>& v) {
  165. return absl::StrJoin(v, ",");
  166. }
  167. } // namespace flags_internal
  168. bool AbslParseFlag(absl::string_view text, absl::LogSeverity* dst,
  169. std::string* err) {
  170. text = absl::StripAsciiWhitespace(text);
  171. if (text.empty()) {
  172. *err = "no value provided";
  173. return false;
  174. }
  175. if (text.front() == 'k' || text.front() == 'K') text.remove_prefix(1);
  176. if (absl::EqualsIgnoreCase(text, "info")) {
  177. *dst = absl::LogSeverity::kInfo;
  178. return true;
  179. }
  180. if (absl::EqualsIgnoreCase(text, "warning")) {
  181. *dst = absl::LogSeverity::kWarning;
  182. return true;
  183. }
  184. if (absl::EqualsIgnoreCase(text, "error")) {
  185. *dst = absl::LogSeverity::kError;
  186. return true;
  187. }
  188. if (absl::EqualsIgnoreCase(text, "fatal")) {
  189. *dst = absl::LogSeverity::kFatal;
  190. return true;
  191. }
  192. std::underlying_type<absl::LogSeverity>::type numeric_value;
  193. if (absl::ParseFlag(text, &numeric_value, err)) {
  194. *dst = static_cast<absl::LogSeverity>(numeric_value);
  195. return true;
  196. }
  197. *err = "only integers and absl::LogSeverity enumerators are accepted";
  198. return false;
  199. }
  200. std::string AbslUnparseFlag(absl::LogSeverity v) {
  201. if (v == absl::NormalizeLogSeverity(v)) return absl::LogSeverityName(v);
  202. return absl::UnparseFlag(static_cast<int>(v));
  203. }
  204. ABSL_NAMESPACE_END
  205. } // namespace absl