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 {
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_start(ap, fmt);

View File

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