Codechange: Pass unformatted strings from GetStringPtr as std::string_view. (#13871)
This commit is contained in:
+15
-18
@@ -184,7 +184,7 @@ struct UnmappedChoiceList {
|
||||
}
|
||||
|
||||
/* "<STRINGDEFAULT>" */
|
||||
dest << this->strings[0].rdbuf() << '\0';
|
||||
dest << this->strings[0].rdbuf();
|
||||
} else {
|
||||
if (this->type == SCC_PLURAL_LIST) {
|
||||
*d++ = lm->plural_form;
|
||||
@@ -611,18 +611,18 @@ StringID GetGRFStringID(uint32_t grfid, GRFStringID stringid)
|
||||
* current language nullptr is returned.
|
||||
* @param text_list The GRFTextList to get the string from.
|
||||
*/
|
||||
const char *GetGRFStringFromGRFText(const GRFTextList &text_list)
|
||||
std::optional<std::string_view> GetGRFStringFromGRFText(const GRFTextList &text_list)
|
||||
{
|
||||
const char *default_text = nullptr;
|
||||
std::optional<std::string_view> default_text;
|
||||
|
||||
/* Search the list of lang-strings of this stringid for current lang */
|
||||
for (const auto &text : text_list) {
|
||||
if (text.langid == _currentLangID) return text.text.c_str();
|
||||
if (text.langid == _currentLangID) return text.text;
|
||||
|
||||
/* If the current string is English or American, set it as the
|
||||
* fallback language if the specific language isn't available. */
|
||||
if (text.langid == GRFLX_UNSPECIFIED || (default_text == nullptr && (text.langid == GRFLX_ENGLISH || text.langid == GRFLX_AMERICAN))) {
|
||||
default_text = text.text.c_str();
|
||||
if (text.langid == GRFLX_UNSPECIFIED || (!default_text.has_value() && (text.langid == GRFLX_ENGLISH || text.langid == GRFLX_AMERICAN))) {
|
||||
default_text = text.text;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -636,21 +636,21 @@ const char *GetGRFStringFromGRFText(const GRFTextList &text_list)
|
||||
* current language nullptr is returned.
|
||||
* @param text The GRFTextList to get the string from.
|
||||
*/
|
||||
const char *GetGRFStringFromGRFText(const GRFTextWrapper &text)
|
||||
std::optional<std::string_view> GetGRFStringFromGRFText(const GRFTextWrapper &text)
|
||||
{
|
||||
return text ? GetGRFStringFromGRFText(*text) : nullptr;
|
||||
return text ? GetGRFStringFromGRFText(*text) : std::nullopt;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a C-string from a stringid set by a newgrf.
|
||||
*/
|
||||
const char *GetGRFStringPtr(StringIndexInTab stringid)
|
||||
std::string_view GetGRFStringPtr(StringIndexInTab stringid)
|
||||
{
|
||||
assert(stringid.base() < _grf_text.size());
|
||||
assert(_grf_text[stringid].grfid != 0);
|
||||
|
||||
const char *str = GetGRFStringFromGRFText(_grf_text[stringid].textholder);
|
||||
if (str != nullptr) return str;
|
||||
auto str = GetGRFStringFromGRFText(_grf_text[stringid].textholder);
|
||||
if (str.has_value()) return *str;
|
||||
|
||||
/* Use the default string ID if the fallback string isn't available */
|
||||
return GetStringPtr(_grf_text[stringid].def_string);
|
||||
@@ -758,7 +758,7 @@ struct TextRefStack {
|
||||
}
|
||||
};
|
||||
|
||||
static void HandleNewGRFStringControlCodes(const char *str, TextRefStack &stack, std::vector<StringParameter> ¶ms);
|
||||
static void HandleNewGRFStringControlCodes(std::string_view str, TextRefStack &stack, std::vector<StringParameter> ¶ms);
|
||||
|
||||
/**
|
||||
* Process NewGRF string control code instructions.
|
||||
@@ -961,11 +961,9 @@ char32_t RemapNewGRFStringControlCode(char32_t scc, const char **str)
|
||||
* @param[in,out] stack Stack to use.
|
||||
* @param[out] params Parameters to fill.
|
||||
*/
|
||||
static void HandleNewGRFStringControlCodes(const char *str, TextRefStack &stack, std::vector<StringParameter> ¶ms)
|
||||
static void HandleNewGRFStringControlCodes(std::string_view str, TextRefStack &stack, std::vector<StringParameter> ¶ms)
|
||||
{
|
||||
if (str == nullptr) return;
|
||||
|
||||
for (const char *p = str; *p != '\0'; /* nothing */) {
|
||||
for (const char *p = str.data(), *end = str.data() + str.size(); p < end; /* nothing */) {
|
||||
char32_t scc;
|
||||
p += Utf8Decode(&scc, p);
|
||||
ProcessNewGRFStringControlCode(scc, p, stack, params);
|
||||
@@ -983,8 +981,7 @@ std::vector<StringParameter> GetGRFSringTextStackParameters(const GRFFile *grffi
|
||||
{
|
||||
if (stringid == INVALID_STRING_ID) return {};
|
||||
|
||||
const char *str = GetStringPtr(stringid);
|
||||
if (str == nullptr) return {};
|
||||
auto str = GetStringPtr(stringid);
|
||||
|
||||
std::vector<StringParameter> params;
|
||||
params.reserve(20);
|
||||
|
||||
Reference in New Issue
Block a user