1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2025-12-23 15:52:55 +01:00

Improve banner formatting fix

This commit is contained in:
Ted John
2020-11-29 00:40:17 +00:00
parent 699103fe86
commit 17edaae02b
7 changed files with 64 additions and 19 deletions

View File

@@ -126,6 +126,23 @@ namespace String
#endif
}
std::string_view ToStringView(const char* ch, size_t maxLen)
{
size_t len{};
for (size_t i = 0; i < maxLen; i++)
{
if (ch[i] == '\0')
{
break;
}
else
{
len++;
}
}
return std::string_view(ch, len);
}
bool IsNullOrEmpty(const utf8* str)
{
return str == nullptr || str[0] == '\0';

View File

@@ -39,6 +39,12 @@ namespace String
std::string ToUtf8(const std::wstring_view& src);
std::wstring ToWideChar(const std::string_view& src);
/**
* Creates a string_view from a char pointer with a length up to either the
* first null terminator or a given maximum length, whatever is smallest.
*/
std::string_view ToStringView(const char* ch, size_t maxLen);
bool IsNullOrEmpty(const utf8* str);
int32_t Compare(const std::string& a, const std::string& b, bool ignoreCase = false);
int32_t Compare(const utf8* a, const utf8* b, bool ignoreCase = false);

View File

@@ -9,7 +9,10 @@
#include "FormatCodes.h"
#include <mutex>
#include <string>
#include <unordered_map>
#include <vector>
// clang-format off
static const std::unordered_map<std::string_view, FormatToken> FormatTokenMap = {
@@ -62,22 +65,48 @@ static const std::unordered_map<std::string_view, FormatToken> FormatTokenMap =
};
// clang-format on
static 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)
std::string_view FormatTokenToString(FormatToken token, bool withBraces)
{
for (const auto& t : FormatTokenMap)
if (withBraces)
{
if (t.second == token)
{
return t.first;
}
return GetFormatTokenStringWithBraces(token);
}
else
{
for (const auto& t : FormatTokenMap)
{
if (t.second == token)
{
return t.first;
}
}
return {};
}
return {};
}
bool FormatTokenTakesArgument(FormatToken token)

View File

@@ -77,7 +77,7 @@ enum class FormatToken
};
FormatToken FormatTokenFromString(std::string_view token);
std::string_view FormatTokenToString(FormatToken token);
std::string_view FormatTokenToString(FormatToken token, bool withBraces = false);
bool FormatTokenTakesArgument(FormatToken token);
bool FormatTokenIsColour(FormatToken token);
size_t FormatTokenGetTextColourIndex(FormatToken token);

View File

@@ -3004,7 +3004,7 @@ private:
std::string GetUserString(rct_string_id stringId)
{
const auto originalString = _s4.string_table[(stringId - USER_STRING_START) % 1024];
std::string_view originalStringView(originalString, USER_STRING_MAX_LENGTH);
auto originalStringView = String::ToStringView(originalString, USER_STRING_MAX_LENGTH);
auto asUtf8 = rct2_to_utf8(originalStringView, RCT2_LANGUAGE_ID_ENGLISH_UK);
auto justText = RCT12RemoveFormattingUTF8(asUtf8);
return justText.data();

View File

@@ -1659,7 +1659,7 @@ public:
std::string GetUserString(rct_string_id stringId)
{
const auto originalString = _s6.custom_strings[(stringId - USER_STRING_START) % 1024];
std::string_view originalStringView(originalString, USER_STRING_MAX_LENGTH);
auto originalStringView = String::ToStringView(originalString, USER_STRING_MAX_LENGTH);
auto asUtf8 = rct2_to_utf8(originalStringView, RCT2_LANGUAGE_ID_ENGLISH_UK);
auto justText = RCT12RemoveFormattingUTF8(asUtf8);
return justText.data();

View File

@@ -45,16 +45,9 @@ void Banner::FormatTextTo(Formatter& ft, bool addColour) const
if (addColour)
{
auto formatToken = FormatTokenFromTextColour(text_colour);
auto tokenText = FormatTokenToString(formatToken);
thread_local std::string tokenTextColourBuffer;
tokenTextColourBuffer.clear();
tokenTextColourBuffer.push_back('{');
tokenTextColourBuffer.append(tokenText);
tokenTextColourBuffer.push_back('}');
auto tokenText = FormatTokenToString(formatToken, true);
ft.Add<rct_string_id>(STR_STRING_STRINGID);
ft.Add<const char*>(tokenTextColourBuffer.c_str());
ft.Add<const char*>(tokenText.data());
}
FormatTextTo(ft);