marshalling.cc 9.2 KB

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