Fixed warnings conversion 'size_t' to 'int' on windows issue #119

This commit is contained in:
gabime 2015-08-14 20:25:44 +03:00
parent 77acf29c4d
commit ec4233f236
2 changed files with 2500 additions and 2691 deletions

View File

@ -133,9 +133,7 @@ struct IntChecker {
unsigned max = INT_MAX; unsigned max = INT_MAX;
return value <= max; return value <= max;
} }
static bool fits_in_int(bool) { static bool fits_in_int(bool) { return true; }
return true;
}
}; };
template <> template <>
@ -250,9 +248,7 @@ void report_error(FormatFunc func,
class IsZeroInt : public fmt::internal::ArgVisitor<IsZeroInt, bool> { class IsZeroInt : public fmt::internal::ArgVisitor<IsZeroInt, bool> {
public: public:
template <typename T> template <typename T>
bool visit_any_int(T value) { bool visit_any_int(T value) { return value == 0; }
return value == 0;
}
}; };
// Parses an unsigned integer advancing s to the end of the parsed input. // Parses an unsigned integer advancing s to the end of the parsed input.
@ -365,20 +361,17 @@ public:
if (is_signed) { if (is_signed) {
arg_.type = Arg::INT; arg_.type = Arg::INT;
arg_.int_value = static_cast<int>(static_cast<T>(value)); arg_.int_value = static_cast<int>(static_cast<T>(value));
} } else {
else {
arg_.type = Arg::UINT; arg_.type = Arg::UINT;
arg_.uint_value = static_cast<unsigned>( arg_.uint_value = static_cast<unsigned>(
static_cast<typename fmt::internal::MakeUnsigned<T>::Type>(value)); static_cast<typename fmt::internal::MakeUnsigned<T>::Type>(value));
} }
} } else {
else {
if (is_signed) { if (is_signed) {
arg_.type = Arg::LONG_LONG; arg_.type = Arg::LONG_LONG;
arg_.long_long_value = arg_.long_long_value =
static_cast<typename fmt::internal::MakeUnsigned<U>::Type>(value); static_cast<typename fmt::internal::MakeUnsigned<U>::Type>(value);
} } else {
else {
arg_.type = Arg::ULONG_LONG; arg_.type = Arg::ULONG_LONG;
arg_.ulong_long_value = arg_.ulong_long_value =
static_cast<typename fmt::internal::MakeUnsigned<U>::Type>(value); static_cast<typename fmt::internal::MakeUnsigned<U>::Type>(value);
@ -416,26 +409,18 @@ private:
FMT_DISALLOW_COPY_AND_ASSIGN(BasicArgFormatter); FMT_DISALLOW_COPY_AND_ASSIGN(BasicArgFormatter);
protected: protected:
BasicWriter<Char> &writer() { BasicWriter<Char> &writer() { return writer_; }
return writer_; const FormatSpec &spec() const { return spec_; }
}
const FormatSpec &spec() const {
return spec_;
}
public: public:
BasicArgFormatter(BasicWriter<Char> &w, FormatSpec &s) BasicArgFormatter(BasicWriter<Char> &w, FormatSpec &s)
: writer_(w), spec_(s) {} : writer_(w), spec_(s) {}
template <typename T> template <typename T>
void visit_any_int(T value) { void visit_any_int(T value) { writer_.write_int(value, spec_); }
writer_.write_int(value, spec_);
}
template <typename T> template <typename T>
void visit_any_double(T value) { void visit_any_double(T value) { writer_.write_double(value, spec_); }
writer_.write_double(value, spec_);
}
void visit_bool(bool value) { void visit_bool(bool value) {
if (spec_.type_) { if (spec_.type_) {
@ -463,15 +448,12 @@ public:
if (spec_.align_ == ALIGN_RIGHT) { if (spec_.align_ == ALIGN_RIGHT) {
std::fill_n(out, spec_.width_ - 1, fill); std::fill_n(out, spec_.width_ - 1, fill);
out += spec_.width_ - 1; out += spec_.width_ - 1;
} } else if (spec_.align_ == ALIGN_CENTER) {
else if (spec_.align_ == ALIGN_CENTER) {
out = writer_.fill_padding(out, spec_.width_, 1, fill); out = writer_.fill_padding(out, spec_.width_, 1, fill);
} } else {
else {
std::fill_n(out + 1, spec_.width_ - 1, fill); std::fill_n(out + 1, spec_.width_ - 1, fill);
} }
} } else {
else {
out = writer_.grow_buffer(1); out = writer_.grow_buffer(1);
} }
*out = internal::CharTraits<Char>::cast(value); *out = internal::CharTraits<Char>::cast(value);
@ -533,12 +515,10 @@ public:
if (fmt_spec.align_ != ALIGN_LEFT) { if (fmt_spec.align_ != ALIGN_LEFT) {
std::fill_n(out, fmt_spec.width_ - 1, fill); std::fill_n(out, fmt_spec.width_ - 1, fill);
out += fmt_spec.width_ - 1; out += fmt_spec.width_ - 1;
} } else {
else {
std::fill_n(out + 1, fmt_spec.width_ - 1, fill); std::fill_n(out + 1, fmt_spec.width_ - 1, fill);
} }
} } else {
else {
out = w.grow_buffer(1); out = w.grow_buffer(1);
} }
*out = static_cast<Char>(value); *out = static_cast<Char>(value);
@ -632,14 +612,17 @@ FMT_FUNC void fmt::internal::report_unknown_type(char code, const char *type) {
#if FMT_USE_WINDOWS_H #if FMT_USE_WINDOWS_H
FMT_FUNC fmt::internal::UTF8ToUTF16::UTF8ToUTF16(fmt::StringRef s) { FMT_FUNC fmt::internal::UTF8ToUTF16::UTF8ToUTF16(fmt::StringRef s) {
int length = MultiByteToWideChar(
CP_UTF8, MB_ERR_INVALID_CHARS, s.data(), s.size(), 0, 0);
static const char ERROR_MSG[] = "cannot convert string from UTF-8 to UTF-16"; static const char ERROR_MSG[] = "cannot convert string from UTF-8 to UTF-16";
if (s.size() > INT_MAX)
FMT_THROW(WindowsError(ERROR_INVALID_PARAMETER, ERROR_MSG));
int s_size = static_cast<int>(s.size());
int length = MultiByteToWideChar(
CP_UTF8, MB_ERR_INVALID_CHARS, s.data(), s_size, 0, 0);
if (length == 0) if (length == 0)
FMT_THROW(WindowsError(GetLastError(), ERROR_MSG)); FMT_THROW(WindowsError(GetLastError(), ERROR_MSG));
buffer_.resize(length + 1); buffer_.resize(length + 1);
length = MultiByteToWideChar( length = MultiByteToWideChar(
CP_UTF8, MB_ERR_INVALID_CHARS, s.data(), s.size(), &buffer_[0], length); CP_UTF8, MB_ERR_INVALID_CHARS, s.data(), s_size, &buffer_[0], length);
if (length == 0) if (length == 0)
FMT_THROW(WindowsError(GetLastError(), ERROR_MSG)); FMT_THROW(WindowsError(GetLastError(), ERROR_MSG));
buffer_[length] = 0; buffer_[length] = 0;
@ -653,12 +636,15 @@ FMT_FUNC fmt::internal::UTF16ToUTF8::UTF16ToUTF8(fmt::WStringRef s) {
} }
FMT_FUNC int fmt::internal::UTF16ToUTF8::convert(fmt::WStringRef s) { FMT_FUNC int fmt::internal::UTF16ToUTF8::convert(fmt::WStringRef s) {
int length = WideCharToMultiByte(CP_UTF8, 0, s.data(), s.size(), 0, 0, 0, 0); if (s.size() > INT_MAX)
return ERROR_INVALID_PARAMETER;
int s_size = static_cast<int>(s.size());
int length = WideCharToMultiByte(CP_UTF8, 0, s.data(), s_size, 0, 0, 0, 0);
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, 0, 0);
if (length == 0) if (length == 0)
return GetLastError(); return GetLastError();
buffer_[length] = 0; buffer_[length] = 0;
@ -683,12 +669,8 @@ FMT_FUNC void fmt::internal::format_windows_error(
public: public:
String() : str_() {} String() : str_() {}
~String() { ~String() { LocalFree(str_); }
LocalFree(str_); LPWSTR *ptr() { return &str_; }
}
LPWSTR *ptr() {
return &str_;
}
LPCWSTR c_str() const { return str_; } LPCWSTR c_str() const { return str_; }
}; };
FMT_TRY { FMT_TRY {
@ -749,8 +731,7 @@ void fmt::internal::ArgMap<Char>::init(const ArgList &args) {
map_.insert(Pair(named_arg->name, *named_arg)); map_.insert(Pair(named_arg->name, *named_arg));
break; break;
default: default:
/*nothing*/ /*nothing*/;
;
} }
} }
return; return;
@ -771,8 +752,7 @@ void fmt::internal::ArgMap<Char>::init(const ArgList &args) {
map_.insert(Pair(named_arg->name, *named_arg)); map_.insert(Pair(named_arg->name, *named_arg));
break; break;
default: default:
/*nothing*/ /*nothing*/;
;
} }
} }
} }
@ -854,8 +834,7 @@ FMT_FUNC Arg fmt::internal::FormatterBase::do_get_arg(
case Arg::NAMED_ARG: case Arg::NAMED_ARG:
arg = *static_cast<const internal::Arg*>(arg.pointer); arg = *static_cast<const internal::Arg*>(arg.pointer);
default: default:
/*nothing*/ /*nothing*/;
;
} }
return arg; return arg;
} }
@ -933,8 +912,7 @@ unsigned fmt::internal::PrintfFormatter<Char>::parse_header(
if (*s == '$') { // value is an argument index if (*s == '$') { // value is an argument index
++s; ++s;
arg_index = value; arg_index = value;
} } else {
else {
if (c == '0') if (c == '0')
spec.fill_ = '0'; spec.fill_ = '0';
if (value != 0) { if (value != 0) {
@ -949,8 +927,7 @@ unsigned fmt::internal::PrintfFormatter<Char>::parse_header(
// Parse width. // Parse width.
if (*s >= '0' && *s <= '9') { if (*s >= '0' && *s <= '9') {
spec.width_ = parse_nonnegative_int(s); spec.width_ = parse_nonnegative_int(s);
} } else if (*s == '*') {
else if (*s == '*') {
++s; ++s;
spec.width_ = WidthHandler(spec).visit(get_arg(s)); spec.width_ = WidthHandler(spec).visit(get_arg(s));
} }
@ -983,8 +960,7 @@ void fmt::internal::PrintfFormatter<Char>::format(
++s; ++s;
if ('0' <= *s && *s <= '9') { if ('0' <= *s && *s <= '9') {
spec.precision_ = parse_nonnegative_int(s); spec.precision_ = parse_nonnegative_int(s);
} } else if (*s == '*') {
else if (*s == '*') {
++s; ++s;
spec.precision_ = PrecisionHandler().visit(get_arg(s)); spec.precision_ = PrecisionHandler().visit(get_arg(s));
} }
@ -1039,8 +1015,7 @@ void fmt::internal::PrintfFormatter<Char>::format(
if (arg.type <= Arg::LAST_INTEGER_TYPE) { if (arg.type <= Arg::LAST_INTEGER_TYPE) {
// Normalize type. // Normalize type.
switch (spec.type_) { switch (spec.type_) {
case 'i': case 'i': case 'u':
case 'u':
spec.type_ = 'd'; spec.type_ = 'd';
break; break;
case 'c': case 'c':
@ -1095,8 +1070,7 @@ const Char *fmt::BasicFormatter<Char>::format(
FMT_THROW(FormatError("invalid fill character '{'")); FMT_THROW(FormatError("invalid fill character '{'"));
s += 2; s += 2;
spec.fill_ = c; spec.fill_ = c;
} } else ++s;
else ++s;
if (spec.align_ == ALIGN_NUMERIC) if (spec.align_ == ALIGN_NUMERIC)
require_numeric_argument(arg, '='); require_numeric_argument(arg, '=');
break; break;
@ -1137,8 +1111,7 @@ const Char *fmt::BasicFormatter<Char>::format(
// Parse width. // Parse width.
if ('0' <= *s && *s <= '9') { if ('0' <= *s && *s <= '9') {
spec.width_ = parse_nonnegative_int(s); spec.width_ = parse_nonnegative_int(s);
} } else if (*s == '{') {
else if (*s == '{') {
++s; ++s;
Arg width_arg = is_name_start(*s) ? Arg width_arg = is_name_start(*s) ?
parse_arg_name(s) : parse_arg_index(s); parse_arg_name(s) : parse_arg_index(s);
@ -1176,8 +1149,7 @@ const Char *fmt::BasicFormatter<Char>::format(
spec.precision_ = 0; spec.precision_ = 0;
if ('0' <= *s && *s <= '9') { if ('0' <= *s && *s <= '9') {
spec.precision_ = parse_nonnegative_int(s); spec.precision_ = parse_nonnegative_int(s);
} } else if (*s == '{') {
else if (*s == '{') {
++s; ++s;
Arg precision_arg = Arg precision_arg =
is_name_start(*s) ? parse_arg_name(s) : parse_arg_index(s); is_name_start(*s) ? parse_arg_name(s) : parse_arg_index(s);
@ -1207,8 +1179,7 @@ const Char *fmt::BasicFormatter<Char>::format(
if (value > INT_MAX) if (value > INT_MAX)
FMT_THROW(FormatError("number is too big")); FMT_THROW(FormatError("number is too big"));
spec.precision_ = static_cast<int>(value); spec.precision_ = static_cast<int>(value);
} } else {
else {
FMT_THROW(FormatError("missing precision specifier")); FMT_THROW(FormatError("missing precision specifier"));
} }
if (arg.type <= Arg::LAST_INTEGER_TYPE || arg.type == Arg::POINTER) { if (arg.type <= Arg::LAST_INTEGER_TYPE || arg.type == Arg::POINTER) {

View File

@ -266,14 +266,10 @@ public:
} }
/** Returns the pointer to a C string. */ /** Returns the pointer to a C string. */
const Char *data() const { const Char *data() const { return data_; }
return data_;
}
/** Returns the string size. */ /** Returns the string size. */
std::size_t size() const { std::size_t size() const { return size_; }
return size_;
}
friend bool operator==(BasicStringRef lhs, BasicStringRef rhs) { friend bool operator==(BasicStringRef lhs, BasicStringRef rhs) {
return lhs.data_ == rhs.data_; return lhs.data_ == rhs.data_;
@ -333,9 +329,7 @@ public:
BasicCStringRef(const std::basic_string<Char> &s) : data_(s.c_str()) {} BasicCStringRef(const std::basic_string<Char> &s) : data_(s.c_str()) {}
/** Returns the pointer to a C string. */ /** Returns the pointer to a C string. */
const Char *c_str() const { const Char *c_str() const { return data_; }
return data_;
}
}; };
typedef BasicCStringRef<char> CStringRef; typedef BasicCStringRef<char> CStringRef;
@ -363,9 +357,7 @@ inline stdext::checked_array_iterator<T*> make_ptr(T *ptr, std::size_t size) {
} }
#else #else
template <typename T> template <typename T>
inline T *make_ptr(T *ptr, std::size_t) { inline T *make_ptr(T *ptr, std::size_t) { return ptr; }
return ptr;
}
#endif #endif
} // namespace internal } // namespace internal
@ -399,14 +391,10 @@ public:
virtual ~Buffer() {} virtual ~Buffer() {}
/** Returns the size of this buffer. */ /** Returns the size of this buffer. */
std::size_t size() const { std::size_t size() const { return size_; }
return size_;
}
/** Returns the capacity of this buffer. */ /** Returns the capacity of this buffer. */
std::size_t capacity() const { std::size_t capacity() const { return capacity_; }
return capacity_;
}
/** /**
Resizes the buffer. If T is a POD type new elements may not be initialized. Resizes the buffer. If T is a POD type new elements may not be initialized.
@ -439,12 +427,8 @@ public:
template <typename U> template <typename U>
void append(const U *begin, const U *end); void append(const U *begin, const U *end);
T &operator[](std::size_t index) { T &operator[](std::size_t index) { return ptr_[index]; }
return ptr_[index]; const T &operator[](std::size_t index) const { return ptr_[index]; }
}
const T &operator[](std::size_t index) const {
return ptr_[index];
}
}; };
template <typename T> template <typename T>
@ -477,9 +461,7 @@ protected:
public: public:
explicit MemoryBuffer(const Allocator &alloc = Allocator()) explicit MemoryBuffer(const Allocator &alloc = Allocator())
: Allocator(alloc), Buffer<T>(data_, SIZE) {} : Allocator(alloc), Buffer<T>(data_, SIZE) {}
~MemoryBuffer() { ~MemoryBuffer() { free(); }
free();
}
#if FMT_USE_RVALUE_REFERENCES #if FMT_USE_RVALUE_REFERENCES
private: private:
@ -493,8 +475,7 @@ private:
this->ptr_ = data_; this->ptr_ = data_;
std::copy(other.data_, std::copy(other.data_,
other.data_ + this->size_, make_ptr(data_, this->capacity_)); other.data_ + this->size_, make_ptr(data_, this->capacity_));
} } else {
else {
this->ptr_ = other.ptr_; this->ptr_ = other.ptr_;
// Set pointer to the inline array so that delete is not called // Set pointer to the inline array so that delete is not called
// when freeing. // when freeing.
@ -516,9 +497,7 @@ public:
#endif #endif
// Returns a copy of the allocator associated with this buffer. // Returns a copy of the allocator associated with this buffer.
Allocator get_allocator() const { Allocator get_allocator() const { return *this; }
return *this;
}
}; };
template <typename T, std::size_t SIZE, typename Allocator> template <typename T, std::size_t SIZE, typename Allocator>
@ -564,19 +543,11 @@ inline int getsign(double x) {
// Portable version of isinf. // Portable version of isinf.
# ifdef isinf # ifdef isinf
inline int isinfinity(double x) { inline int isinfinity(double x) { return isinf(x); }
return isinf(x); inline int isinfinity(long double x) { return isinf(x); }
}
inline int isinfinity(long double x) {
return isinf(x);
}
# else # else
inline int isinfinity(double x) { inline int isinfinity(double x) { return std::isinf(x); }
return std::isinf(x); inline int isinfinity(long double x) { return std::isinf(x); }
}
inline int isinfinity(long double x) {
return std::isinf(x);
}
# endif # endif
#else #else
inline int getsign(double value) { inline int getsign(double value) {
@ -587,9 +558,7 @@ inline int getsign(double value) {
_ecvt_s(buffer, sizeof(buffer), value, 0, &dec, &sign); _ecvt_s(buffer, sizeof(buffer), value, 0, &dec, &sign);
return sign; return sign;
} }
inline int isinfinity(double x) { inline int isinfinity(double x) { return !_finite(x); }
return !_finite(x);
}
inline int isinfinity(long double x) { inline int isinfinity(long double x) {
return !_finite(static_cast<double>(x)); return !_finite(static_cast<double>(x));
} }
@ -603,9 +572,7 @@ public:
#else #else
typedef Char *CharPtr; typedef Char *CharPtr;
#endif #endif
static Char cast(wchar_t value) { static Char cast(wchar_t value) { return static_cast<Char>(value); }
return static_cast<Char>(value);
}
}; };
template <typename Char> template <typename Char>
@ -618,9 +585,7 @@ private:
static char convert(wchar_t); static char convert(wchar_t);
public: public:
static char convert(char value) { static char convert(char value) { return value; }
return value;
}
// Formats a floating-point number. // Formats a floating-point number.
template <typename T> template <typename T>
@ -631,12 +596,8 @@ public:
template <> template <>
class CharTraits<wchar_t> : public BasicCharTraits<wchar_t> { class CharTraits<wchar_t> : public BasicCharTraits<wchar_t> {
public: public:
static wchar_t convert(char value) { static wchar_t convert(char value) { return value; }
return value; static wchar_t convert(wchar_t value) { return value; }
}
static wchar_t convert(wchar_t value) {
return value;
}
template <typename T> template <typename T>
static int format_float(wchar_t *buffer, std::size_t size, static int format_float(wchar_t *buffer, std::size_t size,
@ -647,17 +608,13 @@ public:
template <bool IsSigned> template <bool IsSigned>
struct SignChecker { struct SignChecker {
template <typename T> template <typename T>
static bool is_negative(T value) { static bool is_negative(T value) { return value < 0; }
return value < 0;
}
}; };
template <> template <>
struct SignChecker<false> { struct SignChecker<false> {
template <typename T> template <typename T>
static bool is_negative(T) { static bool is_negative(T) { return false; }
return false;
}
}; };
// Returns true if value is negative, false otherwise. // Returns true if value is negative, false otherwise.
@ -669,14 +626,10 @@ inline bool is_negative(T value) {
// Selects uint32_t if FitsIn32Bits is true, uint64_t otherwise. // Selects uint32_t if FitsIn32Bits is true, uint64_t otherwise.
template <bool FitsIn32Bits> template <bool FitsIn32Bits>
struct TypeSelector { struct TypeSelector { typedef uint32_t Type; };
typedef uint32_t Type;
};
template <> template <>
struct TypeSelector<false> { struct TypeSelector<false> { typedef uint64_t Type; };
typedef uint64_t Type;
};
template <typename T> template <typename T>
struct IntTraits { struct IntTraits {
@ -688,9 +641,7 @@ struct IntTraits {
// MakeUnsigned<T>::Type gives an unsigned type corresponding to integer type T. // MakeUnsigned<T>::Type gives an unsigned type corresponding to integer type T.
template <typename T> template <typename T>
struct MakeUnsigned { struct MakeUnsigned { typedef T Type; };
typedef T Type;
};
#define FMT_SPECIALIZE_MAKE_UNSIGNED(T, U) \ #define FMT_SPECIALIZE_MAKE_UNSIGNED(T, U) \
template <> \ template <> \
@ -798,18 +749,10 @@ private:
public: public:
explicit UTF8ToUTF16(StringRef s); explicit UTF8ToUTF16(StringRef s);
operator WStringRef() const { operator WStringRef() const { return WStringRef(&buffer_[0], size()); }
return WStringRef(&buffer_[0], size()); size_t size() const { return buffer_.size() - 1; }
} const wchar_t *c_str() const { return &buffer_[0]; }
size_t size() const { std::wstring str() const { return std::wstring(&buffer_[0], size()); }
return buffer_.size() - 1;
}
const wchar_t *c_str() const {
return &buffer_[0];
}
std::wstring str() const {
return std::wstring(&buffer_[0], size());
}
}; };
// A converter from UTF-16 to UTF-8. // A converter from UTF-16 to UTF-8.
@ -821,18 +764,10 @@ private:
public: public:
UTF16ToUTF8() {} UTF16ToUTF8() {}
explicit UTF16ToUTF8(WStringRef s); explicit UTF16ToUTF8(WStringRef s);
operator StringRef() const { operator StringRef() const { return StringRef(&buffer_[0], size()); }
return StringRef(&buffer_[0], size()); size_t size() const { return buffer_.size() - 1; }
} const char *c_str() const { return &buffer_[0]; }
size_t size() const { std::string str() const { return std::string(&buffer_[0], size()); }
return buffer_.size() - 1;
}
const char *c_str() const {
return &buffer_[0];
}
std::string str() const {
return std::string(&buffer_[0], size());
}
// Performs conversion returning a system error code instead of // Performs conversion returning a system error code instead of
// throwing exception on conversion error. This method may still throw // throwing exception on conversion error. This method may still throw
@ -945,25 +880,17 @@ template<bool B, class T = void>
struct EnableIf {}; struct EnableIf {};
template<class T> template<class T>
struct EnableIf<true, T> { struct EnableIf<true, T> { typedef T type; };
typedef T type;
};
template<bool B, class T, class F> template<bool B, class T, class F>
struct Conditional { struct Conditional { typedef T type; };
typedef T type;
};
template<class T, class F> template<class T, class F>
struct Conditional<false, T, F> { struct Conditional<false, T, F> { typedef F type; };
typedef F type;
};
// A helper function to suppress bogus "conditional expression is constant" // A helper function to suppress bogus "conditional expression is constant"
// warnings. // warnings.
inline bool check(bool value) { inline bool check(bool value) { return value; }
return value;
}
// Makes an Arg object from any type. // Makes an Arg object from any type.
template <typename Char> template <typename Char>
@ -1059,9 +986,7 @@ public:
MakeValue(typename WCharHelper<wchar_t, Char>::Supported value) { MakeValue(typename WCharHelper<wchar_t, Char>::Supported value) {
int_value = value; int_value = value;
} }
static uint64_t type(wchar_t) { static uint64_t type(wchar_t) { return Arg::CHAR; }
return Arg::CHAR;
}
#define FMT_MAKE_STR_VALUE(Type, TYPE) \ #define FMT_MAKE_STR_VALUE(Type, TYPE) \
MakeValue(Type value) { set_string(value); } \ MakeValue(Type value) { set_string(value); } \
@ -1110,14 +1035,10 @@ public:
// Additional template param `Char_` is needed here because make_type always // Additional template param `Char_` is needed here because make_type always
// uses MakeValue<char>. // uses MakeValue<char>.
template <typename Char_> template <typename Char_>
MakeValue(const NamedArg<Char_> &value) { MakeValue(const NamedArg<Char_> &value) { pointer = &value; }
pointer = &value;
}
template <typename Char_> template <typename Char_>
static uint64_t type(const NamedArg<Char_> &) { static uint64_t type(const NamedArg<Char_> &) { return Arg::NAMED_ARG; }
return Arg::NAMED_ARG;
}
}; };
template <typename Char> template <typename Char>
@ -1357,9 +1278,7 @@ private:
Arg do_get_arg(unsigned arg_index, const char *&error); Arg do_get_arg(unsigned arg_index, const char *&error);
protected: protected:
const ArgList &args() const { const ArgList &args() const { return args_; }
return args_;
}
explicit FormatterBase(const ArgList &args) { explicit FormatterBase(const ArgList &args) {
args_ = args; args_ = args;
@ -1427,9 +1346,7 @@ public:
BasicFormatter(const ArgList &args, BasicWriter<Char> &w) BasicFormatter(const ArgList &args, BasicWriter<Char> &w)
: FormatterBase(args), writer_(w) {} : FormatterBase(args), writer_(w) {}
BasicWriter<Char> &writer() { BasicWriter<Char> &writer() { return writer_; }
return writer_;
}
void format(BasicCStringRef<Char> format_str); void format(BasicCStringRef<Char> format_str);
@ -1452,24 +1369,12 @@ struct EmptySpec {};
// A type specifier. // A type specifier.
template <char TYPE> template <char TYPE>
struct TypeSpec : EmptySpec { struct TypeSpec : EmptySpec {
Alignment align() const { Alignment align() const { return ALIGN_DEFAULT; }
return ALIGN_DEFAULT; unsigned width() const { return 0; }
} int precision() const { return -1; }
unsigned width() const { bool flag(unsigned) const { return false; }
return 0; char type() const { return TYPE; }
} char fill() const { return ' '; }
int precision() const {
return -1;
}
bool flag(unsigned) const {
return false;
}
char type() const {
return TYPE;
}
char fill() const {
return ' ';
}
}; };
// A width specifier. // A width specifier.
@ -1481,12 +1386,8 @@ struct WidthSpec {
WidthSpec(unsigned width, wchar_t fill) : width_(width), fill_(fill) {} WidthSpec(unsigned width, wchar_t fill) : width_(width), fill_(fill) {}
unsigned width() const { unsigned width() const { return width_; }
return width_; wchar_t fill() const { return fill_; }
}
wchar_t fill() const {
return fill_;
}
}; };
// An alignment specifier. // An alignment specifier.
@ -1496,13 +1397,9 @@ struct AlignSpec : WidthSpec {
AlignSpec(unsigned width, wchar_t fill, Alignment align = ALIGN_DEFAULT) AlignSpec(unsigned width, wchar_t fill, Alignment align = ALIGN_DEFAULT)
: WidthSpec(width, fill), align_(align) {} : WidthSpec(width, fill), align_(align) {}
Alignment align() const { Alignment align() const { return align_; }
return align_;
}
int precision() const { int precision() const { return -1; }
return -1;
}
}; };
// An alignment and type specifier. // An alignment and type specifier.
@ -1510,12 +1407,8 @@ template <char TYPE>
struct AlignTypeSpec : AlignSpec { struct AlignTypeSpec : AlignSpec {
AlignTypeSpec(unsigned width, wchar_t fill) : AlignSpec(width, fill) {} AlignTypeSpec(unsigned width, wchar_t fill) : AlignSpec(width, fill) {}
bool flag(unsigned) const { bool flag(unsigned) const { return false; }
return false; char type() const { return TYPE; }
}
char type() const {
return TYPE;
}
}; };
// A full format specifier. // A full format specifier.
@ -1528,15 +1421,9 @@ struct FormatSpec : AlignSpec {
unsigned width = 0, char type = 0, wchar_t fill = ' ') unsigned width = 0, char type = 0, wchar_t fill = ' ')
: AlignSpec(width, fill), flags_(0), precision_(-1), type_(type) {} : AlignSpec(width, fill), flags_(0), precision_(-1), type_(type) {}
bool flag(unsigned f) const { bool flag(unsigned f) const { return (flags_ & f) != 0; }
return (flags_ & f) != 0; int precision() const { return precision_; }
} char type() const { return type_; }
int precision() const {
return precision_;
}
char type() const {
return type_;
}
}; };
// An integer format specifier. // An integer format specifier.
@ -1549,9 +1436,7 @@ public:
IntFormatSpec(T val, const SpecT &spec = SpecT()) IntFormatSpec(T val, const SpecT &spec = SpecT())
: SpecT(spec), value_(val) {} : SpecT(spec), value_(val) {}
T value() const { T value() const { return value_; }
return value_;
}
}; };
// A string format specifier. // A string format specifier.
@ -1567,9 +1452,7 @@ public:
internal::CharTraits<Char>::convert(FillChar()); internal::CharTraits<Char>::convert(FillChar());
} }
const Char *str() const { const Char *str() const { return str_; }
return str_;
}
}; };
/** /**
@ -1711,14 +1594,10 @@ inline StrFormatSpec<wchar_t> pad(
# define FMT_GEN15(f) FMT_GEN14(f), f(14) # define FMT_GEN15(f) FMT_GEN14(f), f(14)
namespace internal { namespace internal {
inline uint64_t make_type() { inline uint64_t make_type() { return 0; }
return 0;
}
template <typename T> template <typename T>
inline uint64_t make_type(const T &arg) { inline uint64_t make_type(const T &arg) { return MakeValue<char>::type(arg); }
return MakeValue<char>::type(arg);
}
template <unsigned N> template <unsigned N>
struct ArgArray { struct ArgArray {
@ -1937,9 +1816,7 @@ public:
} }
FMT_VARIADIC_CTOR(SystemError, init, int, CStringRef) FMT_VARIADIC_CTOR(SystemError, init, int, CStringRef)
int error_code() const { int error_code() const { return error_code_; }
return error_code_;
}
}; };
/** /**
@ -1972,13 +1849,9 @@ private:
#if _SECURE_SCL #if _SECURE_SCL
// Returns pointer value. // Returns pointer value.
static Char *get(CharPtr p) { static Char *get(CharPtr p) { return p.base(); }
return p.base();
}
#else #else
static Char *get(Char *p) { static Char *get(Char *p) { return p; }
return p;
}
#endif #endif
// Fills the padding around the content and returns the pointer to the // Fills the padding around the content and returns the pointer to the
@ -2010,8 +1883,7 @@ private:
if (internal::is_negative(value)) { if (internal::is_negative(value)) {
abs_value = 0 - abs_value; abs_value = 0 - abs_value;
*write_unsigned_decimal(abs_value, 1) = '-'; *write_unsigned_decimal(abs_value, 1) = '-';
} } else {
else {
write_unsigned_decimal(abs_value, 0); write_unsigned_decimal(abs_value, 0);
} }
} }
@ -2085,17 +1957,13 @@ public:
/** /**
Returns the total number of characters written. Returns the total number of characters written.
*/ */
std::size_t size() const { std::size_t size() const { return buffer_.size(); }
return buffer_.size();
}
/** /**
Returns a pointer to the output buffer content. No terminating null Returns a pointer to the output buffer content. No terminating null
character is appended. character is appended.
*/ */
const Char *data() const FMT_NOEXCEPT { const Char *data() const FMT_NOEXCEPT { return &buffer_[0]; }
return &buffer_[0];
}
/** /**
Returns a pointer to the output buffer content with terminating null Returns a pointer to the output buffer content with terminating null
@ -2251,15 +2119,12 @@ typename BasicWriter<Char>::CharPtr BasicWriter<Char>::write_str(
if (spec.align() == ALIGN_RIGHT) { if (spec.align() == ALIGN_RIGHT) {
std::fill_n(out, spec.width() - size, fill); std::fill_n(out, spec.width() - size, fill);
out += spec.width() - size; out += spec.width() - size;
} } else if (spec.align() == ALIGN_CENTER) {
else if (spec.align() == ALIGN_CENTER) {
out = fill_padding(out, spec.width(), size, fill); out = fill_padding(out, spec.width(), size, fill);
} } else {
else {
std::fill_n(out + size, spec.width() - size, fill); std::fill_n(out + size, spec.width() - size, fill);
} }
} } else {
else {
out = grow_buffer(size); out = grow_buffer(size);
} }
std::copy(s, s + size, out); std::copy(s, s + size, out);
@ -2325,20 +2190,17 @@ BasicWriter<Char>::prepare_int_buffer(
std::copy(prefix, prefix + prefix_size, p); std::copy(prefix, prefix + prefix_size, p);
p += size; p += size;
std::fill(p, end, fill); std::fill(p, end, fill);
} } else if (align == ALIGN_CENTER) {
else if (align == ALIGN_CENTER) {
p = fill_padding(p, width, size, fill); p = fill_padding(p, width, size, fill);
std::copy(prefix, prefix + prefix_size, p); std::copy(prefix, prefix + prefix_size, p);
p += size; p += size;
} } else {
else {
if (align == ALIGN_NUMERIC) { if (align == ALIGN_NUMERIC) {
if (prefix_size != 0) { if (prefix_size != 0) {
p = std::copy(prefix, prefix + prefix_size, p); p = std::copy(prefix, prefix + prefix_size, p);
size -= prefix_size; size -= prefix_size;
} }
} } else {
else {
std::copy(prefix, prefix + prefix_size, end - size); std::copy(prefix, prefix + prefix_size, end - size);
} }
std::fill(p, end - size, fill); std::fill(p, end - size, fill);
@ -2358,22 +2220,19 @@ void BasicWriter<Char>::write_int(T value, Spec spec) {
prefix[0] = '-'; prefix[0] = '-';
++prefix_size; ++prefix_size;
abs_value = 0 - abs_value; abs_value = 0 - abs_value;
} } else if (spec.flag(SIGN_FLAG)) {
else if (spec.flag(SIGN_FLAG)) {
prefix[0] = spec.flag(PLUS_FLAG) ? '+' : ' '; prefix[0] = spec.flag(PLUS_FLAG) ? '+' : ' ';
++prefix_size; ++prefix_size;
} }
switch (spec.type()) { switch (spec.type()) {
case 0: case 0: case 'd': {
case 'd': {
unsigned num_digits = internal::count_digits(abs_value); unsigned num_digits = internal::count_digits(abs_value);
CharPtr p = prepare_int_buffer( CharPtr p = prepare_int_buffer(
num_digits, spec, prefix, prefix_size) + 1 - num_digits; num_digits, spec, prefix, prefix_size) + 1 - num_digits;
internal::format_decimal(get(p), abs_value, num_digits); internal::format_decimal(get(p), abs_value, num_digits);
break; break;
} }
case 'x': case 'x': case 'X': {
case 'X': {
UnsignedType n = abs_value; UnsignedType n = abs_value;
if (spec.flag(HASH_FLAG)) { if (spec.flag(HASH_FLAG)) {
prefix[prefix_size++] = '0'; prefix[prefix_size++] = '0';
@ -2393,8 +2252,7 @@ void BasicWriter<Char>::write_int(T value, Spec spec) {
} while ((n >>= 4) != 0); } while ((n >>= 4) != 0);
break; break;
} }
case 'b': case 'b': case 'B': {
case 'B': {
UnsignedType n = abs_value; UnsignedType n = abs_value;
if (spec.flag(HASH_FLAG)) { if (spec.flag(HASH_FLAG)) {
prefix[prefix_size++] = '0'; prefix[prefix_size++] = '0';
@ -2444,10 +2302,7 @@ void BasicWriter<Char>::write_double(
case 0: case 0:
type = 'g'; type = 'g';
break; break;
case 'e': case 'e': case 'f': case 'g': case 'a':
case 'f':
case 'g':
case 'a':
break; break;
case 'F': case 'F':
#ifdef _MSC_VER #ifdef _MSC_VER
@ -2455,9 +2310,7 @@ void BasicWriter<Char>::write_double(
type = 'f'; type = 'f';
#endif #endif
// Fall through. // Fall through.
case 'E': case 'E': case 'G': case 'A':
case 'G':
case 'A':
upper = true; upper = true;
break; break;
default: default:
@ -2471,8 +2324,7 @@ void BasicWriter<Char>::write_double(
if (internal::getsign(static_cast<double>(value))) { if (internal::getsign(static_cast<double>(value))) {
sign = '-'; sign = '-';
value = -value; value = -value;
} } else if (spec.flag(SIGN_FLAG)) {
else if (spec.flag(SIGN_FLAG)) {
sign = spec.flag(PLUS_FLAG) ? '+' : ' '; sign = spec.flag(PLUS_FLAG) ? '+' : ' ';
} }
@ -2525,8 +2377,7 @@ void BasicWriter<Char>::write_double(
*format_ptr++ = '#'; *format_ptr++ = '#';
if (spec.align() == ALIGN_CENTER) { if (spec.align() == ALIGN_CENTER) {
width_for_sprintf = 0; width_for_sprintf = 0;
} } else {
else {
if (spec.align() == ALIGN_LEFT) if (spec.align() == ALIGN_LEFT)
*format_ptr++ = '-'; *format_ptr++ = '-';
if (width != 0) if (width != 0)
@ -2563,8 +2414,7 @@ void BasicWriter<Char>::write_double(
*start != ' ') { *start != ' ') {
*(start - 1) = sign; *(start - 1) = sign;
sign = 0; sign = 0;
} } else {
else {
*(start - 1) = fill; *(start - 1) = fill;
} }
++n; ++n;
@ -2926,15 +2776,9 @@ private:
} }
public: public:
explicit FormatInt(int value) { explicit FormatInt(int value) { FormatSigned(value); }
FormatSigned(value); explicit FormatInt(long value) { FormatSigned(value); }
} explicit FormatInt(LongLong value) { FormatSigned(value); }
explicit FormatInt(long value) {
FormatSigned(value);
}
explicit FormatInt(LongLong value) {
FormatSigned(value);
}
explicit FormatInt(unsigned value) : str_(format_decimal(value)) {} explicit FormatInt(unsigned value) : str_(format_decimal(value)) {}
explicit FormatInt(unsigned long value) : str_(format_decimal(value)) {} explicit FormatInt(unsigned long value) : str_(format_decimal(value)) {}
explicit FormatInt(ULongLong value) : str_(format_decimal(value)) {} explicit FormatInt(ULongLong value) : str_(format_decimal(value)) {}
@ -2942,17 +2786,13 @@ public:
/** /**
Returns the number of characters written to the output buffer. Returns the number of characters written to the output buffer.
*/ */
std::size_t size() const { std::size_t size() const { return buffer_ - str_ + BUFFER_SIZE - 1; }
return buffer_ - str_ + BUFFER_SIZE - 1;
}
/** /**
Returns a pointer to the output buffer content. No terminating null Returns a pointer to the output buffer content. No terminating null
character is appended. character is appended.
*/ */
const char *data() const { const char *data() const { return str_; }
return str_;
}
/** /**
Returns a pointer to the output buffer content with terminating null Returns a pointer to the output buffer content with terminating null
@ -2968,9 +2808,7 @@ public:
Returns the content of the output buffer as an ``std::string``. Returns the content of the output buffer as an ``std::string``.
\endrst \endrst
*/ */
std::string str() const { std::string str() const { return std::string(str_, size()); }
return std::string(str_, size());
}
}; };
// Formats a decimal integer value writing into buffer and returns // Formats a decimal integer value writing into buffer and returns