diff --git a/fmt/format.cc b/fmt/format.cc index d1a449d6..2b6afc2f 100644 --- a/fmt/format.cc +++ b/fmt/format.cc @@ -180,7 +180,7 @@ void format_error_code(buffer &out, int error_code, // Report error code making sure that the output fits into // INLINE_BUFFER_SIZE to avoid dynamic memory allocation and potential // bad_alloc. - out.clear(); + out.resize(0); static const char SEP[] = ": "; static const char ERROR_STR[] = "error "; // Subtract 2 to account for terminating null characters in SEP and ERROR_STR. diff --git a/fmt/format.h b/fmt/format.h index 9d670a6b..8ea618c9 100644 --- a/fmt/format.h +++ b/fmt/format.h @@ -613,8 +613,7 @@ class basic_buffer { Resizes the buffer. If T is a POD type new elements may not be initialized. */ void resize(std::size_t new_size) { - if (new_size > capacity_) - grow(new_size); + reserve(new_size); size_ = new_size; } @@ -628,11 +627,8 @@ class basic_buffer { grow(capacity); } - void clear() FMT_NOEXCEPT { size_ = 0; } - void push_back(const T &value) { - if (size_ == capacity_) - grow(size_ + 1); + reserve(size_ + 1); ptr_[size_++] = value; } @@ -648,8 +644,7 @@ template template void basic_buffer::append(const U *begin, const U *end) { std::size_t new_size = size_ + internal::to_unsigned(end - begin); - if (new_size > capacity_) - grow(new_size); + reserve(new_size); std::uninitialized_copy(begin, end, internal::make_ptr(ptr_, capacity_) + size_); size_ = new_size; @@ -694,11 +689,11 @@ template > class basic_memory_buffer : private Allocator, public basic_buffer { private: - T data_[SIZE]; + T store_[SIZE]; // Deallocate memory allocated by the buffer. void deallocate() { - if (this->ptr_ != data_) Allocator::deallocate(this->ptr_, this->capacity_); + if (this->ptr_ != store_) Allocator::deallocate(this->ptr_, this->capacity_); } protected: @@ -706,7 +701,7 @@ class basic_memory_buffer : private Allocator, public basic_buffer { public: explicit basic_memory_buffer(const Allocator &alloc = Allocator()) - : Allocator(alloc), basic_buffer(data_, SIZE) {} + : Allocator(alloc), basic_buffer(store_, SIZE) {} ~basic_memory_buffer() { deallocate(); } #if FMT_USE_RVALUE_REFERENCES @@ -717,15 +712,15 @@ class basic_memory_buffer : private Allocator, public basic_buffer { this_alloc = std::move(other_alloc); this->size_ = other.size_; this->capacity_ = other.capacity_; - if (other.ptr_ == other.data_) { - this->ptr_ = data_; - std::uninitialized_copy(other.data_, other.data_ + this->size_, - internal::make_ptr(data_, this->capacity_)); + if (other.ptr_ == other.store_) { + this->ptr_ = store_; + std::uninitialized_copy(other.store_, other.store_ + this->size_, + internal::make_ptr(store_, this->capacity_)); } else { this->ptr_ = other.ptr_; // Set pointer to the inline array so that delete is not called // when deallocating. - other.ptr_ = other.data_; + other.ptr_ = other.store_; } } @@ -773,7 +768,7 @@ void basic_memory_buffer::grow(std::size_t size) { // deallocate may throw (at least in principle), but it doesn't matter since // the buffer already uses the new storage and will deallocate it in case // of exception. - if (old_ptr != data_) + if (old_ptr != store_) Allocator::deallocate(old_ptr, old_capacity); } @@ -2420,7 +2415,7 @@ class basic_writer { write_str(str, format_specs(specs...)); } - void clear() FMT_NOEXCEPT { buffer_.clear(); } + void clear() FMT_NOEXCEPT { buffer_.resize(0); } basic_buffer &buffer() FMT_NOEXCEPT { return buffer_; } }; diff --git a/fmt/string.h b/fmt/string.h index eae6d8ab..c0a62510 100644 --- a/fmt/string.h +++ b/fmt/string.h @@ -45,12 +45,12 @@ namespace fmt { */template class basic_string_buffer : public basic_buffer { private: - std::basic_string data_; + std::basic_string str_; protected: virtual void grow(std::size_t size) { - data_.resize(size); - this->ptr_ = &data_[0]; + str_.resize(size); + this->ptr_ = &str_[0]; this->capacity_ = size; } @@ -61,8 +61,8 @@ class basic_string_buffer : public basic_buffer { \endrst */ void move_to(std::basic_string &str) { - data_.resize(this->size_); - str.swap(data_); + str_.resize(this->size_); + str.swap(str_); this->capacity_ = this->size_ = 0; this->ptr_ = 0; } diff --git a/test/format-impl-test.cc b/test/format-impl-test.cc index f12b6450..fbe6d4f7 100644 --- a/test/format-impl-test.cc +++ b/test/format-impl-test.cc @@ -128,7 +128,7 @@ TEST(FormatTest, FormatErrorCode) { EXPECT_EQ(prefix + sep + msg, to_string(buffer)); std::size_t size = fmt::internal::INLINE_BUFFER_SIZE; EXPECT_EQ(size, buffer.size()); - buffer.clear(); + buffer.resize(0); // Test with a message that doesn't fit into the buffer. prefix += 'x'; fmt::format_error_code(buffer, codes[i], prefix); diff --git a/test/util-test.cc b/test/util-test.cc index e8d8ac80..57a74a42 100644 --- a/test/util-test.cc +++ b/test/util-test.cc @@ -208,7 +208,7 @@ TEST(BufferTest, Resize) { TEST(BufferTest, Clear) { TestBuffer buffer; buffer.resize(20); - buffer.clear(); + buffer.resize(0); EXPECT_EQ(0u, buffer.size()); EXPECT_EQ(20u, buffer.capacity()); } @@ -722,7 +722,7 @@ TEST(UtilTest, FormatSystemError) { fmt::format_system_error(message, EDOM, "test"); EXPECT_EQ(fmt::format("test: {}", get_system_error(EDOM)), to_string(message)); - message.clear(); + message.resize(0); fmt::format_system_error( message, EDOM, fmt::string_view(0, std::numeric_limits::max())); EXPECT_EQ(fmt::format("error {}", EDOM), to_string(message)); @@ -758,7 +758,7 @@ TEST(UtilTest, FormatWindowsError) { actual_message, ERROR_FILE_EXISTS, "test"); EXPECT_EQ(fmt::format("test: {}", utf8_message.str()), fmt::to_string(actual_message)); - actual_message.clear(); + actual_message.resize(0); fmt::internal::format_windows_error( actual_message, ERROR_FILE_EXISTS, fmt::string_view(0, std::numeric_limits::max()));