maint(string): update

This commit is contained in:
6arelyFuture 2023-03-29 13:58:58 +01:00
parent 38398db5e9
commit c80d70c698
No known key found for this signature in database
GPG Key ID: 22F9079C86CFAB31
2 changed files with 21 additions and 21 deletions

View File

@ -7,7 +7,7 @@
namespace utils::string { namespace utils::string {
const char* va(const char* fmt, ...) { const char* va(const char* fmt, ...) {
static thread_local va_provider<8, 256> provider; static thread_local va_provider<8, 1024> provider;
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);

View File

@ -6,10 +6,10 @@ template <class Type, size_t n> size_t ARRAYSIZE(Type (&)[n]) { return n; }
#endif #endif
namespace utils::string { namespace utils::string {
template <size_t Buffers, size_t MinBufferSize> class va_provider final { template <size_t buffers, size_t min_buffer_size> class va_provider final {
public: public:
static_assert(Buffers != 0 && MinBufferSize != 0, static_assert(buffers != 0 && min_buffer_size != 0,
"Buffers and MinBufferSize mustn't be 0"); "buffers and min_buffer_size mustn't be 0");
va_provider() : current_buffer_(0) {} va_provider() : current_buffer_(0) {}
@ -17,13 +17,13 @@ public:
++this->current_buffer_ %= ARRAYSIZE(this->string_pool_); ++this->current_buffer_ %= ARRAYSIZE(this->string_pool_);
auto entry = &this->string_pool_[this->current_buffer_]; auto entry = &this->string_pool_[this->current_buffer_];
if (!entry->size || !entry->buffer) { if (!entry->size_ || !entry->buffer_) {
throw std::runtime_error("String pool not initialized"); throw std::runtime_error("String pool not initialized");
} }
while (true) { while (true) {
const int res = const int res =
vsnprintf_s(entry->buffer, entry->size, _TRUNCATE, format, ap); vsnprintf_s(entry->buffer_, entry->size_, _TRUNCATE, format, ap);
if (res > 0) if (res > 0)
break; // Success break; // Success
if (res == 0) if (res == 0)
@ -38,34 +38,34 @@ public:
private: private:
class entry final { class entry final {
public: public:
explicit entry(const size_t _size = MinBufferSize) explicit entry(const size_t size = min_buffer_size)
: size(_size), buffer(nullptr) { : size(size), buffer_(nullptr) {
if (this->size < MinBufferSize) if (this->size_ < min_buffer_size)
this->size = MinBufferSize; this->size_ = min_buffer_size;
this->allocate(); this->allocate();
} }
~entry() { ~entry() {
if (this->buffer) if (this->buffer_)
memory::get_allocator()->free(this->buffer); memory::get_allocator()->free(this->buffer_);
this->size = 0; this->size_ = 0;
this->buffer = nullptr; this->buffer_ = nullptr;
} }
void allocate() { void allocate() {
if (this->buffer) if (this->buffer_)
memory::get_allocator()->free(this->buffer); memory::get_allocator()->free(this->buffer_);
this->buffer = this->buffer_ =
memory::get_allocator()->allocate_array<char>(this->size + 1); memory::get_allocator()->allocate_array<char>(this->size_ + 1);
} }
void double_size() { void double_size() {
this->size *= 2; this->size_ *= 2;
this->allocate(); this->allocate();
} }
size_t size; size_t size_;
char* buffer; char* buffer_;
}; };
size_t current_buffer_; size_t current_buffer_;