mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-07-23 06:09:50 -06:00
Common: Assert that translatable strings use positional arguments
Like PR 9260, but for a different requirement (see PR 9253).
This commit is contained in:
@ -38,4 +38,39 @@ constexpr std::size_t CountFmtReplacementFields(std::string_view s)
|
||||
static_assert(CountFmtReplacementFields("") == 0);
|
||||
static_assert(CountFmtReplacementFields("{} test {:x}") == 2);
|
||||
static_assert(CountFmtReplacementFields("{} {{}} test {{{}}}") == 2);
|
||||
|
||||
constexpr bool ContainsNonPositionalArguments(std::string_view s)
|
||||
{
|
||||
for (std::size_t i = 0; i < s.size(); ++i)
|
||||
{
|
||||
if (s[i] != '{' || i + 1 == s.size())
|
||||
continue;
|
||||
|
||||
const char next = s[i + 1];
|
||||
|
||||
// If the opening brace is followed by another brace, what we have is
|
||||
// an escaped brace, not a replacement field.
|
||||
if (next == '{')
|
||||
{
|
||||
// Skip the second brace.
|
||||
// This ensures that e.g. {{{}}} is counted correctly: when the first brace character
|
||||
// is read and detected as being part of an '{{' escape sequence, the second character
|
||||
// is skipped so the most inner brace (the third character) is not detected
|
||||
// as the end of an '{{' pair.
|
||||
++i;
|
||||
}
|
||||
else if (next == '}' || next == ':')
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static_assert(!ContainsNonPositionalArguments(""));
|
||||
static_assert(ContainsNonPositionalArguments("{}"));
|
||||
static_assert(!ContainsNonPositionalArguments("{0}"));
|
||||
static_assert(ContainsNonPositionalArguments("{:x}"));
|
||||
static_assert(!ContainsNonPositionalArguments("{0:x}"));
|
||||
static_assert(!ContainsNonPositionalArguments("{0} {{}} test {{{1}}}"));
|
||||
} // namespace Common
|
||||
|
Reference in New Issue
Block a user