From f2025a5d72f474853b4cb0d4c4ab86048160b33c Mon Sep 17 00:00:00 2001 From: ITotalJustice <47043333+ITotalJustice@users.noreply.github.com> Date: Tue, 18 Aug 2020 15:54:31 +0100 Subject: [PATCH] changes based on comments --- include/fmt/core.h | 2 +- include/fmt/format.h | 16 ++++++++-------- src/os.cc | 27 ++++++++++++++++----------- 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index e16f23f8..2b474e36 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -669,7 +669,7 @@ template class buffer { size_(sz), capacity_(cap) {} - virtual ~buffer() = default; + ~buffer() = default; /** Sets the buffer data and capacity. */ void set(T* buf_data, size_t buf_capacity) FMT_NOEXCEPT { diff --git a/include/fmt/format.h b/include/fmt/format.h index 272e25f4..26e36ab6 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -800,10 +800,10 @@ template struct FMT_EXTERN_TEMPLATE_API basic_data { // This is a function instead of an array to workaround a bug in GCC10 (#1810). FMT_INLINE uint16_t bsr2log10(int bsr) { constexpr uint16_t data[] = { - 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, - 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, - 10, 11, 11, 11, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 15, 15, - 15, 16, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 19, 20}; + 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, + 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, + 10, 11, 11, 11, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 15, 15, + 15, 16, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 19, 20}; return data[bsr]; } @@ -1632,9 +1632,9 @@ template struct int_writer { } if (prefix_size != 0) p[-1] = static_cast('-'); auto data = buffer.data(); - out = write_padded(out, specs, usize, usize, [=](iterator it) { - return copy_str(data, data + size, it); - }); + out = write_padded( + out, specs, usize, usize, + [=](iterator it) { return copy_str(data, data + size, it); }); } void on_chr() { *out++ = static_cast(abs_value); } @@ -3482,7 +3482,7 @@ inline std::string to_string(T value) { // The buffer should be large enough to store the number including the sign or // "false" for bool. constexpr int max_size = detail::digits10() + 2; - char buffer[static_cast(max_size > 5 ? max_size : 5)]; + char buffer[detail::to_unsigned(max_size > 5 ? max_size : 5)]; char* begin = buffer; return std::string(begin, detail::write(begin, value)); } diff --git a/src/os.cc b/src/os.cc index b7b89309..5df40d01 100644 --- a/src/os.cc +++ b/src/os.cc @@ -72,6 +72,8 @@ inline std::size_t convert_rwcount(std::size_t count) { return count; } FMT_BEGIN_NAMESPACE +inline int get_windows_error() { return static_cast(GetLastError()); } + #ifdef _WIN32 detail::utf16_to_utf8::utf16_to_utf8(wstring_view s) { if (int error_code = convert(s)) { @@ -92,11 +94,11 @@ int detail::utf16_to_utf8::convert(wstring_view s) { int length = WideCharToMultiByte(CP_UTF8, 0, s.data(), s_size, nullptr, 0, nullptr, nullptr); - if (length == 0) return static_cast(GetLastError()); + if (length == 0) return get_windows_error(); buffer_.resize(static_cast(length + 1)); length = WideCharToMultiByte(CP_UTF8, 0, s.data(), s_size, &buffer_[0], length, nullptr, nullptr); - if (length == 0) return static_cast(GetLastError()); + if (length == 0) return get_windows_error(); buffer_[length] = 0; return 0; } @@ -119,12 +121,14 @@ void detail::format_windows_error(detail::buffer& out, int error_code, wchar_t* system_message = &buf[0]; int result = static_cast(FormatMessageW( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, - static_cast(error_code), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), system_message, + static_cast(error_code), + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), system_message, static_cast(buf.size()), nullptr)); if (result != 0) { utf16_to_utf8 utf8_message; if (utf8_message.convert(system_message) == ERROR_SUCCESS) { - format_to(buffer_appender(out), "{}: {}", message, utf8_message); + format_to(buffer_appender(out), "{}: {}", message, + utf8_message); return; } break; @@ -212,10 +216,11 @@ long long file::size() const { if (size_lower == INVALID_FILE_SIZE) { DWORD error = GetLastError(); if (error != NO_ERROR) - FMT_THROW(windows_error(static_cast(GetLastError()), "cannot get file size")); + FMT_THROW(windows_error(get_windows_error(), "cannot get file size")); } unsigned long long long_size = size_upper; - return static_cast((long_size << sizeof(DWORD) * CHAR_BIT) | size_lower); + return static_cast((long_size << sizeof(DWORD) * CHAR_BIT) | + size_lower); # else using Stat = struct stat; Stat file_stat = Stat(); @@ -288,12 +293,12 @@ void file::pipe(file& read_end, file& write_end) { } buffered_file file::fdopen(const char* mode) { - // Don't retry as fdopen doesn't return EINTR. - #if defined(__MINGW32__) && defined(_POSIX_) +// Don't retry as fdopen doesn't return EINTR. +# if defined(__MINGW32__) && defined(_POSIX_) FILE* f = ::fdopen(fd_, mode); - #else +# else FILE* f = FMT_POSIX_CALL(fdopen(fd_, mode)); - #endif +# endif if (!f) FMT_THROW( system_error(errno, "cannot associate stream with file descriptor")); @@ -306,7 +311,7 @@ long getpagesize() { # ifdef _WIN32 SYSTEM_INFO si; GetSystemInfo(&si); - return static_cast(si.dwPageSize); + return static_cast(si.dwPageSize); # else long size = FMT_POSIX_CALL(sysconf(_SC_PAGESIZE)); if (size < 0) FMT_THROW(system_error(errno, "cannot get memory page size"));