Replace clear() with resize(0) and data_ -> store_
This commit is contained in:
parent
23b8c24da4
commit
f423e46835
@ -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.
|
||||
|
31
fmt/format.h
31
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 <typename T>
|
||||
template <typename U>
|
||||
void basic_buffer<T>::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 <typename T, std::size_t SIZE = internal::INLINE_BUFFER_SIZE,
|
||||
typename Allocator = std::allocator<T> >
|
||||
class basic_memory_buffer : private Allocator, public basic_buffer<T> {
|
||||
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<T> {
|
||||
|
||||
public:
|
||||
explicit basic_memory_buffer(const Allocator &alloc = Allocator())
|
||||
: Allocator(alloc), basic_buffer<T>(data_, SIZE) {}
|
||||
: Allocator(alloc), basic_buffer<T>(store_, SIZE) {}
|
||||
~basic_memory_buffer() { deallocate(); }
|
||||
|
||||
#if FMT_USE_RVALUE_REFERENCES
|
||||
@ -717,15 +712,15 @@ class basic_memory_buffer : private Allocator, public basic_buffer<T> {
|
||||
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<T, SIZE, Allocator>::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<Char> &buffer() FMT_NOEXCEPT { return buffer_; }
|
||||
};
|
||||
|
10
fmt/string.h
10
fmt/string.h
@ -45,12 +45,12 @@ namespace fmt {
|
||||
*/template <typename Char>
|
||||
class basic_string_buffer : public basic_buffer<Char> {
|
||||
private:
|
||||
std::basic_string<Char> data_;
|
||||
std::basic_string<Char> 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<Char> {
|
||||
\endrst
|
||||
*/
|
||||
void move_to(std::basic_string<Char> &str) {
|
||||
data_.resize(this->size_);
|
||||
str.swap(data_);
|
||||
str_.resize(this->size_);
|
||||
str.swap(str_);
|
||||
this->capacity_ = this->size_ = 0;
|
||||
this->ptr_ = 0;
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -208,7 +208,7 @@ TEST(BufferTest, Resize) {
|
||||
TEST(BufferTest, Clear) {
|
||||
TestBuffer<char> 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<size_t>::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<size_t>::max()));
|
||||
|
Loading…
Reference in New Issue
Block a user