2 * Copyright 2015 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef FOLLY_FORMAT_H_
18 #error This file may only be included from Format.h.
21 #include <folly/Exception.h>
22 #include <folly/Traits.h>
24 // Ignore -Wformat-nonliteral warnings within this file
25 #pragma GCC diagnostic push
26 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
32 // Updates the end of the buffer after the comma separators have been added.
33 void insertThousandsGroupingUnsafe(char* start_buffer, char** end_buffer);
35 extern const char formatHexUpper[256][2];
36 extern const char formatHexLower[256][2];
37 extern const char formatOctal[512][3];
38 extern const char formatBinary[256][8];
40 const size_t kMaxHexLength = 2 * sizeof(uintmax_t);
41 const size_t kMaxOctalLength = 3 * sizeof(uintmax_t);
42 const size_t kMaxBinaryLength = 8 * sizeof(uintmax_t);
45 * Convert an unsigned to hex, using repr (which maps from each possible
46 * 2-hex-bytes value to the 2-character representation).
48 * Just like folly::detail::uintToBuffer in Conv.h, writes at the *end* of
49 * the supplied buffer and returns the offset of the beginning of the string
50 * from the start of the buffer. The formatted string will be in range
51 * [buf+begin, buf+bufLen).
54 size_t uintToHex(char* buffer, size_t bufLen, Uint v,
55 const char (&repr)[256][2]) {
56 // 'v >>= 7, v >>= 1' is no more than a work around to get rid of shift size
57 // warning when Uint = uint8_t (it's false as v >= 256 implies sizeof(v) > 1).
58 for (; !less_than<unsigned, 256>(v); v >>= 7, v >>= 1) {
61 buffer[bufLen] = repr[b][0];
62 buffer[bufLen + 1] = repr[b][1];
64 buffer[--bufLen] = repr[v][1];
66 buffer[--bufLen] = repr[v][0];
72 * Convert an unsigned to hex, using lower-case letters for the digits
73 * above 9. See the comments for uintToHex.
76 inline size_t uintToHexLower(char* buffer, size_t bufLen, Uint v) {
77 return uintToHex(buffer, bufLen, v, formatHexLower);
81 * Convert an unsigned to hex, using upper-case letters for the digits
82 * above 9. See the comments for uintToHex.
85 inline size_t uintToHexUpper(char* buffer, size_t bufLen, Uint v) {
86 return uintToHex(buffer, bufLen, v, formatHexUpper);
90 * Convert an unsigned to octal.
92 * Just like folly::detail::uintToBuffer in Conv.h, writes at the *end* of
93 * the supplied buffer and returns the offset of the beginning of the string
94 * from the start of the buffer. The formatted string will be in range
95 * [buf+begin, buf+bufLen).
98 size_t uintToOctal(char* buffer, size_t bufLen, Uint v) {
99 auto& repr = formatOctal;
100 // 'v >>= 7, v >>= 2' is no more than a work around to get rid of shift size
101 // warning when Uint = uint8_t (it's false as v >= 512 implies sizeof(v) > 1).
102 for (; !less_than<unsigned, 512>(v); v >>= 7, v >>= 2) {
105 buffer[bufLen] = repr[b][0];
106 buffer[bufLen + 1] = repr[b][1];
107 buffer[bufLen + 2] = repr[b][2];
109 buffer[--bufLen] = repr[v][2];
111 buffer[--bufLen] = repr[v][1];
114 buffer[--bufLen] = repr[v][0];
120 * Convert an unsigned to binary.
122 * Just like folly::detail::uintToBuffer in Conv.h, writes at the *end* of
123 * the supplied buffer and returns the offset of the beginning of the string
124 * from the start of the buffer. The formatted string will be in range
125 * [buf+begin, buf+bufLen).
127 template <class Uint>
128 size_t uintToBinary(char* buffer, size_t bufLen, Uint v) {
129 auto& repr = formatBinary;
131 buffer[--bufLen] = '0';
134 for (; v; v >>= 7, v >>= 1) {
137 memcpy(buffer + bufLen, &(repr[b][0]), 8);
139 while (buffer[bufLen] == '0') {
145 } // namespace detail
147 template <class Derived, bool containerMode, class... Args>
148 BaseFormatter<Derived, containerMode, Args...>::BaseFormatter(StringPiece str,
151 values_(FormatValue<typename std::decay<Args>::type>(
152 std::forward<Args>(args))...) {
153 static_assert(!containerMode || sizeof...(Args) == 1,
154 "Exactly one argument required in container mode");
157 template <class Derived, bool containerMode, class... Args>
158 template <class Output>
159 void BaseFormatter<Derived, containerMode, Args...>::operator()(Output& out)
161 // Copy raw string (without format specifiers) to output;
162 // not as simple as we'd like, as we still need to translate "}}" to "}"
163 // and throw if we see any lone "}"
164 auto outputString = [&out] (StringPiece s) {
168 auto q = static_cast<const char*>(memchr(p, '}', end - p));
170 out(StringPiece(p, end));
174 out(StringPiece(p, q));
177 if (p == end || *p != '}') {
178 throw BadFormatArg("folly::format: single '}' in format string");
184 auto p = str_.begin();
185 auto end = str_.end();
188 bool hasDefaultArgIndex = false;
189 bool hasExplicitArgIndex = false;
191 auto q = static_cast<const char*>(memchr(p, '{', end - p));
193 outputString(StringPiece(p, end));
196 outputString(StringPiece(p, q));
200 throw BadFormatArg("folly::format: '}' at end of format string");
205 out(StringPiece(p, 1));
211 q = static_cast<const char*>(memchr(p, '}', end - p));
213 throw BadFormatArg("folly::format: missing ending '}'");
215 FormatArg arg(StringPiece(p, q));
219 auto piece = arg.splitKey<true>(); // empty key component is okay
220 if (containerMode) { // static
222 arg.setNextIntKey(nextArg++);
223 hasDefaultArgIndex = true;
225 arg.setNextKey(piece);
226 hasExplicitArgIndex = true;
230 argIndex = nextArg++;
231 hasDefaultArgIndex = true;
234 argIndex = to<int>(piece);
235 } catch (const std::out_of_range& e) {
236 arg.error("argument index must be integer");
238 arg.enforce(argIndex >= 0, "argument index must be non-negative");
239 hasExplicitArgIndex = true;
243 if (hasDefaultArgIndex && hasExplicitArgIndex) {
245 "folly::format: may not have both default and explicit arg indexes");
248 doFormat(argIndex, arg, out);
252 template <class Derived, bool containerMode, class... Args>
253 void writeTo(FILE* fp,
254 const BaseFormatter<Derived, containerMode, Args...>& formatter) {
255 auto writer = [fp] (StringPiece sp) {
256 size_t n = fwrite(sp.data(), 1, sp.size(), fp);
258 throwSystemError("Formatter writeTo", "fwrite failed");
264 namespace format_value {
266 template <class FormatCallback>
267 void formatString(StringPiece val, FormatArg& arg, FormatCallback& cb) {
268 if (arg.width != FormatArg::kDefaultWidth && arg.width < 0) {
269 throw BadFormatArg("folly::format: invalid width");
271 if (arg.precision != FormatArg::kDefaultPrecision && arg.precision < 0) {
272 throw BadFormatArg("folly::format: invalid precision");
275 // XXX: clang should be smart enough to not need the two static_cast<size_t>
276 // uses below given the above checks. If clang ever becomes that smart, we
277 // should remove the otherwise unnecessary warts.
279 if (arg.precision != FormatArg::kDefaultPrecision &&
280 val.size() > static_cast<size_t>(arg.precision)) {
281 val.reset(val.data(), arg.precision);
284 constexpr int padBufSize = 128;
285 char padBuf[padBufSize];
287 // Output padding, no more than padBufSize at once
288 auto pad = [&padBuf, &cb, padBufSize] (int chars) {
290 int n = std::min(chars, padBufSize);
291 cb(StringPiece(padBuf, n));
296 int padRemaining = 0;
297 if (arg.width != FormatArg::kDefaultWidth &&
298 val.size() < static_cast<size_t>(arg.width)) {
299 char fill = arg.fill == FormatArg::kDefaultFill ? ' ' : arg.fill;
300 int padChars = static_cast<int> (arg.width - val.size());
301 memset(padBuf, fill, std::min(padBufSize, padChars));
304 case FormatArg::Align::DEFAULT:
305 case FormatArg::Align::LEFT:
306 padRemaining = padChars;
308 case FormatArg::Align::CENTER:
310 padRemaining = padChars - padChars / 2;
312 case FormatArg::Align::RIGHT:
313 case FormatArg::Align::PAD_AFTER_SIGN:
329 template <class FormatCallback>
330 void formatNumber(StringPiece val, int prefixLen, FormatArg& arg,
331 FormatCallback& cb) {
332 // precision means something different for numbers
333 arg.precision = FormatArg::kDefaultPrecision;
334 if (arg.align == FormatArg::Align::DEFAULT) {
335 arg.align = FormatArg::Align::RIGHT;
336 } else if (prefixLen && arg.align == FormatArg::Align::PAD_AFTER_SIGN) {
337 // Split off the prefix, then do any padding if necessary
338 cb(val.subpiece(0, prefixLen));
339 val.advance(prefixLen);
340 arg.width = std::max(arg.width - prefixLen, 0);
342 format_value::formatString(val, arg, cb);
345 template <class FormatCallback,
349 void formatFormatter(
350 const BaseFormatter<Derived, containerMode, Args...>& formatter,
352 FormatCallback& cb) {
353 if (arg.width == FormatArg::kDefaultWidth &&
354 arg.precision == FormatArg::kDefaultPrecision) {
357 } else if (arg.align != FormatArg::Align::LEFT &&
358 arg.align != FormatArg::Align::DEFAULT) {
359 // We can only avoid creating a temporary string if we align left,
360 // as we'd need to know the size beforehand otherwise
361 format_value::formatString(formatter.fbstr(), arg, cb);
363 auto fn = [&arg, &cb] (StringPiece sp) mutable {
364 int sz = static_cast<int>(sp.size());
365 if (arg.precision != FormatArg::kDefaultPrecision) {
366 sz = std::min(arg.precision, sz);
367 sp.reset(sp.data(), sz);
372 if (arg.width != FormatArg::kDefaultWidth) {
373 arg.width = std::max(arg.width - sz, 0);
378 if (arg.width != FormatArg::kDefaultWidth && arg.width != 0) {
379 // Rely on formatString to do appropriate padding
380 format_value::formatString(StringPiece(), arg, cb);
385 } // namespace format_value
387 // Definitions for default FormatValue classes
389 // Integral types (except bool)
392 T, typename std::enable_if<
393 std::is_integral<T>::value &&
394 !std::is_same<T, bool>::value>::type>
397 explicit FormatValue(T val) : val_(val) { }
398 template <class FormatCallback>
399 void format(FormatArg& arg, FormatCallback& cb) const {
400 arg.validate(FormatArg::Type::INTEGER);
404 template <class FormatCallback>
405 void doFormat(FormatArg& arg, FormatCallback& cb) const {
406 char presentation = arg.presentation;
407 if (presentation == FormatArg::kDefaultPresentation) {
408 presentation = std::is_same<T, char>::value ? 'c' : 'd';
411 // Do all work as unsigned, we'll add the prefix ('0' or '0x' if necessary)
412 // and sign ourselves.
413 typedef typename std::make_unsigned<T>::type UT;
416 if (std::is_signed<T>::value) {
417 if (folly::is_negative(val_)) {
418 uval = static_cast<UT>(-val_);
421 uval = static_cast<UT>(val_);
423 case FormatArg::Sign::PLUS_OR_MINUS:
426 case FormatArg::Sign::SPACE_OR_MINUS:
438 arg.enforce(arg.sign == FormatArg::Sign::DEFAULT,
439 "sign specifications not allowed for unsigned values");
443 // #x: 0x prefix + 16 bytes = 18 bytes
444 // #o: 0 prefix + 22 bytes = 23 bytes
445 // #b: 0b prefix + 64 bytes = 65 bytes
446 // ,d: 26 bytes (including thousands separators!)
448 // + 3 for sign and prefix shenanigans (see below)
449 constexpr size_t valBufSize = 69;
450 char valBuf[valBufSize];
451 char* valBufBegin = nullptr;
452 char* valBufEnd = nullptr;
455 switch (presentation) {
457 arg.enforce(!arg.basePrefix,
458 "base prefix not allowed with '", presentation,
461 arg.enforce(!arg.thousandsSeparator,
462 "cannot use ',' with the '", presentation,
465 valBufBegin = valBuf + 3; // room for sign and base prefix
466 valBufEnd = valBufBegin + sprintf(valBufBegin, "%'ju",
467 static_cast<uintmax_t>(uval));
470 arg.enforce(!arg.basePrefix,
471 "base prefix not allowed with '", presentation,
473 valBufBegin = valBuf + 3; // room for sign and base prefix
475 // Use uintToBuffer, faster than sprintf
476 valBufEnd = valBufBegin + uint64ToBufferUnsafe(uval, valBufBegin);
477 if (arg.thousandsSeparator) {
478 detail::insertThousandsGroupingUnsafe(valBufBegin, &valBufEnd);
482 arg.enforce(!arg.basePrefix,
483 "base prefix not allowed with '", presentation,
485 arg.enforce(!arg.thousandsSeparator,
486 "thousands separator (',') not allowed with '",
487 presentation, "' specifier");
488 valBufBegin = valBuf + 3;
489 *valBufBegin = static_cast<char>(uval);
490 valBufEnd = valBufBegin + 1;
494 arg.enforce(!arg.thousandsSeparator,
495 "thousands separator (',') not allowed with '",
496 presentation, "' specifier");
497 valBufEnd = valBuf + valBufSize - 1;
498 valBufBegin = valBuf + detail::uintToOctal(valBuf, valBufSize - 1, uval);
499 if (arg.basePrefix) {
500 *--valBufBegin = '0';
505 arg.enforce(!arg.thousandsSeparator,
506 "thousands separator (',') not allowed with '",
507 presentation, "' specifier");
508 valBufEnd = valBuf + valBufSize - 1;
509 valBufBegin = valBuf + detail::uintToHexLower(valBuf, valBufSize - 1,
511 if (arg.basePrefix) {
512 *--valBufBegin = 'x';
513 *--valBufBegin = '0';
518 arg.enforce(!arg.thousandsSeparator,
519 "thousands separator (',') not allowed with '",
520 presentation, "' specifier");
521 valBufEnd = valBuf + valBufSize - 1;
522 valBufBegin = valBuf + detail::uintToHexUpper(valBuf, valBufSize - 1,
524 if (arg.basePrefix) {
525 *--valBufBegin = 'X';
526 *--valBufBegin = '0';
532 arg.enforce(!arg.thousandsSeparator,
533 "thousands separator (',') not allowed with '",
534 presentation, "' specifier");
535 valBufEnd = valBuf + valBufSize - 1;
536 valBufBegin = valBuf + detail::uintToBinary(valBuf, valBufSize - 1,
538 if (arg.basePrefix) {
539 *--valBufBegin = presentation; // 0b or 0B
540 *--valBufBegin = '0';
545 arg.error("invalid specifier '", presentation, "'");
549 *--valBufBegin = sign;
553 format_value::formatNumber(StringPiece(valBufBegin, valBufEnd), prefixLen,
563 class FormatValue<bool> {
565 explicit FormatValue(bool val) : val_(val) { }
567 template <class FormatCallback>
568 void format(FormatArg& arg, FormatCallback& cb) const {
569 if (arg.presentation == FormatArg::kDefaultPresentation) {
570 arg.validate(FormatArg::Type::OTHER);
571 format_value::formatString(val_ ? "true" : "false", arg, cb);
573 FormatValue<int>(val_).format(arg, cb);
583 class FormatValue<double> {
585 explicit FormatValue(double val) : val_(val) { }
587 template <class FormatCallback>
588 void format(FormatArg& arg, FormatCallback& cb) const {
589 using ::double_conversion::DoubleToStringConverter;
590 using ::double_conversion::StringBuilder;
592 arg.validate(FormatArg::Type::FLOAT);
594 if (arg.presentation == FormatArg::kDefaultPresentation) {
595 arg.presentation = 'g';
598 const char* infinitySymbol = isupper(arg.presentation) ? "INF" : "inf";
599 const char* nanSymbol = isupper(arg.presentation) ? "NAN" : "nan";
600 char exponentSymbol = isupper(arg.presentation) ? 'E' : 'e';
602 if (arg.precision == FormatArg::kDefaultPrecision) {
606 // 2+: for null terminator and optional sign shenanigans.
607 char buf[2 + std::max({
608 (2 + DoubleToStringConverter::kMaxFixedDigitsBeforePoint +
609 DoubleToStringConverter::kMaxFixedDigitsAfterPoint),
610 (8 + DoubleToStringConverter::kMaxExponentialDigits),
611 (7 + DoubleToStringConverter::kMaxPrecisionDigits)})];
612 StringBuilder builder(buf + 1, static_cast<int> (sizeof(buf) - 1));
616 case FormatArg::Sign::PLUS_OR_MINUS:
619 case FormatArg::Sign::SPACE_OR_MINUS:
628 DoubleToStringConverter::EMIT_POSITIVE_EXPONENT_SIGN |
629 (arg.trailingDot ? DoubleToStringConverter::EMIT_TRAILING_DECIMAL_POINT
633 switch (arg.presentation) {
640 DoubleToStringConverter::kMaxFixedDigitsAfterPoint) {
641 arg.precision = DoubleToStringConverter::kMaxFixedDigitsAfterPoint;
643 DoubleToStringConverter conv(flags,
651 arg.enforce(conv.ToFixed(val, arg.precision, &builder),
652 "fixed double conversion failed");
658 if (arg.precision > DoubleToStringConverter::kMaxExponentialDigits) {
659 arg.precision = DoubleToStringConverter::kMaxExponentialDigits;
662 DoubleToStringConverter conv(flags,
670 arg.enforce(conv.ToExponential(val, arg.precision, &builder));
673 case 'n': // should be locale-aware, but isn't
677 if (arg.precision < DoubleToStringConverter::kMinPrecisionDigits) {
678 arg.precision = DoubleToStringConverter::kMinPrecisionDigits;
679 } else if (arg.precision >
680 DoubleToStringConverter::kMaxPrecisionDigits) {
681 arg.precision = DoubleToStringConverter::kMaxPrecisionDigits;
683 DoubleToStringConverter conv(flags,
691 arg.enforce(conv.ToShortest(val, &builder));
695 arg.error("invalid specifier '", arg.presentation, "'");
698 int len = builder.position();
702 // Add '+' or ' ' sign if needed
704 // anything that's neither negative nor nan
706 if (plusSign && (*p != '-' && *p != 'n' && *p != 'N')) {
710 } else if (*p == '-') {
714 format_value::formatNumber(StringPiece(p, len), prefixLen, arg, cb);
721 // float (defer to double)
723 class FormatValue<float> {
725 explicit FormatValue(float val) : val_(val) { }
727 template <class FormatCallback>
728 void format(FormatArg& arg, FormatCallback& cb) const {
729 FormatValue<double>(val_).format(arg, cb);
736 // Sring-y types (implicitly convertible to StringPiece, except char*)
739 T, typename std::enable_if<
740 (!std::is_pointer<T>::value ||
741 !std::is_same<char, typename std::decay<
742 typename std::remove_pointer<T>::type>::type>::value) &&
743 std::is_convertible<T, StringPiece>::value>::type>
746 explicit FormatValue(StringPiece val) : val_(val) { }
748 template <class FormatCallback>
749 void format(FormatArg& arg, FormatCallback& cb) const {
750 if (arg.keyEmpty()) {
751 arg.validate(FormatArg::Type::OTHER);
752 arg.enforce(arg.presentation == FormatArg::kDefaultPresentation ||
753 arg.presentation == 's',
754 "invalid specifier '", arg.presentation, "'");
755 format_value::formatString(val_, arg, cb);
757 FormatValue<char>(val_.at(arg.splitIntKey())).format(arg, cb);
767 class FormatValue<std::nullptr_t> {
769 explicit FormatValue(std::nullptr_t) { }
771 template <class FormatCallback>
772 void format(FormatArg& arg, FormatCallback& cb) const {
773 arg.validate(FormatArg::Type::OTHER);
774 arg.enforce(arg.presentation == FormatArg::kDefaultPresentation,
775 "invalid specifier '", arg.presentation, "'");
776 format_value::formatString("(null)", arg, cb);
780 // Partial specialization of FormatValue for char*
784 typename std::enable_if<
785 std::is_same<char, typename std::decay<T>::type>::value>::type>
788 explicit FormatValue(T* val) : val_(val) { }
790 template <class FormatCallback>
791 void format(FormatArg& arg, FormatCallback& cb) const {
792 if (arg.keyEmpty()) {
794 FormatValue<std::nullptr_t>(nullptr).format(arg, cb);
796 FormatValue<StringPiece>(val_).format(arg, cb);
799 FormatValue<typename std::decay<T>::type>(
800 val_[arg.splitIntKey()]).format(arg, cb);
808 // Partial specialization of FormatValue for void*
812 typename std::enable_if<
813 std::is_same<void, typename std::decay<T>::type>::value>::type>
816 explicit FormatValue(T* val) : val_(val) { }
818 template <class FormatCallback>
819 void format(FormatArg& arg, FormatCallback& cb) const {
821 FormatValue<std::nullptr_t>(nullptr).format(arg, cb);
823 // Print as a pointer, in hex.
824 arg.validate(FormatArg::Type::OTHER);
825 arg.enforce(arg.presentation == FormatArg::kDefaultPresentation,
826 "invalid specifier '", arg.presentation, "'");
827 arg.basePrefix = true;
828 arg.presentation = 'x';
829 if (arg.align == FormatArg::Align::DEFAULT) {
830 arg.align = FormatArg::Align::LEFT;
832 FormatValue<uintptr_t>(
833 reinterpret_cast<uintptr_t>(val_)).doFormat(arg, cb);
841 template <class T, class = void>
842 class TryFormatValue {
844 template <class FormatCallback>
845 static void formatOrFail(T& value, FormatArg& arg, FormatCallback& cb) {
846 arg.error("No formatter available for this type");
851 class TryFormatValue<
853 typename std::enable_if<
854 0 < sizeof(FormatValue<typename std::decay<T>::type>)>::type>
857 template <class FormatCallback>
858 static void formatOrFail(T& value, FormatArg& arg, FormatCallback& cb) {
859 FormatValue<typename std::decay<T>::type>(value).format(arg, cb);
863 // Partial specialization of FormatValue for other pointers
867 typename std::enable_if<
868 !std::is_same<char, typename std::decay<T>::type>::value &&
869 !std::is_same<void, typename std::decay<T>::type>::value>::type>
872 explicit FormatValue(T* val) : val_(val) { }
874 template <class FormatCallback>
875 void format(FormatArg& arg, FormatCallback& cb) const {
876 if (arg.keyEmpty()) {
877 FormatValue<void*>((void*)val_).format(arg, cb);
879 TryFormatValue<T>::formatOrFail(val_[arg.splitIntKey()], arg, cb);
888 // Shortcut, so we don't have to use enable_if everywhere
889 struct FormatTraitsBase {
890 typedef void enabled;
893 // Traits that define enabled, value_type, and at() for anything
894 // indexable with integral keys: pointers, arrays, vectors, and maps
895 // with integral keys
896 template <class T, class Enable=void> struct IndexableTraits;
898 // Base class for sequences (vectors, deques)
900 struct IndexableTraitsSeq : public FormatTraitsBase {
901 typedef C container_type;
902 typedef typename C::value_type value_type;
903 static const value_type& at(const C& c, int idx) {
907 static const value_type& at(const C& c, int idx,
908 const value_type& dflt) {
909 return (idx >= 0 && size_t(idx) < c.size()) ? c.at(idx) : dflt;
913 // Base class for associative types (maps)
915 struct IndexableTraitsAssoc : public FormatTraitsBase {
916 typedef typename C::value_type::second_type value_type;
917 static const value_type& at(const C& c, int idx) {
918 return c.at(static_cast<typename C::key_type>(idx));
920 static const value_type& at(const C& c, int idx,
921 const value_type& dflt) {
922 auto pos = c.find(static_cast<typename C::key_type>(idx));
923 return pos != c.end() ? pos->second : dflt;
928 template <class T, size_t N>
929 struct IndexableTraits<std::array<T, N>>
930 : public IndexableTraitsSeq<std::array<T, N>> {
934 template <class T, class A>
935 struct IndexableTraits<std::vector<T, A>>
936 : public IndexableTraitsSeq<std::vector<T, A>> {
940 template <class T, class A>
941 struct IndexableTraits<std::deque<T, A>>
942 : public IndexableTraitsSeq<std::deque<T, A>> {
946 template <class T, class A>
947 struct IndexableTraits<fbvector<T, A>>
948 : public IndexableTraitsSeq<fbvector<T, A>> {
952 template <class T, size_t M, class A, class B, class C>
953 struct IndexableTraits<small_vector<T, M, A, B, C>>
954 : public IndexableTraitsSeq<small_vector<T, M, A, B, C>> {
957 // std::map with integral keys
958 template <class K, class T, class C, class A>
959 struct IndexableTraits<
960 std::map<K, T, C, A>,
961 typename std::enable_if<std::is_integral<K>::value>::type>
962 : public IndexableTraitsAssoc<std::map<K, T, C, A>> {
965 // std::unordered_map with integral keys
966 template <class K, class T, class H, class E, class A>
967 struct IndexableTraits<
968 std::unordered_map<K, T, H, E, A>,
969 typename std::enable_if<std::is_integral<K>::value>::type>
970 : public IndexableTraitsAssoc<std::unordered_map<K, T, H, E, A>> {
973 } // namespace detail
975 // Partial specialization of FormatValue for integer-indexable containers
979 typename detail::IndexableTraits<T>::enabled> {
981 explicit FormatValue(const T& val) : val_(val) { }
983 template <class FormatCallback>
984 void format(FormatArg& arg, FormatCallback& cb) const {
985 FormatValue<typename std::decay<
986 typename detail::IndexableTraits<T>::value_type>::type>(
987 detail::IndexableTraits<T>::at(
988 val_, arg.splitIntKey())).format(arg, cb);
995 template <class Container, class Value>
997 detail::DefaultValueWrapper<Container, Value>,
998 typename detail::IndexableTraits<Container>::enabled> {
1000 explicit FormatValue(const detail::DefaultValueWrapper<Container, Value>& val)
1003 template <class FormatCallback>
1004 void format(FormatArg& arg, FormatCallback& cb) const {
1005 FormatValue<typename std::decay<
1006 typename detail::IndexableTraits<Container>::value_type>::type>(
1007 detail::IndexableTraits<Container>::at(
1010 val_.defaultValue)).format(arg, cb);
1014 const detail::DefaultValueWrapper<Container, Value>& val_;
1019 // Define enabled, key_type, convert from StringPiece to the key types
1021 template <class T> struct KeyFromStringPiece;
1025 struct KeyFromStringPiece<std::string> : public FormatTraitsBase {
1026 typedef std::string key_type;
1027 static std::string convert(StringPiece s) {
1028 return s.toString();
1030 typedef void enabled;
1035 struct KeyFromStringPiece<fbstring> : public FormatTraitsBase {
1036 typedef fbstring key_type;
1037 static fbstring convert(StringPiece s) {
1038 return s.toFbstring();
1044 struct KeyFromStringPiece<StringPiece> : public FormatTraitsBase {
1045 typedef StringPiece key_type;
1046 static StringPiece convert(StringPiece s) {
1051 // Base class for associative types keyed by strings
1052 template <class T> struct KeyableTraitsAssoc : public FormatTraitsBase {
1053 typedef typename T::key_type key_type;
1054 typedef typename T::value_type::second_type value_type;
1055 static const value_type& at(const T& map, StringPiece key) {
1056 return map.at(KeyFromStringPiece<key_type>::convert(key));
1058 static const value_type& at(const T& map, StringPiece key,
1059 const value_type& dflt) {
1060 auto pos = map.find(KeyFromStringPiece<key_type>::convert(key));
1061 return pos != map.end() ? pos->second : dflt;
1065 // Define enabled, key_type, value_type, at() for supported string-keyed
1067 template <class T, class Enabled=void> struct KeyableTraits;
1069 // std::map with string key
1070 template <class K, class T, class C, class A>
1071 struct KeyableTraits<
1072 std::map<K, T, C, A>,
1073 typename KeyFromStringPiece<K>::enabled>
1074 : public KeyableTraitsAssoc<std::map<K, T, C, A>> {
1077 // std::unordered_map with string key
1078 template <class K, class T, class H, class E, class A>
1079 struct KeyableTraits<
1080 std::unordered_map<K, T, H, E, A>,
1081 typename KeyFromStringPiece<K>::enabled>
1082 : public KeyableTraitsAssoc<std::unordered_map<K, T, H, E, A>> {
1085 } // namespace detail
1087 // Partial specialization of FormatValue for string-keyed containers
1091 typename detail::KeyableTraits<T>::enabled> {
1093 explicit FormatValue(const T& val) : val_(val) { }
1095 template <class FormatCallback>
1096 void format(FormatArg& arg, FormatCallback& cb) const {
1097 FormatValue<typename std::decay<
1098 typename detail::KeyableTraits<T>::value_type>::type>(
1099 detail::KeyableTraits<T>::at(
1100 val_, arg.splitKey())).format(arg, cb);
1107 template <class Container, class Value>
1109 detail::DefaultValueWrapper<Container, Value>,
1110 typename detail::KeyableTraits<Container>::enabled> {
1112 explicit FormatValue(const detail::DefaultValueWrapper<Container, Value>& val)
1115 template <class FormatCallback>
1116 void format(FormatArg& arg, FormatCallback& cb) const {
1117 FormatValue<typename std::decay<
1118 typename detail::KeyableTraits<Container>::value_type>::type>(
1119 detail::KeyableTraits<Container>::at(
1122 val_.defaultValue)).format(arg, cb);
1126 const detail::DefaultValueWrapper<Container, Value>& val_;
1129 // Partial specialization of FormatValue for pairs
1130 template <class A, class B>
1131 class FormatValue<std::pair<A, B>> {
1133 explicit FormatValue(const std::pair<A, B>& val) : val_(val) { }
1135 template <class FormatCallback>
1136 void format(FormatArg& arg, FormatCallback& cb) const {
1137 int key = arg.splitIntKey();
1140 FormatValue<typename std::decay<A>::type>(val_.first).format(arg, cb);
1143 FormatValue<typename std::decay<B>::type>(val_.second).format(arg, cb);
1146 arg.error("invalid index for pair");
1151 const std::pair<A, B>& val_;
1154 // Partial specialization of FormatValue for tuples
1155 template <class... Args>
1156 class FormatValue<std::tuple<Args...>> {
1157 typedef std::tuple<Args...> Tuple;
1159 explicit FormatValue(const Tuple& val) : val_(val) { }
1161 template <class FormatCallback>
1162 void format(FormatArg& arg, FormatCallback& cb) const {
1163 int key = arg.splitIntKey();
1164 arg.enforce(key >= 0, "tuple index must be non-negative");
1165 doFormat(key, arg, cb);
1169 static constexpr size_t valueCount = std::tuple_size<Tuple>::value;
1171 template <size_t K, class Callback>
1172 typename std::enable_if<K == valueCount>::type
1173 doFormatFrom(size_t i, FormatArg& arg, Callback& cb) const {
1174 arg.enforce("tuple index out of range, max=", i);
1177 template <size_t K, class Callback>
1178 typename std::enable_if<(K < valueCount)>::type
1179 doFormatFrom(size_t i, FormatArg& arg, Callback& cb) const {
1181 FormatValue<typename std::decay<
1182 typename std::tuple_element<K, Tuple>::type>::type>(
1183 std::get<K>(val_)).format(arg, cb);
1185 doFormatFrom<K+1>(i, arg, cb);
1189 template <class Callback>
1190 void doFormat(size_t i, FormatArg& arg, Callback& cb) const {
1191 return doFormatFrom<0>(i, arg, cb);
1197 // Partial specialization of FormatValue for nested Formatters
1198 template <bool containerMode, class... Args,
1199 template <bool, class...> class F>
1200 class FormatValue<F<containerMode, Args...>,
1201 typename std::enable_if<detail::IsFormatter<
1202 F<containerMode, Args...>>::value>::type> {
1203 typedef typename F<containerMode, Args...>::BaseType FormatterValue;
1206 explicit FormatValue(const FormatterValue& f) : f_(f) { }
1208 template <class FormatCallback>
1209 void format(FormatArg& arg, FormatCallback& cb) const {
1210 format_value::formatFormatter(f_, arg, cb);
1213 const FormatterValue& f_;
1217 * Formatter objects can be appended to strings, and therefore they're
1218 * compatible with folly::toAppend and folly::to.
1220 template <class Tgt, class Derived, bool containerMode, class... Args>
1221 typename std::enable_if<IsSomeString<Tgt>::value>::type toAppend(
1222 const BaseFormatter<Derived, containerMode, Args...>& value, Tgt* result) {
1223 value.appendTo(*result);
1226 } // namespace folly
1228 #pragma GCC diagnostic pop