Move implementation specific stuff from format.h to format.cc.

This commit is contained in:
Victor Zverovich 2014-08-15 08:40:03 -07:00
parent c7cfa7d4e7
commit fb32161fa4
4 changed files with 25 additions and 24 deletions

View File

@ -109,6 +109,12 @@ inline int safe_printf(char *buffer, size_t size, const char *format, ...) {
#endif // _MSC_VER #endif // _MSC_VER
template <typename T>
struct IsLongDouble { enum {VALUE = 0}; };
template <>
struct IsLongDouble<long double> { enum {VALUE = 1}; };
const char RESET_COLOR[] = "\x1b[0m"; const char RESET_COLOR[] = "\x1b[0m";
typedef void (*FormatFunc)(fmt::Writer &, int , fmt::StringRef); typedef void (*FormatFunc)(fmt::Writer &, int , fmt::StringRef);
@ -260,8 +266,6 @@ inline Arg::StringValue<wchar_t> ignore_incompatible_str(
Arg::StringValue<wchar_t> s) { return s; } Arg::StringValue<wchar_t> s) { return s; }
} // namespace } // namespace
int fmt::internal::signbit_noinline(double value) { return getsign(value); }
void fmt::SystemError::init( void fmt::SystemError::init(
int error_code, StringRef format_str, const ArgList &args) { int error_code, StringRef format_str, const ArgList &args) {
error_code_ = error_code; error_code_ = error_code;
@ -645,7 +649,7 @@ void fmt::BasicWriter<Char>::write_double(T value, const FormatSpec &spec) {
*format_ptr++ = '.'; *format_ptr++ = '.';
*format_ptr++ = '*'; *format_ptr++ = '*';
} }
if (internal::IsLongDouble<T>::VALUE) if (IsLongDouble<T>::VALUE)
*format_ptr++ = 'L'; *format_ptr++ = 'L';
*format_ptr++ = type; *format_ptr++ = type;
*format_ptr = '\0'; *format_ptr = '\0';

View File

@ -410,8 +410,6 @@ inline bool is_negative(T value) {
return SignChecker<std::numeric_limits<T>::is_signed>::is_negative(value); return SignChecker<std::numeric_limits<T>::is_signed>::is_negative(value);
} }
int signbit_noinline(double value);
template <typename T> template <typename T>
struct IntTraits { struct IntTraits {
// Smallest of uint32_t and uint64_t that is large enough to represent // Smallest of uint32_t and uint64_t that is large enough to represent
@ -435,12 +433,6 @@ FMT_SPECIALIZE_MAKE_UNSIGNED(int, unsigned);
FMT_SPECIALIZE_MAKE_UNSIGNED(long, unsigned long); FMT_SPECIALIZE_MAKE_UNSIGNED(long, unsigned long);
FMT_SPECIALIZE_MAKE_UNSIGNED(LongLong, ULongLong); FMT_SPECIALIZE_MAKE_UNSIGNED(LongLong, ULongLong);
template <typename T>
struct IsLongDouble { enum {VALUE = 0}; };
template <>
struct IsLongDouble<long double> { enum {VALUE = 1}; };
void report_unknown_type(char code, const char *type); void report_unknown_type(char code, const char *type);
extern const uint32_t POWERS_OF_10_32[]; extern const uint32_t POWERS_OF_10_32[];
@ -452,7 +444,7 @@ extern const uint64_t POWERS_OF_10_64[];
inline unsigned count_digits(uint64_t n) { inline unsigned count_digits(uint64_t n) {
// Based on http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10 // Based on http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10
// and the benchmark https://github.com/localvoid/cxx-benchmark-count-digits. // and the benchmark https://github.com/localvoid/cxx-benchmark-count-digits.
uint64_t t = (64 - __builtin_clzll(n | 1)) * 1233 >> 12; unsigned t = (64 - __builtin_clzll(n | 1)) * 1233 >> 12;
return t - (n < POWERS_OF_10_64[t]) + 1; return t - (n < POWERS_OF_10_64[t]) + 1;
} }
# if FMT_GCC_VERSION >= 400 || FMT_HAS_BUILTIN(__builtin_clz) # if FMT_GCC_VERSION >= 400 || FMT_HAS_BUILTIN(__builtin_clz)
@ -1862,10 +1854,10 @@ class FormatInt {
} }
void FormatSigned(LongLong value) { void FormatSigned(LongLong value) {
ULongLong abs_value = value; ULongLong abs_value = static_cast<ULongLong>(value);
bool negative = value < 0; bool negative = value < 0;
if (negative) if (negative)
abs_value = 0 - value; abs_value = 0 - abs_value;
str_ = format_decimal(abs_value); str_ = format_decimal(abs_value);
if (negative) if (negative)
*--str_ = '-'; *--str_ = '-';

View File

@ -1,17 +1,21 @@
set(TEST_MAIN_SRC test-main.cc gtest-extra.cc gtest-extra.h util.cc) set(TEST_MAIN_SRC test-main.cc gtest-extra.cc gtest-extra.h util.cc)
add_library(test-main ${TEST_MAIN_SRC}) add_library(test-main ${TEST_MAIN_SRC})
target_link_libraries(test-main gtest format) target_link_libraries(test-main gtest)
# Adds a test. # Adds a test.
# Usage: add_fmt_test(name libs srcs...) # Usage: add_fmt_test(name libs srcs...)
function(add_fmt_test name libs) function(add_fmt_test name libs)
add_executable(${name} ${name}.cc ${ARGN}) cmake_parse_arguments(add_fmt_test CUSTOM_LINK "" "" ${ARGN})
add_executable(${name} ${name}.cc ${add_fmt_test_UNPARSED_ARGUMENTS})
target_link_libraries(${name} ${libs}) target_link_libraries(${name} ${libs})
if (NOT add_fmt_test_CUSTOM_LINK)
target_link_libraries(${name} format)
endif ()
add_test(${name} ${name}) add_test(${name} ${name})
endfunction() endfunction()
add_fmt_test(gtest-extra-test test-main) add_fmt_test(gtest-extra-test test-main)
add_fmt_test(format-test test-main) add_fmt_test(format-test test-main CUSTOM_LINK ../posix.cc ../posix.h)
add_fmt_test(printf-test test-main) add_fmt_test(printf-test test-main)
foreach (target format-test printf-test) foreach (target format-test printf-test)
if (CMAKE_COMPILER_IS_GNUCXX) if (CMAKE_COMPILER_IS_GNUCXX)

View File

@ -35,6 +35,13 @@
#include <memory> #include <memory>
#include <sstream> #include <sstream>
// Include format.cc instead of format.h to test implementation-specific stuff.
#include "format.cc"
#include "util.h"
#include "gtest-extra.h"
#include <stdint.h>
#if defined(_WIN32) && !defined(__MINGW32__) #if defined(_WIN32) && !defined(__MINGW32__)
// Fix MSVC warning about "unsafe" fopen. // Fix MSVC warning about "unsafe" fopen.
FILE *safe_fopen(const char *filename, const char *mode) { FILE *safe_fopen(const char *filename, const char *mode) {
@ -45,12 +52,6 @@ FILE *safe_fopen(const char *filename, const char *mode) {
#define fopen safe_fopen #define fopen safe_fopen
#endif #endif
#include "format.h"
#include "util.h"
#include "gtest-extra.h"
#include <stdint.h>
#undef min #undef min
#undef max #undef max
@ -1230,7 +1231,7 @@ TEST(FormatterTest, FormatNaN) {
double nan = std::numeric_limits<double>::quiet_NaN(); double nan = std::numeric_limits<double>::quiet_NaN();
EXPECT_EQ("nan", format("{}", nan)); EXPECT_EQ("nan", format("{}", nan));
EXPECT_EQ("+nan", format("{:+}", nan)); EXPECT_EQ("+nan", format("{:+}", nan));
if (fmt::internal::signbit_noinline(-nan)) if (getsign(-nan))
EXPECT_EQ("-nan", format("{}", -nan)); EXPECT_EQ("-nan", format("{}", -nan));
else else
fmt::print("Warning: compiler doesn't handle negative NaN correctly"); fmt::print("Warning: compiler doesn't handle negative NaN correctly");