Core/Misc: Remove boost/algorithm dependency

This commit is contained in:
Shauren
2025-06-08 14:37:54 +02:00
parent 0f9a0accf1
commit ff6df050c5
5 changed files with 42 additions and 32 deletions

View File

@@ -21,7 +21,6 @@
#include "IoContext.h"
#include "Log.h"
#include "Util.h"
#include <boost/algorithm/string/replace.hpp>
#include <boost/asio/ip/tcp.hpp>
void Metric::Initialize(std::string const& realmName, Trinity::Asio::IoContext& ioContext, std::function<void()> overallStatusLogger)
@@ -38,8 +37,7 @@ bool Metric::Connect()
{
auto& stream = static_cast<boost::asio::ip::tcp::iostream&>(GetDataStream());
stream.connect(_hostname, _port);
auto error = stream.error();
if (error)
if (boost::system::error_code const& error = stream.error())
{
TC_LOG_ERROR("metric", "Error connecting to '{}:{}', disabling Metric. Error message : {}",
_hostname, _port, error.message());
@@ -266,18 +264,23 @@ void Metric::ScheduleOverallStatusLog()
std::string Metric::FormatInfluxDBValue(bool value)
{
return value ? "t" : "f";
return std::string(1, value ? 't' : 'f');
}
template<class T>
std::string Metric::FormatInfluxDBValue(T value)
{
return std::to_string(value) + 'i';
std::string result = std::to_string(value);
result += 'i';
return result;
}
std::string Metric::FormatInfluxDBValue(std::string const& value)
{
return '"' + boost::replace_all_copy(value, "\"", "\\\"") + '"';
std::string result = StringReplaceAll(value, "\"", "\\\"");
result.insert(result.begin(), '"');
result.append(1, '"');
return result;
}
std::string Metric::FormatInfluxDBValue(char const* value)
@@ -298,7 +301,7 @@ std::string Metric::FormatInfluxDBValue(float value)
std::string Metric::FormatInfluxDBTagValue(std::string const& value)
{
// ToDo: should handle '=' and ',' characters too
return boost::replace_all_copy(value, " ", "\\ ");
return StringReplaceAll(value, " ", "\\ ");
}
std::string Metric::FormatInfluxDBValue(std::chrono::nanoseconds value)

View File

@@ -818,10 +818,10 @@ std::string Trinity::Impl::ByteArrayToHexStr(uint8 const* bytes, size_t arrayLen
}
std::string result;
result.reserve(arrayLen * 2);
auto inserter = std::back_inserter(result);
result.resize(arrayLen * 2);
auto inserter = result.data();
for (int32 i = init; i != end; i += op)
Trinity::StringFormatTo(inserter, "{:02X}", bytes[i]);
inserter = Trinity::StringFormatTo(inserter, "{:02X}", bytes[i]);
return result;
}
@@ -862,6 +862,16 @@ bool StringCompareLessI(std::string_view a, std::string_view b)
return std::ranges::lexicographical_compare(a, b, {}, charToLower, charToLower);
}
void StringReplaceAll(std::string* str, std::string_view text, std::string_view replacement)
{
std::size_t pos = str->find(text, 0);
while (pos != std::string::npos)
{
str->replace(pos, text.length(), replacement);
pos = str->find(text, pos + replacement.length());
}
}
std::string Trinity::Impl::GetTypeName(std::type_info const& info)
{
return boost::core::demangle(info.name());

View File

@@ -464,11 +464,6 @@ TC_COMMON_API bool StringEqualI(std::string_view str1, std::string_view str2);
inline bool StringStartsWith(std::string_view haystack, std::string_view needle) { return (haystack.substr(0, needle.length()) == needle); }
inline bool StringStartsWithI(std::string_view haystack, std::string_view needle) { return StringEqualI(haystack.substr(0, needle.length()), needle); }
TC_COMMON_API bool StringContainsStringI(std::string_view haystack, std::string_view needle);
template <typename T>
inline bool ValueContainsStringI(std::pair<T, std::string_view> const& haystack, std::string_view needle)
{
return StringContainsStringI(haystack.second, needle);
}
TC_COMMON_API bool StringCompareLessI(std::string_view a, std::string_view b);
struct StringCompareLessI_T
@@ -476,6 +471,15 @@ struct StringCompareLessI_T
bool operator()(std::string_view a, std::string_view b) const { return StringCompareLessI(a, b); }
};
TC_COMMON_API void StringReplaceAll(std::string* str, std::string_view text, std::string_view replacement);
[[nodiscard]] inline std::string StringReplaceAll(std::string_view str, std::string_view text, std::string_view replacement)
{
std::string result(str);
StringReplaceAll(&result, text, replacement);
return result;
}
// simple class for not-modifyable list
template <typename T>
class HookList final

View File

@@ -33,8 +33,6 @@
#include "StringConvert.h"
#include "World.h"
#include "WorldSession.h"
#include <boost/algorithm/string/replace.hpp>
#include <sstream>
Player* ChatHandler::GetPlayer() const { return m_session ? m_session->GetPlayer() : nullptr; }
@@ -112,19 +110,11 @@ bool ChatHandler::HasLowerSecurityAccount(WorldSession* target, uint32 target_ac
void ChatHandler::SendSysMessage(std::string_view str, bool escapeCharacters)
{
std::string msg{ str };
std::string msg(str);
// Replace every "|" with "||" in msg
if (escapeCharacters && msg.find('|') != std::string::npos)
{
std::vector<std::string_view> tokens = Trinity::Tokenize(msg, '|', true);
std::ostringstream stream;
for (size_t i = 0; i < tokens.size() - 1; ++i)
stream << tokens[i] << "||";
stream << tokens[tokens.size() - 1];
msg = stream.str();
}
if (escapeCharacters)
StringReplaceAll(&msg, "|"sv, "||"sv);
WorldPackets::Chat::Chat packet;
for (std::string_view line : Trinity::Tokenize(str, '\n', true))
@@ -606,7 +596,10 @@ LocaleConstant ChatHandler::GetSessionDbLocaleIndex() const
std::string ChatHandler::playerLink(std::string const& name) const
{
return m_session ? "|cffffffff|Hplayer:" + name + "|h[" + name + "]|h|r" : name;
if (m_session)
return Trinity::StringFormat("|cffffffff|Hplayer:{0}|h[{0}]|h|r", name);
else
return name;
}
std::string ChatHandler::GetNameLink(Player* chr) const
@@ -787,7 +780,8 @@ void AddonChannelCommandHandler::SendSysMessage(std::string_view str, bool escap
msg.append(echo, 4);
std::string body(str);
if (escapeCharacters)
boost::replace_all(body, "|", "||");
StringReplaceAll(&body, "|"sv, "||"sv);
size_t pos, lastpos;
for (lastpos = 0, pos = body.find('\n', lastpos); pos != std::string::npos; lastpos = pos + 1, pos = body.find('\n', lastpos))
{

View File

@@ -25,7 +25,6 @@
#include "Player.h"
#include "StringConvert.h"
#include "World.h"
#include <boost/algorithm/string/find.hpp>
#include <fstream>
#include <sstream>
@@ -303,7 +302,7 @@ void PlayerDump::InitializeTables()
TableField f;
f.FieldName = columnName;
f.IsBinaryField = !boost::ifind_first(typeName, "binary").empty() || !boost::ifind_first(typeName, "blob").empty();
f.IsBinaryField = StringContainsStringI(typeName, "binary"sv) || StringContainsStringI(typeName, "blob"sv);
bool toUpperResult = Utf8ToUpperOnlyLatin(columnName);
ASSERT(toUpperResult);