mirror of
https://github.com/fmtlib/fmt.git
synced 2025-07-30 18:57:34 +02:00
Simplify handling of flags.
This commit is contained in:
@ -532,8 +532,8 @@ void fmt::BasicWriter<Char>::write_double(T value, const FormatSpec &spec) {
|
|||||||
if (SignBit(static_cast<double>(value))) {
|
if (SignBit(static_cast<double>(value))) {
|
||||||
sign = '-';
|
sign = '-';
|
||||||
value = -value;
|
value = -value;
|
||||||
} else if (spec.sign_flag()) {
|
} else if (spec.flag(SIGN_FLAG)) {
|
||||||
sign = spec.plus_flag() ? '+' : ' ';
|
sign = spec.flag(PLUS_FLAG) ? '+' : ' ';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (value != value) {
|
if (value != value) {
|
||||||
@ -581,7 +581,7 @@ void fmt::BasicWriter<Char>::write_double(T value, const FormatSpec &spec) {
|
|||||||
Char *format_ptr = format;
|
Char *format_ptr = format;
|
||||||
*format_ptr++ = '%';
|
*format_ptr++ = '%';
|
||||||
unsigned width_for_sprintf = width;
|
unsigned width_for_sprintf = width;
|
||||||
if (spec.hash_flag())
|
if (spec.flag(HASH_FLAG))
|
||||||
*format_ptr++ = '#';
|
*format_ptr++ = '#';
|
||||||
if (spec.align() == ALIGN_CENTER) {
|
if (spec.align() == ALIGN_CENTER) {
|
||||||
width_for_sprintf = 0;
|
width_for_sprintf = 0;
|
||||||
@ -852,7 +852,7 @@ void fmt::internal::PrintfFormatter<Char>::Format(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const Arg &arg = handle_arg_index(arg_index);
|
const Arg &arg = handle_arg_index(arg_index);
|
||||||
if (spec.hash_flag() && GetIntValue(arg) == 0)
|
if (spec.flag(HASH_FLAG) && GetIntValue(arg) == 0)
|
||||||
spec.flags_ &= ~HASH_FLAG;
|
spec.flags_ &= ~HASH_FLAG;
|
||||||
if (spec.fill_ == '0') {
|
if (spec.fill_ == '0') {
|
||||||
if (arg.type <= Arg::LAST_NUMERIC_TYPE)
|
if (arg.type <= Arg::LAST_NUMERIC_TYPE)
|
||||||
|
32
format.h
32
format.h
@ -920,12 +920,7 @@ struct TypeSpec : EmptySpec {
|
|||||||
Alignment align() const { return ALIGN_DEFAULT; }
|
Alignment align() const { return ALIGN_DEFAULT; }
|
||||||
unsigned width() const { return 0; }
|
unsigned width() const { return 0; }
|
||||||
int precision() const { return -1; }
|
int precision() const { return -1; }
|
||||||
|
bool flag(unsigned) const { return false; }
|
||||||
bool sign_flag() const { return false; }
|
|
||||||
bool plus_flag() const { return false; }
|
|
||||||
bool hash_flag() const { return false; }
|
|
||||||
bool char_flag() const { return false; }
|
|
||||||
|
|
||||||
char type() const { return TYPE; }
|
char type() const { return TYPE; }
|
||||||
char fill() const { return ' '; }
|
char fill() const { return ' '; }
|
||||||
};
|
};
|
||||||
@ -960,11 +955,7 @@ 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 sign_flag() const { return false; }
|
bool flag(unsigned) const { return false; }
|
||||||
bool plus_flag() const { return false; }
|
|
||||||
bool hash_flag() const { return false; }
|
|
||||||
bool char_flag() const { return false; }
|
|
||||||
|
|
||||||
char type() const { return TYPE; }
|
char type() const { return TYPE; }
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -978,13 +969,8 @@ 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 sign_flag() const { return (flags_ & SIGN_FLAG) != 0; }
|
bool flag(unsigned f) const { return (flags_ & f) != 0; }
|
||||||
bool plus_flag() const { return (flags_ & PLUS_FLAG) != 0; }
|
|
||||||
bool hash_flag() const { return (flags_ & HASH_FLAG) != 0; }
|
|
||||||
bool char_flag() const { return (flags_ & CHAR_FLAG) != 0; }
|
|
||||||
|
|
||||||
int precision() const { return precision_; }
|
int precision() const { return precision_; }
|
||||||
|
|
||||||
char type() const { return type_; }
|
char type() const { return type_; }
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -1622,8 +1608,8 @@ void BasicWriter<Char>::write_int(T value, const Spec &spec) {
|
|||||||
prefix[0] = '-';
|
prefix[0] = '-';
|
||||||
++prefix_size;
|
++prefix_size;
|
||||||
abs_value = 0 - abs_value;
|
abs_value = 0 - abs_value;
|
||||||
} else if (spec.sign_flag()) {
|
} else if (spec.flag(SIGN_FLAG)) {
|
||||||
prefix[0] = spec.plus_flag() ? '+' : ' ';
|
prefix[0] = spec.flag(PLUS_FLAG) ? '+' : ' ';
|
||||||
++prefix_size;
|
++prefix_size;
|
||||||
}
|
}
|
||||||
switch (spec.type()) {
|
switch (spec.type()) {
|
||||||
@ -1636,7 +1622,7 @@ void BasicWriter<Char>::write_int(T value, const Spec &spec) {
|
|||||||
}
|
}
|
||||||
case 'x': case 'X': {
|
case 'x': case 'X': {
|
||||||
UnsignedType n = abs_value;
|
UnsignedType n = abs_value;
|
||||||
if (spec.hash_flag()) {
|
if (spec.flag(HASH_FLAG)) {
|
||||||
prefix[prefix_size++] = '0';
|
prefix[prefix_size++] = '0';
|
||||||
prefix[prefix_size++] = spec.type();
|
prefix[prefix_size++] = spec.type();
|
||||||
}
|
}
|
||||||
@ -1656,7 +1642,7 @@ void BasicWriter<Char>::write_int(T value, const Spec &spec) {
|
|||||||
}
|
}
|
||||||
case 'b': case 'B': {
|
case 'b': case 'B': {
|
||||||
UnsignedType n = abs_value;
|
UnsignedType n = abs_value;
|
||||||
if (spec.hash_flag()) {
|
if (spec.flag(HASH_FLAG)) {
|
||||||
prefix[prefix_size++] = '0';
|
prefix[prefix_size++] = '0';
|
||||||
prefix[prefix_size++] = spec.type();
|
prefix[prefix_size++] = spec.type();
|
||||||
}
|
}
|
||||||
@ -1673,7 +1659,7 @@ void BasicWriter<Char>::write_int(T value, const Spec &spec) {
|
|||||||
}
|
}
|
||||||
case 'o': {
|
case 'o': {
|
||||||
UnsignedType n = abs_value;
|
UnsignedType n = abs_value;
|
||||||
if (spec.hash_flag())
|
if (spec.flag(HASH_FLAG))
|
||||||
prefix[prefix_size++] = '0';
|
prefix[prefix_size++] = '0';
|
||||||
unsigned num_digits = 0;
|
unsigned num_digits = 0;
|
||||||
do {
|
do {
|
||||||
@ -1689,7 +1675,7 @@ void BasicWriter<Char>::write_int(T value, const Spec &spec) {
|
|||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
internal::ReportUnknownType(
|
internal::ReportUnknownType(
|
||||||
spec.type(), spec.char_flag() ? "char" : "integer");
|
spec.type(), spec.flag(CHAR_FLAG) ? "char" : "integer");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user