#pragma once #include "memory.hpp" #include #include template constexpr auto ARRAY_COUNT(Type(&)[n]) { return n; } namespace utils::string { template class va_provider final { public: static_assert(Buffers != 0 && MinBufferSize != 0, "Buffers and MinBufferSize mustn't be 0"); va_provider() : current_buffer_(0) { } char* get(const char* format, const va_list ap) { ++this->current_buffer_ %= ARRAY_COUNT(this->string_pool_); auto entry = &this->string_pool_[this->current_buffer_]; if (!entry->size_ || !entry->buffer_) { throw std::runtime_error("String pool not initialized"); } while (true) { va_list ap_copy; va_copy(ap_copy, ap); const auto res = vsnprintf_s(entry->buffer_, entry->size_, _TRUNCATE, format, ap_copy); va_end(ap_copy); if (res > 0) break; // Success if (res == 0) return nullptr; // Error entry->double_size(); } return entry->buffer_; } private: class entry final { public: explicit entry(const size_t size = MinBufferSize) : size_(size), buffer_(nullptr) { if (this->size_ < MinBufferSize) this->size_ = MinBufferSize; this->allocate(); } ~entry() { 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(this->size_ + 1); } void double_size() { this->size_ *= 2; this->allocate(); } size_t size_; char* buffer_; }; size_t current_buffer_; entry string_pool_[Buffers]; }; const char* va(const char* fmt, ...); std::vector split(const std::string& s, char delim); std::string to_lower(const std::string& text); std::string to_upper(const std::string& text); bool starts_with(const std::string& text, const std::string& substring); bool ends_with(const std::string& text, const std::string& substring); std::string dump_hex(const std::string& data, const std::string& separator = " "); std::string get_clipboard_data(); void strip(const char* in, char* out, size_t max); std::string convert(const std::wstring& wstr); std::wstring convert(const std::string& str); std::string replace(std::string str, const std::string& from, const std::string& to); }