changes based on comments

This commit is contained in:
ITotalJustice 2020-08-18 15:54:31 +01:00
parent 8ff5190d35
commit f2025a5d72
3 changed files with 25 additions and 20 deletions

View File

@ -669,7 +669,7 @@ template <typename T> class buffer {
size_(sz), size_(sz),
capacity_(cap) {} capacity_(cap) {}
virtual ~buffer() = default; ~buffer() = default;
/** Sets the buffer data and capacity. */ /** Sets the buffer data and capacity. */
void set(T* buf_data, size_t buf_capacity) FMT_NOEXCEPT { void set(T* buf_data, size_t buf_capacity) FMT_NOEXCEPT {

View File

@ -800,10 +800,10 @@ template <typename T = void> struct FMT_EXTERN_TEMPLATE_API basic_data {
// This is a function instead of an array to workaround a bug in GCC10 (#1810). // This is a function instead of an array to workaround a bug in GCC10 (#1810).
FMT_INLINE uint16_t bsr2log10(int bsr) { FMT_INLINE uint16_t bsr2log10(int bsr) {
constexpr uint16_t data[] = { constexpr uint16_t data[] = {
1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 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, 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, 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}; 15, 16, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 19, 20};
return data[bsr]; return data[bsr];
} }
@ -1632,9 +1632,9 @@ template <typename OutputIt, typename Char, typename UInt> struct int_writer {
} }
if (prefix_size != 0) p[-1] = static_cast<Char>('-'); if (prefix_size != 0) p[-1] = static_cast<Char>('-');
auto data = buffer.data(); auto data = buffer.data();
out = write_padded<align::right>(out, specs, usize, usize, [=](iterator it) { out = write_padded<align::right>(
return copy_str<Char>(data, data + size, it); out, specs, usize, usize,
}); [=](iterator it) { return copy_str<Char>(data, data + size, it); });
} }
void on_chr() { *out++ = static_cast<Char>(abs_value); } void on_chr() { *out++ = static_cast<Char>(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 // The buffer should be large enough to store the number including the sign or
// "false" for bool. // "false" for bool.
constexpr int max_size = detail::digits10<T>() + 2; constexpr int max_size = detail::digits10<T>() + 2;
char buffer[static_cast<size_t>(max_size > 5 ? max_size : 5)]; char buffer[detail::to_unsigned(max_size > 5 ? max_size : 5)];
char* begin = buffer; char* begin = buffer;
return std::string(begin, detail::write<char>(begin, value)); return std::string(begin, detail::write<char>(begin, value));
} }

View File

@ -72,6 +72,8 @@ inline std::size_t convert_rwcount(std::size_t count) { return count; }
FMT_BEGIN_NAMESPACE FMT_BEGIN_NAMESPACE
inline int get_windows_error() { return static_cast<int>(GetLastError()); }
#ifdef _WIN32 #ifdef _WIN32
detail::utf16_to_utf8::utf16_to_utf8(wstring_view s) { detail::utf16_to_utf8::utf16_to_utf8(wstring_view s) {
if (int error_code = convert(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, int length = WideCharToMultiByte(CP_UTF8, 0, s.data(), s_size, nullptr, 0,
nullptr, nullptr); nullptr, nullptr);
if (length == 0) return static_cast<int>(GetLastError()); if (length == 0) return get_windows_error();
buffer_.resize(static_cast<size_t>(length + 1)); buffer_.resize(static_cast<size_t>(length + 1));
length = WideCharToMultiByte(CP_UTF8, 0, s.data(), s_size, &buffer_[0], length = WideCharToMultiByte(CP_UTF8, 0, s.data(), s_size, &buffer_[0],
length, nullptr, nullptr); length, nullptr, nullptr);
if (length == 0) return static_cast<int>(GetLastError()); if (length == 0) return get_windows_error();
buffer_[length] = 0; buffer_[length] = 0;
return 0; return 0;
} }
@ -119,12 +121,14 @@ void detail::format_windows_error(detail::buffer<char>& out, int error_code,
wchar_t* system_message = &buf[0]; wchar_t* system_message = &buf[0];
int result = static_cast<int>(FormatMessageW( int result = static_cast<int>(FormatMessageW(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr,
static_cast<DWORD>(error_code), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), system_message, static_cast<DWORD>(error_code),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), system_message,
static_cast<uint32_t>(buf.size()), nullptr)); static_cast<uint32_t>(buf.size()), nullptr));
if (result != 0) { if (result != 0) {
utf16_to_utf8 utf8_message; utf16_to_utf8 utf8_message;
if (utf8_message.convert(system_message) == ERROR_SUCCESS) { if (utf8_message.convert(system_message) == ERROR_SUCCESS) {
format_to(buffer_appender<char>(out), "{}: {}", message, utf8_message); format_to(buffer_appender<char>(out), "{}: {}", message,
utf8_message);
return; return;
} }
break; break;
@ -212,10 +216,11 @@ long long file::size() const {
if (size_lower == INVALID_FILE_SIZE) { if (size_lower == INVALID_FILE_SIZE) {
DWORD error = GetLastError(); DWORD error = GetLastError();
if (error != NO_ERROR) if (error != NO_ERROR)
FMT_THROW(windows_error(static_cast<int>(GetLastError()), "cannot get file size")); FMT_THROW(windows_error(get_windows_error(), "cannot get file size"));
} }
unsigned long long long_size = size_upper; unsigned long long long_size = size_upper;
return static_cast<long long int>((long_size << sizeof(DWORD) * CHAR_BIT) | size_lower); return static_cast<long long>((long_size << sizeof(DWORD) * CHAR_BIT) |
size_lower);
# else # else
using Stat = struct stat; using Stat = struct stat;
Stat file_stat = 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) { buffered_file file::fdopen(const char* mode) {
// Don't retry as fdopen doesn't return EINTR. // Don't retry as fdopen doesn't return EINTR.
#if defined(__MINGW32__) && defined(_POSIX_) # if defined(__MINGW32__) && defined(_POSIX_)
FILE* f = ::fdopen(fd_, mode); FILE* f = ::fdopen(fd_, mode);
#else # else
FILE* f = FMT_POSIX_CALL(fdopen(fd_, mode)); FILE* f = FMT_POSIX_CALL(fdopen(fd_, mode));
#endif # endif
if (!f) if (!f)
FMT_THROW( FMT_THROW(
system_error(errno, "cannot associate stream with file descriptor")); system_error(errno, "cannot associate stream with file descriptor"));
@ -306,7 +311,7 @@ long getpagesize() {
# ifdef _WIN32 # ifdef _WIN32
SYSTEM_INFO si; SYSTEM_INFO si;
GetSystemInfo(&si); GetSystemInfo(&si);
return static_cast<long int>(si.dwPageSize); return static_cast<long>(si.dwPageSize);
# else # else
long size = FMT_POSIX_CALL(sysconf(_SC_PAGESIZE)); long size = FMT_POSIX_CALL(sysconf(_SC_PAGESIZE));
if (size < 0) FMT_THROW(system_error(errno, "cannot get memory page size")); if (size < 0) FMT_THROW(system_error(errno, "cannot get memory page size"));