Files
OpenRCT2-Web/src/openrct2/localisation/Formatting.cpp
T

834 lines
26 KiB
C++
Raw Normal View History

2020-10-09 00:50:08 +01:00
/*****************************************************************************
2023-01-01 04:58:01 -06:00
* Copyright (c) 2014-2023 OpenRCT2 developers
2020-10-09 00:50:08 +01:00
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#include "Formatting.h"
#include "../config/Config.h"
#include "../util/Util.h"
2021-12-11 18:06:06 -05:00
#include "Formatter.h"
2020-10-11 01:00:29 +01:00
#include "Localisation.h"
2020-10-09 00:50:08 +01:00
#include "StringIds.h"
2020-10-19 20:37:07 +01:00
#include <cmath>
2020-10-09 00:50:08 +01:00
#include <cstdint>
namespace OpenRCT2
{
static void FormatMonthYear(FormatBuffer& ss, int32_t month, int32_t year);
2020-12-05 13:55:44 +00:00
2020-10-14 23:46:09 +01:00
static std::optional<int32_t> ParseNumericToken(std::string_view s)
{
if (s.size() >= 3 && s.size() <= 5 && s[0] == '{' && s[s.size() - 1] == '}')
{
char buffer[8]{};
std::memcpy(buffer, s.data() + 1, s.size() - 2);
return std::atoi(buffer);
}
return std::nullopt;
}
static std::optional<int32_t> ParseNumericToken(std::string_view str, size_t& i)
{
if (i < str.size() && str[i] == '{')
{
auto parameterStart = i;
do
{
i++;
} while (i < str.size() && str[i] != '}');
if (i < str.size() && str[i] == '}')
{
i++;
}
auto paramter = str.substr(parameterStart, i - parameterStart);
return ParseNumericToken(paramter);
}
return std::nullopt;
}
FmtString::token::token(FormatToken k, std::string_view s, uint32_t p)
2020-10-09 19:48:15 +01:00
: kind(k)
, text(s)
2020-10-14 23:46:09 +01:00
, parameter(p)
2020-10-09 19:48:15 +01:00
{
}
bool FmtString::token::IsLiteral() const
{
2020-10-16 00:13:52 +01:00
return kind == FormatToken::Literal;
2020-10-09 19:48:15 +01:00
}
2020-11-21 18:16:56 +00:00
bool FmtString::token::IsCodepoint() const
{
return kind == FormatToken::Escaped;
}
codepoint_t FmtString::token::GetCodepoint() const
{
if (kind == FormatToken::Escaped)
{
// Assume text is only "{{" or "}}" for now
return text[0];
}
return 0;
}
2020-10-09 19:48:15 +01:00
FmtString::iterator::iterator(std::string_view s, size_t i)
: str(s)
, index(i)
{
update();
}
void FmtString::iterator::update()
{
auto i = index;
if (i >= str.size())
{
current = token();
return;
}
2020-10-15 22:12:41 +01:00
if (str[i] == '\n' || str[i] == '\r')
{
i++;
}
2020-11-21 18:16:56 +00:00
else if (str[i] == '{' && i + 1 < str.size() && str[i + 1] == '{')
{
i += 2;
}
else if (str[i] == '}' && i + 1 < str.size() && str[i + 1] == '}')
{
i += 2;
}
2020-10-15 22:12:41 +01:00
else if (str[i] == '{' && i + 1 < str.size() && str[i + 1] != '{')
2020-10-09 19:48:15 +01:00
{
// Move to end brace
2020-10-14 23:46:09 +01:00
auto startIndex = i;
2020-10-09 19:48:15 +01:00
do
{
i++;
} while (i < str.size() && str[i] != '}');
if (i < str.size() && str[i] == '}')
2020-10-14 23:46:09 +01:00
{
2020-10-09 19:48:15 +01:00
i++;
2020-10-14 23:46:09 +01:00
auto inner = str.substr(startIndex + 1, i - startIndex - 2);
if (inner == "MOVE_X")
{
uint32_t p = 0;
auto p0 = ParseNumericToken(str, i);
if (p0)
{
p = *p0;
}
2020-10-16 00:13:52 +01:00
current = token(FormatToken::Move, str.substr(startIndex, i - startIndex), p);
2020-10-14 23:46:09 +01:00
return;
}
2021-09-15 22:22:15 +02:00
if (inner == "INLINE_SPRITE")
2020-10-14 23:46:09 +01:00
{
uint32_t p = 0;
auto p0 = ParseNumericToken(str, i);
auto p1 = ParseNumericToken(str, i);
auto p2 = ParseNumericToken(str, i);
auto p3 = ParseNumericToken(str, i);
if (p0 && p1 && p2 && p3)
{
p |= (*p0);
p |= (*p1) << 8;
p |= (*p2) << 16;
p |= (*p3) << 24;
}
2020-10-16 00:13:52 +01:00
current = token(FormatToken::InlineSprite, str.substr(startIndex, i - startIndex), p);
2020-10-14 23:46:09 +01:00
return;
}
}
2020-10-09 19:48:15 +01:00
}
else
{
do
{
i++;
2020-11-21 18:16:56 +00:00
} while (i < str.size() && str[i] != '{' && str[i] != '}' && str[i] != '\n' && str[i] != '\r');
2020-10-09 19:48:15 +01:00
}
current = CreateToken(i - index);
}
bool FmtString::iterator::operator==(iterator& rhs)
{
return index == rhs.index;
}
bool FmtString::iterator::operator!=(iterator& rhs)
{
return index != rhs.index;
}
FmtString::token FmtString::iterator::CreateToken(size_t len)
{
std::string_view sztoken = str.substr(index, len);
2020-11-21 18:16:56 +00:00
if (sztoken.size() >= 2 && ((sztoken[0] == '{' && sztoken[1] == '{') || (sztoken[0] == '}' && sztoken[1] == '}')))
{
return token(FormatToken::Escaped, sztoken);
}
2021-09-15 22:22:15 +02:00
if (sztoken.size() >= 2 && sztoken[0] == '{' && sztoken[1] != '{')
2020-10-09 19:48:15 +01:00
{
2020-10-16 00:13:52 +01:00
auto kind = FormatTokenFromString(sztoken.substr(1, len - 2));
2020-10-09 19:48:15 +01:00
return token(kind, sztoken);
}
2021-09-15 22:22:15 +02:00
if (sztoken == "\n" || sztoken == "\r")
2020-10-15 22:12:41 +01:00
{
2020-10-16 00:13:52 +01:00
return token(FormatToken::Newline, sztoken);
2020-10-15 22:12:41 +01:00
}
2020-10-16 00:13:52 +01:00
return token(FormatToken::Literal, sztoken);
2020-10-09 19:48:15 +01:00
}
const FmtString::token* FmtString::iterator::operator->() const
{
return &current;
}
const FmtString::token& FmtString::iterator::operator*()
{
return current;
}
FmtString::iterator& FmtString::iterator::operator++()
{
if (index < str.size())
{
index += current.text.size();
update();
}
return *this;
}
2020-10-11 01:00:29 +01:00
FmtString::iterator FmtString::iterator::operator++(int)
{
auto result = *this;
if (index < str.size())
{
index += current.text.size();
update();
}
return result;
}
bool FmtString::iterator::eol() const
{
return index >= str.size();
}
2020-10-09 19:48:15 +01:00
FmtString::FmtString(std::string&& s)
{
_strOwned = std::move(s);
_str = _strOwned;
}
FmtString::FmtString(std::string_view s)
: _str(s)
{
}
FmtString::FmtString(const char* s)
2020-10-13 01:14:39 +01:00
: FmtString(s == nullptr ? std::string_view() : std::string_view(s))
2020-10-09 19:48:15 +01:00
{
}
FmtString::iterator FmtString::begin() const
{
return iterator(_str, 0);
}
FmtString::iterator FmtString::end() const
{
return iterator(_str, _str.size());
}
std::string FmtString::WithoutFormatTokens() const
{
std::string result;
result.reserve(_str.size() * 4);
for (const auto& t : *this)
{
if (t.IsLiteral())
{
result += t.text;
}
}
return result;
}
2020-11-04 23:44:57 +00:00
static std::string_view GetDigitSeparator()
2020-10-09 00:50:08 +01:00
{
auto sz = LanguageGetString(STR_LOCALE_THOUSANDS_SEPARATOR);
2020-10-11 01:00:29 +01:00
return sz != nullptr ? sz : std::string_view();
2020-10-09 00:50:08 +01:00
}
2020-11-04 23:44:57 +00:00
static std::string_view GetDecimalSeparator()
2020-10-09 00:50:08 +01:00
{
auto sz = LanguageGetString(STR_LOCALE_DECIMAL_POINT);
2020-10-11 01:00:29 +01:00
return sz != nullptr ? sz : std::string_view();
}
2022-07-31 14:22:58 +02:00
void FormatRealName(FormatBuffer& ss, StringId id)
2020-10-13 01:14:39 +01:00
{
if (IsRealNameStringId(id))
{
auto realNameIndex = id - REAL_NAME_START;
ss << real_names[realNameIndex % std::size(real_names)];
ss << ' ';
ss << real_name_initials[(realNameIndex >> 10) % std::size(real_name_initials)];
ss << '.';
}
}
2020-11-04 23:44:57 +00:00
template<size_t TSize, typename TIndex> static void AppendSeparator(char (&buffer)[TSize], TIndex& i, std::string_view sep)
2020-10-11 01:00:29 +01:00
{
if (i < TSize)
{
auto remainingLen = TSize - i;
auto cpyLen = std::min(sep.size(), remainingLen);
std::memcpy(&buffer[i], sep.data(), cpyLen);
i += static_cast<TIndex>(cpyLen);
}
2020-10-09 00:50:08 +01:00
}
template<size_t TDecimalPlace, bool TDigitSep, typename T> void FormatNumber(FormatBuffer& ss, T value)
2020-10-09 00:50:08 +01:00
{
char buffer[32];
2020-10-19 20:37:07 +01:00
size_t i = 0;
2020-10-09 00:50:08 +01:00
2020-11-21 16:46:00 +00:00
uint64_t num;
if constexpr (std::is_signed<T>::value)
2020-10-09 00:50:08 +01:00
{
2020-11-21 16:46:00 +00:00
if (value < 0)
{
ss << '-';
if (value == std::numeric_limits<int64_t>::min())
{
// Edge case: int64_t can not store this number so manually assign num to (int64_t::max + 1)
num = static_cast<uint64_t>(std::numeric_limits<int64_t>::max()) + 1;
}
else
{
// Cast negative number to int64_t and then reverse sign
num = -static_cast<int64_t>(value);
}
}
else
{
num = value;
}
2020-10-09 00:50:08 +01:00
}
else
{
num = value;
}
// Decimal digits
if constexpr (TDecimalPlace > 0)
{
while (num != 0 && i < sizeof(buffer) && i < TDecimalPlace)
{
2020-10-19 20:37:07 +01:00
buffer[i++] = static_cast<char>('0' + (num % 10));
2020-10-09 00:50:08 +01:00
num /= 10;
}
// handle case where value has fewer sig figs than required decimal places
while (num == 0 && i < TDecimalPlace && i < sizeof(buffer))
{
buffer[i++] = '0';
}
2020-10-11 01:00:29 +01:00
2020-11-04 23:44:57 +00:00
auto decSep = GetDecimalSeparator();
AppendSeparator(buffer, i, decSep);
2020-10-09 00:50:08 +01:00
}
// Whole digits
2020-11-04 23:44:57 +00:00
[[maybe_unused]] auto digitSep = GetDigitSeparator();
[[maybe_unused]] size_t groupLen = 0;
2020-10-09 00:50:08 +01:00
do
{
if constexpr (TDigitSep)
{
if (groupLen >= 3)
{
groupLen = 0;
2020-11-04 23:44:57 +00:00
AppendSeparator(buffer, i, digitSep);
2020-10-09 00:50:08 +01:00
}
}
2020-10-19 20:37:07 +01:00
buffer[i++] = static_cast<char>('0' + (num % 10));
2020-10-09 00:50:08 +01:00
num /= 10;
if constexpr (TDigitSep)
{
groupLen++;
}
} while (num != 0 && i < sizeof(buffer));
// Finally reverse append the string
2020-10-19 20:37:07 +01:00
for (int32_t j = static_cast<int32_t>(i - 1); j >= 0; j--)
2020-10-09 00:50:08 +01:00
{
ss << buffer[j];
}
}
template<size_t TDecimalPlace, bool TDigitSep, typename T> void FormatCurrency(FormatBuffer& ss, T rawValue)
2020-10-11 01:00:29 +01:00
{
2022-10-16 17:27:01 +02:00
auto currencyDesc = &CurrencyDescriptors[EnumValue(gConfigGeneral.CurrencyFormat)];
2020-10-11 01:00:29 +01:00
auto value = static_cast<int64_t>(rawValue) * currencyDesc->rate;
// Negative sign
if (value < 0)
{
ss << '-';
value = -value;
}
// Round the value away from zero
if constexpr (TDecimalPlace < 2)
{
value = (value + 99) / 100;
}
// Currency symbol
auto symbol = currencyDesc->symbol_unicode;
auto affix = currencyDesc->affix_unicode;
if (!FontSupportsString(symbol, FontStyle::Medium))
2020-10-11 01:00:29 +01:00
{
symbol = currencyDesc->symbol_ascii;
affix = currencyDesc->affix_ascii;
}
// Currency symbol prefix
if (affix == CurrencyAffix::Prefix)
{
ss << symbol;
}
// Drop the pennies for "large" currencies
auto dropPennies = false;
if constexpr (TDecimalPlace >= 2)
{
dropPennies = currencyDesc->rate >= 100;
}
if (dropPennies)
{
FormatNumber<0, TDigitSep>(ss, value / 100);
}
else
{
FormatNumber<TDecimalPlace, TDigitSep>(ss, value);
}
// Currency symbol suffix
if (affix == CurrencyAffix::Suffix)
{
ss << symbol;
}
}
template<typename T> static void FormatMinutesSeconds(FormatBuffer& ss, T value)
2020-10-11 01:00:29 +01:00
{
2022-07-31 14:22:58 +02:00
static constexpr const StringId Formats[][2] = {
2020-10-11 01:00:29 +01:00
{ STR_DURATION_SEC, STR_DURATION_SECS },
{ STR_DURATION_MIN_SEC, STR_DURATION_MIN_SECS },
{ STR_DURATION_MINS_SEC, STR_DURATION_MINS_SECS },
};
auto minutes = value / 60;
auto seconds = value % 60;
if (minutes == 0)
{
auto fmt = Formats[0][seconds == 1 ? 0 : 1];
FormatStringID(ss, fmt, seconds);
2020-10-11 01:00:29 +01:00
}
else
{
auto fmt = Formats[minutes == 1 ? 1 : 2][seconds == 1 ? 0 : 1];
FormatStringID(ss, fmt, minutes, seconds);
2020-10-11 01:00:29 +01:00
}
}
template<typename T> static void FormatHoursMinutes(FormatBuffer& ss, T value)
2020-10-11 01:00:29 +01:00
{
2022-07-31 14:22:58 +02:00
static constexpr const StringId Formats[][2] = {
2020-10-11 01:00:29 +01:00
{ STR_REALTIME_MIN, STR_REALTIME_MINS },
{ STR_REALTIME_HOUR_MIN, STR_REALTIME_HOUR_MINS },
{ STR_REALTIME_HOURS_MIN, STR_REALTIME_HOURS_MINS },
};
auto hours = value / 60;
auto minutes = value % 60;
if (hours == 0)
{
auto fmt = Formats[0][minutes == 1 ? 0 : 1];
FormatStringID(ss, fmt, minutes);
2020-10-11 01:00:29 +01:00
}
else
{
auto fmt = Formats[hours == 1 ? 1 : 2][minutes == 1 ? 0 : 1];
FormatStringID(ss, fmt, hours, minutes);
2020-10-11 01:00:29 +01:00
}
}
template<typename T> void FormatArgument(FormatBuffer& ss, FormatToken token, T arg)
2020-10-09 00:50:08 +01:00
{
switch (token)
{
2020-10-19 20:37:07 +01:00
case FormatToken::UInt16:
2020-10-16 00:13:52 +01:00
case FormatToken::Int32:
2020-10-09 00:50:08 +01:00
if constexpr (std::is_integral<T>())
{
FormatNumber<0, false>(ss, arg);
}
break;
2020-10-16 00:13:52 +01:00
case FormatToken::Comma16:
case FormatToken::Comma32:
2020-10-09 00:50:08 +01:00
if constexpr (std::is_integral<T>())
{
FormatNumber<0, true>(ss, arg);
}
break;
2020-10-16 00:13:52 +01:00
case FormatToken::Comma1dp16:
2020-10-09 00:50:08 +01:00
if constexpr (std::is_integral<T>())
{
FormatNumber<1, true>(ss, arg);
}
else if constexpr (std::is_floating_point<T>())
{
FormatNumber<1, true>(ss, std::round(arg * 10));
}
break;
2020-10-16 00:13:52 +01:00
case FormatToken::Comma2dp32:
2020-10-09 00:50:08 +01:00
if constexpr (std::is_integral<T>())
{
FormatNumber<2, true>(ss, arg);
}
else if constexpr (std::is_floating_point<T>())
{
FormatNumber<2, true>(ss, std::round(arg * 100));
}
break;
2020-10-16 00:13:52 +01:00
case FormatToken::Currency2dp:
2020-10-11 01:00:29 +01:00
if constexpr (std::is_integral<T>())
{
FormatCurrency<2, true>(ss, arg);
}
break;
2020-10-16 00:13:52 +01:00
case FormatToken::Currency:
2020-10-11 01:00:29 +01:00
if constexpr (std::is_integral<T>())
{
FormatCurrency<0, true>(ss, arg);
}
break;
2020-10-16 00:13:52 +01:00
case FormatToken::Velocity:
2020-10-09 00:50:08 +01:00
if constexpr (std::is_integral<T>())
{
2022-10-16 17:27:01 +02:00
switch (gConfigGeneral.MeasurementFormat)
2020-10-09 00:50:08 +01:00
{
default:
case MeasurementFormat::Imperial:
FormatStringID(ss, STR_UNIT_SUFFIX_MILES_PER_HOUR, arg);
2020-10-09 00:50:08 +01:00
break;
case MeasurementFormat::Metric:
FormatStringID(ss, STR_UNIT_SUFFIX_KILOMETRES_PER_HOUR, MphToKmph(arg));
2020-10-09 00:50:08 +01:00
break;
case MeasurementFormat::SI:
FormatStringID(ss, STR_UNIT_SUFFIX_METRES_PER_SECOND, MphToDmps(arg));
2020-10-09 00:50:08 +01:00
break;
}
}
break;
2020-10-16 00:13:52 +01:00
case FormatToken::DurationShort:
2020-10-11 01:00:29 +01:00
if constexpr (std::is_integral<T>())
{
FormatMinutesSeconds(ss, arg);
}
break;
2020-10-16 00:13:52 +01:00
case FormatToken::DurationLong:
2020-10-11 01:00:29 +01:00
if constexpr (std::is_integral<T>())
{
FormatHoursMinutes(ss, arg);
}
break;
2020-10-16 00:13:52 +01:00
case FormatToken::Length:
2020-10-11 01:00:29 +01:00
if constexpr (std::is_integral<T>())
{
2022-10-16 17:27:01 +02:00
switch (gConfigGeneral.MeasurementFormat)
2020-10-11 01:00:29 +01:00
{
default:
case MeasurementFormat::Imperial:
FormatStringID(ss, STR_UNIT_SUFFIX_FEET, MetresToFeet(arg));
2020-10-11 01:00:29 +01:00
break;
case MeasurementFormat::Metric:
case MeasurementFormat::SI:
FormatStringID(ss, STR_UNIT_SUFFIX_METRES, arg);
2020-10-11 01:00:29 +01:00
break;
}
}
break;
2020-10-16 00:13:52 +01:00
case FormatToken::MonthYear:
2020-10-11 01:00:29 +01:00
if constexpr (std::is_integral<T>())
{
auto month = DateGetMonth(arg);
auto year = DateGetYear(arg) + 1;
2020-12-05 13:55:44 +00:00
FormatMonthYear(ss, month, year);
2020-10-11 01:00:29 +01:00
}
break;
2020-10-16 00:13:52 +01:00
case FormatToken::Month:
2020-10-11 01:00:29 +01:00
if constexpr (std::is_integral<T>())
{
auto szMonth = LanguageGetString(DateGameMonthNames[DateGetMonth(arg)]);
2020-10-11 01:00:29 +01:00
if (szMonth != nullptr)
{
ss << szMonth;
}
}
break;
2020-10-16 00:13:52 +01:00
case FormatToken::String:
ss << arg;
2020-10-09 00:50:08 +01:00
break;
2020-10-16 00:13:52 +01:00
case FormatToken::Sprite:
2020-10-15 00:10:38 +01:00
if constexpr (std::is_integral<T>())
{
auto idx = static_cast<uint32_t>(arg);
char inlineBuf[64];
size_t len = snprintf(
inlineBuf, sizeof(inlineBuf), "{INLINE_SPRITE}{%u}{%u}{%u}{%u}", ((idx >> 0) & 0xFF),
((idx >> 8) & 0xFF), ((idx >> 16) & 0xFF), ((idx >> 24) & 0xFF));
ss.append(inlineBuf, len);
2020-10-15 00:10:38 +01:00
}
break;
2020-10-19 20:37:07 +01:00
default:
break;
2020-10-09 00:50:08 +01:00
}
}
template void FormatArgument(FormatBuffer&, FormatToken, uint16_t);
template void FormatArgument(FormatBuffer&, FormatToken, int16_t);
template void FormatArgument(FormatBuffer&, FormatToken, int32_t);
template void FormatArgument(FormatBuffer&, FormatToken, int64_t);
2021-02-08 20:24:33 +00:00
template void FormatArgument(FormatBuffer&, FormatToken, uint32_t);
template void FormatArgument(FormatBuffer&, FormatToken, uint64_t);
template void FormatArgument(FormatBuffer&, FormatToken, const char*);
template void FormatArgument(FormatBuffer&, FormatToken, std::string_view);
2020-10-09 00:50:08 +01:00
2022-07-31 14:22:58 +02:00
bool IsRealNameStringId(StringId id)
2020-10-13 01:14:39 +01:00
{
return id >= REAL_NAME_START && id <= REAL_NAME_END;
}
2022-07-31 14:22:58 +02:00
FmtString GetFmtStringById(StringId id)
{
auto fmtc = LanguageGetString(id);
2020-10-13 01:14:39 +01:00
return FmtString(fmtc);
}
FormatBuffer& GetThreadFormatStream()
2020-10-12 20:25:22 +01:00
{
thread_local FormatBuffer ss;
ss.clear();
2020-10-12 20:25:22 +01:00
return ss;
}
size_t CopyStringStreamToBuffer(char* buffer, size_t bufferLen, FormatBuffer& ss)
2020-10-12 20:25:22 +01:00
{
auto copyLen = std::min<size_t>(bufferLen - 1, ss.size());
2020-10-12 20:25:22 +01:00
std::copy(ss.data(), ss.data() + copyLen, buffer);
2020-10-12 20:25:22 +01:00
buffer[copyLen] = '\0';
return ss.size();
2020-10-12 20:25:22 +01:00
}
static void FormatArgumentAny(FormatBuffer& ss, FormatToken token, const FormatArg_t& value)
2020-10-09 01:30:24 +01:00
{
2020-10-12 21:57:43 +01:00
if (std::holds_alternative<uint16_t>(value))
2020-10-12 20:25:22 +01:00
{
2020-10-12 21:57:43 +01:00
FormatArgument(ss, token, std::get<uint16_t>(value));
2020-10-12 20:25:22 +01:00
}
2020-10-12 21:57:43 +01:00
else if (std::holds_alternative<int32_t>(value))
2020-10-09 01:30:24 +01:00
{
2020-10-12 21:57:43 +01:00
FormatArgument(ss, token, std::get<int32_t>(value));
2020-10-09 01:30:24 +01:00
}
else if (std::holds_alternative<int64_t>(value))
{
FormatArgument(ss, token, std::get<int64_t>(value));
}
2020-10-12 21:57:43 +01:00
else if (std::holds_alternative<const char*>(value))
2020-10-09 01:30:24 +01:00
{
2020-10-12 21:57:43 +01:00
FormatArgument(ss, token, std::get<const char*>(value));
2020-10-09 01:30:24 +01:00
}
else if (std::holds_alternative<std::string>(value))
{
FormatArgument(ss, token, std::get<std::string>(value));
}
2020-10-09 01:30:24 +01:00
else
{
throw std::runtime_error("No support for format argument type.");
}
}
static void FormatStringAny(FormatBuffer& ss, const FmtString& fmt, const std::vector<FormatArg_t>& args, size_t& argIndex)
2020-10-09 01:30:24 +01:00
{
2020-10-11 01:00:29 +01:00
for (const auto& token : fmt)
2020-10-09 01:30:24 +01:00
{
2022-07-31 15:04:08 +02:00
if (token.kind == FormatToken::StringById)
{
if (argIndex < args.size())
{
2020-10-12 21:57:43 +01:00
const auto& arg = args[argIndex++];
2022-07-31 14:22:58 +02:00
std::optional<StringId> stringId;
if (auto value16 = std::get_if<uint16_t>(&arg))
{
stringId = *value16;
}
else if (auto value32 = std::get_if<int32_t>(&arg))
{
stringId = *value32;
}
if (stringId)
{
if (IsRealNameStringId(*stringId))
2020-10-13 01:14:39 +01:00
{
FormatRealName(ss, *stringId);
2020-10-13 01:14:39 +01:00
}
else
{
auto subfmt = GetFmtStringById(*stringId);
2020-10-13 01:14:39 +01:00
FormatStringAny(ss, subfmt, args, argIndex);
}
}
}
else
{
argIndex++;
}
}
2020-10-16 00:13:52 +01:00
else if (FormatTokenTakesArgument(token.kind))
2020-10-09 01:30:24 +01:00
{
if (argIndex < args.size())
{
2020-10-11 01:00:29 +01:00
FormatArgumentAny(ss, token.kind, args[argIndex]);
2020-10-09 01:30:24 +01:00
}
argIndex++;
}
2020-10-16 00:13:52 +01:00
else if (token.kind != FormatToken::Push16 && token.kind != FormatToken::Pop16)
2020-10-09 01:30:24 +01:00
{
2020-10-11 01:00:29 +01:00
ss << token.text;
2020-10-09 01:30:24 +01:00
}
}
}
2020-10-12 21:57:43 +01:00
std::string FormatStringAny(const FmtString& fmt, const std::vector<FormatArg_t>& args)
{
2020-10-12 20:25:22 +01:00
auto& ss = GetThreadFormatStream();
size_t argIndex = 0;
FormatStringAny(ss, fmt, args, argIndex);
return ss.data();
2020-10-09 01:30:24 +01:00
}
2020-10-12 20:25:22 +01:00
2020-10-12 21:57:43 +01:00
size_t FormatStringAny(char* buffer, size_t bufferLen, const FmtString& fmt, const std::vector<FormatArg_t>& args)
2020-10-12 20:25:22 +01:00
{
auto& ss = GetThreadFormatStream();
size_t argIndex = 0;
FormatStringAny(ss, fmt, args, argIndex);
return CopyStringStreamToBuffer(buffer, bufferLen, ss);
}
template<typename T> static T ReadFromArgs(const void*& args)
{
T value;
std::memcpy(&value, args, sizeof(T));
args = reinterpret_cast<const char*>(reinterpret_cast<uintptr_t>(args) + sizeof(T));
return value;
}
2020-10-15 00:10:38 +01:00
static void BuildAnyArgListFromLegacyArgBuffer(const FmtString& fmt, std::vector<FormatArg_t>& anyArgs, const void*& args)
2020-10-12 20:25:22 +01:00
{
for (const auto& t : fmt)
{
switch (t.kind)
{
2020-10-16 00:13:52 +01:00
case FormatToken::Comma32:
case FormatToken::Int32:
case FormatToken::Comma2dp32:
case FormatToken::Sprite:
2020-10-12 20:25:22 +01:00
anyArgs.push_back(ReadFromArgs<int32_t>(args));
break;
case FormatToken::Currency2dp:
case FormatToken::Currency:
anyArgs.push_back(ReadFromArgs<int64_t>(args));
break;
2020-10-19 20:37:07 +01:00
case FormatToken::UInt16:
2020-10-16 00:13:52 +01:00
case FormatToken::MonthYear:
case FormatToken::Month:
case FormatToken::Velocity:
case FormatToken::DurationShort:
case FormatToken::DurationLong:
anyArgs.push_back(ReadFromArgs<uint16_t>(args));
break;
case FormatToken::Comma16:
2020-10-16 00:13:52 +01:00
case FormatToken::Length:
case FormatToken::Comma1dp16:
anyArgs.push_back(ReadFromArgs<int16_t>(args));
2020-10-12 20:25:22 +01:00
break;
2022-07-31 15:04:08 +02:00
case FormatToken::StringById:
2020-10-12 20:25:22 +01:00
{
2022-07-31 14:22:58 +02:00
auto stringId = ReadFromArgs<StringId>(args);
2020-10-12 20:25:22 +01:00
anyArgs.push_back(stringId);
BuildAnyArgListFromLegacyArgBuffer(GetFmtStringById(stringId), anyArgs, args);
break;
}
2020-10-16 00:13:52 +01:00
case FormatToken::String:
2020-10-12 20:25:22 +01:00
{
auto sz = ReadFromArgs<const char*>(args);
anyArgs.push_back(sz);
break;
}
2020-10-16 00:13:52 +01:00
case FormatToken::Pop16:
2020-10-12 20:25:22 +01:00
args = reinterpret_cast<const char*>(reinterpret_cast<uintptr_t>(args) + 2);
break;
2020-10-16 00:13:52 +01:00
case FormatToken::Push16:
2020-10-12 20:25:22 +01:00
args = reinterpret_cast<const char*>(reinterpret_cast<uintptr_t>(args) - 2);
break;
2020-10-19 20:37:07 +01:00
default:
break;
2020-10-12 20:25:22 +01:00
}
}
}
2022-07-31 14:22:58 +02:00
size_t FormatStringLegacy(char* buffer, size_t bufferLen, StringId id, const void* args)
2020-10-12 20:25:22 +01:00
{
thread_local std::vector<FormatArg_t> anyArgs;
anyArgs.clear();
2020-10-12 20:25:22 +01:00
auto fmt = GetFmtStringById(id);
BuildAnyArgListFromLegacyArgBuffer(fmt, anyArgs, args);
return FormatStringAny(buffer, bufferLen, fmt, anyArgs);
}
2020-12-05 13:55:44 +00:00
static void FormatMonthYear(FormatBuffer& ss, int32_t month, int32_t year)
2020-12-05 13:55:44 +00:00
{
thread_local std::vector<FormatArg_t> tempArgs;
tempArgs.clear();
auto fmt = GetFmtStringById(STR_DATE_FORMAT_MY);
Formatter ft;
ft.Add<uint16_t>(month);
ft.Add<uint16_t>(year);
const void* legacyArgs = ft.Data();
BuildAnyArgListFromLegacyArgBuffer(fmt, tempArgs, legacyArgs);
size_t argIndex = 0;
FormatStringAny(ss, fmt, tempArgs, argIndex);
}
2020-10-09 00:50:08 +01:00
} // namespace OpenRCT2