Refactor FormatCodes stringification

This commit is contained in:
Aaron van Geffen 2024-03-01 19:47:23 +01:00 committed by GitHub
parent 42844d1667
commit a79b062136
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 12 additions and 35 deletions

View file

@ -27,7 +27,6 @@
#include "../common.h"
#include "../localisation/ConversionTables.h"
#include "../localisation/FormatCodes.h"
#include "../localisation/Language.h"
#include "../util/Util.h"
#include "Memory.hpp"

View file

@ -11,9 +11,7 @@
#include "../core/EnumMap.hpp"
#include <mutex>
#include <string>
#include <vector>
// clang-format off
static const EnumMap<FormatToken> FormatTokenMap = {
@ -65,45 +63,26 @@ static const EnumMap<FormatToken> FormatTokenMap = {
};
// clang-format on
std::string_view GetFormatTokenStringWithBraces(FormatToken token)
{
// Ensure cache is thread safe
static std::mutex mutex;
std::lock_guard<std::mutex> guard(mutex);
static std::vector<std::string> cache;
auto index = static_cast<size_t>(token);
if (cache.size() <= index)
{
cache.resize(index + 1);
}
if (cache[index].empty())
{
cache[index] = "{" + std::string(FormatTokenToString(token)) + "}";
}
return cache[index];
}
FormatToken FormatTokenFromString(std::string_view token)
{
auto result = FormatTokenMap.find(token);
return result != std::end(FormatTokenMap) ? result->second : FormatToken::Unknown;
}
std::string_view FormatTokenToString(FormatToken token, bool withBraces)
std::string FormatTokenToString(FormatToken token)
{
if (withBraces)
{
return GetFormatTokenStringWithBraces(token);
}
auto it = FormatTokenMap.find(token);
if (it != FormatTokenMap.end())
return it->first;
return std::string(it->first);
return {};
}
std::string FormatTokenToStringWithBraces(FormatToken token)
{
return "{" + FormatTokenToString(token) + "}";
}
bool FormatTokenTakesArgument(FormatToken token)
{
switch (token)

View file

@ -9,8 +9,7 @@
#pragma once
#include "../common.h"
#include <cstdint>
#include <string_view>
enum class FormatToken
@ -75,9 +74,9 @@ enum class FormatToken
OutlineDisable,
};
std::string_view GetFormatTokenStringWithBraces(FormatToken token);
FormatToken FormatTokenFromString(std::string_view token);
std::string_view FormatTokenToString(FormatToken token, bool withBraces = false);
std::string FormatTokenToString(FormatToken token);
std::string FormatTokenToStringWithBraces(FormatToken token);
bool FormatTokenTakesArgument(FormatToken token);
bool FormatTokenIsColour(FormatToken token);
size_t FormatTokenGetTextColourIndex(FormatToken token);

View file

@ -650,7 +650,7 @@ std::string ConvertFormattedStringToOpenRCT2(std::string_view buffer)
auto token = GetFormatTokenFromRCT12Code(codepoint);
if (token != FormatToken::Unknown)
{
result += GetFormatTokenStringWithBraces(token);
result += FormatTokenToStringWithBraces(token);
}
else
{

View file

@ -51,7 +51,7 @@ void Banner::FormatTextTo(Formatter& ft, bool addColour) const
if (addColour)
{
auto formatToken = FormatTokenFromTextColour(text_colour);
auto tokenText = FormatTokenToString(formatToken, true);
auto tokenText = FormatTokenToStringWithBraces(formatToken);
ft.Add<StringId>(STR_STRING_STRINGID);
ft.Add<const char*>(tokenText.data());
}