maint: cleanup string utils
This commit is contained in:
parent
18201b2457
commit
2daceef646
2
deps/libtommath
vendored
2
deps/libtommath
vendored
@ -1 +1 @@
|
|||||||
Subproject commit 787c6147218759ed076ad8f3742d15505ed0822e
|
Subproject commit f6507b7a1bef22965dd7a0e17bd010cd16704463
|
@ -83,65 +83,6 @@ namespace utils::string
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void strip(const char* in, char* out, int max)
|
|
||||||
{
|
|
||||||
if (!in || !out) return;
|
|
||||||
|
|
||||||
max--;
|
|
||||||
auto current = 0;
|
|
||||||
while (*in != 0 && current < max)
|
|
||||||
{
|
|
||||||
const auto color_index = (*(in + 1) - 48) >= 0xC ? 7 : (*(in + 1) - 48);
|
|
||||||
|
|
||||||
if (*in == '^' && (color_index != 7 || *(in + 1) == '7'))
|
|
||||||
{
|
|
||||||
++in;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
*out = *in;
|
|
||||||
++out;
|
|
||||||
++current;
|
|
||||||
}
|
|
||||||
|
|
||||||
++in;
|
|
||||||
}
|
|
||||||
*out = '\0';
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
#pragma warning(push)
|
|
||||||
#pragma warning(disable: 4100)
|
|
||||||
#endif
|
|
||||||
std::string convert(const std::wstring& wstr)
|
|
||||||
{
|
|
||||||
std::string result;
|
|
||||||
result.reserve(wstr.size());
|
|
||||||
|
|
||||||
for (const auto& chr : wstr)
|
|
||||||
{
|
|
||||||
result.push_back(static_cast<char>(chr));
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::wstring convert(const std::string& str)
|
|
||||||
{
|
|
||||||
std::wstring result;
|
|
||||||
result.reserve(str.size());
|
|
||||||
|
|
||||||
for (const auto& chr : str)
|
|
||||||
{
|
|
||||||
result.push_back(static_cast<wchar_t>(chr));
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
#ifdef _WIN32
|
|
||||||
#pragma warning(pop)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
std::string replace(std::string str, const std::string& from, const std::string& to)
|
std::string replace(std::string str, const std::string& from, const std::string& to)
|
||||||
{
|
{
|
||||||
if (from.empty())
|
if (from.empty())
|
||||||
|
@ -2,18 +2,16 @@
|
|||||||
#include "memory.hpp"
|
#include "memory.hpp"
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
|
||||||
#ifndef ARRAYSIZE
|
template <class Type, std::size_t n>
|
||||||
template <class Type, size_t n>
|
constexpr std::size_t ARRAY_COUNT(Type(&)[n]) { return n; }
|
||||||
size_t ARRAYSIZE(Type (&)[n]) { return n; }
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace utils::string
|
namespace utils::string
|
||||||
{
|
{
|
||||||
template <size_t Buffers, size_t MinBufferSize>
|
template <std::size_t buffers, std::size_t min_buffer_size>
|
||||||
class va_provider final
|
class va_provider final
|
||||||
{
|
{
|
||||||
public:
|
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)
|
va_provider() : current_buffer_(0)
|
||||||
{
|
{
|
||||||
@ -21,10 +19,10 @@ namespace utils::string
|
|||||||
|
|
||||||
char* get(const char* format, va_list ap)
|
char* get(const char* format, va_list ap)
|
||||||
{
|
{
|
||||||
++this->current_buffer_ %= ARRAYSIZE(this->string_pool_);
|
++this->current_buffer_ %= ARRAY_COUNT(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");
|
||||||
}
|
}
|
||||||
@ -32,9 +30,9 @@ namespace utils::string
|
|||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
const int res = vsnprintf_s(entry->buffer, entry->size, _TRUNCATE, format, ap);
|
const auto res = vsnprintf_s(entry->buffer_, entry->size_, _TRUNCATE, format, ap);
|
||||||
#else
|
#else
|
||||||
const int res = vsnprintf(entry->buffer, entry->size, format, ap);
|
const auto res = vsnprintf(entry->buffer_, entry->size_, format, ap);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (res > 0) break; // Success
|
if (res > 0) break; // Success
|
||||||
@ -43,44 +41,44 @@ namespace utils::string
|
|||||||
entry->double_size();
|
entry->double_size();
|
||||||
}
|
}
|
||||||
|
|
||||||
return entry->buffer;
|
return entry->buffer_;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
class entry final
|
class entry final
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit entry(const size_t _size = MinBufferSize) : size(_size), buffer(nullptr)
|
explicit entry(const std::size_t size = min_buffer_size) : size_(size), buffer_(nullptr)
|
||||||
{
|
{
|
||||||
if (this->size < MinBufferSize) this->size = MinBufferSize;
|
if (this->size_ < min_buffer_size) this->size_ = min_buffer_size;
|
||||||
this->allocate();
|
this->allocate();
|
||||||
}
|
}
|
||||||
|
|
||||||
~entry()
|
~entry()
|
||||||
{
|
{
|
||||||
if (this->buffer) memory::get_allocator()->free(this->buffer);
|
if (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) memory::get_allocator()->free(this->buffer);
|
if (this->buffer_) memory::get_allocator()->free(this->buffer_);
|
||||||
this->buffer = memory::get_allocator()->allocate_array<char>(this->size + 1);
|
this->buffer_ = 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;
|
std::size_t size_;
|
||||||
char* buffer;
|
char* buffer_;
|
||||||
};
|
};
|
||||||
|
|
||||||
size_t current_buffer_;
|
std::size_t current_buffer_;
|
||||||
entry string_pool_[Buffers];
|
entry string_pool_[buffers];
|
||||||
};
|
};
|
||||||
|
|
||||||
const char* va(const char* fmt, ...);
|
const char* va(const char* fmt, ...);
|
||||||
@ -96,10 +94,5 @@ namespace utils::string
|
|||||||
|
|
||||||
std::string dump_hex(const std::string& data, const std::string& separator = " ");
|
std::string dump_hex(const std::string& data, const std::string& separator = " ");
|
||||||
|
|
||||||
void strip(const char* in, char* out, int 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);
|
std::string replace(std::string str, const std::string& from, const std::string& to);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user