From e5a547650c5c2b5dba7976169eef6a3eb44f0ea9 Mon Sep 17 00:00:00 2001 From: Caball Date: Mon, 30 Dec 2024 23:21:50 +0100 Subject: [PATCH] Added helper function to strip colors from strings. --- src/common/utils/string.cpp | 26 ++++++++++++++++++++++++++ src/common/utils/string.hpp | 2 ++ 2 files changed, 28 insertions(+) diff --git a/src/common/utils/string.cpp b/src/common/utils/string.cpp index 6236584..2e0bbad 100644 --- a/src/common/utils/string.cpp +++ b/src/common/utils/string.cpp @@ -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(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; diff --git a/src/common/utils/string.hpp b/src/common/utils/string.hpp index 6a50c3e..285bc77 100644 --- a/src/common/utils/string.hpp +++ b/src/common/utils/string.hpp @@ -1,6 +1,7 @@ #pragma once #include "memory.hpp" #include +#include template 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);