Use nullptr if available

This commit is contained in:
Victor Zverovich
2018-01-20 18:37:57 -08:00
parent e95e4659d9
commit d8c25a175a
6 changed files with 36 additions and 24 deletions

View File

@ -31,6 +31,16 @@
# define FMT_MSC_VER 0 # define FMT_MSC_VER 0
#endif #endif
#ifndef FMT_NULL
# if FMT_HAS_FEATURE(cxx_nullptr) || \
(FMT_GCC_VERSION >= 408 && FMT_HAS_GXX_CXX11) || \
FMT_MSC_VER >= 1600
# define FMT_NULL nullptr
# else
# define FMT_NULL NULL
# endif
#endif
// Check if exceptions are disabled. // Check if exceptions are disabled.
#if defined(__GNUC__) && !defined(__EXCEPTIONS) #if defined(__GNUC__) && !defined(__EXCEPTIONS)
# define FMT_EXCEPTIONS 0 # define FMT_EXCEPTIONS 0
@ -211,7 +221,7 @@ class basic_buffer {
std::size_t capacity_; std::size_t capacity_;
protected: protected:
basic_buffer(T *p = 0, std::size_t size = 0, std::size_t capacity = 0) basic_buffer(T *p = FMT_NULL, std::size_t size = 0, std::size_t capacity = 0)
FMT_NOEXCEPT: ptr_(p), size_(size), capacity_(capacity) {} FMT_NOEXCEPT: ptr_(p), size_(size), capacity_(capacity) {}
/** Sets the buffer data and capacity. */ /** Sets the buffer data and capacity. */

View File

@ -154,7 +154,8 @@ int safe_strerror(
: error_code_(err_code), buffer_(buf), buffer_size_(buf_size) {} : error_code_(err_code), buffer_(buf), buffer_size_(buf_size) {}
int run() { int run() {
strerror_r(0, 0, ""); // Suppress a warning about unused strerror_r. // Suppress a warning about unused strerror_r.
strerror_r(0, FMT_NULL, "");
return handle(strerror_r(error_code_, buffer_, buffer_size_)); return handle(strerror_r(error_code_, buffer_, buffer_size_));
} }
}; };
@ -282,7 +283,7 @@ FMT_FUNC internal::utf8_to_utf16::utf8_to_utf16(string_view s) {
FMT_THROW(windows_error(ERROR_INVALID_PARAMETER, ERROR_MSG)); FMT_THROW(windows_error(ERROR_INVALID_PARAMETER, ERROR_MSG));
int s_size = static_cast<int>(s.size()); int s_size = static_cast<int>(s.size());
int length = MultiByteToWideChar( int length = MultiByteToWideChar(
CP_UTF8, MB_ERR_INVALID_CHARS, s.data(), s_size, 0, 0); CP_UTF8, MB_ERR_INVALID_CHARS, s.data(), s_size, FMT_NULL, 0);
if (length == 0) if (length == 0)
FMT_THROW(windows_error(GetLastError(), ERROR_MSG)); FMT_THROW(windows_error(GetLastError(), ERROR_MSG));
buffer_.resize(length + 1); buffer_.resize(length + 1);
@ -304,12 +305,13 @@ FMT_FUNC int internal::utf16_to_utf8::convert(wstring_view s) {
if (s.size() > INT_MAX) if (s.size() > INT_MAX)
return ERROR_INVALID_PARAMETER; return ERROR_INVALID_PARAMETER;
int s_size = static_cast<int>(s.size()); int s_size = static_cast<int>(s.size());
int length = WideCharToMultiByte(CP_UTF8, 0, s.data(), s_size, 0, 0, 0, 0); int length = WideCharToMultiByte(
CP_UTF8, 0, s.data(), s_size, FMT_NULL, 0, FMT_NULL, FMT_NULL);
if (length == 0) if (length == 0)
return GetLastError(); return GetLastError();
buffer_.resize(length + 1); buffer_.resize(length + 1);
length = WideCharToMultiByte( length = WideCharToMultiByte(
CP_UTF8, 0, s.data(), s_size, &buffer_[0], length, 0, 0); CP_UTF8, 0, s.data(), s_size, &buffer_[0], length, FMT_NULL, FMT_NULL);
if (length == 0) if (length == 0)
return GetLastError(); return GetLastError();
buffer_[length] = 0; buffer_[length] = 0;
@ -334,8 +336,8 @@ FMT_FUNC void internal::format_windows_error(
wchar_t *system_message = &buf[0]; wchar_t *system_message = &buf[0];
int result = FormatMessageW( int result = FormatMessageW(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
0, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), FMT_NULL, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
system_message, static_cast<uint32_t>(buf.size()), 0); system_message, static_cast<uint32_t>(buf.size()), FMT_NULL);
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) {

View File

@ -2560,7 +2560,7 @@ void basic_writer<Range>::write_double(T value, const format_specs &spec) {
// Format using snprintf. // Format using snprintf.
unsigned n = 0; unsigned n = 0;
char_type *start = 0; char_type *start = FMT_NULL;
for (;;) { for (;;) {
std::size_t buffer_size = buffer.capacity(); std::size_t buffer_size = buffer.capacity();
#if FMT_MSC_VER #if FMT_MSC_VER

View File

@ -77,7 +77,7 @@ void fmt::BufferedFile::close() {
if (!file_) if (!file_)
return; return;
int result = FMT_SYSTEM(fclose(file_)); int result = FMT_SYSTEM(fclose(file_));
file_ = 0; file_ = FMT_NULL;
if (result != 0) if (result != 0)
FMT_THROW(system_error(errno, "cannot close file")); FMT_THROW(system_error(errno, "cannot close file"));
} }

View File

@ -156,7 +156,7 @@ public:
// A "move constructor" for moving from an lvalue. // A "move constructor" for moving from an lvalue.
BufferedFile(BufferedFile &f) FMT_NOEXCEPT : file_(f.file_) { BufferedFile(BufferedFile &f) FMT_NOEXCEPT : file_(f.file_) {
f.file_ = 0; f.file_ = FMT_NULL;
} }
// A "move assignment operator" for moving from a temporary. // A "move assignment operator" for moving from a temporary.
@ -170,7 +170,7 @@ public:
BufferedFile &operator=(BufferedFile &other) { BufferedFile &operator=(BufferedFile &other) {
close(); close();
file_ = other.file_; file_ = other.file_;
other.file_ = 0; other.file_ = FMT_NULL;
return *this; return *this;
} }
@ -178,7 +178,7 @@ public:
// BufferedFile file = BufferedFile(...); // BufferedFile file = BufferedFile(...);
operator Proxy() FMT_NOEXCEPT { operator Proxy() FMT_NOEXCEPT {
Proxy p = {file_}; Proxy p = {file_};
file_ = 0; file_ = FMT_NULL;
return p; return p;
} }
@ -188,13 +188,13 @@ public:
public: public:
BufferedFile(BufferedFile &&other) FMT_NOEXCEPT : file_(other.file_) { BufferedFile(BufferedFile &&other) FMT_NOEXCEPT : file_(other.file_) {
other.file_ = 0; other.file_ = FMT_NULL;
} }
BufferedFile& operator=(BufferedFile &&other) { BufferedFile& operator=(BufferedFile &&other) {
close(); close();
file_ = other.file_; file_ = other.file_;
other.file_ = 0; other.file_ = FMT_NULL;
return *this; return *this;
} }
#endif #endif
@ -386,7 +386,7 @@ class Locale {
public: public:
typedef locale_t Type; typedef locale_t Type;
Locale() : locale_(newlocale(LC_NUMERIC_MASK, "C", NULL)) { Locale() : locale_(newlocale(LC_NUMERIC_MASK, "C", FMT_NULL)) {
if (!locale_) if (!locale_)
FMT_THROW(fmt::system_error(errno, "cannot create locale")); FMT_THROW(fmt::system_error(errno, "cannot create locale"));
} }
@ -397,7 +397,7 @@ class Locale {
// Converts string to floating-point number and advances str past the end // Converts string to floating-point number and advances str past the end
// of the parsed input. // of the parsed input.
double strtod(const char *&str) const { double strtod(const char *&str) const {
char *end = 0; char *end = FMT_NULL;
double result = strtod_l(str, &end, locale_); double result = strtod_l(str, &end, locale_);
str = end; str = end;
return result; return result;

View File

@ -33,7 +33,7 @@ inline std::tm localtime(std::time_t time) {
return handle(localtime_r(&time_, &tm_)); return handle(localtime_r(&time_, &tm_));
} }
bool handle(std::tm* tm) { return tm != 0; } bool handle(std::tm *tm) { return tm != FMT_NULL; }
bool handle(internal::null<>) { bool handle(internal::null<>) {
using namespace fmt::internal; using namespace fmt::internal;
@ -44,9 +44,9 @@ inline std::tm localtime(std::time_t time) {
bool fallback(internal::null<>) { bool fallback(internal::null<>) {
using namespace fmt::internal; using namespace fmt::internal;
std::tm* tm = std::localtime(&time_); std::tm *tm = std::localtime(&time_);
if (tm != 0) tm_ = *tm; if (tm) tm_ = *tm;
return tm != 0; return tm != FMT_NULL;
} }
}; };
LocalTime lt(time); LocalTime lt(time);
@ -70,7 +70,7 @@ inline std::tm gmtime(std::time_t time) {
return handle(gmtime_r(&time_, &tm_)); return handle(gmtime_r(&time_, &tm_));
} }
bool handle(std::tm* tm) { return tm != 0; } bool handle(std::tm *tm) { return tm != FMT_NULL; }
bool handle(internal::null<>) { bool handle(internal::null<>) {
using namespace fmt::internal; using namespace fmt::internal;
@ -80,9 +80,9 @@ inline std::tm gmtime(std::time_t time) {
bool fallback(int res) { return res == 0; } bool fallback(int res) { return res == 0; }
bool fallback(internal::null<>) { bool fallback(internal::null<>) {
std::tm* tm = std::gmtime(&time_); std::tm *tm = std::gmtime(&time_);
if (tm != 0) tm_ = *tm; if (tm) tm_ = *tm;
return tm != 0; return tm != FMT_NULL;
} }
}; };
GMTime gt(time); GMTime gt(time);