From 922fe90ac30bee0f51b1b73c261fa815f70de545 Mon Sep 17 00:00:00 2001 From: vitaut Date: Fri, 31 Jul 2015 08:02:38 -0700 Subject: [PATCH] Improve integer formatting performance and fix bogus Coverity warning about uncaught exception in format_error_code (via write_int). --- format.h | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/format.h b/format.h index a7c2389b..2a985f16 100644 --- a/format.h +++ b/format.h @@ -1867,6 +1867,27 @@ class BasicWriter { return internal::make_ptr(&buffer_[size], n); } + // Writes an unsigned decimal integer. + template + Char *write_unsigned_decimal(UInt value, unsigned prefix_size = 0) { + unsigned num_digits = internal::count_digits(value); + Char *ptr = get(grow_buffer(prefix_size + num_digits)); + internal::format_decimal(ptr + prefix_size, value, num_digits); + return ptr; + } + + // Writes a decimal integer. + template + void write_decimal(Int value) { + typename internal::IntTraits::MainType abs_value = value; + if (internal::is_negative(value)) { + abs_value = 0 - abs_value; + *write_unsigned_decimal(abs_value, 1) = '-'; + } else { + write_unsigned_decimal(abs_value, 0); + } + } + // Prepare a buffer for integer formatting. CharPtr prepare_int_buffer(unsigned num_digits, const EmptySpec &, const char *prefix, unsigned prefix_size) { @@ -1995,19 +2016,22 @@ class BasicWriter { FMT_VARIADIC_VOID(write, BasicCStringRef) BasicWriter &operator<<(int value) { - return *this << IntFormatSpec(value); + write_decimal(value); + return *this; } BasicWriter &operator<<(unsigned value) { return *this << IntFormatSpec(value); } BasicWriter &operator<<(long value) { - return *this << IntFormatSpec(value); + write_decimal(value); + return *this; } BasicWriter &operator<<(unsigned long value) { return *this << IntFormatSpec(value); } BasicWriter &operator<<(LongLong value) { - return *this << IntFormatSpec(value); + write_decimal(value); + return *this; } /**