mirror of
https://github.com/fmtlib/fmt.git
synced 2025-03-25 23:37:33 +00:00
Make formatted_size part of the core API
This commit is contained in:
parent
46a63b7087
commit
47f8d7a345
@ -48,6 +48,8 @@ participate in an overload resolution if the latter is not a string.
|
|||||||
.. doxygenfunction:: format(const S&, Args&&...)
|
.. doxygenfunction:: format(const S&, Args&&...)
|
||||||
.. doxygenfunction:: vformat(const S&, basic_format_args<buffer_context<type_identity_t<Char>>>)
|
.. doxygenfunction:: vformat(const S&, basic_format_args<buffer_context<type_identity_t<Char>>>)
|
||||||
|
|
||||||
|
.. doxygenfunction:: fmt::formatted_size(string_view, Args&&...)
|
||||||
|
|
||||||
.. _print:
|
.. _print:
|
||||||
|
|
||||||
.. doxygenfunction:: print(const S&, Args&&...)
|
.. doxygenfunction:: print(const S&, Args&&...)
|
||||||
@ -300,8 +302,6 @@ Utilities
|
|||||||
|
|
||||||
.. doxygentypedef:: fmt::char_t
|
.. doxygentypedef:: fmt::char_t
|
||||||
|
|
||||||
.. doxygenfunction:: fmt::formatted_size(string_view, const Args&...)
|
|
||||||
|
|
||||||
.. doxygenfunction:: fmt::to_string(const T&)
|
.. doxygenfunction:: fmt::to_string(const T&)
|
||||||
|
|
||||||
.. doxygenfunction:: fmt::to_wstring(const T&)
|
.. doxygenfunction:: fmt::to_wstring(const T&)
|
||||||
|
@ -269,8 +269,7 @@ struct monostate {};
|
|||||||
|
|
||||||
namespace detail {
|
namespace detail {
|
||||||
|
|
||||||
// A helper function to suppress bogus "conditional expression is constant"
|
// A helper function to suppress "conditional expression is constant" warnings.
|
||||||
// warnings.
|
|
||||||
template <typename T> constexpr T const_check(T value) { return value; }
|
template <typename T> constexpr T const_check(T value) { return value; }
|
||||||
|
|
||||||
FMT_NORETURN FMT_API void assert_fail(const char* file, int line,
|
FMT_NORETURN FMT_API void assert_fail(const char* file, int line,
|
||||||
@ -797,9 +796,25 @@ class iterator_buffer<std::back_insert_iterator<Container>,
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename Container>
|
// A buffer that counts the number of code units written discarding the output.
|
||||||
using container_buffer = iterator_buffer<std::back_insert_iterator<Container>,
|
template <typename T = char> class counting_buffer : public buffer<T> {
|
||||||
typename Container::value_type>;
|
private:
|
||||||
|
enum { buffer_size = 256 };
|
||||||
|
T data_[buffer_size];
|
||||||
|
size_t count_ = 0;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void grow(size_t) final {
|
||||||
|
if (this->size() != buffer_size) return;
|
||||||
|
count_ += this->size();
|
||||||
|
this->clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
counting_buffer() : buffer<T>(data_, 0, buffer_size) {}
|
||||||
|
|
||||||
|
size_t count() { return count_ + this->size(); }
|
||||||
|
};
|
||||||
|
|
||||||
// An output iterator that appends to the buffer.
|
// An output iterator that appends to the buffer.
|
||||||
// It is used to reduce symbol sizes for the common case.
|
// It is used to reduce symbol sizes for the common case.
|
||||||
@ -1957,6 +1972,18 @@ inline OutputIt format_to(OutputIt out, const S& format_str, Args&&... args) {
|
|||||||
return vformat_to(out, to_string_view(format_str), vargs);
|
return vformat_to(out, to_string_view(format_str), vargs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns the number of characters in the output of
|
||||||
|
``format(format_str, args...)``.
|
||||||
|
*/
|
||||||
|
template <typename... Args>
|
||||||
|
inline size_t formatted_size(string_view format_str, Args&&... args) {
|
||||||
|
const auto& vargs = fmt::make_args_checked<Args...>(format_str, args...);
|
||||||
|
detail::counting_buffer<> buf;
|
||||||
|
detail::vformat_to(buf, format_str, vargs);
|
||||||
|
return buf.count();
|
||||||
|
}
|
||||||
|
|
||||||
template <typename S, typename Char = char_t<S>>
|
template <typename S, typename Char = char_t<S>>
|
||||||
FMT_INLINE std::basic_string<Char> vformat(
|
FMT_INLINE std::basic_string<Char> vformat(
|
||||||
const S& format_str,
|
const S& format_str,
|
||||||
|
@ -3586,15 +3586,6 @@ std::basic_string<Char> detail::vformat(
|
|||||||
return to_string(buffer);
|
return to_string(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
Returns the number of characters in the output of
|
|
||||||
``format(format_str, args...)``.
|
|
||||||
*/
|
|
||||||
template <typename... Args>
|
|
||||||
inline size_t formatted_size(string_view format_str, const Args&... args) {
|
|
||||||
return format_to(detail::counting_iterator(), format_str, args...).count();
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename Char, FMT_ENABLE_IF(std::is_same<Char, wchar_t>::value)>
|
template <typename Char, FMT_ENABLE_IF(std::is_same<Char, wchar_t>::value)>
|
||||||
void vprint(std::FILE* f, basic_string_view<Char> format_str,
|
void vprint(std::FILE* f, basic_string_view<Char> format_str,
|
||||||
wformat_args args) {
|
wformat_args args) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user