From 4d049cf598ad32cbeba744e903b03ee3557c6a75 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Mon, 28 Jul 2014 08:41:50 -0700 Subject: [PATCH] More fixes for https://github.com/cppformat/cppformat/issues/50. --- format.cc | 60 ++++++++++++++++++++--------------------- format.h | 69 +++++++++++++++++++++++------------------------ posix.cc | 4 +-- test/util-test.cc | 21 +++++++-------- 4 files changed, 75 insertions(+), 79 deletions(-) diff --git a/format.cc b/format.cc index 3b502891..733e10f1 100644 --- a/format.cc +++ b/format.cc @@ -443,17 +443,17 @@ class fmt::internal::ArgFormatter : CharPtr out = CharPtr(); if (spec_.width_ > 1) { Char fill = static_cast(spec_.fill()); - out = writer_.GrowBuffer(spec_.width_); + out = writer_.grow_buffer(spec_.width_); if (spec_.align_ == fmt::ALIGN_RIGHT) { std::fill_n(out, spec_.width_ - 1, fill); out += spec_.width_ - 1; } else if (spec_.align_ == fmt::ALIGN_CENTER) { - out = writer_.FillPadding(out, spec_.width_, 1, fill); + out = writer_.fill_padding(out, spec_.width_, 1, fill); } else { std::fill_n(out + 1, spec_.width_ - 1, fill); } } else { - out = writer_.GrowBuffer(1); + out = writer_.grow_buffer(1); } *out = static_cast(value); } @@ -489,7 +489,7 @@ void fmt::internal::FormatErrorReporter::operator()( // content area. template typename fmt::BasicWriter::CharPtr - fmt::BasicWriter::FillPadding(CharPtr buffer, + fmt::BasicWriter::fill_padding(CharPtr buffer, unsigned total_size, std::size_t content_size, wchar_t fill) { std::size_t padding = total_size - content_size; std::size_t left_padding = padding / 2; @@ -631,9 +631,9 @@ void fmt::BasicWriter::write_double(T value, const FormatSpec &spec) { if (spec.align() == ALIGN_CENTER && spec.width() > static_cast(n)) { unsigned width = spec.width(); - CharPtr p = GrowBuffer(width); + CharPtr p = grow_buffer(width); std::copy(p, p + n, p + (width - n) / 2); - FillPadding(p, spec.width(), n, fill); + fill_padding(p, spec.width(), n, fill); return; } if (spec.fill() != ' ' || sign) { @@ -642,7 +642,7 @@ void fmt::BasicWriter::write_double(T value, const FormatSpec &spec) { if (sign) *(start - 1) = sign; } - GrowBuffer(n); + grow_buffer(n); return; } // If n is negative we ask to increase the capacity by at least 1, @@ -672,7 +672,7 @@ void fmt::BasicWriter::write_str( template inline const Arg - &fmt::BasicFormatter::ParseArgIndex(const Char *&s) { + &fmt::BasicFormatter::parse_arg_index(const Char *&s) { unsigned arg_index = 0; if (*s < '0' || *s > '9') { if (*s != '}' && *s != ':') @@ -696,7 +696,7 @@ inline const Arg } template -void fmt::BasicFormatter::CheckSign( +void fmt::BasicFormatter::check_sign( const Char *&s, const Arg &arg) { char sign = static_cast(*s); if (arg.type > Arg::LAST_NUMERIC_TYPE) { @@ -742,7 +742,7 @@ const Arg &fmt::internal::FormatterBase::handle_arg_index(unsigned arg_index) { } template -void fmt::internal::PrintfFormatter::ParseFlags( +void fmt::internal::PrintfFormatter::parse_flags( FormatSpec &spec, const Char *&s) { for (;;) { switch (*s++) { @@ -769,7 +769,7 @@ void fmt::internal::PrintfFormatter::ParseFlags( } template -unsigned fmt::internal::PrintfFormatter::ParseHeader( +unsigned fmt::internal::PrintfFormatter::parse_header( const Char *&s, FormatSpec &spec) { unsigned arg_index = UINT_MAX; Char c = *s; @@ -791,7 +791,7 @@ unsigned fmt::internal::PrintfFormatter::ParseHeader( } } } - ParseFlags(spec, s); + parse_flags(spec, s); // Parse width. if (*s >= '0' && *s <= '9') { spec.width_ = ParseNonnegativeInt(s, error_); @@ -803,7 +803,7 @@ unsigned fmt::internal::PrintfFormatter::ParseHeader( } template -void fmt::internal::PrintfFormatter::Format( +void fmt::internal::PrintfFormatter::format( BasicWriter &writer, BasicStringRef format, const ArgList &args) { const Char *start = format.c_str(); @@ -835,7 +835,7 @@ void fmt::internal::PrintfFormatter::Format( // is OK for both cases. // Parse argument index, flags and width. - unsigned arg_index = ParseHeader(s, spec); + unsigned arg_index = parse_header(s, spec); // Parse precision. if (*s == '.') { @@ -906,7 +906,7 @@ void fmt::internal::PrintfFormatter::Format( CharPtr out = CharPtr(); if (spec.width_ > 1) { Char fill = ' '; - out = writer.GrowBuffer(spec.width_); + out = writer.grow_buffer(spec.width_); if (spec.align_ != ALIGN_LEFT) { std::fill_n(out, spec.width_ - 1, fill); out += spec.width_ - 1; @@ -914,7 +914,7 @@ void fmt::internal::PrintfFormatter::Format( std::fill_n(out + 1, spec.width_ - 1, fill); } } else { - out = writer.GrowBuffer(1); + out = writer.grow_buffer(1); } *out = static_cast(arg.int_value); break; @@ -1000,15 +1000,15 @@ const Char *fmt::BasicFormatter::format( // Parse sign. switch (*s) { case '+': - CheckSign(s, arg); + check_sign(s, arg); spec.flags_ |= SIGN_FLAG | PLUS_FLAG; break; case '-': - CheckSign(s, arg); + check_sign(s, arg); spec.flags_ |= MINUS_FLAG; break; case ' ': - CheckSign(s, arg); + check_sign(s, arg); spec.flags_ |= SIGN_FLAG; break; } @@ -1046,7 +1046,7 @@ const Char *fmt::BasicFormatter::format( } else if (*s == '{') { ++s; ++report_error_.num_open_braces; - const Arg &precision_arg = ParseArgIndex(s); + const Arg &precision_arg = parse_arg_index(s); ULongLong value = 0; switch (precision_arg.type) { case Arg::INT: @@ -1098,7 +1098,7 @@ const Char *fmt::BasicFormatter::format( } template -void fmt::BasicFormatter::Format( +void fmt::BasicFormatter::format( BasicStringRef format_str, const ArgList &args) { const Char *s = start_ = format_str.c_str(); args_ = args; @@ -1115,20 +1115,20 @@ void fmt::BasicFormatter::Format( throw FormatError("unmatched '}' in format"); report_error_.num_open_braces = 1; write(writer_, start_, s - 1); - Arg arg = ParseArgIndex(s); + Arg arg = parse_arg_index(s); s = format(s, arg); } write(writer_, start_, s); } -void fmt::ReportSystemError( +void fmt::report_system_error( int error_code, fmt::StringRef message) FMT_NOEXCEPT(true) { // FIXME: format_system_error may throw ReportError(internal::format_system_error, error_code, message); } #ifdef _WIN32 -void fmt::ReportWinError( +void fmt::report_windows_error( int error_code, fmt::StringRef message) FMT_NOEXCEPT(true) { // FIXME: format_windows_error may throw ReportError(internal::format_windows_error, error_code, message); @@ -1170,25 +1170,25 @@ void fmt::printf(StringRef format, const ArgList &args) { // Explicit instantiations for char. template fmt::BasicWriter::CharPtr - fmt::BasicWriter::FillPadding(CharPtr buffer, + fmt::BasicWriter::fill_padding(CharPtr buffer, unsigned total_size, std::size_t content_size, wchar_t fill); -template void fmt::BasicFormatter::Format( +template void fmt::BasicFormatter::format( BasicStringRef format, const ArgList &args); -template void fmt::internal::PrintfFormatter::Format( +template void fmt::internal::PrintfFormatter::format( BasicWriter &writer, BasicStringRef format, const ArgList &args); // Explicit instantiations for wchar_t. template fmt::BasicWriter::CharPtr - fmt::BasicWriter::FillPadding(CharPtr buffer, + fmt::BasicWriter::fill_padding(CharPtr buffer, unsigned total_size, std::size_t content_size, wchar_t fill); -template void fmt::BasicFormatter::Format( +template void fmt::BasicFormatter::format( BasicStringRef format, const ArgList &args); -template void fmt::internal::PrintfFormatter::Format( +template void fmt::internal::PrintfFormatter::format( BasicWriter &writer, BasicStringRef format, const ArgList &args); diff --git a/format.h b/format.h index c25825c4..a401724d 100644 --- a/format.h +++ b/format.h @@ -867,14 +867,14 @@ protected: template class PrintfFormatter : private FormatterBase { private: - void ParseFlags(FormatSpec &spec, const Char *&s); + void parse_flags(FormatSpec &spec, const Char *&s); // Parses argument index, flags and width and returns the parsed // argument index. - unsigned ParseHeader(const Char *&s, FormatSpec &spec); + unsigned parse_header(const Char *&s, FormatSpec &spec); public: - void Format(BasicWriter &writer, + void format(BasicWriter &writer, BasicStringRef format, const ArgList &args); }; } // namespace internal @@ -888,16 +888,16 @@ private: internal::FormatErrorReporter report_error_; // Parses argument index and returns an argument with this index. - const internal::Arg &ParseArgIndex(const Char *&s); + const internal::Arg &parse_arg_index(const Char *&s); - void CheckSign(const Char *&s, const internal::Arg &arg); + void check_sign(const Char *&s, const internal::Arg &arg); public: explicit BasicFormatter(BasicWriter &w) : writer_(w) {} BasicWriter &writer() { return writer_; } - void Format(BasicStringRef format_str, const ArgList &args); + void format(BasicStringRef format_str, const ArgList &args); const Char *format(const Char *format_str, const internal::Arg &arg); }; @@ -1299,33 +1299,34 @@ class BasicWriter { typedef typename internal::CharTraits::CharPtr CharPtr; #if _SECURE_SCL - static Char *GetBase(CharPtr p) { return p.base(); } + // Returns pointer value. + static Char *get(CharPtr p) { return p.base(); } #else - static Char *GetBase(Char *p) { return p; } + static Char *get(Char *p) { return p; } #endif - static CharPtr FillPadding(CharPtr buffer, + static CharPtr fill_padding(CharPtr buffer, unsigned total_size, std::size_t content_size, wchar_t fill); // Grows the buffer by n characters and returns a pointer to the newly // allocated area. - CharPtr GrowBuffer(std::size_t n) { + CharPtr grow_buffer(std::size_t n) { std::size_t size = buffer_.size(); buffer_.resize(size + n); return internal::make_ptr(&buffer_[size], n); } // Prepare a buffer for integer formatting. - CharPtr PrepareBufferForInt(unsigned num_digits, + CharPtr prepare_int_buffer(unsigned num_digits, const EmptySpec &, const char *prefix, unsigned prefix_size) { unsigned size = prefix_size + num_digits; - CharPtr p = GrowBuffer(size); + CharPtr p = grow_buffer(size); std::copy(prefix, prefix + prefix_size, p); return p + size - 1; } template - CharPtr PrepareBufferForInt(unsigned num_digits, + CharPtr prepare_int_buffer(unsigned num_digits, const Spec &spec, const char *prefix, unsigned prefix_size); // Formats an integer. @@ -1432,7 +1433,7 @@ class BasicWriter { \endrst */ void write(BasicStringRef format, const ArgList &args) { - BasicFormatter(*this).Format(format, args); + BasicFormatter(*this).format(format, args); } FMT_VARIADIC_VOID(write, fmt::BasicStringRef) @@ -1519,18 +1520,18 @@ typename BasicWriter::CharPtr BasicWriter::write_str( const StrChar *s, std::size_t size, const AlignSpec &spec) { CharPtr out = CharPtr(); if (spec.width() > size) { - out = GrowBuffer(spec.width()); + out = grow_buffer(spec.width()); Char fill = static_cast(spec.fill()); if (spec.align() == ALIGN_RIGHT) { std::fill_n(out, spec.width() - size, fill); out += spec.width() - size; } else if (spec.align() == ALIGN_CENTER) { - out = FillPadding(out, spec.width(), size, fill); + out = fill_padding(out, spec.width(), size, fill); } else { std::fill_n(out + size, spec.width() - size, fill); } } else { - out = GrowBuffer(size); + out = grow_buffer(size); } std::copy(s, s + size, out); return out; @@ -1539,7 +1540,7 @@ typename BasicWriter::CharPtr BasicWriter::write_str( template template typename fmt::BasicWriter::CharPtr - fmt::BasicWriter::PrepareBufferForInt( + fmt::BasicWriter::prepare_int_buffer( unsigned num_digits, const Spec &spec, const char *prefix, unsigned prefix_size) { unsigned width = spec.width(); @@ -1553,35 +1554,35 @@ typename fmt::BasicWriter::CharPtr unsigned number_size = prefix_size + spec.precision(); AlignSpec subspec(number_size, '0', ALIGN_NUMERIC); if (number_size >= width) - return PrepareBufferForInt(num_digits, subspec, prefix, prefix_size); + return prepare_int_buffer(num_digits, subspec, prefix, prefix_size); buffer_.reserve(width); unsigned fill_size = width - number_size; if (align != ALIGN_LEFT) { - CharPtr p = GrowBuffer(fill_size); + CharPtr p = grow_buffer(fill_size); std::fill(p, p + fill_size, fill); } - CharPtr result = PrepareBufferForInt( + CharPtr result = prepare_int_buffer( num_digits, subspec, prefix, prefix_size); if (align == ALIGN_LEFT) { - CharPtr p = GrowBuffer(fill_size); + CharPtr p = grow_buffer(fill_size); std::fill(p, p + fill_size, fill); } return result; } unsigned size = prefix_size + num_digits; if (width <= size) { - CharPtr p = GrowBuffer(size); + CharPtr p = grow_buffer(size); std::copy(prefix, prefix + prefix_size, p); return p + size - 1; } - CharPtr p = GrowBuffer(width); + CharPtr p = grow_buffer(width); CharPtr end = p + width; if (align == ALIGN_LEFT) { std::copy(prefix, prefix + prefix_size, p); p += size; std::fill(p, end, fill); } else if (align == ALIGN_CENTER) { - p = FillPadding(p, width, size, fill); + p = fill_padding(p, width, size, fill); std::copy(prefix, prefix + prefix_size, p); p += size; } else { @@ -1617,9 +1618,9 @@ void BasicWriter::write_int(T value, const Spec &spec) { switch (spec.type()) { case 0: case 'd': { unsigned num_digits = internal::count_digits(abs_value); - CharPtr p = PrepareBufferForInt( + CharPtr p = prepare_int_buffer( num_digits, spec, prefix, prefix_size) + 1 - num_digits; - internal::format_decimal(GetBase(p), abs_value, num_digits); + internal::format_decimal(get(p), abs_value, num_digits); break; } case 'x': case 'X': { @@ -1632,7 +1633,7 @@ void BasicWriter::write_int(T value, const Spec &spec) { do { ++num_digits; } while ((n >>= 4) != 0); - Char *p = GetBase(PrepareBufferForInt( + Char *p = get(prepare_int_buffer( num_digits, spec, prefix, prefix_size)); n = abs_value; const char *digits = spec.type() == 'x' ? @@ -1652,8 +1653,7 @@ void BasicWriter::write_int(T value, const Spec &spec) { do { ++num_digits; } while ((n >>= 1) != 0); - Char *p = GetBase(PrepareBufferForInt( - num_digits, spec, prefix, prefix_size)); + Char *p = get(prepare_int_buffer(num_digits, spec, prefix, prefix_size)); n = abs_value; do { *p-- = '0' + (n & 1); @@ -1668,8 +1668,7 @@ void BasicWriter::write_int(T value, const Spec &spec) { do { ++num_digits; } while ((n >>= 3) != 0); - Char *p = GetBase(PrepareBufferForInt( - num_digits, spec, prefix, prefix_size)); + Char *p = get(prepare_int_buffer(num_digits, spec, prefix, prefix_size)); n = abs_value; do { *p-- = '0' + (n & 7); @@ -1693,7 +1692,7 @@ void format(BasicFormatter &f, const Char *format_str, const T &value) { // Reports a system error without throwing an exception. // Can be used to report errors from destructors. -void ReportSystemError(int error_code, StringRef message) FMT_NOEXCEPT(true); +void report_system_error(int error_code, StringRef message) FMT_NOEXCEPT(true); #ifdef _WIN32 @@ -1722,7 +1721,7 @@ class WindowsError : public SystemError { // Reports a Windows error without throwing an exception. // Can be used to report errors from destructors. -void ReportWinError(int error_code, StringRef message) FMT_NOEXCEPT(true); +void report_windows_error(int error_code, StringRef message) FMT_NOEXCEPT(true); #endif @@ -1803,7 +1802,7 @@ void print(std::ostream &os, StringRef format, const ArgList &args); template void printf(BasicWriter &w, BasicStringRef format, const ArgList &args) { - internal::PrintfFormatter().Format(w, format, args); + internal::PrintfFormatter().format(w, format, args); } inline std::string sprintf(StringRef format, const ArgList &args) { diff --git a/posix.cc b/posix.cc index 852559af..03e02d4e 100644 --- a/posix.cc +++ b/posix.cc @@ -67,7 +67,7 @@ inline std::size_t convert_rwcount(std::size_t count) { return count; } fmt::BufferedFile::~BufferedFile() FMT_NOEXCEPT(true) { if (file_ && FMT_SYSTEM(fclose(file_)) != 0) - fmt::ReportSystemError(errno, "cannot close file"); + fmt::report_system_error(errno, "cannot close file"); } void fmt::BufferedFile::close() { @@ -102,7 +102,7 @@ fmt::File::~File() FMT_NOEXCEPT(true) { // Don't retry close in case of EINTR! // See http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html if (fd_ != -1 && FMT_POSIX_CALL(close(fd_)) != 0) - fmt::ReportSystemError(errno, "cannot close file"); + fmt::report_system_error(errno, "cannot close file"); } void fmt::File::close() { diff --git a/test/util-test.cc b/test/util-test.cc index 58874a5a..b06d7ece 100644 --- a/test/util-test.cc +++ b/test/util-test.cc @@ -448,12 +448,6 @@ TEST(UtilTest, StrError) { #endif } -TEST(UtilTest, SystemError) { - fmt::SystemError e(EDOM, "test"); - EXPECT_EQ(fmt::format("test: {}", GetSystemErrorMessage(EDOM)), e.what()); - EXPECT_EQ(EDOM, e.error_code()); -} - typedef void (*FormatErrorMessage)( fmt::Writer &out, int error_code, StringRef message); @@ -478,7 +472,10 @@ TEST(UtilTest, FormatSystemError) { GetSystemErrorMessage(EDOM)), message.str()); } -TEST(UtilTest, ThrowSystemError) { +TEST(UtilTest, SystemError) { + fmt::SystemError e(EDOM, "test"); + EXPECT_EQ(fmt::format("test: {}", GetSystemErrorMessage(EDOM)), e.what()); + EXPECT_EQ(EDOM, e.error_code()); CheckThrowError(EDOM, fmt::internal::format_system_error); } @@ -486,12 +483,12 @@ TEST(UtilTest, ReportSystemError) { fmt::Writer out; fmt::internal::format_system_error(out, EDOM, "test error"); out << '\n'; - EXPECT_WRITE(stderr, fmt::ReportSystemError(EDOM, "test error"), out.str()); + EXPECT_WRITE(stderr, fmt::report_system_error(EDOM, "test error"), out.str()); } #ifdef _WIN32 -TEST(UtilTest, FormatWinErrorMessage) { +TEST(UtilTest, FormatWindowsError) { LPWSTR message = 0; FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, @@ -506,17 +503,17 @@ TEST(UtilTest, FormatWinErrorMessage) { actual_message.str()); } -TEST(UtilTest, ThrowWinError) { +TEST(UtilTest, WindowsError) { CheckThrowError( ERROR_FILE_EXISTS, fmt::internal::format_windows_error); } -TEST(UtilTest, ReportWinError) { +TEST(UtilTest, ReportWindowsError) { fmt::Writer out; fmt::internal::format_windows_error(out, ERROR_FILE_EXISTS, "test error"); out << '\n'; EXPECT_WRITE(stderr, - fmt::ReportWinError(ERROR_FILE_EXISTS, "test error"), out.str()); + fmt::report_windows_error(ERROR_FILE_EXISTS, "test error"), out.str()); } #endif // _WIN32