From 1416edabbb0f9b91053555d80015e6857f6dc433 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sun, 1 Sep 2024 12:06:40 -0700 Subject: [PATCH] Cleanup base API --- include/fmt/base.h | 1983 ++++++++++++++++++++++---------------------- 1 file changed, 989 insertions(+), 994 deletions(-) diff --git a/include/fmt/base.h b/include/fmt/base.h index 14369ca276e4..da573887ebb0 100644 --- a/include/fmt/base.h +++ b/include/fmt/base.h @@ -686,6 +686,11 @@ struct has_to_string_view< T, void_t()))>> : std::true_type {}; +template +using unsigned_char = typename conditional_t::value, + std::make_unsigned, + type_identity>::type; + enum class type { none_type, // Integer types should go first, @@ -776,6 +781,184 @@ template ()))> using char_t = typename V::value_type; +enum class presentation_type : unsigned char { + // Common specifiers: + none = 0, + debug = 1, // '?' + string = 2, // 's' (string, bool) + + // Integral, bool and character specifiers: + dec = 3, // 'd' + hex, // 'x' or 'X' + oct, // 'o' + bin, // 'b' or 'B' + chr, // 'c' + + // String and pointer specifiers: + pointer = 3, // 'p' + + // Floating-point specifiers: + exp = 1, // 'e' or 'E' (1 since there is no FP debug presentation) + fixed, // 'f' or 'F' + general, // 'g' or 'G' + hexfloat // 'a' or 'A' +}; + +enum class align { none, left, right, center, numeric }; +enum class sign { none, minus, plus, space }; +enum class arg_id_kind { none, index, name }; + +// Basic format specifiers for built-in and string types. +class basic_specs { + private: + // 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 + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // |type |align| w | p | s |u|#|L| f | unused | + // +-----+-----+---+---+---+-+-+-+-----+---------------------------+ + // + // w - dynamic width info + // p - dynamic precision info + // s - sign + // u - uppercase (e.g. 'X' for 'x') + // # - alternate form ('#') + // L - localized + // f - fill size + // + // Bitfields are not used because of compiler bugs such as gcc bug 61414. + enum : unsigned { + type_mask = 0x00007, + align_mask = 0x00038, + width_mask = 0x000C0, + precision_mask = 0x00300, + sign_mask = 0x00C00, + uppercase_mask = 0x01000, + alternate_mask = 0x02000, + localized_mask = 0x04000, + fill_size_mask = 0x38000, + + align_shift = 3, + width_shift = 6, + precision_shift = 8, + sign_shift = 10, + fill_size_shift = 15, + + max_fill_size = 4 + }; + + unsigned long data_ = 1 << fill_size_shift; + + // Character (code unit) type is erased to prevent template bloat. + char fill_data_[max_fill_size] = {' '}; + + FMT_CONSTEXPR void set_fill_size(size_t size) { + data_ = (data_ & ~fill_size_mask) | (size << fill_size_shift); + } + + public: + constexpr auto type() const -> presentation_type { + return static_cast(data_ & type_mask); + } + FMT_CONSTEXPR void set_type(presentation_type t) { + data_ = (data_ & ~type_mask) | static_cast(t); + } + + constexpr auto align() const -> align { + return static_cast((data_ & align_mask) >> align_shift); + } + FMT_CONSTEXPR void set_align(fmt::align a) { + data_ = (data_ & ~align_mask) | (static_cast(a) << align_shift); + } + + constexpr auto dynamic_width() const -> arg_id_kind { + return static_cast((data_ & width_mask) >> width_shift); + } + FMT_CONSTEXPR void set_dynamic_width(arg_id_kind w) { + data_ = (data_ & ~width_mask) | (static_cast(w) << width_shift); + } + + FMT_CONSTEXPR auto dynamic_precision() const -> arg_id_kind { + return static_cast((data_ & precision_mask) >> + precision_shift); + } + FMT_CONSTEXPR void set_dynamic_precision(arg_id_kind p) { + data_ = (data_ & ~precision_mask) | + (static_cast(p) << precision_shift); + } + + constexpr bool dynamic() const { + return (data_ & (width_mask | precision_mask)) != 0; + } + + constexpr auto sign() const -> sign { + return static_cast((data_ & sign_mask) >> sign_shift); + } + FMT_CONSTEXPR void set_sign(fmt::sign s) { + data_ = (data_ & ~sign_mask) | (static_cast(s) << sign_shift); + } + + constexpr auto upper() const -> bool { return (data_ & uppercase_mask) != 0; } + FMT_CONSTEXPR void set_upper() { data_ |= uppercase_mask; } + + constexpr auto alt() const -> bool { return (data_ & alternate_mask) != 0; } + FMT_CONSTEXPR void set_alt() { data_ |= alternate_mask; } + FMT_CONSTEXPR void clear_alt() { data_ &= ~alternate_mask; } + + constexpr auto localized() const -> bool { + return (data_ & localized_mask) != 0; + } + FMT_CONSTEXPR void set_localized() { data_ |= localized_mask; } + + constexpr auto fill_size() const -> size_t { + return (data_ & fill_size_mask) >> fill_size_shift; + } + + template ::value)> + constexpr auto fill() const -> const Char* { + return fill_data_; + } + template ::value)> + constexpr auto fill() const -> const Char* { + return nullptr; + } + + template constexpr auto fill_unit() const -> Char { + using uchar = unsigned char; + return static_cast(static_cast(fill_data_[0]) | + (static_cast(fill_data_[1]) << 8)); + } + + FMT_CONSTEXPR void set_fill(char c) { + fill_data_[0] = c; + set_fill_size(1); + } + + template + FMT_CONSTEXPR void set_fill(basic_string_view s) { + auto size = s.size(); + set_fill_size(size); + if (size == 1) { + unsigned uchar = static_cast>(s[0]); + fill_data_[0] = static_cast(uchar); + fill_data_[1] = static_cast(uchar >> 8); + return; + } + FMT_ASSERT(size <= max_fill_size, "invalid fill"); + for (size_t i = 0; i < size; ++i) + fill_data_[i & 3] = static_cast(s[i]); + } +}; + +// Format specifiers for built-in and string types. +struct format_specs : basic_specs { + int width; + int precision; + + constexpr format_specs() : width(0), precision(-1) {} +}; + /** * Parsing context consisting of a format string range being parsed and an * argument counter for automatic indexing. @@ -880,236 +1063,672 @@ class compile_parse_context : public parse_context { } }; -/// A contiguous memory buffer with an optional growing ability. It is an -/// internal class and shouldn't be used directly, only via `memory_buffer`. -template class buffer { - private: - T* ptr_; - size_t size_; - size_t capacity_; - - using grow_fun = void (*)(buffer& buf, size_t capacity); - grow_fun grow_; +// An argument reference. +template union arg_ref { + FMT_CONSTEXPR arg_ref(int idx = 0) : index(idx) {} + FMT_CONSTEXPR arg_ref(basic_string_view n) : name(n) {} - protected: - // Don't initialize ptr_ since it is not accessed to save a few cycles. - FMT_MSC_WARNING(suppress : 26495) - FMT_CONSTEXPR20 buffer(grow_fun grow, size_t sz) noexcept - : size_(sz), capacity_(sz), grow_(grow) {} + int index; + basic_string_view name; +}; - constexpr buffer(grow_fun grow, T* p = nullptr, size_t sz = 0, - size_t cap = 0) noexcept - : ptr_(p), size_(sz), capacity_(cap), grow_(grow) {} +// Format specifiers with width and precision resolved at formatting rather +// than parsing time to allow reusing the same parsed specifiers with +// different sets of arguments (precompilation of format strings). +template struct dynamic_format_specs : format_specs { + arg_ref width_ref; + arg_ref precision_ref; +}; - FMT_CONSTEXPR20 ~buffer() = default; - buffer(buffer&&) = default; +// Converts a character to ASCII. Returns '\0' on conversion failure. +template ::value)> +constexpr auto to_ascii(Char c) -> char { + return c <= 0xff ? static_cast(c) : '\0'; +} - /// Sets the buffer data and capacity. - FMT_CONSTEXPR void set(T* buf_data, size_t buf_capacity) noexcept { - ptr_ = buf_data; - capacity_ = buf_capacity; - } +// Returns the number of code units in a code point or 1 on error. +template +FMT_CONSTEXPR auto code_point_length(const Char* begin) -> int { + if (const_check(sizeof(Char) != 1)) return 1; + auto c = static_cast(*begin); + return static_cast((0x3a55000000000000ull >> (2 * (c >> 3))) & 3) + 1; +} - public: - using value_type = T; - using const_reference = const T&; +// Parses the range [begin, end) as an unsigned integer. This function assumes +// that the range is non-empty and the first character is a digit. +template +FMT_CONSTEXPR auto parse_nonnegative_int(const Char*& begin, const Char* end, + int error_value) noexcept -> int { + FMT_ASSERT(begin != end && '0' <= *begin && *begin <= '9', ""); + unsigned value = 0, prev = 0; + auto p = begin; + do { + prev = value; + value = value * 10 + unsigned(*p - '0'); + ++p; + } while (p != end && '0' <= *p && *p <= '9'); + auto num_digits = p - begin; + begin = p; + int digits10 = static_cast(sizeof(int) * CHAR_BIT * 3 / 10); + if (num_digits <= digits10) return static_cast(value); + // Check for overflow. + unsigned max = INT_MAX; + return num_digits == digits10 + 1 && + prev * 10ull + unsigned(p[-1] - '0') <= max + ? static_cast(value) + : error_value; +} - buffer(const buffer&) = delete; - void operator=(const buffer&) = delete; - - auto begin() noexcept -> T* { return ptr_; } - auto end() noexcept -> T* { return ptr_ + size_; } - - auto begin() const noexcept -> const T* { return ptr_; } - auto end() const noexcept -> const T* { return ptr_ + size_; } - - /// Returns the size of this buffer. - constexpr auto size() const noexcept -> size_t { return size_; } - - /// Returns the capacity of this buffer. - constexpr auto capacity() const noexcept -> size_t { return capacity_; } - - /// Returns a pointer to the buffer data (not null-terminated). - FMT_CONSTEXPR auto data() noexcept -> T* { return ptr_; } - FMT_CONSTEXPR auto data() const noexcept -> const T* { return ptr_; } - - /// Clears this buffer. - FMT_CONSTEXPR void clear() { size_ = 0; } - - // Tries resizing the buffer to contain `count` elements. If T is a POD type - // the new elements may not be initialized. - FMT_CONSTEXPR void try_resize(size_t count) { - try_reserve(count); - size_ = count <= capacity_ ? count : capacity_; +FMT_CONSTEXPR inline auto parse_align(char c) -> align { + switch (c) { + case '<': + return align::left; + case '>': + return align::right; + case '^': + return align::center; } + return align::none; +} - // Tries increasing the buffer capacity to `new_capacity`. It can increase the - // capacity by a smaller amount than requested but guarantees there is space - // for at least one additional element either by increasing the capacity or by - // flushing the buffer if it is full. - FMT_CONSTEXPR void try_reserve(size_t new_capacity) { - if (new_capacity > capacity_) grow_(*this, new_capacity); - } +template constexpr auto is_name_start(Char c) -> bool { + return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == '_'; +} - FMT_CONSTEXPR void push_back(const T& value) { - try_reserve(size_ + 1); - ptr_[size_++] = value; +template +FMT_CONSTEXPR auto parse_arg_id(const Char* begin, const Char* end, + Handler&& handler) -> const Char* { + Char c = *begin; + if (c >= '0' && c <= '9') { + int index = 0; + if (c != '0') + index = parse_nonnegative_int(begin, end, INT_MAX); + else + ++begin; + if (begin == end || (*begin != '}' && *begin != ':')) + report_error("invalid format string"); + else + handler.on_index(index); + return begin; } - - /// Appends data to the end of the buffer. - template -// Workaround for MSVC2019 to fix error C2893: Failed to specialize function -// template 'void fmt::v11::detail::buffer::append(const U *,const U *)'. -#if !FMT_MSC_VERSION || FMT_MSC_VERSION >= 1930 - FMT_CONSTEXPR20 -#endif - void - append(const U* begin, const U* end) { - while (begin != end) { - auto count = to_unsigned(end - begin); - try_reserve(size_ + count); - auto free_cap = capacity_ - size_; - if (free_cap < count) count = free_cap; - // A loop is faster than memcpy on small sizes. - T* out = ptr_ + size_; - for (size_t i = 0; i < count; ++i) out[i] = begin[i]; - size_ += count; - begin += count; - } + if (!is_name_start(c)) { + report_error("invalid format string"); + return begin; } + auto it = begin; + do { + ++it; + } while (it != end && (is_name_start(*it) || ('0' <= *it && *it <= '9'))); + handler.on_name({begin, to_unsigned(it - begin)}); + return it; +} - template FMT_CONSTEXPR auto operator[](Idx index) -> T& { - return ptr_[index]; +template struct dynamic_spec_handler { + parse_context& ctx; + arg_ref& ref; + arg_id_kind& kind; + + FMT_CONSTEXPR void on_index(int id) { + ref = id; + kind = arg_id_kind::index; + ctx.check_arg_id(id); + ctx.check_dynamic_spec(id); } - template - FMT_CONSTEXPR auto operator[](Idx index) const -> const T& { - return ptr_[index]; + FMT_CONSTEXPR void on_name(basic_string_view id) { + ref = id; + kind = arg_id_kind::name; + ctx.check_arg_id(id); } }; -struct buffer_traits { - explicit buffer_traits(size_t) {} - auto count() const -> size_t { return 0; } - auto limit(size_t size) -> size_t { return size; } +template struct parse_dynamic_spec_result { + const Char* end; + arg_id_kind kind; }; -class fixed_buffer_traits { - private: - size_t count_ = 0; - size_t limit_; - - public: - explicit fixed_buffer_traits(size_t limit) : limit_(limit) {} - auto count() const -> size_t { return count_; } - auto limit(size_t size) -> size_t { - size_t n = limit_ > count_ ? limit_ - count_ : 0; - count_ += size; - return size < n ? size : n; +// Parses integer | "{" [arg_id] "}". +template +FMT_CONSTEXPR auto parse_dynamic_spec(const Char* begin, const Char* end, + int& value, arg_ref& ref, + parse_context& ctx) + -> parse_dynamic_spec_result { + FMT_ASSERT(begin != end, ""); + auto kind = arg_id_kind::none; + if ('0' <= *begin && *begin <= '9') { + int val = parse_nonnegative_int(begin, end, -1); + if (val == -1) report_error("number is too big"); + value = val; + } else { + if (*begin == '{') { + ++begin; + if (begin != end) { + Char c = *begin; + if (c == '}' || c == ':') { + int id = ctx.next_arg_id(); + ref = id; + kind = arg_id_kind::index; + ctx.check_dynamic_spec(id); + } else { + begin = parse_arg_id(begin, end, + dynamic_spec_handler{ctx, ref, kind}); + } + } + if (begin != end && *begin == '}') return {++begin, kind}; + } + report_error("invalid format string"); } -}; + return {begin, kind}; +} -// A buffer that writes to an output iterator when flushed. -template -class iterator_buffer : public Traits, public buffer { - private: - OutputIt out_; - enum { buffer_size = 256 }; - T data_[buffer_size]; +template +FMT_CONSTEXPR auto parse_width(const Char* begin, const Char* end, + format_specs& specs, arg_ref& width_ref, + parse_context& ctx) -> const Char* { + auto result = parse_dynamic_spec(begin, end, specs.width, width_ref, ctx); + specs.set_dynamic_width(result.kind); + return result.end; +} - static FMT_CONSTEXPR void grow(buffer& buf, size_t) { - if (buf.size() == buffer_size) static_cast(buf).flush(); +template +FMT_CONSTEXPR auto parse_precision(const Char* begin, const Char* end, + format_specs& specs, + arg_ref& precision_ref, + parse_context& ctx) -> const Char* { + ++begin; + if (begin == end) { + report_error("invalid precision"); + return begin; } + auto result = + parse_dynamic_spec(begin, end, specs.precision, precision_ref, ctx); + specs.set_dynamic_precision(result.kind); + return result.end; +} - void flush() { - auto size = this->size(); - this->clear(); - const T* begin = data_; - const T* end = begin + this->limit(size); - while (begin != end) *out_++ = *begin++; - } +enum class state { start, align, sign, hash, zero, width, precision, locale }; - public: - explicit iterator_buffer(OutputIt out, size_t n = buffer_size) - : Traits(n), buffer(grow, data_, 0, buffer_size), out_(out) {} - iterator_buffer(iterator_buffer&& other) noexcept - : Traits(other), - buffer(grow, data_, 0, buffer_size), - out_(other.out_) {} - ~iterator_buffer() { - // Don't crash if flush fails during unwinding. - FMT_TRY { flush(); } - FMT_CATCH(...) {} +// Parses standard format specifiers. +template +FMT_CONSTEXPR auto parse_format_specs(const Char* begin, const Char* end, + dynamic_format_specs& specs, + parse_context& ctx, type arg_type) + -> const Char* { + auto c = '\0'; + if (end - begin > 1) { + auto next = to_ascii(begin[1]); + c = parse_align(next) == align::none ? to_ascii(*begin) : '\0'; + } else { + if (begin == end) return begin; + c = to_ascii(*begin); } - auto out() -> OutputIt { - flush(); - return out_; - } - auto count() const -> size_t { return Traits::count() + this->size(); } -}; + struct { + state current_state = state::start; + FMT_CONSTEXPR void operator()(state s, bool valid = true) { + if (current_state >= s || !valid) + report_error("invalid format specifier"); + current_state = s; + } + } enter_state; -template -class iterator_buffer : public fixed_buffer_traits, - public buffer { - private: - T* out_; - enum { buffer_size = 256 }; - T data_[buffer_size]; + using pres = presentation_type; + constexpr auto integral_set = sint_set | uint_set | bool_set | char_set; + struct { + const Char*& begin; + format_specs& specs; + type arg_type; - static FMT_CONSTEXPR void grow(buffer& buf, size_t) { - if (buf.size() == buf.capacity()) - static_cast(buf).flush(); - } + FMT_CONSTEXPR auto operator()(pres pres_type, int set) -> const Char* { + if (!in(arg_type, set)) report_error("invalid format specifier"); + specs.set_type(pres_type); + return begin + 1; + } + } parse_presentation_type{begin, specs, arg_type}; - void flush() { - size_t n = this->limit(this->size()); - if (this->data() == out_) { - out_ += n; - this->set(data_, buffer_size); + for (;;) { + switch (c) { + case '<': + case '>': + case '^': + enter_state(state::align); + specs.set_align(parse_align(c)); + ++begin; + break; + case '+': + FMT_FALLTHROUGH; + case ' ': + specs.set_sign(c == ' ' ? sign::space : sign::plus); + FMT_FALLTHROUGH; + case '-': + enter_state(state::sign, in(arg_type, sint_set | float_set)); + ++begin; + break; + case '#': + enter_state(state::hash, is_arithmetic_type(arg_type)); + specs.set_alt(); + ++begin; + break; + case '0': + enter_state(state::zero); + if (!is_arithmetic_type(arg_type)) + report_error("format specifier requires numeric argument"); + if (specs.align() == align::none) { + // Ignore 0 if align is specified for compatibility with std::format. + specs.set_align(align::numeric); + specs.set_fill('0'); + } + ++begin; + break; + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + case '{': + enter_state(state::width); + begin = parse_width(begin, end, specs, specs.width_ref, ctx); + break; + case '.': + enter_state(state::precision, + in(arg_type, float_set | string_set | cstring_set)); + begin = parse_precision(begin, end, specs, specs.precision_ref, ctx); + break; + case 'L': + enter_state(state::locale, is_arithmetic_type(arg_type)); + specs.set_localized(); + ++begin; + break; + case 'd': + return parse_presentation_type(pres::dec, integral_set); + case 'X': + specs.set_upper(); + FMT_FALLTHROUGH; + case 'x': + return parse_presentation_type(pres::hex, integral_set); + case 'o': + return parse_presentation_type(pres::oct, integral_set); + case 'B': + specs.set_upper(); + FMT_FALLTHROUGH; + case 'b': + return parse_presentation_type(pres::bin, integral_set); + case 'E': + specs.set_upper(); + FMT_FALLTHROUGH; + case 'e': + return parse_presentation_type(pres::exp, float_set); + case 'F': + specs.set_upper(); + FMT_FALLTHROUGH; + case 'f': + return parse_presentation_type(pres::fixed, float_set); + case 'G': + specs.set_upper(); + FMT_FALLTHROUGH; + case 'g': + return parse_presentation_type(pres::general, float_set); + case 'A': + specs.set_upper(); + FMT_FALLTHROUGH; + case 'a': + return parse_presentation_type(pres::hexfloat, float_set); + case 'c': + if (arg_type == type::bool_type) report_error("invalid format specifier"); + return parse_presentation_type(pres::chr, integral_set); + case 's': + return parse_presentation_type(pres::string, + bool_set | string_set | cstring_set); + case 'p': + return parse_presentation_type(pres::pointer, pointer_set | cstring_set); + case '?': + return parse_presentation_type(pres::debug, + char_set | string_set | cstring_set); + case '}': + return begin; + default: { + if (*begin == '}') return begin; + // Parse fill and alignment. + auto fill_end = begin + code_point_length(begin); + if (end - fill_end <= 0) { + report_error("invalid format specifier"); + return begin; + } + if (*begin == '{') { + report_error("invalid fill character '{'"); + return begin; + } + auto alignment = parse_align(to_ascii(*fill_end)); + enter_state(state::align, alignment != align::none); + specs.set_fill( + basic_string_view(begin, to_unsigned(fill_end - begin))); + specs.set_align(alignment); + begin = fill_end + 1; } - this->clear(); + } + if (begin == end) return begin; + c = to_ascii(*begin); } +} - public: - explicit iterator_buffer(T* out, size_t n = buffer_size) - : fixed_buffer_traits(n), buffer(grow, out, 0, n), out_(out) {} - iterator_buffer(iterator_buffer&& other) noexcept - : fixed_buffer_traits(other), - buffer(static_cast(other)), - out_(other.out_) { - if (this->data() != out_) { - this->set(data_, buffer_size); - this->clear(); - } +template +FMT_CONSTEXPR FMT_INLINE auto parse_replacement_field(const Char* begin, + const Char* end, + Handler&& handler) + -> const Char* { + ++begin; + if (begin == end) { + handler.on_error("invalid format string"); + return end; } - ~iterator_buffer() { flush(); } + int arg_id = 0; + switch (*begin) { + case '}': + handler.on_replacement_field(handler.on_arg_id(), begin); + return begin + 1; + case '{': + handler.on_text(begin, begin + 1); + return begin + 1; + case ':': + arg_id = handler.on_arg_id(); + break; + default: { + struct id_adapter { + Handler& handler; + int arg_id; - auto out() -> T* { - flush(); - return out_; + FMT_CONSTEXPR void on_index(int id) { arg_id = handler.on_arg_id(id); } + FMT_CONSTEXPR void on_name(basic_string_view id) { + arg_id = handler.on_arg_id(id); + } + } adapter = {handler, 0}; + begin = parse_arg_id(begin, end, adapter); + arg_id = adapter.arg_id; + Char c = begin != end ? *begin : Char(); + if (c == '}') { + handler.on_replacement_field(arg_id, begin); + return begin + 1; + } + if (c != ':') { + handler.on_error("missing '}' in format string"); + return end; + } + break; } - auto count() const -> size_t { - return fixed_buffer_traits::count() + this->size(); } -}; + begin = handler.on_format_specs(arg_id, begin + 1, end); + if (begin == end || *begin != '}') + return handler.on_error("unknown format specifier"), end; + return begin + 1; +} -template class iterator_buffer : public buffer { - public: - explicit iterator_buffer(T* out, size_t = 0) - : buffer([](buffer&, size_t) {}, out, 0, ~size_t()) {} +template +FMT_CONSTEXPR void parse_format_string(basic_string_view fmt, + Handler&& handler) { + auto begin = fmt.data(), end = begin + fmt.size(); + auto p = begin; + while (p != end) { + auto c = *p++; + if (c == '{') { + handler.on_text(begin, p - 1); + begin = p = parse_replacement_field(p - 1, end, handler); + } else if (c == '}') { + if (p == end || *p != '}') + return handler.on_error("unmatched '}' in format string"); + handler.on_text(begin, p); + begin = ++p; + } + } + handler.on_text(begin, end); +} - auto out() -> T* { return &*this->end(); } -}; +// Checks char specs and returns true iff the presentation type is char-like. +FMT_CONSTEXPR inline auto check_char_specs(const format_specs& specs) -> bool { + auto type = specs.type(); + if (type != presentation_type::none && type != presentation_type::chr && + type != presentation_type::debug) { + return false; + } + if (specs.align() == align::numeric || specs.sign() != sign::none || + specs.alt()) { + report_error("invalid format specifier for char"); + } + return true; +} -template -class container_buffer : public buffer { +/// A contiguous memory buffer with an optional growing ability. It is an +/// internal class and shouldn't be used directly, only via `memory_buffer`. +template class buffer { private: - using value_type = typename Container::value_type; + T* ptr_; + size_t size_; + size_t capacity_; - static FMT_CONSTEXPR void grow(buffer& buf, size_t capacity) { - auto& self = static_cast(buf); - self.container.resize(capacity); - self.set(&self.container[0], capacity); + using grow_fun = void (*)(buffer& buf, size_t capacity); + grow_fun grow_; + + protected: + // Don't initialize ptr_ since it is not accessed to save a few cycles. + FMT_MSC_WARNING(suppress : 26495) + FMT_CONSTEXPR20 buffer(grow_fun grow, size_t sz) noexcept + : size_(sz), capacity_(sz), grow_(grow) {} + + constexpr buffer(grow_fun grow, T* p = nullptr, size_t sz = 0, + size_t cap = 0) noexcept + : ptr_(p), size_(sz), capacity_(cap), grow_(grow) {} + + FMT_CONSTEXPR20 ~buffer() = default; + buffer(buffer&&) = default; + + /// Sets the buffer data and capacity. + FMT_CONSTEXPR void set(T* buf_data, size_t buf_capacity) noexcept { + ptr_ = buf_data; + capacity_ = buf_capacity; + } + + public: + using value_type = T; + using const_reference = const T&; + + buffer(const buffer&) = delete; + void operator=(const buffer&) = delete; + + auto begin() noexcept -> T* { return ptr_; } + auto end() noexcept -> T* { return ptr_ + size_; } + + auto begin() const noexcept -> const T* { return ptr_; } + auto end() const noexcept -> const T* { return ptr_ + size_; } + + /// Returns the size of this buffer. + constexpr auto size() const noexcept -> size_t { return size_; } + + /// Returns the capacity of this buffer. + constexpr auto capacity() const noexcept -> size_t { return capacity_; } + + /// Returns a pointer to the buffer data (not null-terminated). + FMT_CONSTEXPR auto data() noexcept -> T* { return ptr_; } + FMT_CONSTEXPR auto data() const noexcept -> const T* { return ptr_; } + + /// Clears this buffer. + FMT_CONSTEXPR void clear() { size_ = 0; } + + // Tries resizing the buffer to contain `count` elements. If T is a POD type + // the new elements may not be initialized. + FMT_CONSTEXPR void try_resize(size_t count) { + try_reserve(count); + size_ = count <= capacity_ ? count : capacity_; + } + + // Tries increasing the buffer capacity to `new_capacity`. It can increase the + // capacity by a smaller amount than requested but guarantees there is space + // for at least one additional element either by increasing the capacity or by + // flushing the buffer if it is full. + FMT_CONSTEXPR void try_reserve(size_t new_capacity) { + if (new_capacity > capacity_) grow_(*this, new_capacity); + } + + FMT_CONSTEXPR void push_back(const T& value) { + try_reserve(size_ + 1); + ptr_[size_++] = value; + } + + /// Appends data to the end of the buffer. + template +// Workaround for MSVC2019 to fix error C2893: Failed to specialize function +// template 'void fmt::v11::detail::buffer::append(const U *,const U *)'. +#if !FMT_MSC_VERSION || FMT_MSC_VERSION >= 1930 + FMT_CONSTEXPR20 +#endif + void + append(const U* begin, const U* end) { + while (begin != end) { + auto count = to_unsigned(end - begin); + try_reserve(size_ + count); + auto free_cap = capacity_ - size_; + if (free_cap < count) count = free_cap; + // A loop is faster than memcpy on small sizes. + T* out = ptr_ + size_; + for (size_t i = 0; i < count; ++i) out[i] = begin[i]; + size_ += count; + begin += count; + } + } + + template FMT_CONSTEXPR auto operator[](Idx index) -> T& { + return ptr_[index]; + } + template + FMT_CONSTEXPR auto operator[](Idx index) const -> const T& { + return ptr_[index]; + } +}; + +struct buffer_traits { + explicit buffer_traits(size_t) {} + auto count() const -> size_t { return 0; } + auto limit(size_t size) -> size_t { return size; } +}; + +class fixed_buffer_traits { + private: + size_t count_ = 0; + size_t limit_; + + public: + explicit fixed_buffer_traits(size_t limit) : limit_(limit) {} + auto count() const -> size_t { return count_; } + auto limit(size_t size) -> size_t { + size_t n = limit_ > count_ ? limit_ - count_ : 0; + count_ += size; + return size < n ? size : n; + } +}; + +// A buffer that writes to an output iterator when flushed. +template +class iterator_buffer : public Traits, public buffer { + private: + OutputIt out_; + enum { buffer_size = 256 }; + T data_[buffer_size]; + + static FMT_CONSTEXPR void grow(buffer& buf, size_t) { + if (buf.size() == buffer_size) static_cast(buf).flush(); + } + + void flush() { + auto size = this->size(); + this->clear(); + const T* begin = data_; + const T* end = begin + this->limit(size); + while (begin != end) *out_++ = *begin++; + } + + public: + explicit iterator_buffer(OutputIt out, size_t n = buffer_size) + : Traits(n), buffer(grow, data_, 0, buffer_size), out_(out) {} + iterator_buffer(iterator_buffer&& other) noexcept + : Traits(other), + buffer(grow, data_, 0, buffer_size), + out_(other.out_) {} + ~iterator_buffer() { + // Don't crash if flush fails during unwinding. + FMT_TRY { flush(); } + FMT_CATCH(...) {} + } + + auto out() -> OutputIt { + flush(); + return out_; + } + auto count() const -> size_t { return Traits::count() + this->size(); } +}; + +template +class iterator_buffer : public fixed_buffer_traits, + public buffer { + private: + T* out_; + enum { buffer_size = 256 }; + T data_[buffer_size]; + + static FMT_CONSTEXPR void grow(buffer& buf, size_t) { + if (buf.size() == buf.capacity()) + static_cast(buf).flush(); + } + + void flush() { + size_t n = this->limit(this->size()); + if (this->data() == out_) { + out_ += n; + this->set(data_, buffer_size); + } + this->clear(); + } + + public: + explicit iterator_buffer(T* out, size_t n = buffer_size) + : fixed_buffer_traits(n), buffer(grow, out, 0, n), out_(out) {} + iterator_buffer(iterator_buffer&& other) noexcept + : fixed_buffer_traits(other), + buffer(static_cast(other)), + out_(other.out_) { + if (this->data() != out_) { + this->set(data_, buffer_size); + this->clear(); + } + } + ~iterator_buffer() { flush(); } + + auto out() -> T* { + flush(); + return out_; + } + auto count() const -> size_t { + return fixed_buffer_traits::count() + this->size(); + } +}; + +template class iterator_buffer : public buffer { + public: + explicit iterator_buffer(T* out, size_t = 0) + : buffer([](buffer&, size_t) {}, out, 0, ~size_t()) {} + + auto out() -> T* { return &*this->end(); } +}; + +template +class container_buffer : public buffer { + private: + using value_type = typename Container::value_type; + + static FMT_CONSTEXPR void grow(buffer& buf, size_t capacity) { + auto& self = static_cast(buf); + self.container.resize(capacity); + self.set(&self.container[0], capacity); } public: @@ -1987,810 +2606,110 @@ template class basic_format_args { store) : desc_(DESC), args_(store.args + (NUM_NAMED_ARGS != 0 ? 1 : 0)) {} - /// Constructs a `basic_format_args` object from `dynamic_format_arg_store`. - constexpr basic_format_args(const dynamic_format_arg_store& store) - : desc_(store.get_types()), args_(store.data()) {} - - /// Constructs a `basic_format_args` object from a dynamic list of arguments. - constexpr basic_format_args(const format_arg* args, int count) - : desc_(detail::is_unpacked_bit | detail::to_unsigned(count)), - args_(args) {} - - /// Returns the argument with the specified id. - FMT_CONSTEXPR auto get(int id) const -> format_arg { - format_arg arg; - if (!is_packed()) { - if (id < max_size()) arg = args_[id]; - return arg; - } - if (static_cast(id) >= detail::max_packed_args) return arg; - arg.type_ = type(id); - if (arg.type_ != detail::type::none_type) arg.value_ = values_[id]; - return arg; - } - - template - auto get(basic_string_view name) const -> format_arg { - int id = get_id(name); - return id >= 0 ? get(id) : format_arg(); - } - - template - FMT_CONSTEXPR auto get_id(basic_string_view name) const -> int { - if (!has_named_args()) return -1; - const auto& named_args = - (is_packed() ? values_[-1] : args_[-1].value_).named_args; - for (size_t i = 0; i < named_args.size; ++i) { - if (named_args.data[i].name == name) return named_args.data[i].id; - } - return -1; - } - - auto max_size() const -> int { - unsigned long long max_packed = detail::max_packed_args; - return static_cast(is_packed() ? max_packed - : desc_ & ~detail::is_unpacked_bit); - } -}; - -// A formatting context. -class context { - private: - appender out_; - basic_format_args args_; - FMT_NO_UNIQUE_ADDRESS detail::locale_ref loc_; - - public: - /// The character type for the output. - using char_type = char; - - using iterator = appender; - using format_arg = basic_format_arg; - using parse_context_type = parse_context; - template using formatter_type = formatter; - enum { builtin_types = FMT_BUILTIN_TYPES }; - - /// Constructs a `basic_format_context` object. References to the arguments - /// are stored in the object so make sure they have appropriate lifetimes. - FMT_CONSTEXPR context(iterator out, basic_format_args ctx_args, - detail::locale_ref loc = {}) - : out_(out), args_(ctx_args), loc_(loc) {} - context(context&&) = default; - context(const context&) = delete; - void operator=(const context&) = delete; - - FMT_CONSTEXPR auto arg(int id) const -> format_arg { return args_.get(id); } - auto arg(string_view name) -> format_arg { return args_.get(name); } - FMT_CONSTEXPR auto arg_id(string_view name) -> int { - return args_.get_id(name); - } - auto args() const -> const basic_format_args& { return args_; } - - // Returns an iterator to the beginning of the output range. - FMT_CONSTEXPR auto out() -> iterator { return out_; } - - // Advances the begin iterator to `it`. - void advance_to(iterator) {} - - FMT_CONSTEXPR auto locale() -> detail::locale_ref { return loc_; } -}; - -template class generic_context; - -// Longer aliases for C++20 compatibility. -template -using basic_format_context = - conditional_t::value, context, - generic_context>; -using format_context = context; - -template -using buffered_context = basic_format_context, Char>; - -template -using is_formattable = bool_constant< - !std::is_base_of>::map( - std::declval()))>::value>; - -#if FMT_USE_CONCEPTS -template -concept formattable = is_formattable, Char>::value; -#endif - -/** - * Constructs an object that stores references to arguments and can be - * implicitly converted to `format_args`. `Context` can be omitted in which case - * it defaults to `format_context`. See `arg` for lifetime considerations. - */ -// Take arguments by lvalue references to avoid some lifetime issues, e.g. -// auto args = make_format_args(std::string()); -template (), - unsigned long long DESC = detail::make_descriptor(), - FMT_ENABLE_IF(NUM_NAMED_ARGS == 0)> -constexpr FMT_ALWAYS_INLINE auto make_format_args(T&... args) - -> detail::format_arg_store { - return {{detail::make_arg( - args)...}}; -} - -#ifndef FMT_DOC -template (), - unsigned long long DESC = - detail::make_descriptor() | - static_cast(detail::has_named_args_bit), - FMT_ENABLE_IF(NUM_NAMED_ARGS != 0)> -constexpr auto make_format_args(T&... args) - -> detail::format_arg_store { - return {args...}; -} -#endif - -/** - * Returns a named argument to be used in a formatting function. - * It should only be used in a call to a formatting function or - * `dynamic_format_arg_store::push_back`. - * - * **Example**: - * - * fmt::print("The answer is {answer}.", fmt::arg("answer", 42)); - */ -template -inline auto arg(const Char* name, const T& arg) -> detail::named_arg { - static_assert(!detail::is_named_arg(), "nested named arguments"); - return {name, arg}; -} -FMT_END_EXPORT - -/// An alias for `basic_format_args`. -// A separate type would result in shorter symbols but break ABI compatibility -// between clang and gcc on ARM (#1919). -FMT_EXPORT using format_args = basic_format_args; - -namespace detail { - -template -struct locking : bool_constant::value == - type::custom_type> {}; -template -struct locking>::nonlocking>> - : std::false_type {}; - -template FMT_CONSTEXPR inline auto is_locking() -> bool { - return locking::value; -} -template -FMT_CONSTEXPR inline auto is_locking() -> bool { - return locking::value || is_locking(); -} - -template -using unsigned_char = typename conditional_t::value, - std::make_unsigned, - type_identity>::type; - -enum class arg_id_kind { none, index, name }; - -} // namespace detail - -enum class presentation_type : unsigned char { - // Common specifiers: - none = 0, - debug = 1, // '?' - string = 2, // 's' (string, bool) - - // Integral, bool and character specifiers: - dec = 3, // 'd' - hex, // 'x' or 'X' - oct, // 'o' - bin, // 'b' or 'B' - chr, // 'c' - - // String and pointer specifiers: - pointer = 3, // 'p' - - // Floating-point specifiers: - exp = 1, // 'e' or 'E' (1 since there is no FP debug presentation) - fixed, // 'f' or 'F' - general, // 'g' or 'G' - hexfloat // 'a' or 'A' -}; - -enum class align { none, left, right, center, numeric }; -enum class sign { none, minus, plus, space }; - -// Basic format specifiers for built-in and string types. -class basic_specs { - private: - // 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 - // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - // |type |align| w | p | s |u|#|L| f | unused | - // +-----+-----+---+---+---+-+-+-+-----+---------------------------+ - // - // w - dynamic width info - // p - dynamic precision info - // s - sign - // u - uppercase (e.g. 'X' for 'x') - // # - alternate form ('#') - // L - localized - // f - fill size - // - // Bitfields are not used because of compiler bugs such as gcc bug 61414. - enum : unsigned { - type_mask = 0x00007, - align_mask = 0x00038, - width_mask = 0x000C0, - precision_mask = 0x00300, - sign_mask = 0x00C00, - uppercase_mask = 0x01000, - alternate_mask = 0x02000, - localized_mask = 0x04000, - fill_size_mask = 0x38000, - - align_shift = 3, - width_shift = 6, - precision_shift = 8, - sign_shift = 10, - fill_size_shift = 15, - - max_fill_size = 4 - }; - - unsigned long data_ = 1 << fill_size_shift; - - // Character (code unit) type is erased to prevent template bloat. - char fill_data_[max_fill_size] = {' '}; - - FMT_CONSTEXPR void set_fill_size(size_t size) { - data_ = (data_ & ~fill_size_mask) | (size << fill_size_shift); - } - - public: - constexpr auto type() const -> presentation_type { - return static_cast(data_ & type_mask); - } - FMT_CONSTEXPR void set_type(presentation_type t) { - data_ = (data_ & ~type_mask) | static_cast(t); - } - - constexpr auto align() const -> align { - return static_cast((data_ & align_mask) >> align_shift); - } - FMT_CONSTEXPR void set_align(fmt::align a) { - data_ = (data_ & ~align_mask) | (static_cast(a) << align_shift); - } - - constexpr auto dynamic_width() const -> detail::arg_id_kind { - return static_cast((data_ & width_mask) >> - width_shift); - } - FMT_CONSTEXPR void set_dynamic_width(detail::arg_id_kind w) { - data_ = (data_ & ~width_mask) | (static_cast(w) << width_shift); - } - - FMT_CONSTEXPR auto dynamic_precision() const -> detail::arg_id_kind { - return static_cast((data_ & precision_mask) >> - precision_shift); - } - FMT_CONSTEXPR void set_dynamic_precision(detail::arg_id_kind p) { - data_ = (data_ & ~precision_mask) | - (static_cast(p) << precision_shift); - } - - constexpr bool dynamic() const { - return (data_ & (width_mask | precision_mask)) != 0; - } - - constexpr auto sign() const -> sign { - return static_cast((data_ & sign_mask) >> sign_shift); - } - FMT_CONSTEXPR void set_sign(fmt::sign s) { - data_ = (data_ & ~sign_mask) | (static_cast(s) << sign_shift); - } - - constexpr auto upper() const -> bool { return (data_ & uppercase_mask) != 0; } - FMT_CONSTEXPR void set_upper() { data_ |= uppercase_mask; } - - constexpr auto alt() const -> bool { return (data_ & alternate_mask) != 0; } - FMT_CONSTEXPR void set_alt() { data_ |= alternate_mask; } - FMT_CONSTEXPR void clear_alt() { data_ &= ~alternate_mask; } - - constexpr auto localized() const -> bool { - return (data_ & localized_mask) != 0; - } - FMT_CONSTEXPR void set_localized() { data_ |= localized_mask; } - - constexpr auto fill_size() const -> size_t { - return (data_ & fill_size_mask) >> fill_size_shift; - } - - template ::value)> - constexpr auto fill() const -> const Char* { - return fill_data_; - } - template ::value)> - constexpr auto fill() const -> const Char* { - return nullptr; - } - - template constexpr auto fill_unit() const -> Char { - using uchar = unsigned char; - return static_cast(static_cast(fill_data_[0]) | - (static_cast(fill_data_[1]) << 8)); - } - - FMT_CONSTEXPR void set_fill(char c) { - fill_data_[0] = c; - set_fill_size(1); - } - - template - FMT_CONSTEXPR void set_fill(basic_string_view s) { - auto size = s.size(); - set_fill_size(size); - if (size == 1) { - unsigned uchar = static_cast>(s[0]); - fill_data_[0] = static_cast(uchar); - fill_data_[1] = static_cast(uchar >> 8); - return; - } - FMT_ASSERT(size <= max_fill_size, "invalid fill"); - for (size_t i = 0; i < size; ++i) - fill_data_[i & 3] = static_cast(s[i]); - } -}; - -// Format specifiers for built-in and string types. -struct format_specs : basic_specs { - int width; - int precision; - - constexpr format_specs() : width(0), precision(-1) {} -}; - -namespace detail { - -// An argument reference. -template union arg_ref { - FMT_CONSTEXPR arg_ref(int idx = 0) : index(idx) {} - FMT_CONSTEXPR arg_ref(basic_string_view n) : name(n) {} - - int index; - basic_string_view name; -}; - -// Format specifiers with width and precision resolved at formatting rather -// than parsing time to allow reusing the same parsed specifiers with -// different sets of arguments (precompilation of format strings). -template struct dynamic_format_specs : format_specs { - arg_ref width_ref; - arg_ref precision_ref; -}; - -// Converts a character to ASCII. Returns '\0' on conversion failure. -template ::value)> -constexpr auto to_ascii(Char c) -> char { - return c <= 0xff ? static_cast(c) : '\0'; -} - -// Returns the number of code units in a code point or 1 on error. -template -FMT_CONSTEXPR auto code_point_length(const Char* begin) -> int { - if (const_check(sizeof(Char) != 1)) return 1; - auto c = static_cast(*begin); - return static_cast((0x3a55000000000000ull >> (2 * (c >> 3))) & 3) + 1; -} - -// Parses the range [begin, end) as an unsigned integer. This function assumes -// that the range is non-empty and the first character is a digit. -template -FMT_CONSTEXPR auto parse_nonnegative_int(const Char*& begin, const Char* end, - int error_value) noexcept -> int { - FMT_ASSERT(begin != end && '0' <= *begin && *begin <= '9', ""); - unsigned value = 0, prev = 0; - auto p = begin; - do { - prev = value; - value = value * 10 + unsigned(*p - '0'); - ++p; - } while (p != end && '0' <= *p && *p <= '9'); - auto num_digits = p - begin; - begin = p; - int digits10 = static_cast(sizeof(int) * CHAR_BIT * 3 / 10); - if (num_digits <= digits10) return static_cast(value); - // Check for overflow. - unsigned max = INT_MAX; - return num_digits == digits10 + 1 && - prev * 10ull + unsigned(p[-1] - '0') <= max - ? static_cast(value) - : error_value; -} - -FMT_CONSTEXPR inline auto parse_align(char c) -> align { - switch (c) { - case '<': - return align::left; - case '>': - return align::right; - case '^': - return align::center; - } - return align::none; -} - -template constexpr auto is_name_start(Char c) -> bool { - return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == '_'; -} - -template -FMT_CONSTEXPR auto parse_arg_id(const Char* begin, const Char* end, - Handler&& handler) -> const Char* { - Char c = *begin; - if (c >= '0' && c <= '9') { - int index = 0; - if (c != '0') - index = parse_nonnegative_int(begin, end, INT_MAX); - else - ++begin; - if (begin == end || (*begin != '}' && *begin != ':')) - report_error("invalid format string"); - else - handler.on_index(index); - return begin; - } - if (!is_name_start(c)) { - report_error("invalid format string"); - return begin; - } - auto it = begin; - do { - ++it; - } while (it != end && (is_name_start(*it) || ('0' <= *it && *it <= '9'))); - handler.on_name({begin, to_unsigned(it - begin)}); - return it; -} - -template struct dynamic_spec_handler { - parse_context& ctx; - arg_ref& ref; - arg_id_kind& kind; - - FMT_CONSTEXPR void on_index(int id) { - ref = id; - kind = arg_id_kind::index; - ctx.check_arg_id(id); - ctx.check_dynamic_spec(id); - } - FMT_CONSTEXPR void on_name(basic_string_view id) { - ref = id; - kind = arg_id_kind::name; - ctx.check_arg_id(id); - } -}; - -template struct parse_dynamic_spec_result { - const Char* end; - arg_id_kind kind; -}; - -// Parses integer | "{" [arg_id] "}". -template -FMT_CONSTEXPR auto parse_dynamic_spec(const Char* begin, const Char* end, - int& value, arg_ref& ref, - parse_context& ctx) - -> parse_dynamic_spec_result { - FMT_ASSERT(begin != end, ""); - auto kind = arg_id_kind::none; - if ('0' <= *begin && *begin <= '9') { - int val = parse_nonnegative_int(begin, end, -1); - if (val == -1) report_error("number is too big"); - value = val; - } else { - if (*begin == '{') { - ++begin; - if (begin != end) { - Char c = *begin; - if (c == '}' || c == ':') { - int id = ctx.next_arg_id(); - ref = id; - kind = arg_id_kind::index; - ctx.check_dynamic_spec(id); - } else { - begin = parse_arg_id(begin, end, - dynamic_spec_handler{ctx, ref, kind}); - } - } - if (begin != end && *begin == '}') return {++begin, kind}; - } - report_error("invalid format string"); - } - return {begin, kind}; -} - -template -FMT_CONSTEXPR auto parse_width(const Char* begin, const Char* end, - format_specs& specs, arg_ref& width_ref, - parse_context& ctx) -> const Char* { - auto result = parse_dynamic_spec(begin, end, specs.width, width_ref, ctx); - specs.set_dynamic_width(result.kind); - return result.end; -} - -template -FMT_CONSTEXPR auto parse_precision(const Char* begin, const Char* end, - format_specs& specs, - arg_ref& precision_ref, - parse_context& ctx) -> const Char* { - ++begin; - if (begin == end) { - report_error("invalid precision"); - return begin; - } - auto result = - parse_dynamic_spec(begin, end, specs.precision, precision_ref, ctx); - specs.set_dynamic_precision(result.kind); - return result.end; -} - -enum class state { start, align, sign, hash, zero, width, precision, locale }; - -// Parses standard format specifiers. -template -FMT_CONSTEXPR auto parse_format_specs(const Char* begin, const Char* end, - dynamic_format_specs& specs, - parse_context& ctx, type arg_type) - -> const Char* { - auto c = '\0'; - if (end - begin > 1) { - auto next = to_ascii(begin[1]); - c = parse_align(next) == align::none ? to_ascii(*begin) : '\0'; - } else { - if (begin == end) return begin; - c = to_ascii(*begin); - } - - struct { - state current_state = state::start; - FMT_CONSTEXPR void operator()(state s, bool valid = true) { - if (current_state >= s || !valid) - report_error("invalid format specifier"); - current_state = s; - } - } enter_state; - - using pres = presentation_type; - constexpr auto integral_set = sint_set | uint_set | bool_set | char_set; - struct { - const Char*& begin; - format_specs& specs; - type arg_type; - - FMT_CONSTEXPR auto operator()(pres pres_type, int set) -> const Char* { - if (!in(arg_type, set)) report_error("invalid format specifier"); - specs.set_type(pres_type); - return begin + 1; - } - } parse_presentation_type{begin, specs, arg_type}; - - for (;;) { - switch (c) { - case '<': - case '>': - case '^': - enter_state(state::align); - specs.set_align(parse_align(c)); - ++begin; - break; - case '+': - FMT_FALLTHROUGH; - case ' ': - specs.set_sign(c == ' ' ? sign::space : sign::plus); - FMT_FALLTHROUGH; - case '-': - enter_state(state::sign, in(arg_type, sint_set | float_set)); - ++begin; - break; - case '#': - enter_state(state::hash, is_arithmetic_type(arg_type)); - specs.set_alt(); - ++begin; - break; - case '0': - enter_state(state::zero); - if (!is_arithmetic_type(arg_type)) - report_error("format specifier requires numeric argument"); - if (specs.align() == align::none) { - // Ignore 0 if align is specified for compatibility with std::format. - specs.set_align(align::numeric); - specs.set_fill('0'); - } - ++begin; - break; - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - case '{': - enter_state(state::width); - begin = parse_width(begin, end, specs, specs.width_ref, ctx); - break; - case '.': - enter_state(state::precision, - in(arg_type, float_set | string_set | cstring_set)); - begin = parse_precision(begin, end, specs, specs.precision_ref, ctx); - break; - case 'L': - enter_state(state::locale, is_arithmetic_type(arg_type)); - specs.set_localized(); - ++begin; - break; - case 'd': - return parse_presentation_type(pres::dec, integral_set); - case 'X': - specs.set_upper(); - FMT_FALLTHROUGH; - case 'x': - return parse_presentation_type(pres::hex, integral_set); - case 'o': - return parse_presentation_type(pres::oct, integral_set); - case 'B': - specs.set_upper(); - FMT_FALLTHROUGH; - case 'b': - return parse_presentation_type(pres::bin, integral_set); - case 'E': - specs.set_upper(); - FMT_FALLTHROUGH; - case 'e': - return parse_presentation_type(pres::exp, float_set); - case 'F': - specs.set_upper(); - FMT_FALLTHROUGH; - case 'f': - return parse_presentation_type(pres::fixed, float_set); - case 'G': - specs.set_upper(); - FMT_FALLTHROUGH; - case 'g': - return parse_presentation_type(pres::general, float_set); - case 'A': - specs.set_upper(); - FMT_FALLTHROUGH; - case 'a': - return parse_presentation_type(pres::hexfloat, float_set); - case 'c': - if (arg_type == type::bool_type) report_error("invalid format specifier"); - return parse_presentation_type(pres::chr, integral_set); - case 's': - return parse_presentation_type(pres::string, - bool_set | string_set | cstring_set); - case 'p': - return parse_presentation_type(pres::pointer, pointer_set | cstring_set); - case '?': - return parse_presentation_type(pres::debug, - char_set | string_set | cstring_set); - case '}': - return begin; - default: { - if (*begin == '}') return begin; - // Parse fill and alignment. - auto fill_end = begin + code_point_length(begin); - if (end - fill_end <= 0) { - report_error("invalid format specifier"); - return begin; - } - if (*begin == '{') { - report_error("invalid fill character '{'"); - return begin; - } - auto alignment = parse_align(to_ascii(*fill_end)); - enter_state(state::align, alignment != align::none); - specs.set_fill( - basic_string_view(begin, to_unsigned(fill_end - begin))); - specs.set_align(alignment); - begin = fill_end + 1; - } - } - if (begin == end) return begin; - c = to_ascii(*begin); - } -} - -template -FMT_CONSTEXPR FMT_INLINE auto parse_replacement_field(const Char* begin, - const Char* end, - Handler&& handler) - -> const Char* { - ++begin; - if (begin == end) { - handler.on_error("invalid format string"); - return end; - } - int arg_id = 0; - switch (*begin) { - case '}': - handler.on_replacement_field(handler.on_arg_id(), begin); - return begin + 1; - case '{': - handler.on_text(begin, begin + 1); - return begin + 1; - case ':': - arg_id = handler.on_arg_id(); - break; - default: { - struct id_adapter { - Handler& handler; - int arg_id; + /// Constructs a `basic_format_args` object from `dynamic_format_arg_store`. + constexpr basic_format_args(const dynamic_format_arg_store& store) + : desc_(store.get_types()), args_(store.data()) {} - FMT_CONSTEXPR void on_index(int id) { arg_id = handler.on_arg_id(id); } - FMT_CONSTEXPR void on_name(basic_string_view id) { - arg_id = handler.on_arg_id(id); - } - } adapter = {handler, 0}; - begin = parse_arg_id(begin, end, adapter); - arg_id = adapter.arg_id; - Char c = begin != end ? *begin : Char(); - if (c == '}') { - handler.on_replacement_field(arg_id, begin); - return begin + 1; - } - if (c != ':') { - handler.on_error("missing '}' in format string"); - return end; + /// Constructs a `basic_format_args` object from a dynamic list of arguments. + constexpr basic_format_args(const format_arg* args, int count) + : desc_(detail::is_unpacked_bit | detail::to_unsigned(count)), + args_(args) {} + + /// Returns the argument with the specified id. + FMT_CONSTEXPR auto get(int id) const -> format_arg { + format_arg arg; + if (!is_packed()) { + if (id < max_size()) arg = args_[id]; + return arg; } - break; + if (static_cast(id) >= detail::max_packed_args) return arg; + arg.type_ = type(id); + if (arg.type_ != detail::type::none_type) arg.value_ = values_[id]; + return arg; } + + template + auto get(basic_string_view name) const -> format_arg { + int id = get_id(name); + return id >= 0 ? get(id) : format_arg(); } - begin = handler.on_format_specs(arg_id, begin + 1, end); - if (begin == end || *begin != '}') - return handler.on_error("unknown format specifier"), end; - return begin + 1; -} -template -FMT_CONSTEXPR void parse_format_string(basic_string_view fmt, - Handler&& handler) { - auto begin = fmt.data(), end = begin + fmt.size(); - auto p = begin; - while (p != end) { - auto c = *p++; - if (c == '{') { - handler.on_text(begin, p - 1); - begin = p = parse_replacement_field(p - 1, end, handler); - } else if (c == '}') { - if (p == end || *p != '}') - return handler.on_error("unmatched '}' in format string"); - handler.on_text(begin, p); - begin = ++p; + template + FMT_CONSTEXPR auto get_id(basic_string_view name) const -> int { + if (!has_named_args()) return -1; + const auto& named_args = + (is_packed() ? values_[-1] : args_[-1].value_).named_args; + for (size_t i = 0; i < named_args.size; ++i) { + if (named_args.data[i].name == name) return named_args.data[i].id; } + return -1; } - handler.on_text(begin, end); -} -// Checks char specs and returns true iff the presentation type is char-like. -FMT_CONSTEXPR inline auto check_char_specs(const format_specs& specs) -> bool { - auto type = specs.type(); - if (type != presentation_type::none && type != presentation_type::chr && - type != presentation_type::debug) { - return false; + auto max_size() const -> int { + unsigned long long max_packed = detail::max_packed_args; + return static_cast(is_packed() ? max_packed + : desc_ & ~detail::is_unpacked_bit); } - if (specs.align() == align::numeric || specs.sign() != sign::none || - specs.alt()) { - report_error("invalid format specifier for char"); +}; + +// A formatting context. +class context { + private: + appender out_; + basic_format_args args_; + FMT_NO_UNIQUE_ADDRESS detail::locale_ref loc_; + + public: + /// The character type for the output. + using char_type = char; + + using iterator = appender; + using format_arg = basic_format_arg; + using parse_context_type = parse_context; + template using formatter_type = formatter; + enum { builtin_types = FMT_BUILTIN_TYPES }; + + /// Constructs a `context` object. References to the arguments are stored + /// in the object so make sure they have appropriate lifetimes. + FMT_CONSTEXPR context(iterator out, basic_format_args ctx_args, + detail::locale_ref loc = {}) + : out_(out), args_(ctx_args), loc_(loc) {} + context(context&&) = default; + context(const context&) = delete; + void operator=(const context&) = delete; + + FMT_CONSTEXPR auto arg(int id) const -> format_arg { return args_.get(id); } + auto arg(string_view name) -> format_arg { return args_.get(name); } + FMT_CONSTEXPR auto arg_id(string_view name) -> int { + return args_.get_id(name); } - return true; -} + auto args() const -> const basic_format_args& { return args_; } + + // Returns an iterator to the beginning of the output range. + FMT_CONSTEXPR auto out() -> iterator { return out_; } + + // Advances the begin iterator to `it`. + void advance_to(iterator) {} + + FMT_CONSTEXPR auto locale() -> detail::locale_ref { return loc_; } +}; + +template class generic_context; + +template +using buffered_context = + conditional_t::value, context, + generic_context, Char>>; + +// A separate type would result in shorter symbols but break ABI compatibility +// between clang and gcc on ARM (#1919). +using format_args = basic_format_args; +FMT_END_EXPORT + +namespace detail { + +// A base class for compile-time strings. +struct compile_string {}; template FMT_VISIBILITY("hidden") // Suppress an ld warning on macOS (#3769). @@ -2866,9 +2785,6 @@ class format_string_checker { } }; -// A base class for compile-time strings. -struct compile_string {}; - // TYPE can be different from type_constant, e.g. for __float128. template struct native_formatter { private: @@ -2896,6 +2812,21 @@ template struct native_formatter { -> decltype(ctx.out()); }; +template +struct locking : bool_constant::value == + type::custom_type> {}; +template +struct locking>::nonlocking>> + : std::false_type {}; + +template FMT_CONSTEXPR inline auto is_locking() -> bool { + return locking::value; +} +template +FMT_CONSTEXPR inline auto is_locking() -> bool { + return locking::value || is_locking(); +} + // Use vformat_args and avoid type_identity to keep symbols short. template struct vformat_args { using type = basic_format_args>; @@ -2916,14 +2847,6 @@ inline void vprint_mojibake(FILE*, string_view, format_args, bool) {} FMT_BEGIN_EXPORT -// A formatter specialization for natively supported types. -template -struct formatter::value != - detail::type::custom_type>> - : detail::native_formatter::value> { -}; - template struct runtime_format_string { basic_string_view str; }; @@ -2989,6 +2912,78 @@ template class basic_format_string { template using format_string = basic_format_string...>; +// Longer aliases for C++20 compatibility. +template +using basic_format_context = + conditional_t::value, context, + generic_context>; +using format_context = context; + +template +using is_formattable = bool_constant< + !std::is_base_of>::map( + std::declval()))>::value>; + +#if FMT_USE_CONCEPTS +template +concept formattable = is_formattable, Char>::value; +#endif + +// A formatter specialization for natively supported types. +template +struct formatter::value != + detail::type::custom_type>> + : detail::native_formatter::value> { +}; + +/** + * Constructs an object that stores references to arguments and can be + * implicitly converted to `format_args`. `Context` can be omitted in which case + * it defaults to `context`. See `arg` for lifetime considerations. + */ +// Take arguments by lvalue references to avoid some lifetime issues, e.g. +// auto args = make_format_args(std::string()); +template (), + unsigned long long DESC = detail::make_descriptor(), + FMT_ENABLE_IF(NUM_NAMED_ARGS == 0)> +constexpr FMT_ALWAYS_INLINE auto make_format_args(T&... args) + -> detail::format_arg_store { + return {{detail::make_arg( + args)...}}; +} + +#ifndef FMT_DOC +template (), + unsigned long long DESC = + detail::make_descriptor() | + static_cast(detail::has_named_args_bit), + FMT_ENABLE_IF(NUM_NAMED_ARGS != 0)> +constexpr auto make_format_args(T&... args) + -> detail::format_arg_store { + return {args...}; +} +#endif + +/** + * Returns a named argument to be used in a formatting function. + * It should only be used in a call to a formatting function or + * `dynamic_format_arg_store::push_back`. + * + * **Example**: + * + * fmt::print("The answer is {answer}.", fmt::arg("answer", 42)); + */ +template +inline auto arg(const Char* name, const T& arg) -> detail::named_arg { + static_assert(!detail::is_named_arg(), "nested named arguments"); + return {name, arg}; +} + /// Formats a string and writes the output to `out`. template ,