This commit is contained in:
Victor Zverovich 2019-06-14 13:18:00 -07:00
parent 6c3d584e67
commit f03a6c5325
3 changed files with 28 additions and 49 deletions

View File

@ -63,8 +63,7 @@ inline fmt::internal::null<> strerror_s(char*, std::size_t, ...) {
}
FMT_BEGIN_NAMESPACE
namespace {
namespace internal {
#ifndef _MSC_VER
# define FMT_SNPRINTF snprintf
@ -79,12 +78,6 @@ inline int fmt_snprintf(char* buffer, size_t size, const char* format, ...) {
# define FMT_SNPRINTF fmt_snprintf
#endif // _MSC_VER
#if defined(_WIN32) && defined(__MINGW32__) && !defined(__NO_ISOCEXT)
# define FMT_SWPRINTF snwprintf
#else
# define FMT_SWPRINTF swprintf
#endif // defined(_WIN32) && defined(__MINGW32__) && !defined(__NO_ISOCEXT)
typedef void (*FormatFunc)(internal::buffer<char>&, int, string_view);
// Portable thread-safe version of strerror.
@ -198,7 +191,7 @@ void report_error(FormatFunc func, int error_code,
fwrite_fully(full_message.data(), 1, full_message.size(), stderr);
std::fputc('\n', stderr);
}
} // namespace
} // namespace internal
#if !defined(FMT_STATIC_THOUSANDS_SEPARATOR)
namespace internal {
@ -921,7 +914,8 @@ FMT_FUNC void format_system_error(internal::buffer<char>& out, int error_code,
buf.resize(inline_buffer_size);
for (;;) {
char* system_message = &buf[0];
int result = safe_strerror(error_code, system_message, buf.size());
int result =
internal::safe_strerror(error_code, system_message, buf.size());
if (result == 0) {
writer w(out);
w.write(message);
@ -958,13 +952,13 @@ FMT_FUNC void vprint(std::FILE* f, string_view format_str, format_args args) {
memory_buffer buffer;
internal::vformat_to(buffer, format_str,
basic_format_args<buffer_context<char>>(args));
fwrite_fully(buffer.data(), 1, buffer.size(), f);
internal::fwrite_fully(buffer.data(), 1, buffer.size(), f);
}
FMT_FUNC void vprint(std::FILE* f, wstring_view format_str, wformat_args args) {
wmemory_buffer buffer;
internal::vformat_to(buffer, format_str, args);
fwrite_fully(buffer.data(), sizeof(wchar_t), buffer.size(), f);
internal::fwrite_fully(buffer.data(), sizeof(wchar_t), buffer.size(), f);
}
FMT_FUNC void vprint(string_view format_str, format_args args) {

View File

@ -60,13 +60,9 @@
# define FMT_CUDA_VERSION 0
#endif
#if FMT_GCC_VERSION >= 406 || FMT_CLANG_VERSION
#if FMT_GCC_VERSION || FMT_CLANG_VERSION
# pragma GCC diagnostic push
// Disable warning about not handling all enums in switch statement even with
// a default case
# pragma GCC diagnostic ignored "-Wswitch-enum"
// Disable the warning about declaration shadowing because it affects too
// many valid cases.
# pragma GCC diagnostic ignored "-Wshadow"
@ -124,7 +120,7 @@ FMT_END_NAMESPACE
do { \
static_cast<void>(sizeof(x)); \
assert(false); \
} while (false);
} while (false)
# endif
#endif
@ -1311,27 +1307,14 @@ void arg_map<Context>::init(const basic_format_args<Context>& args) {
if (args.is_packed()) {
for (unsigned i = 0; /*nothing*/; ++i) {
internal::type arg_type = args.type(i);
switch (arg_type) {
case internal::none_type:
return;
case internal::named_arg_type:
push_back(args.values_[i]);
break;
default:
break; // Do nothing.
}
if (arg_type == internal::none_type) return;
if (arg_type == internal::named_arg_type) push_back(args.values_[i]);
}
}
for (unsigned i = 0;; ++i) {
switch (args.args_[i].type_) {
case internal::none_type:
return;
case internal::named_arg_type:
push_back(args.args_[i].value_);
break;
default:
break; // Do nothing.
}
auto type = args.args_[i].type_;
if (type == internal::none_type) return;
if (type == internal::named_arg_type) push_back(args.args_[i].value_);
}
}
@ -3308,14 +3291,14 @@ arg_join<It, wchar_t> join(It begin, It end, wstring_view sep) {
\endrst
*/
template <typename Range>
arg_join<internal::iterator_t<const Range>, char> join(
const Range& range, string_view sep) {
arg_join<internal::iterator_t<const Range>, char> join(const Range& range,
string_view sep) {
return join(std::begin(range), std::end(range), sep);
}
template <typename Range>
arg_join<internal::iterator_t<const Range>, wchar_t> join(
const Range& range, wstring_view sep) {
arg_join<internal::iterator_t<const Range>, wchar_t> join(const Range& range,
wstring_view sep) {
return join(std::begin(range), std::end(range), sep);
}
#endif

View File

@ -171,9 +171,9 @@ TEST(FormatTest, FormatNegativeNaN) {
TEST(FormatTest, StrError) {
char* message = nullptr;
char buffer[BUFFER_SIZE];
EXPECT_ASSERT(fmt::safe_strerror(EDOM, message = nullptr, 0),
EXPECT_ASSERT(fmt::internal::safe_strerror(EDOM, message = nullptr, 0),
"invalid buffer");
EXPECT_ASSERT(fmt::safe_strerror(EDOM, message = buffer, 0),
EXPECT_ASSERT(fmt::internal::safe_strerror(EDOM, message = buffer, 0),
"invalid buffer");
buffer[0] = 'x';
#if defined(_GNU_SOURCE) && !defined(__COVERITY__)
@ -184,7 +184,8 @@ TEST(FormatTest, StrError) {
int error_code = EDOM;
#endif
int result = fmt::safe_strerror(error_code, message = buffer, BUFFER_SIZE);
int result =
fmt::internal::safe_strerror(error_code, message = buffer, BUFFER_SIZE);
EXPECT_EQ(result, 0);
std::size_t message_size = std::strlen(message);
EXPECT_GE(BUFFER_SIZE - 1u, message_size);
@ -192,9 +193,10 @@ TEST(FormatTest, StrError) {
// safe_strerror never uses buffer on MinGW.
#ifndef __MINGW32__
result = fmt::safe_strerror(error_code, message = buffer, message_size);
result =
fmt::internal::safe_strerror(error_code, message = buffer, message_size);
EXPECT_EQ(ERANGE, result);
result = fmt::safe_strerror(error_code, message = buffer, 1);
result = fmt::internal::safe_strerror(error_code, message = buffer, 1);
EXPECT_EQ(buffer, message); // Message should point to buffer.
EXPECT_EQ(ERANGE, result);
EXPECT_STREQ("", message);
@ -206,14 +208,14 @@ TEST(FormatTest, FormatErrorCode) {
{
fmt::memory_buffer buffer;
format_to(buffer, "garbage");
fmt::format_error_code(buffer, 42, "test");
fmt::internal::format_error_code(buffer, 42, "test");
EXPECT_EQ("test: " + msg, to_string(buffer));
}
{
fmt::memory_buffer buffer;
std::string prefix(fmt::inline_buffer_size - msg.size() - sep.size() + 1,
'x');
fmt::format_error_code(buffer, 42, prefix);
fmt::internal::format_error_code(buffer, 42, prefix);
EXPECT_EQ(msg, to_string(buffer));
}
int codes[] = {42, -1};
@ -222,14 +224,14 @@ TEST(FormatTest, FormatErrorCode) {
msg = fmt::format("error {}", codes[i]);
fmt::memory_buffer buffer;
std::string prefix(fmt::inline_buffer_size - msg.size() - sep.size(), 'x');
fmt::format_error_code(buffer, codes[i], prefix);
fmt::internal::format_error_code(buffer, codes[i], prefix);
EXPECT_EQ(prefix + sep + msg, to_string(buffer));
std::size_t size = fmt::inline_buffer_size;
EXPECT_EQ(size, buffer.size());
buffer.resize(0);
// Test with a message that doesn't fit into the buffer.
prefix += 'x';
fmt::format_error_code(buffer, codes[i], prefix);
fmt::internal::format_error_code(buffer, codes[i], prefix);
EXPECT_EQ(msg, to_string(buffer));
}
}