mirror of https://github.com/OpenTTD/OpenTTD
Codechange: Replace remaining Utf8Encode usages with StringBuilder.
parent
5878db1d02
commit
fa284af263
|
@ -37,6 +37,7 @@
|
||||||
#include "../gfx_func.h"
|
#include "../gfx_func.h"
|
||||||
#include "../error.h"
|
#include "../error.h"
|
||||||
#include "../misc_cmd.h"
|
#include "../misc_cmd.h"
|
||||||
|
#include "../core/string_builder.hpp"
|
||||||
#ifdef DEBUG_DUMP_COMMANDS
|
#ifdef DEBUG_DUMP_COMMANDS
|
||||||
# include "../fileio_func.h"
|
# include "../fileio_func.h"
|
||||||
#endif
|
#endif
|
||||||
|
@ -231,49 +232,48 @@ uint8_t NetworkSpectatorCount()
|
||||||
* If 'self_send' is true, this is the client who is sending the message */
|
* If 'self_send' is true, this is the client who is sending the message */
|
||||||
void NetworkTextMessage(NetworkAction action, TextColour colour, bool self_send, const std::string &name, const std::string &str, StringParameter &&data)
|
void NetworkTextMessage(NetworkAction action, TextColour colour, bool self_send, const std::string &name, const std::string &str, StringParameter &&data)
|
||||||
{
|
{
|
||||||
std::string string;
|
std::string message;
|
||||||
switch (action) {
|
StringBuilder builder(message);
|
||||||
case NETWORK_ACTION_SERVER_MESSAGE:
|
|
||||||
/* Ignore invalid messages */
|
|
||||||
string = GetString(STR_NETWORK_SERVER_MESSAGE, str);
|
|
||||||
colour = CC_DEFAULT;
|
|
||||||
break;
|
|
||||||
case NETWORK_ACTION_COMPANY_SPECTATOR:
|
|
||||||
colour = CC_DEFAULT;
|
|
||||||
string = GetString(STR_NETWORK_MESSAGE_CLIENT_COMPANY_SPECTATE, name);
|
|
||||||
break;
|
|
||||||
case NETWORK_ACTION_COMPANY_JOIN:
|
|
||||||
colour = CC_DEFAULT;
|
|
||||||
string = GetString(STR_NETWORK_MESSAGE_CLIENT_COMPANY_JOIN, name, str);
|
|
||||||
break;
|
|
||||||
case NETWORK_ACTION_COMPANY_NEW:
|
|
||||||
colour = CC_DEFAULT;
|
|
||||||
string = GetString(STR_NETWORK_MESSAGE_CLIENT_COMPANY_NEW, name, std::move(data));
|
|
||||||
break;
|
|
||||||
case NETWORK_ACTION_JOIN:
|
|
||||||
/* Show the Client ID for the server but not for the client. */
|
|
||||||
string = _network_server ?
|
|
||||||
GetString(STR_NETWORK_MESSAGE_CLIENT_JOINED_ID, name, std::move(data)) :
|
|
||||||
GetString(STR_NETWORK_MESSAGE_CLIENT_JOINED, name);
|
|
||||||
break;
|
|
||||||
case NETWORK_ACTION_LEAVE: string = GetString(STR_NETWORK_MESSAGE_CLIENT_LEFT, name, std::move(data)); break;
|
|
||||||
case NETWORK_ACTION_NAME_CHANGE: string = GetString(STR_NETWORK_MESSAGE_NAME_CHANGE, name, str); break;
|
|
||||||
case NETWORK_ACTION_GIVE_MONEY: string = GetString(STR_NETWORK_MESSAGE_GIVE_MONEY, name, std::move(data), str); break;
|
|
||||||
case NETWORK_ACTION_KICKED: string = GetString(STR_NETWORK_MESSAGE_KICKED, name, str); break;
|
|
||||||
case NETWORK_ACTION_CHAT_COMPANY: string = GetString(self_send ? STR_NETWORK_CHAT_TO_COMPANY : STR_NETWORK_CHAT_COMPANY, name, str); break;
|
|
||||||
case NETWORK_ACTION_CHAT_CLIENT: string = GetString(self_send ? STR_NETWORK_CHAT_TO_CLIENT : STR_NETWORK_CHAT_CLIENT, name, str); break;
|
|
||||||
case NETWORK_ACTION_EXTERNAL_CHAT: string = GetString(STR_NETWORK_CHAT_EXTERNAL, std::move(data), name, str); break;
|
|
||||||
default: string = GetString(STR_NETWORK_CHAT_ALL, name, str); break;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* All of these strings start with "***". These characters are interpreted as both left-to-right and
|
/* All of these strings start with "***". These characters are interpreted as both left-to-right and
|
||||||
* right-to-left characters depending on the context. As the next text might be an user's name, the
|
* right-to-left characters depending on the context. As the next text might be an user's name, the
|
||||||
* user name's characters will influence the direction of the "***" instead of the language setting
|
* user name's characters will influence the direction of the "***" instead of the language setting
|
||||||
* of the game. Manually set the direction of the "***" by inserting a text-direction marker. */
|
* of the game. Manually set the direction of the "***" by inserting a text-direction marker. */
|
||||||
std::ostringstream stream;
|
builder.PutUtf8(_current_text_dir == TD_LTR ? CHAR_TD_LRM : CHAR_TD_RLM);
|
||||||
std::ostreambuf_iterator<char> iterator(stream);
|
|
||||||
Utf8Encode(iterator, _current_text_dir == TD_LTR ? CHAR_TD_LRM : CHAR_TD_RLM);
|
switch (action) {
|
||||||
std::string message = stream.str() + string;
|
case NETWORK_ACTION_SERVER_MESSAGE:
|
||||||
|
/* Ignore invalid messages */
|
||||||
|
builder += GetString(STR_NETWORK_SERVER_MESSAGE, str);
|
||||||
|
colour = CC_DEFAULT;
|
||||||
|
break;
|
||||||
|
case NETWORK_ACTION_COMPANY_SPECTATOR:
|
||||||
|
colour = CC_DEFAULT;
|
||||||
|
builder += GetString(STR_NETWORK_MESSAGE_CLIENT_COMPANY_SPECTATE, name);
|
||||||
|
break;
|
||||||
|
case NETWORK_ACTION_COMPANY_JOIN:
|
||||||
|
colour = CC_DEFAULT;
|
||||||
|
builder += GetString(STR_NETWORK_MESSAGE_CLIENT_COMPANY_JOIN, name, str);
|
||||||
|
break;
|
||||||
|
case NETWORK_ACTION_COMPANY_NEW:
|
||||||
|
colour = CC_DEFAULT;
|
||||||
|
builder += GetString(STR_NETWORK_MESSAGE_CLIENT_COMPANY_NEW, name, std::move(data));
|
||||||
|
break;
|
||||||
|
case NETWORK_ACTION_JOIN:
|
||||||
|
/* Show the Client ID for the server but not for the client. */
|
||||||
|
builder += _network_server ?
|
||||||
|
GetString(STR_NETWORK_MESSAGE_CLIENT_JOINED_ID, name, std::move(data)) :
|
||||||
|
GetString(STR_NETWORK_MESSAGE_CLIENT_JOINED, name);
|
||||||
|
break;
|
||||||
|
case NETWORK_ACTION_LEAVE: builder += GetString(STR_NETWORK_MESSAGE_CLIENT_LEFT, name, std::move(data)); break;
|
||||||
|
case NETWORK_ACTION_NAME_CHANGE: builder += GetString(STR_NETWORK_MESSAGE_NAME_CHANGE, name, str); break;
|
||||||
|
case NETWORK_ACTION_GIVE_MONEY: builder += GetString(STR_NETWORK_MESSAGE_GIVE_MONEY, name, std::move(data), str); break;
|
||||||
|
case NETWORK_ACTION_KICKED: builder += GetString(STR_NETWORK_MESSAGE_KICKED, name, str); break;
|
||||||
|
case NETWORK_ACTION_CHAT_COMPANY: builder += GetString(self_send ? STR_NETWORK_CHAT_TO_COMPANY : STR_NETWORK_CHAT_COMPANY, name, str); break;
|
||||||
|
case NETWORK_ACTION_CHAT_CLIENT: builder += GetString(self_send ? STR_NETWORK_CHAT_TO_CLIENT : STR_NETWORK_CHAT_CLIENT, name, str); break;
|
||||||
|
case NETWORK_ACTION_EXTERNAL_CHAT: builder += GetString(STR_NETWORK_CHAT_EXTERNAL, std::move(data), name, str); break;
|
||||||
|
default: builder += GetString(STR_NETWORK_CHAT_ALL, name, str); break;
|
||||||
|
}
|
||||||
|
|
||||||
Debug(desync, 1, "msg: {:08x}; {:02x}; {}", TimerGameEconomy::date, TimerGameEconomy::date_fract, message);
|
Debug(desync, 1, "msg: {:08x}; {:02x}; {}", TimerGameEconomy::date, TimerGameEconomy::date_fract, message);
|
||||||
IConsolePrint(colour, message);
|
IConsolePrint(colour, message);
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
#include "../stdafx.h"
|
#include "../stdafx.h"
|
||||||
#include "../string_func.h"
|
#include "../string_func.h"
|
||||||
#include "../strings_func.h"
|
#include "../strings_func.h"
|
||||||
|
#include "../core/string_builder.hpp"
|
||||||
#include "saveload_internal.h"
|
#include "saveload_internal.h"
|
||||||
|
|
||||||
#include "table/strings.h"
|
#include "table/strings.h"
|
||||||
|
@ -63,8 +64,8 @@ std::string CopyFromOldName(StringID id)
|
||||||
if (IsSavegameVersionBefore(SLV_37)) {
|
if (IsSavegameVersionBefore(SLV_37)) {
|
||||||
const std::string &strfrom = _old_name_array[GB(id, 0, 9)];
|
const std::string &strfrom = _old_name_array[GB(id, 0, 9)];
|
||||||
|
|
||||||
std::ostringstream tmp;
|
std::string result;
|
||||||
std::ostreambuf_iterator<char> strto(tmp);
|
StringBuilder builder(result);
|
||||||
for (char s : strfrom) {
|
for (char s : strfrom) {
|
||||||
if (s == '\0') break;
|
if (s == '\0') break;
|
||||||
char32_t c = static_cast<uint8_t>(s); // cast to unsigned before integer promotion
|
char32_t c = static_cast<uint8_t>(s); // cast to unsigned before integer promotion
|
||||||
|
@ -82,10 +83,10 @@ std::string CopyFromOldName(StringID id)
|
||||||
default: break;
|
default: break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (IsPrintable(c)) Utf8Encode(strto, c);
|
if (IsPrintable(c)) builder.PutUtf8(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
return tmp.str();
|
return result;
|
||||||
} else {
|
} else {
|
||||||
/* Name will already be in UTF-8. */
|
/* Name will already be in UTF-8. */
|
||||||
return StrMakeValid(_old_name_array[GB(id, 0, 9)]);
|
return StrMakeValid(_old_name_array[GB(id, 0, 9)]);
|
||||||
|
|
|
@ -2371,13 +2371,12 @@ void CheckForMissingGlyphs(bool base_font, MissingGlyphSearcher *searcher)
|
||||||
if (!bad_font && any_font_configured) {
|
if (!bad_font && any_font_configured) {
|
||||||
/* If the user configured a bad font, and we found a better one,
|
/* If the user configured a bad font, and we found a better one,
|
||||||
* show that we loaded the better font instead of the configured one.
|
* show that we loaded the better font instead of the configured one.
|
||||||
* The colour 'character' might change in the
|
*/
|
||||||
* future, so for safety we just Utf8 Encode it into the string,
|
std::string err_str;
|
||||||
* which takes exactly three characters, so it replaces the "XXX"
|
StringBuilder builder(err_str);
|
||||||
* with the colour marker. */
|
builder.PutUtf8(SCC_YELLOW);
|
||||||
static std::string err_str("XXXThe current font is missing some of the characters used in the texts for this language. Using system fallback font instead.");
|
builder.Put("The current font is missing some of the characters used in the texts for this language. Using system fallback font instead.");
|
||||||
Utf8Encode(err_str.data(), SCC_YELLOW);
|
ShowErrorMessage(GetEncodedString(STR_JUST_RAW_STRING, std::move(err_str)), {}, WL_WARNING);
|
||||||
ShowErrorMessage(GetEncodedString(STR_JUST_RAW_STRING, err_str), {}, WL_WARNING);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bad_font && base_font) {
|
if (bad_font && base_font) {
|
||||||
|
@ -2393,11 +2392,12 @@ void CheckForMissingGlyphs(bool base_font, MissingGlyphSearcher *searcher)
|
||||||
/* All attempts have failed. Display an error. As we do not want the string to be translated by
|
/* All attempts have failed. Display an error. As we do not want the string to be translated by
|
||||||
* the translators, we 'force' it into the binary and 'load' it via a BindCString. To do this
|
* the translators, we 'force' it into the binary and 'load' it via a BindCString. To do this
|
||||||
* properly we have to set the colour of the string, otherwise we end up with a lot of artifacts.
|
* properly we have to set the colour of the string, otherwise we end up with a lot of artifacts.
|
||||||
* The colour 'character' might change in the future, so for safety we just Utf8 Encode it into
|
*/
|
||||||
* the string, which takes exactly three characters, so it replaces the "XXX" with the colour marker. */
|
std::string err_str;
|
||||||
static std::string err_str("XXXThe current font is missing some of the characters used in the texts for this language. Go to Help & Manuals > Fonts, or read the file docs/fonts.md in your OpenTTD directory, to see how to solve this.");
|
StringBuilder builder(err_str);
|
||||||
Utf8Encode(err_str.data(), SCC_YELLOW);
|
builder.PutUtf8(SCC_YELLOW);
|
||||||
ShowErrorMessage(GetEncodedString(STR_JUST_RAW_STRING, err_str), {}, WL_WARNING);
|
builder.Put("The current font is missing some of the characters used in the texts for this language. Go to Help & Manuals > Fonts, or read the file docs/fonts.md in your OpenTTD directory, to see how to solve this.");
|
||||||
|
ShowErrorMessage(GetEncodedString(STR_JUST_RAW_STRING, std::move(err_str)), {}, WL_WARNING);
|
||||||
|
|
||||||
/* Reset the font width */
|
/* Reset the font width */
|
||||||
LoadStringWidthTable(searcher->Monospace());
|
LoadStringWidthTable(searcher->Monospace());
|
||||||
|
@ -2415,16 +2415,14 @@ void CheckForMissingGlyphs(bool base_font, MissingGlyphSearcher *searcher)
|
||||||
* be translated by the translators, we 'force' it into the
|
* be translated by the translators, we 'force' it into the
|
||||||
* binary and 'load' it via a BindCString. To do this
|
* binary and 'load' it via a BindCString. To do this
|
||||||
* properly we have to set the colour of the string,
|
* properly we have to set the colour of the string,
|
||||||
* otherwise we end up with a lot of artifacts. The colour
|
* otherwise we end up with a lot of artifacts.
|
||||||
* 'character' might change in the future, so for safety
|
|
||||||
* we just Utf8 Encode it into the string, which takes
|
|
||||||
* exactly three characters, so it replaces the "XXX" with
|
|
||||||
* the colour marker.
|
|
||||||
*/
|
*/
|
||||||
if (_current_text_dir != TD_LTR) {
|
if (_current_text_dir != TD_LTR) {
|
||||||
static std::string err_str("XXXThis version of OpenTTD does not support right-to-left languages. Recompile with ICU + Harfbuzz enabled.");
|
std::string err_str;
|
||||||
Utf8Encode(err_str.data(), SCC_YELLOW);
|
StringBuilder builder(err_str);
|
||||||
ShowErrorMessage(GetEncodedString(STR_JUST_RAW_STRING, err_str), {}, WL_ERROR);
|
builder.PutUtf8(SCC_YELLOW);
|
||||||
|
builder.Put("This version of OpenTTD does not support right-to-left languages. Recompile with ICU + Harfbuzz enabled.");
|
||||||
|
ShowErrorMessage(GetEncodedString(STR_JUST_RAW_STRING, std::move(err_str)), {}, WL_ERROR);
|
||||||
}
|
}
|
||||||
#endif /* !(WITH_ICU_I18N && WITH_HARFBUZZ) && !WITH_UNISCRIBE && !WITH_COCOA */
|
#endif /* !(WITH_ICU_I18N && WITH_HARFBUZZ) && !WITH_UNISCRIBE && !WITH_COCOA */
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,7 @@
|
||||||
|
|
||||||
#include "../string_func.h"
|
#include "../string_func.h"
|
||||||
#include "../strings_func.h"
|
#include "../strings_func.h"
|
||||||
|
#include "../core/string_builder.hpp"
|
||||||
#include "../table/control_codes.h"
|
#include "../table/control_codes.h"
|
||||||
|
|
||||||
#include "table/strings.h"
|
#include "table/strings.h"
|
||||||
|
@ -423,15 +424,15 @@ static std::string FixSCCEncodedWrapper(const std::string &str, bool fix_code)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Helper to compose a string part from a unicode character */
|
/* Helper to compose a string part from a unicode character */
|
||||||
static void ComposePart(std::back_insert_iterator<std::string> &output, char32_t c)
|
static void ComposePart(StringBuilder &builder, char32_t c)
|
||||||
{
|
{
|
||||||
Utf8Encode(output, c);
|
builder.PutUtf8(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Helper to compose a string part from a string. */
|
/* Helper to compose a string part from a string. */
|
||||||
static void ComposePart(std::back_insert_iterator<std::string> &output, const std::string &value)
|
static void ComposePart(StringBuilder &builder, const std::string &value)
|
||||||
{
|
{
|
||||||
for (const auto &c : value) *output = c;
|
builder += value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Helper to compose a string from unicode or string parts. */
|
/* Helper to compose a string from unicode or string parts. */
|
||||||
|
@ -439,8 +440,8 @@ template <typename... Args>
|
||||||
static std::string Compose(Args &&... args)
|
static std::string Compose(Args &&... args)
|
||||||
{
|
{
|
||||||
std::string result;
|
std::string result;
|
||||||
auto output = std::back_inserter(result);
|
StringBuilder builder(result);
|
||||||
(ComposePart(output, args), ...);
|
(ComposePart(builder, args), ...);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
#include "gfx_type.h"
|
#include "gfx_type.h"
|
||||||
#include "gfx_func.h"
|
#include "gfx_func.h"
|
||||||
#include "string_func.h"
|
#include "string_func.h"
|
||||||
|
#include "core/string_builder.hpp"
|
||||||
#include "textfile_gui.h"
|
#include "textfile_gui.h"
|
||||||
#include "dropdown_type.h"
|
#include "dropdown_type.h"
|
||||||
#include "dropdown_func.h"
|
#include "dropdown_func.h"
|
||||||
|
@ -245,7 +246,7 @@ void TextfileWindow::FindHyperlinksInMarkdown(Line &line, size_t line_index)
|
||||||
{
|
{
|
||||||
std::string::const_iterator last_match_end = line.text.cbegin();
|
std::string::const_iterator last_match_end = line.text.cbegin();
|
||||||
std::string fixed_line;
|
std::string fixed_line;
|
||||||
char ccbuf[5];
|
StringBuilder builder(fixed_line);
|
||||||
|
|
||||||
std::sregex_iterator matcher{ line.text.cbegin(), line.text.cend(), _markdown_link_regex};
|
std::sregex_iterator matcher{ line.text.cbegin(), line.text.cend(), _markdown_link_regex};
|
||||||
while (matcher != std::sregex_iterator()) {
|
while (matcher != std::sregex_iterator()) {
|
||||||
|
@ -273,13 +274,13 @@ void TextfileWindow::FindHyperlinksInMarkdown(Line &line, size_t line_index)
|
||||||
|
|
||||||
if (link_colour != SCC_CONTROL_END) {
|
if (link_colour != SCC_CONTROL_END) {
|
||||||
/* Format the link to look like a link. */
|
/* Format the link to look like a link. */
|
||||||
fixed_line += std::string(last_match_end, match[0].first);
|
builder += std::string_view(last_match_end, match[0].first);
|
||||||
link.begin = fixed_line.length();
|
link.begin = fixed_line.length();
|
||||||
fixed_line += std::string(ccbuf, Utf8Encode(ccbuf, SCC_PUSH_COLOUR));
|
builder.PutUtf8(SCC_PUSH_COLOUR);
|
||||||
fixed_line += std::string(ccbuf, Utf8Encode(ccbuf, link_colour));
|
builder.PutUtf8(link_colour);
|
||||||
fixed_line += match[1].str();
|
builder += match[1].str();
|
||||||
link.end = fixed_line.length();
|
link.end = fixed_line.length();
|
||||||
fixed_line += std::string(ccbuf, Utf8Encode(ccbuf, SCC_POP_COLOUR));
|
builder.PutUtf8(SCC_POP_COLOUR);
|
||||||
last_match_end = match[0].second;
|
last_match_end = match[0].second;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue