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 ab9a264
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
15 changes: 6 additions & 9 deletions include/fmt/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -2051,6 +2051,8 @@ FMT_END_EXPORT
// between clang and gcc on ARM (#1919).
FMT_EXPORT using format_args = basic_format_args<format_context>;

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

// We cannot use enum classes as bit fields because of a gcc bug, so we put them
// in namespaces instead (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61414).
// Additionally, if an underlying type is specified, older gcc incorrectly warns
Expand All @@ -2060,11 +2062,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 @@ -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
8 changes: 4 additions & 4 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -1731,7 +1731,7 @@ 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 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 {
Expand All @@ -1741,7 +1741,7 @@ FMT_CONSTEXPR auto write_padded(OutputIt out, const format_specs& specs,
// 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()];
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,14 +1750,14 @@ 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 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);
}

template <typename Char, align::type align = align::left, typename OutputIt>
template <typename Char, align 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>(
Expand Down

0 comments on commit ab9a264

Please sign in to comment.