39 #if defined(_WIN32) && defined(__MINGW32__) 44 # if defined(NOMINMAX) || defined(FMT_WIN_MINMAX) 57 # define FMT_CATCH(x) catch (x) 59 # define FMT_TRY if (true) 60 # define FMT_CATCH(x) if (false) 64 # pragma warning(push) 65 # pragma warning(disable: 4127) // conditional expression is constant 66 # pragma warning(disable: 4702) // unreachable code 69 # pragma warning(disable: 4996) 90 # define FMT_SNPRINTF snprintf 92 inline int fmt_snprintf(
char *buffer,
size_t size,
const char *
format, ...) {
94 va_start(args, format);
95 int result = vsnprintf_s(buffer, size, _TRUNCATE, format, args);
99 # define FMT_SNPRINTF fmt_snprintf 102 #if defined(_WIN32) && defined(__MINGW32__) && !defined(__NO_ISOCEXT) 103 # define FMT_SWPRINTF snwprintf 105 # define FMT_SWPRINTF swprintf 106 #endif // defined(_WIN32) && defined(__MINGW32__) && !defined(__NO_ISOCEXT) 110 template <
bool IsSigned>
112 template <
typename T>
113 static bool fits_in_int(T value) {
114 unsigned max = INT_MAX;
117 static bool fits_in_int(
bool) {
return true; }
121 struct IntChecker<true> {
122 template <
typename T>
123 static bool fits_in_int(T value) {
124 return value >= INT_MIN && value <= INT_MAX;
126 static bool fits_in_int(
int) {
return true; }
129 const char RESET_COLOR[] =
"\x1b[0m";
144 FMT_ASSERT(buffer != 0 && buffer_size != 0,
"invalid buffer");
150 std::size_t buffer_size_;
158 return result == -1 ? errno : result;
164 if (message == buffer_ && strlen(buffer_) == buffer_size_ - 1)
171 int handle(internal::Null<>) {
172 return fallback(
strerror_s(buffer_, buffer_size_, error_code_));
176 int fallback(
int result) {
178 return result == 0 && strlen(buffer_) == buffer_size_ - 1 ?
183 int fallback(internal::Null<>) {
185 buffer_ = strerror(error_code_);
190 StrError(
int err_code,
char *&buf, std::size_t buf_size)
191 : error_code_(err_code), buffer_(buf), buffer_size_(buf_size) {}
198 return StrError(error_code, buffer, buffer_size).run();
201 void format_error_code(Writer &out,
int error_code,
207 static const char SEP[] =
": ";
208 static const char ERROR_STR[] =
"error ";
210 std::size_t error_code_size =
sizeof(SEP) +
sizeof(ERROR_STR) - 2;
211 typedef internal::IntTraits<int>::MainType MainType;
212 MainType abs_value =
static_cast<MainType
>(error_code);
214 abs_value = 0 - abs_value;
219 out << message << SEP;
220 out << ERROR_STR << error_code;
224 void report_error(FormatFunc func,
int error_code,
227 func(full_message, error_code, message);
230 std::fwrite(full_message.data(), full_message.size(), 1, stderr);
231 std::fputc(
'\n', stderr);
235 class IsZeroInt :
public ArgVisitor<IsZeroInt, bool> {
237 template <
typename T>
238 bool visit_any_int(T value) {
return value == 0; }
243 class WidthHandler :
public ArgVisitor<WidthHandler, unsigned> {
250 explicit WidthHandler(FormatSpec &spec) : spec_(spec) {}
252 void report_unhandled_arg() {
253 FMT_THROW(FormatError(
"width is not integer"));
256 template <
typename T>
257 unsigned visit_any_int(T value) {
258 typedef typename internal::IntTraits<T>::MainType UnsignedType;
259 UnsignedType width =
static_cast<UnsignedType
>(value);
265 FMT_THROW(FormatError(
"number is too big"));
266 return static_cast<unsigned>(width);
270 class PrecisionHandler :
public ArgVisitor<PrecisionHandler, int> {
272 void report_unhandled_arg() {
273 FMT_THROW(FormatError(
"precision is not integer"));
276 template <
typename T>
277 int visit_any_int(T value) {
278 if (!IntChecker<std::numeric_limits<T>::is_signed>::fits_in_int(value))
279 FMT_THROW(FormatError(
"number is too big"));
280 return static_cast<int>(value);
284 template <
typename T,
typename U>
289 template <
typename T>
290 struct is_same<T, T> {
298 template <
typename T =
void>
299 class ArgConverter :
public ArgVisitor<ArgConverter<T>, void> {
307 ArgConverter(internal::Arg &
arg,
wchar_t type)
308 : arg_(arg), type_(type) {}
310 void visit_bool(
bool value) {
312 visit_any_int(value);
315 template <
typename U>
316 void visit_any_int(U value) {
317 bool is_signed = type_ ==
'd' || type_ ==
'i';
319 typedef typename internal::Conditional<
320 is_same<T, void>::value, U, T>::type TargetType;
321 if (
sizeof(TargetType) <=
sizeof(
int)) {
325 arg_.int_value =
static_cast<int>(
static_cast<TargetType
>(value));
329 arg_.uint_value =
static_cast<unsigned>(
static_cast<Unsigned
>(value));
337 arg_.long_long_value =
static_cast<LongLong>(value);
340 arg_.ulong_long_value =
348 class CharConverter :
public ArgVisitor<CharConverter, void> {
355 explicit CharConverter(internal::Arg &
arg) : arg_(arg) {}
357 template <
typename T>
358 void visit_any_int(T value) {
360 arg_.int_value =
static_cast<char>(value);
367 template <
typename Char>
372 this->spec().type_ = 0;
373 this->
write(
"(nil)");
384 if (fmt_spec.
type_ !=
's')
385 return this->visit_any_int(value);
397 if (fmt_spec.
width_ > 1) {
401 std::fill_n(out, fmt_spec.
width_ - 1, fill);
402 out += fmt_spec.
width_ - 1;
404 std::fill_n(out + 1, fmt_spec.
width_ - 1, fill);
409 *out =
static_cast<Char>(value);
414 Base::visit_cstring(value);
415 else if (this->spec().type_ ==
'p')
416 write_null_pointer();
418 this->
write(
"(null)");
423 return Base::visit_pointer(value);
424 this->spec().type_ = 0;
425 write_null_pointer();
430 const Char format_str[] = {
'}', 0};
440 error_code_ = err_code;
443 std::runtime_error &base = *
this;
444 base = std::runtime_error(w.
str());
447 template <
typename T>
449 char *buffer, std::size_t size,
const char *
format,
450 unsigned width,
int precision, T value) {
452 return precision < 0 ?
456 return precision < 0 ?
458 FMT_SNPRINTF(buffer, size, format, width, precision, value);
461 template <
typename T>
463 wchar_t *buffer, std::size_t size,
const wchar_t *
format,
464 unsigned width,
int precision, T value) {
466 return precision < 0 ?
470 return precision < 0 ?
472 FMT_SWPRINTF(buffer, size, format, width, precision, value);
475 template <
typename T>
477 "0001020304050607080910111213141516171819" 478 "2021222324252627282930313233343536373839" 479 "4041424344454647484950515253545556575859" 480 "6061626364656667686970717273747576777879" 481 "8081828384858687888990919293949596979899";
483 #define FMT_POWERS_OF_10(factor) \ 491 factor * 100000000, \ 494 template <
typename T>
499 template <
typename T>
511 if (std::isprint(static_cast<unsigned char>(code))) {
513 fmt::format(
"unknown format code '{}' for {}", code, type)));
516 fmt::format(
"unknown format code '\\x{:02x}' for {}",
517 static_cast<unsigned>(code), type)));
520 #if FMT_USE_WINDOWS_H 523 static const char ERROR_MSG[] =
"cannot convert string from UTF-8 to UTF-16";
524 if (s.
size() > INT_MAX)
525 FMT_THROW(WindowsError(ERROR_INVALID_PARAMETER, ERROR_MSG));
526 int s_size =
static_cast<int>(s.
size());
527 int length = MultiByteToWideChar(
528 CP_UTF8, MB_ERR_INVALID_CHARS, s.
data(), s_size, 0, 0);
530 FMT_THROW(WindowsError(GetLastError(), ERROR_MSG));
531 buffer_.resize(length + 1);
532 length = MultiByteToWideChar(
533 CP_UTF8, MB_ERR_INVALID_CHARS, s.
data(), s_size, &buffer_[0], length);
535 FMT_THROW(WindowsError(GetLastError(), ERROR_MSG));
540 if (
int error_code =
convert(s)) {
542 "cannot convert string from UTF-16 to UTF-8"));
547 if (s.
size() > INT_MAX)
548 return ERROR_INVALID_PARAMETER;
549 int s_size =
static_cast<int>(s.
size());
550 int length = WideCharToMultiByte(CP_UTF8, 0, s.
data(), s_size, 0, 0, 0, 0);
552 return GetLastError();
553 buffer_.resize(length + 1);
554 length = WideCharToMultiByte(
555 CP_UTF8, 0, s.
data(), s_size, &buffer_[0], length, 0, 0);
557 return GetLastError();
562 FMT_FUNC void fmt::WindowsError::init(
564 error_code_ = err_code;
566 internal::format_windows_error(w, err_code,
format(format_str, args));
567 std::runtime_error &base = *
this;
568 base = std::runtime_error(w.
str());
571 FMT_FUNC void fmt::internal::format_windows_error(
578 wchar_t *system_message = &buffer[0];
579 int result = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
580 0, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
581 system_message, static_cast<uint32_t>(buffer.
size()), 0);
583 UTF16ToUTF8 utf8_message;
584 if (utf8_message.convert(system_message) == ERROR_SUCCESS) {
585 out << message <<
": " << utf8_message;
590 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
595 fmt::format_error_code(out, error_code, message);
598 #endif // FMT_USE_WINDOWS_H 607 char *system_message = &buffer[0];
608 int result = safe_strerror(error_code, system_message, buffer.
size());
610 out << message <<
": " << system_message;
613 if (result != ERANGE)
618 fmt::format_error_code(out, error_code, message);
621 template <
typename Char>
626 const NamedArg *named_arg = 0;
630 for (
unsigned i = 0;; ++i) {
636 named_arg =
static_cast<const NamedArg*
>(args.
values_[i].
pointer);
637 map_.push_back(
Pair(named_arg->
name, *named_arg));
648 named_arg =
static_cast<const NamedArg*
>(args.
args_[i].
pointer);
649 map_.push_back(
Pair(named_arg->
name, *named_arg));
657 named_arg =
static_cast<const NamedArg*
>(args.
args_[i].
pointer);
658 map_.push_back(
Pair(named_arg->
name, *named_arg));
666 template <
typename Char>
668 FMT_THROW(std::runtime_error(
"buffer overflow"));
672 unsigned arg_index,
const char *&error) {
673 Arg arg = args_[arg_index];
676 error =
"argument index out of range";
687 template <
typename Char>
714 template <
typename Char>
716 const Char *s,
unsigned arg_index) {
718 const char *error = 0;
719 Arg arg = arg_index == UINT_MAX ?
726 template <
typename Char>
729 unsigned arg_index = UINT_MAX;
731 if (c >=
'0' && c <=
'9') {
749 parse_flags(spec, s);
751 if (*s >=
'0' && *s <=
'9') {
753 }
else if (*s ==
'*') {
755 spec.
width_ = WidthHandler(spec).visit(get_arg(s));
760 template <
typename Char>
764 const Char *s = start;
767 if (c !=
'%')
continue;
769 write(writer, start, s);
773 write(writer, start, s - 1);
779 unsigned arg_index = parse_header(s, spec);
784 if (
'0' <= *s && *s <=
'9') {
786 }
else if (*s ==
'*') {
788 spec.
precision_ = PrecisionHandler().visit(get_arg(s));
792 Arg arg = get_arg(s, arg_index);
795 if (spec.
fill_ ==
'0') {
806 ArgConverter<signed char>(
arg, *++s).visit(arg);
808 ArgConverter<short>(
arg, *s).visit(arg);
812 ArgConverter<fmt::LongLong>(
arg, *++s).visit(arg);
814 ArgConverter<long>(
arg, *s).visit(arg);
817 ArgConverter<intmax_t>(
arg, *s).visit(arg);
820 ArgConverter<std::size_t>(
arg, *s).visit(arg);
823 ArgConverter<std::ptrdiff_t>(
arg, *s).visit(arg);
831 ArgConverter<void>(
arg, *s).visit(arg);
837 spec.
type_ =
static_cast<char>(*s++);
840 switch (spec.
type_) {
846 CharConverter(arg).visit(arg);
856 write(writer, start, s);
865 #if FMT_USE_WINDOWS_H 866 FMT_FUNC void fmt::report_windows_error(
869 fmt::report_error(internal::format_windows_error, error_code, message);
875 w.
write(format_str, args);
876 std::fwrite(w.
data(), 1, w.
size(), f);
880 print(stdout, format_str, args);
884 char escape[] =
"\x1b[30m";
885 escape[3] =
static_cast<char>(
'0' +
c);
886 std::fputs(escape, stdout);
888 std::fputs(RESET_COLOR, stdout);
894 std::size_t size = w.
size();
895 return std::fwrite(w.
data(), 1, size, f) < size ? -1 : static_cast<int>(size);
898 #ifndef FMT_HEADER_ONLY 912 char *buffer, std::size_t size,
const char *format,
913 unsigned width,
int precision,
double value);
915 template int fmt::internal::CharTraits<char>::format_float(
916 char *buffer, std::size_t size,
const char *format,
917 unsigned width,
int precision,
long double value);
929 wchar_t *buffer, std::size_t size,
const wchar_t *format,
930 unsigned width,
int precision,
double value);
932 template int fmt::internal::CharTraits<wchar_t>::format_float(
933 wchar_t *buffer, std::size_t size,
const wchar_t *format,
934 unsigned width,
int precision,
long double value);
936 #endif // FMT_HEADER_ONLY 939 # pragma warning(pop)
bool is_negative(T value)
const Char * data() const
FMT_API void format_system_error(fmt::Writer &out, int error_code, fmt::StringRef message) FMT_NOEXCEPT
void resize(std::size_t new_size)
FMT_API void print(std::FILE *f, CStringRef format_str, ArgList args)
FMT_API void report_unknown_type(char code, const char *type)
void write(BasicCStringRef< Char > format, ArgList args)
std::string format(CStringRef format_str, ArgList args)
const Char * c_str() const
FMT_GCC_EXTENSION typedef unsigned long long ULongLong
void init(int err_code, CStringRef format_str, ArgList args)
std::basic_string< Char > str() const
BasicStringRef< Char > name
unsigned parse_nonnegative_int(const Char *&s)
const Char * data() const FMT_NOEXCEPT
FMT_API void grow(std::size_t size)
Yes & convert(fmt::ULongLong)
FMT_API void print_colored(Color c, CStringRef format, ArgList args)
bool write(ros_opcua_srvs::Write::Request &req, ros_opcua_srvs::Write::Response &res)
BasicStringRef< char > StringRef
BasicMemoryWriter< char > MemoryWriter
CharPtr grow_buffer(std::size_t n)
internal::PairMatcher< FirstMatcher, SecondMatcher > Pair(FirstMatcher first_matcher, SecondMatcher second_matcher)
BasicWriter< char > Writer
FMT_API void report_system_error(int error_code, StringRef message) FMT_NOEXCEPT
const internal::Arg * args_
void write_int(T value, Spec spec)
internal::CharTraits< Char >::CharPtr CharPtr
internal::NamedArg< char > arg(StringRef name, const T &arg)
FMT_API void init(const ArgList &args)
const char * CharPtr(const char *s)
unsigned count_digits(uint64_t n)
const char * StrError(int errnum)
FMT_API int fprintf(std::FILE *f, CStringRef format, ArgList args)
const internal::Value * values_
internal::Arg::Type type(unsigned index) const
FMT_GCC_EXTENSION typedef long long LongLong
void printf(BasicWriter< Char > &w, BasicCStringRef< Char > format, ArgList args)