2017-01-20 14:36:52 +01:00
|
|
|
#pragma once
|
|
|
|
|
2022-11-22 14:09:25 +00:00
|
|
|
namespace Utils::String
|
2017-01-19 22:23:59 +01:00
|
|
|
{
|
2022-11-22 14:09:25 +00:00
|
|
|
template <std::size_t Buffers, std::size_t MinBufferSize>
|
|
|
|
class VAProvider
|
2017-01-19 22:23:59 +01:00
|
|
|
{
|
2022-11-22 14:09:25 +00:00
|
|
|
public:
|
|
|
|
static_assert(Buffers != 0 && MinBufferSize != 0, "Buffers and MinBufferSize mustn't be 0");
|
|
|
|
|
|
|
|
VAProvider() : currentBuffer(0) {}
|
|
|
|
~VAProvider() = default;
|
|
|
|
|
|
|
|
const char* get(const char* format, va_list ap)
|
2017-05-31 18:03:33 +02:00
|
|
|
{
|
2022-11-22 14:09:25 +00:00
|
|
|
++this->currentBuffer %= ARRAYSIZE(this->stringPool);
|
|
|
|
auto entry = &this->stringPool[this->currentBuffer];
|
2017-06-04 13:46:50 +02:00
|
|
|
|
2022-11-22 14:09:25 +00:00
|
|
|
if (!entry->size || !entry->buffer)
|
|
|
|
{
|
|
|
|
throw std::runtime_error("String pool not initialized");
|
|
|
|
}
|
2017-06-02 22:05:32 +02:00
|
|
|
|
2022-11-22 14:09:25 +00:00
|
|
|
while (true)
|
2017-06-02 15:36:20 +02:00
|
|
|
{
|
2022-11-22 14:09:25 +00:00
|
|
|
const auto res = vsnprintf_s(entry->buffer, entry->size, _TRUNCATE, format, ap);
|
|
|
|
if (res > 0) break; // Success
|
|
|
|
if (res == 0) return ""; // Error
|
2017-05-31 18:03:33 +02:00
|
|
|
|
2022-11-22 14:09:25 +00:00
|
|
|
entry->doubleSize();
|
|
|
|
}
|
2017-06-02 15:36:20 +02:00
|
|
|
|
2022-11-22 14:09:25 +00:00
|
|
|
return entry->buffer;
|
|
|
|
}
|
2017-06-02 15:36:20 +02:00
|
|
|
|
2022-11-22 14:09:25 +00:00
|
|
|
private:
|
|
|
|
class Entry
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Entry(std::size_t _size = MinBufferSize) : size(_size), buffer(nullptr)
|
|
|
|
{
|
|
|
|
if (this->size < MinBufferSize) this->size = MinBufferSize;
|
|
|
|
this->allocate();
|
|
|
|
}
|
|
|
|
|
|
|
|
~Entry()
|
|
|
|
{
|
|
|
|
if (this->buffer) Memory::GetAllocator()->free(this->buffer);
|
|
|
|
this->size = 0;
|
|
|
|
this->buffer = nullptr;
|
|
|
|
}
|
2017-06-02 15:36:20 +02:00
|
|
|
|
2022-11-22 14:09:25 +00:00
|
|
|
void allocate()
|
|
|
|
{
|
|
|
|
if (this->buffer) Memory::GetAllocator()->free(this->buffer);
|
|
|
|
this->buffer = Memory::GetAllocator()->allocateArray<char>(this->size + 1);
|
2017-06-02 15:36:20 +02:00
|
|
|
}
|
2017-05-31 18:03:33 +02:00
|
|
|
|
2022-11-22 14:09:25 +00:00
|
|
|
void doubleSize()
|
2017-06-02 15:36:20 +02:00
|
|
|
{
|
2022-11-22 14:09:25 +00:00
|
|
|
this->size *= 2;
|
|
|
|
this->allocate();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t size;
|
|
|
|
char* buffer;
|
2017-05-31 18:03:33 +02:00
|
|
|
};
|
|
|
|
|
2022-11-22 14:09:25 +00:00
|
|
|
std::size_t currentBuffer;
|
|
|
|
Entry stringPool[Buffers];
|
|
|
|
};
|
|
|
|
|
|
|
|
const char *VA(const char *fmt, ...);
|
2017-01-19 22:23:59 +01:00
|
|
|
|
2022-11-22 14:09:25 +00:00
|
|
|
std::string ToLower(std::string text);
|
|
|
|
std::string ToUpper(std::string text);
|
|
|
|
bool Compare(const std::string& lhs, const std::string& rhs);
|
|
|
|
std::vector<std::string> Split(const std::string& str, char delim);
|
|
|
|
void Replace(std::string& str, const std::string& from, const std::string& to);
|
|
|
|
bool StartsWith(const std::string& haystack, const std::string& needle);
|
|
|
|
bool EndsWith(const std::string& haystack, const std::string& needle);
|
|
|
|
bool IsNumber(const std::string& str);
|
2022-03-01 20:34:25 +00:00
|
|
|
|
2022-11-22 14:09:25 +00:00
|
|
|
std::string& LTrim(std::string& str);
|
|
|
|
std::string& RTrim(std::string& str);
|
|
|
|
std::string& Trim(std::string& str);
|
2022-02-27 16:42:53 +00:00
|
|
|
|
2022-11-22 14:09:25 +00:00
|
|
|
std::string Convert(const std::wstring& wstr);
|
|
|
|
std::wstring Convert(const std::string& str);
|
2017-01-19 22:23:59 +01:00
|
|
|
|
2022-11-22 14:09:25 +00:00
|
|
|
std::string FormatTimeSpan(int milliseconds);
|
|
|
|
std::string FormatBandwidth(std::size_t bytes, int milliseconds);
|
2017-01-19 22:23:59 +01:00
|
|
|
|
2022-11-22 14:09:25 +00:00
|
|
|
std::string DumpHex(const std::string& data, const std::string& separator = " ");
|
2017-01-19 22:23:59 +01:00
|
|
|
|
2022-11-22 14:09:25 +00:00
|
|
|
std::string XOR(std::string str, char value);
|
2017-01-19 22:23:59 +01:00
|
|
|
|
2022-11-22 14:09:25 +00:00
|
|
|
std::string EncodeBase64(const char* input, const unsigned long inputSize);
|
|
|
|
std::string EncodeBase64(const std::string& input);
|
2017-01-19 22:23:59 +01:00
|
|
|
|
2022-11-22 14:09:25 +00:00
|
|
|
std::string EncodeBase128(const std::string& input);
|
2017-01-19 22:23:59 +01:00
|
|
|
}
|