iw4x-client/src/Utils/String.hpp

157 lines
4.1 KiB
C++
Raw Normal View History

2017-01-20 14:36:52 +01:00
#pragma once
2022-12-17 18:54:41 +01:00
template <class Type, std::size_t n>
constexpr auto ARRAY_COUNT(Type(&)[n]) { return n; }
namespace Utils::String
2017-01-19 22:23:59 +01:00
{
template <std::size_t Buffers, std::size_t MinBufferSize>
class VAProvider
2017-01-19 22:23:59 +01:00
{
public:
static_assert(Buffers != 0 && MinBufferSize != 0, "Buffers and MinBufferSize mustn't be 0");
VAProvider() : currentBuffer(0) {}
~VAProvider() = default;
2022-11-29 14:18:10 +00:00
[[nodiscard]] const char* get(const char* format, va_list ap)
{
2022-12-17 18:54:41 +01:00
++this->currentBuffer %= ARRAY_COUNT(this->stringPool);
auto entry = &this->stringPool[this->currentBuffer];
if (!entry->size || !entry->buffer)
{
throw std::runtime_error("String pool not initialized");
}
while (true)
2017-06-02 15:36:20 +02:00
{
const auto res = vsnprintf_s(entry->buffer, entry->size, _TRUNCATE, format, ap);
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:
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
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
}
void doubleSize()
2017-06-02 15:36:20 +02:00
{
this->size *= 2;
this->allocate();
}
std::size_t size;
char* buffer;
};
std::size_t currentBuffer;
Entry stringPool[Buffers];
};
template <typename Arg> // This should display a nice "null" instead of a number
static void SanitizeFormatArgs(Arg& arg)
{
if constexpr (std::is_same_v<Arg, char*> || std::is_same_v<Arg, const char*>)
{
if (arg == nullptr)
{
arg = const_cast<char*>("nullptr");
}
}
}
2022-11-29 14:18:10 +00:00
[[nodiscard]] const char* VA(const char* fmt, ...);
template <typename... Args>
[[nodiscard]] const char* Format(std::string_view fmt, Args&&... args)
{
static thread_local std::string vaBuffer;
vaBuffer.clear();
(SanitizeFormatArgs(args), ...);
std::vformat_to(std::back_inserter(vaBuffer), fmt, std::make_format_args(args...));
return vaBuffer.data();
}
2022-11-29 14:18:10 +00:00
[[nodiscard]] std::string ToLower(const std::string& text);
[[nodiscard]] std::string ToUpper(const std::string& text);
2017-01-19 22:23:59 +01:00
2022-11-29 14:18:10 +00:00
template <class OutputIter>
[[nodiscard]] OutputIter ApplyToLower(OutputIter container)
{
OutputIter result;
std::ranges::transform(container, std::back_inserter(result), [](const std::string& s) -> std::string
{
return ToLower(s);
});
return result;
}
template <class OutputIter>
[[nodiscard]] OutputIter ApplyToUpper(OutputIter container)
{
OutputIter result;
std::ranges::transform(container, std::back_inserter(result), [](const std::string& s) -> std::string
{
return ToUpper(s);
});
return result;
}
[[nodiscard]] bool Compare(const std::string& lhs, const std::string& rhs);
2022-11-29 14:18:10 +00:00
[[nodiscard]] std::vector<std::string> Split(const std::string& str, char delim);
void Replace(std::string& str, const std::string& from, const std::string& to);
2022-11-29 14:18:10 +00:00
[[nodiscard]] bool StartsWith(const std::string& haystack, const std::string& needle);
[[nodiscard]] bool EndsWith(const std::string& haystack, const std::string& needle);
2022-11-29 14:18:10 +00:00
[[nodiscard]] bool IsNumber(const std::string& str);
std::string& LTrim(std::string& str);
std::string& RTrim(std::string& str);
2022-11-29 14:18:10 +00:00
void Trim(std::string& str);
2022-02-27 16:42:53 +00:00
2022-11-29 14:18:10 +00:00
[[nodiscard]] std::string Convert(const std::wstring& wstr);
[[nodiscard]] std::wstring Convert(const std::string& str);
2017-01-19 22:23:59 +01:00
2022-11-29 14:18:10 +00:00
[[nodiscard]] std::string FormatTimeSpan(int milliseconds);
[[nodiscard]] std::string FormatBandwidth(std::size_t bytes, int milliseconds);
2017-01-19 22:23:59 +01:00
2022-11-29 14:18:10 +00:00
[[nodiscard]] std::string DumpHex(const std::string& data, const std::string& separator = " ");
2017-01-19 22:23:59 +01:00
2022-11-29 14:18:10 +00:00
[[nodiscard]] std::string XOR(std::string str, char value);
2017-01-19 22:23:59 +01:00
2022-11-29 14:18:10 +00:00
[[nodiscard]] std::string EncodeBase64(const char* input, unsigned long inputSize);
[[nodiscard]] std::string EncodeBase64(const std::string& input);
2017-01-19 22:23:59 +01:00
2022-11-29 14:18:10 +00:00
[[nodiscard]] std::string EncodeBase128(const std::string& input);
2017-01-19 22:23:59 +01:00
}