Make inline_buffer_size public and update docs

This commit is contained in:
Victor Zverovich 2018-03-04 10:33:42 -08:00
parent 995b63adfe
commit f1ede6380b
6 changed files with 20 additions and 20 deletions

View File

@ -253,7 +253,7 @@ A custom allocator class can be specified as a template argument to
:class:`fmt::basic_memory_buffer`::
using custom_memory_buffer =
fmt::basic_memory_buffer<char, custom_allocator>;
fmt::basic_memory_buffer<char, fmt::inline_buffer_size, custom_allocator>;
It is also possible to write a formatting function that uses a custom
allocator::
@ -261,8 +261,8 @@ allocator::
using custom_string =
std::basic_string<char, std::char_traits<char>, custom_allocator>;
custom_string format(custom_allocator alloc, fmt::string_view format_str,
fmt::format_args args) {
custom_string vformat(custom_allocator alloc, fmt::string_view format_str,
fmt::format_args args) {
custom_memory_buffer buf(alloc);
fmt::vformat_to(buf, format_str, args);
return custom_string(buf.data(), buf.size(), alloc);

View File

@ -166,7 +166,7 @@ int safe_strerror(
void format_error_code(internal::buffer &out, int error_code,
string_view message) FMT_NOEXCEPT {
// Report error code making sure that the output fits into
// INLINE_BUFFER_SIZE to avoid dynamic memory allocation and potential
// inline_buffer_size to avoid dynamic memory allocation and potential
// bad_alloc.
out.resize(0);
static const char SEP[] = ": ";
@ -181,13 +181,13 @@ void format_error_code(internal::buffer &out, int error_code,
}
error_code_size += internal::count_digits(abs_value);
writer w(out);
if (message.size() <= internal::INLINE_BUFFER_SIZE - error_code_size) {
if (message.size() <= inline_buffer_size - error_code_size) {
w.write(message);
w.write(SEP);
}
w.write(ERROR_STR);
w.write(error_code);
assert(out.size() <= internal::INLINE_BUFFER_SIZE);
assert(out.size() <= inline_buffer_size);
}
void report_error(FormatFunc func, int error_code,
@ -332,7 +332,7 @@ FMT_FUNC void internal::format_windows_error(
internal::buffer &out, int error_code, string_view message) FMT_NOEXCEPT {
FMT_TRY {
wmemory_buffer buf;
buf.resize(INLINE_BUFFER_SIZE);
buf.resize(inline_buffer_size);
for (;;) {
wchar_t *system_message = &buf[0];
int result = FormatMessageW(
@ -364,7 +364,7 @@ FMT_FUNC void format_system_error(
internal::buffer &out, int error_code, string_view message) FMT_NOEXCEPT {
FMT_TRY {
memory_buffer buf;
buf.resize(internal::INLINE_BUFFER_SIZE);
buf.resize(inline_buffer_size);
for (;;) {
char *system_message = &buf[0];
int result = safe_strerror(error_code, system_message, buf.size());

View File

@ -335,10 +335,6 @@ FMT_CONSTEXPR typename std::make_unsigned<Int>::type to_unsigned(Int value) {
return static_cast<typename std::make_unsigned<Int>::type>(value);
}
// The number of characters to store in the basic_memory_buffer object itself
// to avoid dynamic memory allocation.
enum { INLINE_BUFFER_SIZE = 500 };
#if FMT_SECURE_SCL
// Use checked iterator to avoid warnings on MSVC.
template <typename T>
@ -371,6 +367,10 @@ class locale_provider {
virtual fmt::locale locale();
};
// The number of characters to store in the basic_memory_buffer object itself
// to avoid dynamic memory allocation.
enum { inline_buffer_size = 500 };
/**
\rst
A dynamically growing memory buffer for trivially copyable/constructible types
@ -400,7 +400,7 @@ class locale_provider {
The output can be converted to an ``std::string`` with ``to_string(out)``.
\endrst
*/
template <typename T, std::size_t SIZE = internal::INLINE_BUFFER_SIZE,
template <typename T, std::size_t SIZE = inline_buffer_size,
typename Allocator = std::allocator<T> >
class basic_memory_buffer: private Allocator, public internal::basic_buffer<T> {
private:

View File

@ -94,7 +94,7 @@ TEST(FormatTest, FormatErrorCode) {
{
fmt::memory_buffer buffer;
std::string prefix(
fmt::internal::INLINE_BUFFER_SIZE - msg.size() - sep.size() + 1, 'x');
fmt::inline_buffer_size - msg.size() - sep.size() + 1, 'x');
fmt::format_error_code(buffer, 42, prefix);
EXPECT_EQ(msg, to_string(buffer));
}
@ -104,10 +104,10 @@ TEST(FormatTest, FormatErrorCode) {
msg = fmt::format("error {}", codes[i]);
fmt::memory_buffer buffer;
std::string prefix(
fmt::internal::INLINE_BUFFER_SIZE - msg.size() - sep.size(), 'x');
fmt::inline_buffer_size - msg.size() - sep.size(), 'x');
fmt::format_error_code(buffer, codes[i], prefix);
EXPECT_EQ(prefix + sep + msg, to_string(buffer));
std::size_t size = fmt::internal::INLINE_BUFFER_SIZE;
std::size_t size = fmt::inline_buffer_size;
EXPECT_EQ(size, buffer.size());
buffer.resize(0);
// Test with a message that doesn't fit into the buffer.

View File

@ -192,11 +192,11 @@ TEST(WriterTest, WriteDoubleWithFilledBuffer) {
memory_buffer buf;
fmt::writer writer(buf);
// Fill the buffer.
for (int i = 0; i < fmt::internal::INLINE_BUFFER_SIZE; ++i)
for (int i = 0; i < fmt::inline_buffer_size; ++i)
writer.write(' ');
writer.write(1.2);
fmt::string_view sv(buf.data(), buf.size());
sv.remove_prefix(fmt::internal::INLINE_BUFFER_SIZE);
sv.remove_prefix(fmt::inline_buffer_size);
EXPECT_EQ("1.2", sv);
}

View File

@ -341,7 +341,7 @@ TEST(MemoryBufferTest, Allocator) {
{
basic_memory_buffer<char, 10, TestAllocator> buffer2((TestAllocator(&alloc)));
EXPECT_EQ(&alloc, buffer2.get_allocator().get());
std::size_t size = 2 * fmt::internal::INLINE_BUFFER_SIZE;
std::size_t size = 2 * fmt::inline_buffer_size;
EXPECT_CALL(alloc, allocate(size)).WillOnce(Return(&mem));
buffer2.reserve(size);
EXPECT_CALL(alloc, deallocate(&mem, size));
@ -352,7 +352,7 @@ TEST(MemoryBufferTest, ExceptionInDeallocate) {
typedef AllocatorRef< MockAllocator<char> > TestAllocator;
StrictMock< MockAllocator<char> > alloc;
basic_memory_buffer<char, 10, TestAllocator> buffer((TestAllocator(&alloc)));
std::size_t size = 2 * fmt::internal::INLINE_BUFFER_SIZE;
std::size_t size = 2 * fmt::inline_buffer_size;
std::vector<char> mem(size);
{
EXPECT_CALL(alloc, allocate(size)).WillOnce(Return(&mem[0]));