mirror of
https://github.com/OpenRCT2/OpenRCT2.git
synced 2025-01-22 18:31:59 -05:00
Refactor FormatCodes stringification
This commit is contained in:
parent
42844d1667
commit
a79b062136
5 changed files with 12 additions and 35 deletions
|
@ -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"
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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
|
||||
{
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue