Added helper function to strip colors from strings.

This commit is contained in:
Caball 2024-12-30 23:21:50 +01:00
parent adc33983a8
commit e5a547650c
2 changed files with 28 additions and 0 deletions

View File

@ -140,6 +140,32 @@ namespace utils::string
*out = '\0';
}
std::string strip(std::string_view sv, bool strip_default_color)
{
std::string in(sv);
for (std::size_t i = 0; i + 1 < in.size();)
{
if (in[i] == '^' && static_cast<std::size_t>(in[i + 1] - '0') < 0xC)
{
if (in[i + 1] != '7' || strip_default_color)
{
in.erase(in.begin() + i, in.begin() + i + 2);
}
else
{
i += 2;
}
}
else
{
i += 1;
}
}
return in;
}
std::string convert(const std::wstring& wstr)
{
std::string result;

View File

@ -1,6 +1,7 @@
#pragma once
#include "memory.hpp"
#include <cstdint>
#include <span>
template <class Type, size_t n>
constexpr auto ARRAY_COUNT(Type(&)[n]) { return n; }
@ -91,6 +92,7 @@ namespace utils::string
std::string get_clipboard_data();
void strip(const char* in, char* out, size_t max);
std::string strip(std::string_view sv, bool strip_default_color = false);
std::string convert(const std::wstring& wstr);
std::wstring convert(const std::string& str);