mirror of
https://github.com/fmtlib/fmt.git
synced 2024-10-02 13:02:03 +00:00
Use precision from FormatSpec.
This commit is contained in:
parent
f8c058e99c
commit
b1bbc90919
24
format.cc
24
format.cc
@ -111,7 +111,7 @@ void ReportError(FormatFunc func,
|
|||||||
int error_code, fmt::StringRef message) FMT_NOEXCEPT(true) {
|
int error_code, fmt::StringRef message) FMT_NOEXCEPT(true) {
|
||||||
try {
|
try {
|
||||||
fmt::Writer full_message;
|
fmt::Writer full_message;
|
||||||
func(full_message, error_code, message); // TODO: this may throw?
|
func(full_message, error_code, message); // TODO: make sure this doesn't throw
|
||||||
std::fwrite(full_message.c_str(), full_message.size(), 1, stderr);
|
std::fwrite(full_message.c_str(), full_message.size(), 1, stderr);
|
||||||
std::fputc('\n', stderr);
|
std::fputc('\n', stderr);
|
||||||
} catch (...) {}
|
} catch (...) {}
|
||||||
@ -352,8 +352,7 @@ typename fmt::BasicWriter<Char>::CharPtr
|
|||||||
|
|
||||||
template <typename Char>
|
template <typename Char>
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void fmt::BasicWriter<Char>::FormatDouble(
|
void fmt::BasicWriter<Char>::FormatDouble(T value, const FormatSpec &spec) {
|
||||||
T value, const FormatSpec &spec, int precision) {
|
|
||||||
// Check type.
|
// Check type.
|
||||||
char type = spec.type();
|
char type = spec.type();
|
||||||
bool upper = false;
|
bool upper = false;
|
||||||
@ -442,7 +441,7 @@ void fmt::BasicWriter<Char>::FormatDouble(
|
|||||||
if (width != 0)
|
if (width != 0)
|
||||||
*format_ptr++ = '*';
|
*format_ptr++ = '*';
|
||||||
}
|
}
|
||||||
if (precision >= 0) {
|
if (spec.precision() >= 0) {
|
||||||
*format_ptr++ = '.';
|
*format_ptr++ = '.';
|
||||||
*format_ptr++ = '*';
|
*format_ptr++ = '*';
|
||||||
}
|
}
|
||||||
@ -466,7 +465,7 @@ void fmt::BasicWriter<Char>::FormatDouble(
|
|||||||
#endif
|
#endif
|
||||||
Char *start = &buffer_[offset];
|
Char *start = &buffer_[offset];
|
||||||
int n = internal::CharTraits<Char>::FormatFloat(
|
int n = internal::CharTraits<Char>::FormatFloat(
|
||||||
start, size, format, width_for_sprintf, precision, value);
|
start, size, format, width_for_sprintf, spec.precision(), value);
|
||||||
if (n >= 0 && offset + n < buffer_.capacity()) {
|
if (n >= 0 && offset + n < buffer_.capacity()) {
|
||||||
if (sign) {
|
if (sign) {
|
||||||
if ((spec.align() != ALIGN_RIGHT && spec.align() != ALIGN_DEFAULT) ||
|
if ((spec.align() != ALIGN_RIGHT && spec.align() != ALIGN_DEFAULT) ||
|
||||||
@ -781,10 +780,10 @@ void fmt::BasicWriter<Char>::PrintfParser::Format(
|
|||||||
writer.FormatInt(arg.ulong_long_value, spec);
|
writer.FormatInt(arg.ulong_long_value, spec);
|
||||||
break;
|
break;
|
||||||
case DOUBLE:
|
case DOUBLE:
|
||||||
writer.FormatDouble(arg.double_value, spec, spec.precision_);
|
writer.FormatDouble(arg.double_value, spec);
|
||||||
break;
|
break;
|
||||||
case LONG_DOUBLE:
|
case LONG_DOUBLE:
|
||||||
writer.FormatDouble(arg.long_double_value, spec, spec.precision_);
|
writer.FormatDouble(arg.long_double_value, spec);
|
||||||
break;
|
break;
|
||||||
case CHAR: {
|
case CHAR: {
|
||||||
if (spec.type_ && spec.type_ != 'c')
|
if (spec.type_ && spec.type_ != 'c')
|
||||||
@ -868,7 +867,6 @@ void fmt::BasicWriter<Char>::FormatParser::Format(
|
|||||||
const ArgInfo &arg = ParseArgIndex(s);
|
const ArgInfo &arg = ParseArgIndex(s);
|
||||||
|
|
||||||
FormatSpec spec;
|
FormatSpec spec;
|
||||||
int precision = -1;
|
|
||||||
if (*s == ':') {
|
if (*s == ':') {
|
||||||
++s;
|
++s;
|
||||||
|
|
||||||
@ -946,9 +944,9 @@ void fmt::BasicWriter<Char>::FormatParser::Format(
|
|||||||
// Parse precision.
|
// Parse precision.
|
||||||
if (*s == '.') {
|
if (*s == '.') {
|
||||||
++s;
|
++s;
|
||||||
precision = 0;
|
spec.precision_ = 0;
|
||||||
if ('0' <= *s && *s <= '9') {
|
if ('0' <= *s && *s <= '9') {
|
||||||
precision = internal::ParseNonnegativeInt(s, error);
|
spec.precision_ = internal::ParseNonnegativeInt(s, error);
|
||||||
if (error)
|
if (error)
|
||||||
report_error_(s, error);
|
report_error_(s, error);
|
||||||
} else if (*s == '{') {
|
} else if (*s == '{') {
|
||||||
@ -986,7 +984,7 @@ void fmt::BasicWriter<Char>::FormatParser::Format(
|
|||||||
}
|
}
|
||||||
if (value > INT_MAX)
|
if (value > INT_MAX)
|
||||||
report_error_(s, "number is too big in format");
|
report_error_(s, "number is too big in format");
|
||||||
precision = static_cast<int>(value);
|
spec.precision_ = static_cast<int>(value);
|
||||||
if (*s++ != '}')
|
if (*s++ != '}')
|
||||||
throw FormatError("unmatched '{' in format");
|
throw FormatError("unmatched '{' in format");
|
||||||
--report_error_.num_open_braces;
|
--report_error_.num_open_braces;
|
||||||
@ -1029,10 +1027,10 @@ void fmt::BasicWriter<Char>::FormatParser::Format(
|
|||||||
writer.FormatInt(arg.ulong_long_value, spec);
|
writer.FormatInt(arg.ulong_long_value, spec);
|
||||||
break;
|
break;
|
||||||
case DOUBLE:
|
case DOUBLE:
|
||||||
writer.FormatDouble(arg.double_value, spec, precision);
|
writer.FormatDouble(arg.double_value, spec);
|
||||||
break;
|
break;
|
||||||
case LONG_DOUBLE:
|
case LONG_DOUBLE:
|
||||||
writer.FormatDouble(arg.long_double_value, spec, precision);
|
writer.FormatDouble(arg.long_double_value, spec);
|
||||||
break;
|
break;
|
||||||
case CHAR: {
|
case CHAR: {
|
||||||
if (spec.type_ && spec.type_ != 'c')
|
if (spec.type_ && spec.type_ != 'c')
|
||||||
|
7
format.h
7
format.h
@ -876,9 +876,8 @@ class BasicWriter {
|
|||||||
void FormatInt(T value, const Spec &spec);
|
void FormatInt(T value, const Spec &spec);
|
||||||
|
|
||||||
// Formats a floating-point number (double or long double).
|
// Formats a floating-point number (double or long double).
|
||||||
// TODO: use precision from spec
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void FormatDouble(T value, const FormatSpec &spec, int precision);
|
void FormatDouble(T value, const FormatSpec &spec);
|
||||||
|
|
||||||
// Formats a string.
|
// Formats a string.
|
||||||
template <typename StringChar>
|
template <typename StringChar>
|
||||||
@ -1221,7 +1220,7 @@ class BasicWriter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
BasicWriter &operator<<(double value) {
|
BasicWriter &operator<<(double value) {
|
||||||
FormatDouble(value, FormatSpec(), -1);
|
FormatDouble(value, FormatSpec());
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1230,7 +1229,7 @@ class BasicWriter {
|
|||||||
(``'g'``) and writes it to the stream.
|
(``'g'``) and writes it to the stream.
|
||||||
*/
|
*/
|
||||||
BasicWriter &operator<<(long double value) {
|
BasicWriter &operator<<(long double value) {
|
||||||
FormatDouble(value, FormatSpec(), -1);
|
FormatDouble(value, FormatSpec());
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user