Format.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. * Copyright 2012-present Facebook, Inc.
  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. * http://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 <folly/Format.h>
  17. #include <folly/ConstexprMath.h>
  18. #include <folly/CppAttributes.h>
  19. #include <folly/container/Array.h>
  20. #include <double-conversion/double-conversion.h>
  21. namespace folly {
  22. namespace detail {
  23. // ctor for items in the align table
  24. struct format_table_align_make_item {
  25. static constexpr std::size_t size = 256;
  26. constexpr FormatArg::Align operator()(std::size_t index) const {
  27. // clang-format off
  28. return
  29. index == '<' ? FormatArg::Align::LEFT:
  30. index == '>' ? FormatArg::Align::RIGHT :
  31. index == '=' ? FormatArg::Align::PAD_AFTER_SIGN :
  32. index == '^' ? FormatArg::Align::CENTER :
  33. FormatArg::Align::INVALID;
  34. // clang-format on
  35. }
  36. };
  37. // ctor for items in the conv tables for representing parts of nonnegative
  38. // integers into ascii digits of length Size, over a given base Base
  39. template <std::size_t Base, std::size_t Size, bool Upper = false>
  40. struct format_table_conv_make_item {
  41. static_assert(Base <= 36, "Base is unrepresentable");
  42. struct make_item {
  43. std::size_t index{};
  44. constexpr explicit make_item(std::size_t index_) : index(index_) {} // gcc49
  45. constexpr char alpha(std::size_t ord) const {
  46. return ord < 10 ? '0' + ord : (Upper ? 'A' : 'a') + (ord - 10);
  47. }
  48. constexpr char operator()(std::size_t offset) const {
  49. return alpha(index / constexpr_pow(Base, Size - offset - 1) % Base);
  50. }
  51. };
  52. constexpr std::array<char, Size> operator()(std::size_t index) const {
  53. return make_array_with<Size>(make_item{index});
  54. }
  55. };
  56. // ctor for items in the sign table
  57. struct format_table_sign_make_item {
  58. static constexpr std::size_t size = 256;
  59. constexpr FormatArg::Sign operator()(std::size_t index) const {
  60. // clang-format off
  61. return
  62. index == '+' ? FormatArg::Sign::PLUS_OR_MINUS :
  63. index == '-' ? FormatArg::Sign::MINUS :
  64. index == ' ' ? FormatArg::Sign::SPACE_OR_MINUS :
  65. FormatArg::Sign::INVALID;
  66. // clang-format on
  67. }
  68. };
  69. // the tables
  70. FOLLY_STORAGE_CONSTEXPR auto formatAlignTable =
  71. make_array_with<256>(format_table_align_make_item{});
  72. FOLLY_STORAGE_CONSTEXPR auto formatSignTable =
  73. make_array_with<256>(format_table_sign_make_item{});
  74. FOLLY_STORAGE_CONSTEXPR decltype(formatHexLower) formatHexLower =
  75. make_array_with<256>(format_table_conv_make_item<16, 2, false>{});
  76. FOLLY_STORAGE_CONSTEXPR decltype(formatHexUpper) formatHexUpper =
  77. make_array_with<256>(format_table_conv_make_item<16, 2, true>{});
  78. FOLLY_STORAGE_CONSTEXPR decltype(formatOctal) formatOctal =
  79. make_array_with<512>(format_table_conv_make_item<8, 3>{});
  80. FOLLY_STORAGE_CONSTEXPR decltype(formatBinary) formatBinary =
  81. make_array_with<256>(format_table_conv_make_item<2, 8>{});
  82. } // namespace detail
  83. using namespace folly::detail;
  84. void FormatValue<double>::formatHelper(
  85. fbstring& piece,
  86. int& prefixLen,
  87. FormatArg& arg) const {
  88. using ::double_conversion::DoubleToStringConverter;
  89. using ::double_conversion::StringBuilder;
  90. arg.validate(FormatArg::Type::FLOAT);
  91. if (arg.presentation == FormatArg::kDefaultPresentation) {
  92. arg.presentation = 'g';
  93. }
  94. const char* infinitySymbol = isupper(arg.presentation) ? "INF" : "inf";
  95. const char* nanSymbol = isupper(arg.presentation) ? "NAN" : "nan";
  96. char exponentSymbol = isupper(arg.presentation) ? 'E' : 'e';
  97. if (arg.precision == FormatArg::kDefaultPrecision) {
  98. arg.precision = 6;
  99. }
  100. // 2+: for null terminator and optional sign shenanigans.
  101. constexpr int bufLen = 2 +
  102. constexpr_max(2 + DoubleToStringConverter::kMaxFixedDigitsBeforePoint +
  103. DoubleToStringConverter::kMaxFixedDigitsAfterPoint,
  104. constexpr_max(
  105. 8 + DoubleToStringConverter::kMaxExponentialDigits,
  106. 7 + DoubleToStringConverter::kMaxPrecisionDigits));
  107. char buf[bufLen];
  108. StringBuilder builder(buf + 1, bufLen - 1);
  109. char plusSign;
  110. switch (arg.sign) {
  111. case FormatArg::Sign::PLUS_OR_MINUS:
  112. plusSign = '+';
  113. break;
  114. case FormatArg::Sign::SPACE_OR_MINUS:
  115. plusSign = ' ';
  116. break;
  117. default:
  118. plusSign = '\0';
  119. break;
  120. };
  121. auto flags = DoubleToStringConverter::EMIT_POSITIVE_EXPONENT_SIGN |
  122. (arg.trailingDot ? DoubleToStringConverter::EMIT_TRAILING_DECIMAL_POINT
  123. : 0);
  124. double val = val_;
  125. switch (arg.presentation) {
  126. case '%':
  127. val *= 100;
  128. FOLLY_FALLTHROUGH;
  129. case 'f':
  130. case 'F': {
  131. if (arg.precision > DoubleToStringConverter::kMaxFixedDigitsAfterPoint) {
  132. arg.precision = DoubleToStringConverter::kMaxFixedDigitsAfterPoint;
  133. }
  134. DoubleToStringConverter conv(
  135. flags,
  136. infinitySymbol,
  137. nanSymbol,
  138. exponentSymbol,
  139. -4,
  140. arg.precision,
  141. 0,
  142. 0);
  143. arg.enforce(
  144. conv.ToFixed(val, arg.precision, &builder),
  145. "fixed double conversion failed");
  146. break;
  147. }
  148. case 'e':
  149. case 'E': {
  150. if (arg.precision > DoubleToStringConverter::kMaxExponentialDigits) {
  151. arg.precision = DoubleToStringConverter::kMaxExponentialDigits;
  152. }
  153. DoubleToStringConverter conv(
  154. flags,
  155. infinitySymbol,
  156. nanSymbol,
  157. exponentSymbol,
  158. -4,
  159. arg.precision,
  160. 0,
  161. 0);
  162. arg.enforce(conv.ToExponential(val, arg.precision, &builder));
  163. break;
  164. }
  165. case 'n': // should be locale-aware, but isn't
  166. case 'g':
  167. case 'G': {
  168. if (arg.precision < DoubleToStringConverter::kMinPrecisionDigits) {
  169. arg.precision = DoubleToStringConverter::kMinPrecisionDigits;
  170. } else if (arg.precision > DoubleToStringConverter::kMaxPrecisionDigits) {
  171. arg.precision = DoubleToStringConverter::kMaxPrecisionDigits;
  172. }
  173. DoubleToStringConverter conv(
  174. flags,
  175. infinitySymbol,
  176. nanSymbol,
  177. exponentSymbol,
  178. -4,
  179. arg.precision,
  180. 0,
  181. 0);
  182. arg.enforce(conv.ToShortest(val, &builder));
  183. break;
  184. }
  185. default:
  186. arg.error("invalid specifier '", arg.presentation, "'");
  187. }
  188. int len = builder.position();
  189. builder.Finalize();
  190. DCHECK_GT(len, 0);
  191. // Add '+' or ' ' sign if needed
  192. char* p = buf + 1;
  193. // anything that's neither negative nor nan
  194. prefixLen = 0;
  195. if (plusSign && (*p != '-' && *p != 'n' && *p != 'N')) {
  196. *--p = plusSign;
  197. ++len;
  198. prefixLen = 1;
  199. } else if (*p == '-') {
  200. prefixLen = 1;
  201. }
  202. piece = fbstring(p, size_t(len));
  203. }
  204. void FormatArg::initSlow() {
  205. auto b = fullArgString.begin();
  206. auto end = fullArgString.end();
  207. // Parse key
  208. auto p = static_cast<const char*>(memchr(b, ':', size_t(end - b)));
  209. if (!p) {
  210. key_ = StringPiece(b, end);
  211. return;
  212. }
  213. key_ = StringPiece(b, p);
  214. if (*p == ':') {
  215. // parse format spec
  216. if (++p == end) {
  217. return;
  218. }
  219. // fill/align, or just align
  220. Align a;
  221. if (p + 1 != end &&
  222. (a = formatAlignTable[static_cast<unsigned char>(p[1])]) !=
  223. Align::INVALID) {
  224. fill = *p;
  225. align = a;
  226. p += 2;
  227. if (p == end) {
  228. return;
  229. }
  230. } else if (
  231. (a = formatAlignTable[static_cast<unsigned char>(*p)]) !=
  232. Align::INVALID) {
  233. align = a;
  234. if (++p == end) {
  235. return;
  236. }
  237. }
  238. Sign s;
  239. unsigned char uSign = static_cast<unsigned char>(*p);
  240. if ((s = formatSignTable[uSign]) != Sign::INVALID) {
  241. sign = s;
  242. if (++p == end) {
  243. return;
  244. }
  245. }
  246. if (*p == '#') {
  247. basePrefix = true;
  248. if (++p == end) {
  249. return;
  250. }
  251. }
  252. if (*p == '0') {
  253. enforce(align == Align::DEFAULT, "alignment specified twice");
  254. fill = '0';
  255. align = Align::PAD_AFTER_SIGN;
  256. if (++p == end) {
  257. return;
  258. }
  259. }
  260. auto readInt = [&] {
  261. auto const c = p;
  262. do {
  263. ++p;
  264. } while (p != end && *p >= '0' && *p <= '9');
  265. return to<int>(StringPiece(c, p));
  266. };
  267. if (*p == '*') {
  268. width = kDynamicWidth;
  269. ++p;
  270. if (p == end) {
  271. return;
  272. }
  273. if (*p >= '0' && *p <= '9') {
  274. widthIndex = readInt();
  275. }
  276. if (p == end) {
  277. return;
  278. }
  279. } else if (*p >= '0' && *p <= '9') {
  280. width = readInt();
  281. if (p == end) {
  282. return;
  283. }
  284. }
  285. if (*p == ',') {
  286. thousandsSeparator = true;
  287. if (++p == end) {
  288. return;
  289. }
  290. }
  291. if (*p == '.') {
  292. auto d = ++p;
  293. while (p != end && *p >= '0' && *p <= '9') {
  294. ++p;
  295. }
  296. if (p != d) {
  297. precision = to<int>(StringPiece(d, p));
  298. if (p != end && *p == '.') {
  299. trailingDot = true;
  300. ++p;
  301. }
  302. } else {
  303. trailingDot = true;
  304. }
  305. if (p == end) {
  306. return;
  307. }
  308. }
  309. presentation = *p;
  310. if (++p == end) {
  311. return;
  312. }
  313. }
  314. error("extra characters in format string");
  315. }
  316. void FormatArg::validate(Type type) const {
  317. enforce(keyEmpty(), "index not allowed");
  318. switch (type) {
  319. case Type::INTEGER:
  320. enforce(
  321. precision == kDefaultPrecision, "precision not allowed on integers");
  322. break;
  323. case Type::FLOAT:
  324. enforce(
  325. !basePrefix, "base prefix ('#') specifier only allowed on integers");
  326. enforce(
  327. !thousandsSeparator,
  328. "thousands separator (',') only allowed on integers");
  329. break;
  330. case Type::OTHER:
  331. enforce(
  332. align != Align::PAD_AFTER_SIGN,
  333. "'='alignment only allowed on numbers");
  334. enforce(sign == Sign::DEFAULT, "sign specifier only allowed on numbers");
  335. enforce(
  336. !basePrefix, "base prefix ('#') specifier only allowed on integers");
  337. enforce(
  338. !thousandsSeparator,
  339. "thousands separator (',') only allowed on integers");
  340. break;
  341. }
  342. }
  343. namespace detail {
  344. void insertThousandsGroupingUnsafe(char* start_buffer, char** end_buffer) {
  345. uint32_t remaining_digits = uint32_t(*end_buffer - start_buffer);
  346. uint32_t separator_size = (remaining_digits - 1) / 3;
  347. uint32_t result_size = remaining_digits + separator_size;
  348. *end_buffer = *end_buffer + separator_size;
  349. // get the end of the new string with the separators
  350. uint32_t buffer_write_index = result_size - 1;
  351. uint32_t buffer_read_index = remaining_digits - 1;
  352. start_buffer[buffer_write_index + 1] = 0;
  353. bool done = false;
  354. uint32_t next_group_size = 3;
  355. while (!done) {
  356. uint32_t current_group_size = std::max<uint32_t>(
  357. 1, std::min<uint32_t>(remaining_digits, next_group_size));
  358. // write out the current group's digits to the buffer index
  359. for (uint32_t i = 0; i < current_group_size; i++) {
  360. start_buffer[buffer_write_index--] = start_buffer[buffer_read_index--];
  361. }
  362. // if not finished, write the separator before the next group
  363. if (buffer_write_index < buffer_write_index + 1) {
  364. start_buffer[buffer_write_index--] = ',';
  365. } else {
  366. done = true;
  367. }
  368. remaining_digits -= current_group_size;
  369. }
  370. }
  371. } // namespace detail
  372. FormatKeyNotFoundException::FormatKeyNotFoundException(StringPiece key)
  373. : std::out_of_range(kMessagePrefix.str() + key.str()) {}
  374. constexpr StringPiece const FormatKeyNotFoundException::kMessagePrefix;
  375. } // namespace folly