116 #include <string_view>
117 #include <type_traits>
132 template <
typename TypeT>
133 using RemoveCvref =
typename std::remove_cv_t<std::remove_reference_t<TypeT>>;
135 template <
typename TypeT,
typename =
void>
138 template <
typename TypeT>
139 struct IsOutputIteratorImpl<TypeT, std::enable_if_t<std::is_assignable_v<decltype(*std::declval<TypeT&>()++), char>>>
140 final : std::true_type {};
145 template <
typename TypeT,
size_t NumT>
148 template <
typename TypeT>
168 template <
typename T>
169 struct TypeConstant final : std::integral_constant<Type, Type::kNone> {};
172 struct TypeConstant<signed char> final : std::integral_constant<Type, Type::kInt> {};
175 struct TypeConstant<unsigned char> final : std::integral_constant<Type, Type::kUint> {};
178 struct TypeConstant<short> final : std::integral_constant<Type, Type::kInt> {};
181 struct TypeConstant<unsigned short> final : std::integral_constant<Type, Type::kUint> {};
184 struct TypeConstant<int> final : std::integral_constant<Type, Type::kInt> {};
187 struct TypeConstant<unsigned> final : std::integral_constant<Type, Type::kUint> {};
190 struct TypeConstant<long> final : std::integral_constant<Type, Type::kLongLong> {};
193 struct TypeConstant<unsigned long> final : std::integral_constant<Type, Type::kUlongLong> {};
196 struct TypeConstant<long long> final : std::integral_constant<Type, Type::kLongLong> {};
199 struct TypeConstant<unsigned long long> final : std::integral_constant<Type, Type::kUlongLong> {};
202 struct TypeConstant<bool> final : std::integral_constant<Type, Type::kBool> {};
205 struct TypeConstant<char> final : std::integral_constant<Type, Type::kChar> {};
208 struct TypeConstant<float> final : std::integral_constant<Type, Type::kFloat> {};
211 struct TypeConstant<double> final : std::integral_constant<Type, Type::kDouble> {};
214 struct TypeConstant<long double> final : std::integral_constant<Type, Type::kLongDouble> {};
217 struct TypeConstant<std::nullptr_t> final : std::integral_constant<Type, Type::kPointer> {};
220 struct TypeConstant<const char*> final : std::integral_constant<Type, Type::kCstring> {};
223 struct TypeConstant<char*> final : std::integral_constant<Type, Type::kCstring> {};
226 struct TypeConstant<std::string_view> final : std::integral_constant<Type, Type::kString> {};
229 struct TypeConstant<std::string> final : std::integral_constant<Type, Type::kString> {};
231 template <
size_t NumT>
232 struct TypeConstant<char[NumT]> final : std::integral_constant<Type, Type::kCstring> {};
234 template <
size_t NumT>
235 struct TypeConstant<const char[NumT]> final : std::integral_constant<Type, Type::kCstring> {};
237 template <
typename T>
238 struct TypeConstant<T*> final : std::integral_constant<Type, Type::kPointer> {};
241 template <
typename TypeT,
typename =
void>
244 template <
typename TypeT>
245 struct HasFormatAs<TypeT, std::void_t<decltype(format_as(std::declval<const TypeT&>()))>> final : std::true_type {};
252 char* buf,
unsigned long long value) noexcept;
255 long long value) noexcept;
264 long double value) noexcept;
299 bool upper) noexcept;
305 char* out()
const noexcept;
307 size_t written()
const noexcept;
309 size_t total_size()
const noexcept;
313 void write(
const char* s,
size_t count);
315 void write(std::string_view sv);
317 void write_fill(
char fill,
size_t count);
320 char* begin_{
nullptr};
323 size_t total_size_{0};
326 template <
typename OutputItT>
331 inline OutputItT
out() const noexcept {
return out_; }
333 inline size_t size() const noexcept {
return count_; }
340 inline void write(
const char* s,
size_t count) {
341 for (
size_t i = 0; i < count; ++i) {
348 inline void write(std::string_view sv) {
write(sv.data(), sv.size()); }
351 for (
size_t i = 0; i < count; ++i) {
363 template <
typename CharT>
386 constexpr
explicit Value(
unsigned char val) :
uint_value(static_cast<unsigned>(val)) {}
390 constexpr
explicit Value(
unsigned short val) :
uint_value(static_cast<unsigned>(val)) {}
424 template <
size_t NumT>
427 template <
size_t NumT>
430 template <
typename TypeT>
435 template <
typename CharT>
440 template <
typename TypeT>
443 using ResultT = decltype(format_as(val));
446 "[vlink::format] format_as must return a directly formattable type");
447 static_assert(std::is_reference_v<ResultT> || !std::is_same_v<MappedT, std::string>,
448 "[vlink::format] format_as returning std::string by value would dangle, "
449 "return a reference or std::string_view");
451 if constexpr (std::is_enum_v<MappedT>) {
452 using UnderlyingT = std::underlying_type_t<MappedT>;
453 value_ =
Value<CharT>(
static_cast<UnderlyingT
>(format_as(val)));
459 }
else if constexpr (std::is_enum_v<TypeT>) {
460 using UnderlyingT = std::underlying_type_t<TypeT>;
467 static_assert(!
sizeof(TypeT),
468 "[vlink::format] unsupported type for format_to/MLOG, "
469 "convert to string first or provide format_as");
481 template <
typename CharT,
typename... ArgsT>
483 static constexpr
size_t kNumArgs =
sizeof...(ArgsT);
486 template <
typename... ValuesT>
490 template <
typename CharT>
495 template <
typename... ArgsT>
497 : args_(store.args), size_(sizeof...(ArgsT)) {}
500 return id < size_ ? args_[id] : FormatArg<CharT>();
503 constexpr
size_t size()
const {
return size_; }
512 template <
typename CharT,
typename WriterT>
518 const char* p = fmt.data();
519 const char* end = p + fmt.size();
525 if (p != end && *p ==
'}') {
536 const char* begin = p - 1;
538 while (p != end && *p !=
'{' && *p !=
'}') {
542 writer_.write(begin,
static_cast<size_t>(p - begin));
559 write_arg(args.
get(arg_id++));
567 size_t index = arg_id;
568 bool has_explicit_index =
false;
570 if (*p >=
'0' && *p <=
'9') {
572 has_explicit_index =
true;
574 while (p != end && *p >=
'0' && *p <=
'9') {
575 index = index * 10 +
static_cast<size_t>(*p++ -
'0');
579 if (!has_explicit_index) {
584 bool has_spec =
false;
586 if (p != end && *p ==
':') {
589 resolve_spec_refs(spec, args, arg_id);
593 while (p != end && *p !=
'}') {
600 write_arg_spec(args.
get(index), spec);
602 write_arg(args.
get(index));
611 template <
typename... ArgsT>
612 bool try_format(std::string_view& fmt,
size_t& arg_id,
const ArgsT&... args) {
613 return try_format_args(fmt, arg_id, args...);
616 inline auto out()
const {
return writer_.out(); }
618 template <
typename WriterImplT = WriterT>
620 return writer_.total_size();
623 inline size_t size()
const {
return writer_.size(); }
627 bool try_format_args(std::string_view& fmt,
size_t&) {
638 template <
typename ArgT,
typename... ArgsT>
639 bool try_format_args(std::string_view& fmt,
size_t& arg_id,
const ArgT& arg,
const ArgsT&... args) {
642 while (pos < fmt.size() && fmt[pos] !=
'{' && fmt[pos] !=
'}') {
646 if (pos == fmt.size()) {
651 if VUNLIKELY (fmt[pos] !=
'{' || pos + 1 >= fmt.size() || fmt[pos + 1] !=
'}') {
655 if constexpr (HasFormatAs<RemoveCvref<ArgT>>::value) {
658 writer_.write(fmt.data(), pos);
661 fmt.remove_prefix(pos + 2);
663 return try_format_args(fmt, arg_id, args...);
667 template <
typename TypeT>
668 void write_value(
const TypeT& value) {
669 using ValueT = RemoveCvref<TypeT>;
671 if constexpr (std::is_enum_v<ValueT>) {
672 write_value(
static_cast<std::underlying_type_t<ValueT>
>(value));
673 }
else if constexpr (TypeConstant<ValueT>::value ==
Type::kInt) {
674 write_int(
static_cast<int>(value));
675 }
else if constexpr (TypeConstant<ValueT>::value ==
Type::kUint) {
676 write_uint(
static_cast<unsigned>(value));
678 write_long_long(
static_cast<long long>(value));
680 write_ulong_long(
static_cast<unsigned long long>(value));
681 }
else if constexpr (TypeConstant<ValueT>::value ==
Type::kBool) {
683 }
else if constexpr (TypeConstant<ValueT>::value ==
Type::kChar) {
685 }
else if constexpr (TypeConstant<ValueT>::value ==
Type::kFloat) {
687 }
else if constexpr (TypeConstant<ValueT>::value ==
Type::kDouble) {
690 write_long_double(value);
691 }
else if constexpr (TypeConstant<ValueT>::value ==
Type::kCstring) {
693 }
else if constexpr (TypeConstant<ValueT>::value ==
Type::kString) {
694 write_string_view(value);
695 }
else if constexpr (TypeConstant<ValueT>::value ==
Type::kPointer) {
696 write_pointer(
static_cast<const void*
>(value));
698 static_assert(!
sizeof(TypeT),
699 "[vlink::format] unsupported type for format_to/MLOG, "
700 "convert to string first");
704 void write_int(
int value) {
707 writer_.write(buf, n);
710 void write_uint(
unsigned value) {
713 writer_.write(buf, n);
716 void write_long_long(
long long value) {
719 writer_.write(buf, n);
722 void write_ulong_long(
unsigned long long value) {
725 writer_.write(buf, n);
728 void write_bool(
bool value) {
730 writer_.write(
"true", 4);
732 writer_.write(
"false", 5);
736 void write_char(
char value) { writer_.write(value); }
738 void write_string(
const char* str) {
740 writer_.write(str, std::strlen(str));
742 writer_.write(
"(null)", 6);
746 void write_string_view(std::string_view sv) { writer_.write(sv); }
748 void write_pointer(
const void* ptr) {
751 writer_.write(buf, n);
754 void write_float(
float value) {
757 writer_.write(buf, n);
760 void write_double(
double value) {
763 writer_.write(buf, n);
766 void write_long_double(
long double value) {
769 writer_.write(buf, n);
772 void write_arg(
const FormatArg<CharT>& arg) {
773 switch (arg.type()) {
775 write_int(arg.value().int_value);
778 write_uint(arg.value().uint_value);
781 write_long_long(arg.value().long_long_value);
784 write_ulong_long(arg.value().ulong_long_value);
787 write_bool(arg.value().bool_value);
790 write_char(arg.value().char_value);
793 write_float(arg.value().float_value);
796 write_double(arg.value().double_value);
799 write_long_double(arg.value().long_double_value);
802 write_string(arg.value().string_value);
805 write_string_view(arg.value().string_view_value);
808 write_pointer(arg.value().pointer_value);
815 static long long spec_ref_value(
const FormatArg<CharT>& arg) {
816 switch (arg.type()) {
818 return arg.value().int_value;
820 return arg.value().uint_value;
822 return arg.value().long_long_value;
824 return arg.value().ulong_long_value >
static_cast<unsigned long long>(LLONG_MAX)
826 :
static_cast<long long>(arg.value().ulong_long_value);
832 void resolve_spec_refs(FormatSpec& spec,
const BasicFormatArgs<CharT>& args,
size_t& arg_id) {
834 const size_t ref = spec.width_ref ==
kSpecRefAuto ? arg_id++ :
static_cast<size_t>(spec.width_ref);
835 const long long value = spec_ref_value(args.get(ref));
836 spec.width = value > 0 ? (value > 999999999 ? 999999999 :
static_cast<int>(value)) : 0;
840 const size_t ref = spec.precision_ref ==
kSpecRefAuto ? arg_id++ :
static_cast<size_t>(spec.precision_ref);
841 const long long value = spec_ref_value(args.get(ref));
842 spec.precision = value >= 0 ? (value > 999999999 ? 999999999 :
static_cast<int>(value)) : -1;
846 void write_fill(
char fill,
size_t count) { writer_.write_fill(fill, count); }
848 void write_padded(
const FormatSpec& spec, std::string_view prefix, std::string_view body,
Align default_align,
850 const size_t content = prefix.size() + body.size();
851 const size_t width =
static_cast<size_t>(spec.width);
852 const size_t pad = width > content ? width - content : 0U;
855 if (!prefix.empty()) {
856 writer_.write(prefix);
864 if (spec.zero && spec.align ==
Align::kNone && allow_zero) {
865 if (!prefix.empty()) {
866 writer_.write(prefix);
869 write_fill(
'0', pad);
884 write_fill(spec.fill, left);
886 if (!prefix.empty()) {
887 writer_.write(prefix);
891 write_fill(spec.fill, pad - left);
894 void write_int_spec(
long long value,
const FormatSpec& spec) {
895 if VUNLIKELY (spec.type ==
'c' && value >= CHAR_MIN && value <= CHAR_MAX) {
896 write_char_spec(
static_cast<char>(value), spec);
902 write_uint_spec(
static_cast<unsigned long long>(-(value + 1)) + 1U,
true, spec);
904 write_uint_spec(
static_cast<unsigned long long>(value),
false, spec);
908 void write_uint_spec(
unsigned long long magnitude,
bool negative,
const FormatSpec& spec) {
909 if VUNLIKELY (spec.type ==
'c' && !negative && magnitude <=
static_cast<unsigned long long>(CHAR_MAX)) {
910 write_char_spec(
static_cast<char>(magnitude), spec);
916 size_t prefix_size = 0;
919 prefix[prefix_size++] =
'-';
921 prefix[prefix_size++] =
'+';
923 prefix[prefix_size++] =
' ';
927 size_t digit_count = 0;
928 const char* digit_begin = digits;
934 digit_begin = digits +
sizeof(digits) - digit_count;
937 prefix[prefix_size++] =
'0';
938 prefix[prefix_size++] = spec.type;
944 digit_begin = digits +
sizeof(digits) - digit_count;
946 if (spec.alt && magnitude != 0U) {
947 prefix[prefix_size++] =
'0';
954 digit_begin = digits +
sizeof(digits) - digit_count;
957 prefix[prefix_size++] =
'0';
958 prefix[prefix_size++] = spec.type;
967 write_padded(spec, std::string_view(prefix, prefix_size), std::string_view(digit_begin, digit_count),
Align::kRight,
971 void write_bool_spec(
bool value,
const FormatSpec& spec) {
980 FormatSpec decimal_spec = spec;
981 decimal_spec.type = spec.type ==
'c' ?
'd' : spec.type;
982 write_uint_spec(value ? 1U : 0U,
false, decimal_spec);
990 write_padded(spec, std::string_view(), value ? std::string_view(
"true") : std::string_view(
"false"),
Align::kLeft,
994 void write_char_spec(
char value,
const FormatSpec& spec) {
996 write_debug_spec(std::string_view(&value, 1),
'\'', spec);
1001 switch (spec.type) {
1008 write_int_spec(
static_cast<long long>(
static_cast<unsigned char>(value)), spec);
1014 write_padded(spec, std::string_view(), std::string_view(&value, 1),
Align::kLeft,
false);
1017 template <
typename FloatT>
1018 void write_float_spec(FloatT value,
const FormatSpec& spec) {
1019 constexpr
size_t kMaxIntegerDigits = std::is_same_v<FloatT, long double> ? 4933U : 309U;
1020 constexpr
int kMaxPrecision = 340;
1021 char buf[kMaxIntegerDigits + 3U + kMaxPrecision];
1023 const int precision = spec.precision > kMaxPrecision ? kMaxPrecision : spec.precision;
1025 if (spec.type ==
'\0' && precision < 0 && !spec.alt) {
1026 if constexpr (std::is_same_v<FloatT, long double>) {
1028 }
else if constexpr (std::is_same_v<FloatT, float>) {
1034 if constexpr (std::is_same_v<FloatT, long double>) {
1041 std::string_view text(buf,
size);
1044 if (!text.empty() && text.front() ==
'-') {
1046 text.remove_prefix(1);
1053 const bool numeric = !text.empty() && text.front() >=
'0' && text.front() <=
'9';
1055 write_padded(spec, sign !=
'\0' ? std::string_view(&sign, 1) : std::string_view(), text,
Align::kRight, numeric);
1058 static size_t escape_debug_char(
char c,
char quote,
bool escape_high,
char (&
out)[8]) {
1086 const auto code =
static_cast<unsigned char>(c);
1088 if VUNLIKELY (code < 0x20U || code == 0x7FU || (escape_high && code > 0x7FU)) {
1089 static constexpr
const char kHexDigits[] =
"0123456789abcdef";
1092 out[n++] = code > 0x7FU ?
'x' :
'u';
1095 if (code >= 0x10U) {
1096 out[n++] = kHexDigits[code >> 4U];
1099 out[n++] = kHexDigits[code & 0xFU];
1110 void write_debug_spec(std::string_view sv,
char quote,
const FormatSpec& spec) {
1111 const bool escape_high = quote ==
'\'';
1115 for (
const char c : sv) {
1116 body += escape_debug_char(c, quote, escape_high, piece);
1119 const size_t width =
static_cast<size_t>(spec.width);
1120 const size_t pad = width > body ? width - body : 0U;
1130 write_fill(spec.fill, left);
1132 writer_.write(quote);
1134 for (
const char c : sv) {
1135 writer_.write(piece, escape_debug_char(c, quote, escape_high, piece));
1138 writer_.write(quote);
1139 write_fill(spec.fill, pad - left);
1142 void write_cstring_spec(
const char* str,
const FormatSpec& spec) {
1144 write_string_spec(std::string_view(str, std::strlen(str)), spec);
1146 write_string_spec(std::string_view(
"(null)"), spec);
1150 void write_string_spec(std::string_view sv,
const FormatSpec& spec) {
1151 if (spec.precision >= 0 &&
static_cast<size_t>(spec.precision) < sv.size()) {
1152 sv = sv.substr(0,
static_cast<size_t>(spec.precision));
1156 write_debug_spec(sv,
'"', spec);
1161 write_padded(spec, std::string_view(), sv,
Align::kLeft,
false);
1164 void write_pointer_spec(
const void* ptr,
const FormatSpec& spec) {
1167 write_padded(spec, std::string_view(buf, 2), std::string_view(buf + 2,
size - 2),
Align::kRight,
true);
1170 void write_arg_spec(
const FormatArg<CharT>& arg,
const FormatSpec& spec) {
1171 switch (arg.type()) {
1173 write_int_spec(arg.value().int_value, spec);
1176 write_uint_spec(arg.value().uint_value,
false, spec);
1179 write_int_spec(arg.value().long_long_value, spec);
1182 write_uint_spec(arg.value().ulong_long_value,
false, spec);
1185 write_bool_spec(arg.value().bool_value, spec);
1188 write_char_spec(arg.value().char_value, spec);
1191 write_float_spec(arg.value().float_value, spec);
1194 write_float_spec(arg.value().double_value, spec);
1197 write_float_spec(arg.value().long_double_value, spec);
1200 write_cstring_spec(arg.value().string_value, spec);
1203 write_string_spec(arg.value().string_view_value, spec);
1206 write_pointer_spec(arg.value().pointer_value, spec);
1219 template <
typename... ArgsT>
1224 template <
size_t NumT>
1229 template <
typename StrT, std::enable_if_t<std::is_convertible_v<const StrT&, std::
string_view>,
int> = 0>
1234 inline operator std::string_view()
const {
return str; }
1239 template <
typename... ArgsT>
1242 template <
typename OutputItT>
1266 template <
typename... ArgsT>
1284 template <
typename... ArgsT>
1297 template <
size_t NumT,
typename... ArgsT>
1314 template <
typename OutputItT,
typename... ArgsT,
1316 std::enable_if_t<detail::kIsOutputIterator<detail::RemoveCvref<OutputItT>> &&
1317 !std::is_array_v<std::remove_reference_t<OutputItT>>,
1333 template <
typename... ArgsT>
1346 template <
typename UIntT>
1358 template <
typename UIntT>
1360 char* end = buf + num_digits;
1362 while (value >= 10) {
1363 auto digit =
static_cast<unsigned>(value % 10);
1364 *--end =
static_cast<char>(
'0' + digit);
1368 *--end =
static_cast<char>(
'0' + value);
1375 return static_cast<size_t>(num_digits);
1381 unsigned u =
static_cast<unsigned>(-(value + 1)) + 1;
1393 return static_cast<size_t>(num_digits);
1399 unsigned long long u =
static_cast<unsigned long long>(-(value + 1)) + 1;
1408 bool upper) noexcept {
1409 static constexpr
const char kLowerDigits[] =
"0123456789abcdef";
1410 static constexpr
const char kUpperDigits[] =
"0123456789ABCDEF";
1411 const char* digit_chars = upper ? kUpperDigits : kLowerDigits;
1412 const unsigned mask = (1U << base_bits) - 1U;
1413 char* p = buf +
sizeof(buf);
1416 *--p = digit_chars[value & mask];
1417 value >>= base_bits;
1418 }
while (value != 0U);
1420 return static_cast<size_t>(buf +
sizeof(buf) - p);
1428 : begin_(buf), ptr_(buf), end_(buf + size) {}
1445 total_size_ += count;
1447 auto avail =
static_cast<size_t>(end_ - ptr_);
1448 size_t n = (count <= avail) ? count : avail;
1451 std::memcpy(ptr_, s, n);
1459 total_size_ += count;
1461 const auto avail =
static_cast<size_t>(end_ - ptr_);
1462 const size_t n = count <= avail ? count : avail;
1465 std::memset(ptr_, fill, n);
1470 template <
typename... ArgsT>
1472 const ArgsT&... args) {
1476 template <
typename... ArgsT>
1478 const ArgsT&... args) {
1481 std::string_view remaining = fmt.get();
1484 if VLIKELY (writer.try_format(remaining, arg_id, args...)) {
1485 size_t total = writer.total_size();
1487 return {writer.out(), total, total > n};
1490 format::detail::FormatArgStore<char, format::detail::RemoveCvref<ArgsT>...> arg_store{args...};
1492 writer.format(remaining, fargs, arg_id);
1494 size_t total = writer.total_size();
1496 return {writer.out(), total, total > n};
1499 template <
size_t NumT,
typename... ArgsT>
1500 inline format::FormatToResult
format::format_to(
char (&out)[NumT], format_string<ArgsT...> fmt,
const ArgsT&... args) {
1503 return {result.out, result.size, result.truncated};
1506 template <
typename OutputItT,
typename... ArgsT,
1507 std::enable_if_t<format::detail::kIsOutputIterator<format::detail::RemoveCvref<OutputItT>> &&
1508 !std::is_array_v<std::remove_reference_t<OutputItT>>,
1510 inline format::detail::RemoveCvref<OutputItT>
format::format_to(OutputItT&& out, format_string<ArgsT...> fmt,
1511 const ArgsT&... args) {
1512 using ItT = format::detail::RemoveCvref<OutputItT>;
1517 format::detail::IteratorWriter<ItT> iter_writer(out);
1518 format::detail::FormatWriter<char, format::detail::IteratorWriter<ItT>> writer(iter_writer);
1520 writer.format(fmt.get(), fargs);
1522 return writer.out();
1525 template <
typename... ArgsT>
1530 if VLIKELY (!result.truncated) {
1531 return std::string(buf, result.size);
1534 std::string full(result.size,
'\0');
Cross-platform macros for visibility, branch hints, copy prevention, singletons and string helpers.
#define VUNLIKELY(...)
Short alias for VLINK_UNLIKELY.
Definition: macros.h:292
#define VLINK_EXPORT
Definition: macros.h:81
#define VLIKELY(...)
Short alias for VLINK_LIKELY.
Definition: macros.h:284
constexpr auto values() noexcept
Definition: name_detector.h:511