iw4x-client/src/Utils/String.hpp

106 lines
2.7 KiB
C++
Raw Normal View History

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