Skip to content

Commit

Permalink
Make align a proper enum class
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Aug 11, 2024
1 parent b906c32 commit e123466
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
25 changes: 11 additions & 14 deletions include/fmt/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -2060,11 +2060,6 @@ FMT_EXPORT using format_args = basic_format_args<format_context>;
#else
# define FMT_ENUM_UNDERLYING_TYPE(type) : type
#endif
namespace align {
enum type FMT_ENUM_UNDERLYING_TYPE(unsigned char){none, left, right, center,
numeric};
}
using align_t = align::type;
namespace sign {
enum type FMT_ENUM_UNDERLYING_TYPE(unsigned char){none, minus, plus, space};
}
Expand Down Expand Up @@ -2119,10 +2114,12 @@ enum class presentation_type : unsigned char {
hexfloat // 'a' or 'A'
};

enum class align { none, left, right, center, numeric };

// Basic format specifiers for built-in and string types.
class basic_specs {
private:
// Upper 32-bit of data contain fill and lower 32-bit are arranged as follows:
// Data is arranged as follows:
//
// 0 1 2 3
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
Expand Down Expand Up @@ -2159,7 +2156,7 @@ class basic_specs {
max_fill_size = 4
};

unsigned long long data_ = 1 << fill_size_shift;
unsigned long data_ = 1 << fill_size_shift;

// Character (code unit) type is erased to prevent template bloat.
char fill_data_[max_fill_size] = {' '};
Expand All @@ -2176,10 +2173,10 @@ class basic_specs {
data_ = (data_ & ~type_mask) | static_cast<unsigned>(t);
}

constexpr auto align() const -> align_t {
return static_cast<align_t>((data_ & align_mask) >> align_shift);
constexpr auto align() const -> align {
return static_cast<fmt::align>((data_ & align_mask) >> align_shift);
}
FMT_CONSTEXPR void set_align(align_t a) {
FMT_CONSTEXPR void set_align(fmt::align a) {
data_ = (data_ & ~align_mask) | (static_cast<unsigned>(a) << align_shift);
}

Expand Down Expand Up @@ -2346,7 +2343,7 @@ FMT_CONSTEXPR auto parse_nonnegative_int(const Char*& begin, const Char* end,
: error_value;
}

FMT_CONSTEXPR inline auto parse_align(char c) -> align_t {
FMT_CONSTEXPR inline auto parse_align(char c) -> align {
switch (c) {
case '<':
return align::left;
Expand Down Expand Up @@ -2630,11 +2627,11 @@ FMT_CONSTEXPR auto parse_format_specs(const Char* begin, const Char* end,
report_error("invalid fill character '{'");
return begin;
}
auto align = parse_align(to_ascii(*fill_end));
enter_state(state::align, align != align::none);
auto alignment = parse_align(to_ascii(*fill_end));
enter_state(state::align, alignment != align::none);
specs.set_fill(
basic_string_view<Char>(begin, to_unsigned(fill_end - begin)));
specs.set_align(align);
specs.set_align(alignment);
begin = fill_end + 1;
}
}
Expand Down
18 changes: 10 additions & 8 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -1731,17 +1731,19 @@ FMT_NOINLINE FMT_CONSTEXPR auto fill(OutputIt it, size_t n,
// Writes the output of f, padded according to format specifications in specs.
// size: output size in code units.
// width: output display width in (terminal) column positions.
template <typename Char, align::type align = align::left, typename OutputIt,
template <typename Char, align default_align = align::left, typename OutputIt,
typename F>
FMT_CONSTEXPR auto write_padded(OutputIt out, const format_specs& specs,
size_t size, size_t width, F&& f) -> OutputIt {
static_assert(align == align::left || align == align::right, "");
static_assert(default_align == align::left || default_align == align::right,
"");
unsigned spec_width = to_unsigned(specs.width);
size_t padding = spec_width > width ? spec_width - width : 0;
// Shifts are encoded as string literals because static constexpr is not
// supported in constexpr functions.
auto* shifts = align == align::left ? "\x1f\x1f\x00\x01" : "\x00\x1f\x00\x01";
size_t left_padding = padding >> shifts[specs.align()];
auto* shifts =
default_align == align::left ? "\x1f\x1f\x00\x01" : "\x00\x1f\x00\x01";
size_t left_padding = padding >> shifts[static_cast<int>(specs.align())];
size_t right_padding = padding - left_padding;
auto it = reserve(out, size + padding * specs.fill_size());
if (left_padding != 0) it = fill<Char>(it, left_padding, specs);
Expand All @@ -1750,17 +1752,17 @@ FMT_CONSTEXPR auto write_padded(OutputIt out, const format_specs& specs,
return base_iterator(out, it);
}

template <typename Char, align::type align = align::left, typename OutputIt,
template <typename Char, align default_align = align::left, typename OutputIt,
typename F>
constexpr auto write_padded(OutputIt out, const format_specs& specs,
size_t size, F&& f) -> OutputIt {
return write_padded<Char, align>(out, specs, size, size, f);
return write_padded<Char, default_align>(out, specs, size, size, f);
}

template <typename Char, align::type align = align::left, typename OutputIt>
template <typename Char, align default_align = align::left, typename OutputIt>
FMT_CONSTEXPR auto write_bytes(OutputIt out, string_view bytes,
const format_specs& specs = {}) -> OutputIt {
return write_padded<Char, align>(
return write_padded<Char, default_align>(
out, specs, bytes.size(), [bytes](reserve_iterator<OutputIt> it) {
const char* data = bytes.data();
return copy<Char>(data, data + bytes.size(), it);
Expand Down

0 comments on commit e123466

Please sign in to comment.