mirror of
https://github.com/wesnoth/wesnoth
synced 2025-04-22 16:40:25 +00:00
deploy use of std::string_view
This commit is contained in:
parent
e28217b213
commit
2dcfe2435c
@ -159,7 +159,7 @@ bool addons_client::upload_addon(const std::string& id, std::string& response_me
|
|||||||
response_message.clear();
|
response_message.clear();
|
||||||
|
|
||||||
utils::string_map i18n_symbols;
|
utils::string_map i18n_symbols;
|
||||||
i18n_symbols["addon_title"] = font::escape_text(cfg["title"]);
|
i18n_symbols["addon_title"] = font::escape_text(cfg["title"].str());
|
||||||
if(i18n_symbols["addon_title"].empty()) {
|
if(i18n_symbols["addon_title"].empty()) {
|
||||||
i18n_symbols["addon_title"] = font::escape_text(make_addon_title(id));
|
i18n_symbols["addon_title"] = font::escape_text(make_addon_title(id));
|
||||||
}
|
}
|
||||||
@ -275,7 +275,7 @@ bool addons_client::delete_remote_addon(const std::string& id, std::string& resp
|
|||||||
config cfg = get_addon_pbl_info(id, false);
|
config cfg = get_addon_pbl_info(id, false);
|
||||||
|
|
||||||
utils::string_map i18n_symbols;
|
utils::string_map i18n_symbols;
|
||||||
i18n_symbols["addon_title"] = font::escape_text(cfg["title"]);
|
i18n_symbols["addon_title"] = font::escape_text(cfg["title"].str());
|
||||||
if(i18n_symbols["addon_title"].empty()) {
|
if(i18n_symbols["addon_title"].empty()) {
|
||||||
i18n_symbols["addon_title"] = font::escape_text(make_addon_title(id));
|
i18n_symbols["addon_title"] = font::escape_text(make_addon_title(id));
|
||||||
}
|
}
|
||||||
|
@ -30,7 +30,7 @@ namespace font
|
|||||||
*
|
*
|
||||||
* @returns The escaped text.
|
* @returns The escaped text.
|
||||||
*/
|
*/
|
||||||
inline std::string escape_text(const std::string& text)
|
inline std::string escape_text(std::string_view text)
|
||||||
{
|
{
|
||||||
std::ostringstream ss;
|
std::ostringstream ss;
|
||||||
for(const char c : text) {
|
for(const char c : text) {
|
||||||
@ -49,7 +49,7 @@ inline std::string escape_text(const std::string& text)
|
|||||||
|
|
||||||
// Escape only the ampersands. This is used by pango_text to try to recover from
|
// Escape only the ampersands. This is used by pango_text to try to recover from
|
||||||
// markup parsing failure.
|
// markup parsing failure.
|
||||||
inline std::string semi_escape_text(const std::string & text)
|
inline std::string semi_escape_text(std::string_view text)
|
||||||
{
|
{
|
||||||
std::ostringstream ss;
|
std::ostringstream ss;
|
||||||
for(const char c : text) {
|
for(const char c : text) {
|
||||||
|
@ -1065,7 +1065,7 @@ bool pango_text::validate_markup(std::string_view text, char** raw_text, std::st
|
|||||||
* So only try to recover from broken ampersands, by simply replacing them
|
* So only try to recover from broken ampersands, by simply replacing them
|
||||||
* with the escaped version.
|
* with the escaped version.
|
||||||
*/
|
*/
|
||||||
semi_escaped = semi_escape_text(std::string(text));
|
semi_escaped = semi_escape_text(text);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* If at least one ampersand is replaced the semi-escaped string
|
* If at least one ampersand is replaced the semi-escaped string
|
||||||
|
@ -112,7 +112,7 @@ std::string make_game_type_marker(const std::string& text, bool color_for_missin
|
|||||||
game_info::game_info(const config& game, const std::vector<std::string>& installed_addons)
|
game_info::game_info(const config& game, const std::vector<std::string>& installed_addons)
|
||||||
: id(game["id"].to_int())
|
: id(game["id"].to_int())
|
||||||
, map_data(game["map_data"])
|
, map_data(game["map_data"])
|
||||||
, name(font::escape_text(game["name"]))
|
, name(font::escape_text(game["name"].str()))
|
||||||
, scenario()
|
, scenario()
|
||||||
, type_marker()
|
, type_marker()
|
||||||
, remote_scenario(false)
|
, remote_scenario(false)
|
||||||
|
@ -168,7 +168,7 @@ static config unit_name(const unit *u)
|
|||||||
* The name needs to be escaped, it might be set by the user and using
|
* The name needs to be escaped, it might be set by the user and using
|
||||||
* markup. Also names often contain a forbidden single quote.
|
* markup. Also names often contain a forbidden single quote.
|
||||||
*/
|
*/
|
||||||
const std::string& name = font::escape_text(u->name());
|
const std::string& name = font::escape_text(u->name().str());
|
||||||
std::ostringstream str, tooltip;
|
std::ostringstream str, tooltip;
|
||||||
str << markup::bold(name);
|
str << markup::bold(name);
|
||||||
tooltip << _("Name: ") << markup::bold(name);
|
tooltip << _("Name: ") << markup::bold(name);
|
||||||
|
@ -413,14 +413,14 @@ int apply_modifier( const int number, const std::string &amount, const int minim
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string escape(const std::string &str, const char *special_chars)
|
std::string escape(std::string_view str, const char *special_chars)
|
||||||
{
|
{
|
||||||
std::string::size_type pos = str.find_first_of(special_chars);
|
std::string::size_type pos = str.find_first_of(special_chars);
|
||||||
if (pos == std::string::npos) {
|
if (pos == std::string::npos) {
|
||||||
// Fast path, possibly involving only reference counting.
|
// Fast path, possibly involving only reference counting.
|
||||||
return str;
|
return std::string(str);
|
||||||
}
|
}
|
||||||
std::string res = str;
|
std::string res = std::string(str);
|
||||||
do {
|
do {
|
||||||
res.insert(pos, 1, '\\');
|
res.insert(pos, 1, '\\');
|
||||||
pos = res.find_first_of(special_chars, pos + 2);
|
pos = res.find_first_of(special_chars, pos + 2);
|
||||||
@ -428,14 +428,14 @@ std::string escape(const std::string &str, const char *special_chars)
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string unescape(const std::string &str)
|
std::string unescape(std::string_view str)
|
||||||
{
|
{
|
||||||
std::string::size_type pos = str.find('\\');
|
std::string::size_type pos = str.find('\\');
|
||||||
if (pos == std::string::npos) {
|
if (pos == std::string::npos) {
|
||||||
// Fast path, possibly involving only reference counting.
|
// Fast path, possibly involving only reference counting.
|
||||||
return str;
|
return std::string(str);
|
||||||
}
|
}
|
||||||
std::string res = str;
|
std::string res = std::string(str);
|
||||||
do {
|
do {
|
||||||
res.erase(pos, 1);
|
res.erase(pos, 1);
|
||||||
pos = res.find('\\', pos + 1);
|
pos = res.find('\\', pos + 1);
|
||||||
@ -443,7 +443,7 @@ std::string unescape(const std::string &str)
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string urlencode(const std::string &str)
|
std::string urlencode(std::string_view str)
|
||||||
{
|
{
|
||||||
static const std::string nonresv_str =
|
static const std::string nonresv_str =
|
||||||
"-."
|
"-."
|
||||||
@ -837,7 +837,7 @@ namespace
|
|||||||
* If str contains two elements and a separator such as "a-b", returns a and b.
|
* If str contains two elements and a separator such as "a-b", returns a and b.
|
||||||
* Otherwise, returns the original string and utils::nullopt.
|
* Otherwise, returns the original string and utils::nullopt.
|
||||||
*/
|
*/
|
||||||
std::pair<std::string, utils::optional<std::string>> parse_range_internal_separator(const std::string& str)
|
std::pair<std::string_view, utils::optional<std::string_view>> parse_range_internal_separator(std::string_view str)
|
||||||
{
|
{
|
||||||
// If turning this into a list with additional options, ensure that "-" (if present) is last. Otherwise a
|
// If turning this into a list with additional options, ensure that "-" (if present) is last. Otherwise a
|
||||||
// range such as "-2..-1" might be incorrectly split as "-2..", "1".
|
// range such as "-2..-1" might be incorrectly split as "-2..", "1".
|
||||||
@ -857,7 +857,7 @@ std::pair<std::string, utils::optional<std::string>> parse_range_internal_separa
|
|||||||
}
|
}
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
std::pair<int, int> parse_range(const std::string& str)
|
std::pair<int, int> parse_range(std::string_view str)
|
||||||
{
|
{
|
||||||
auto [a, b] = parse_range_internal_separator(str);
|
auto [a, b] = parse_range_internal_separator(str);
|
||||||
std::pair<int, int> res{0, 0};
|
std::pair<int, int> res{0, 0};
|
||||||
@ -887,7 +887,7 @@ std::pair<int, int> parse_range(const std::string& str)
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::pair<double, double> parse_range_real(const std::string& str)
|
std::pair<double, double> parse_range_real(std::string_view str)
|
||||||
{
|
{
|
||||||
auto [a, b] = parse_range_internal_separator(str);
|
auto [a, b] = parse_range_internal_separator(str);
|
||||||
std::pair<double, double> res{0, 0};
|
std::pair<double, double> res{0, 0};
|
||||||
|
@ -287,7 +287,7 @@ std::string indent(const std::string& string, std::size_t indent_size = 4);
|
|||||||
* * Although "-infinity--1", "2-infinity" and "-infinity-infinity" are all supported,
|
* * Although "-infinity--1", "2-infinity" and "-infinity-infinity" are all supported,
|
||||||
* * ranges that can't match a reasonable number, e.g. "-infinity" or "infinity..infinity", may be treated as errors.
|
* * ranges that can't match a reasonable number, e.g. "-infinity" or "infinity..infinity", may be treated as errors.
|
||||||
*/
|
*/
|
||||||
std::pair<int, int> parse_range(const std::string& str);
|
std::pair<int, int> parse_range(std::string_view str);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles a comma-separated list of inputs to parse_range, in a context that does not expect
|
* Handles a comma-separated list of inputs to parse_range, in a context that does not expect
|
||||||
@ -306,7 +306,7 @@ std::vector<std::pair<int, int>> parse_ranges_int(const std::string& str);
|
|||||||
*
|
*
|
||||||
* For this function, "infinity" results in std::numeric_limits<double>::infinity.
|
* For this function, "infinity" results in std::numeric_limits<double>::infinity.
|
||||||
*/
|
*/
|
||||||
std::pair<double, double> parse_range_real(const std::string& str);
|
std::pair<double, double> parse_range_real(std::string_view str);
|
||||||
|
|
||||||
std::vector<std::pair<double, double>> parse_ranges_real(const std::string& str);
|
std::vector<std::pair<double, double>> parse_ranges_real(const std::string& str);
|
||||||
|
|
||||||
@ -319,7 +319,7 @@ inline std::string print_modifier(const std::string &mod)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** Prepends a configurable set of characters with a backslash */
|
/** Prepends a configurable set of characters with a backslash */
|
||||||
std::string escape(const std::string &str, const char *special_chars);
|
std::string escape(std::string_view str, const char *special_chars);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prepend all special characters with a backslash.
|
* Prepend all special characters with a backslash.
|
||||||
@ -327,21 +327,21 @@ std::string escape(const std::string &str, const char *special_chars);
|
|||||||
* Special characters are:
|
* Special characters are:
|
||||||
* #@{}+-,\*=
|
* #@{}+-,\*=
|
||||||
*/
|
*/
|
||||||
inline std::string escape(const std::string &str)
|
inline std::string escape(std::string_view str)
|
||||||
{
|
{
|
||||||
return escape(str, "#@{}+-,\\*=");
|
return escape(str, "#@{}+-,\\*=");
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Remove all escape characters (backslash) */
|
/** Remove all escape characters (backslash) */
|
||||||
std::string unescape(const std::string &str);
|
std::string unescape(std::string_view str);
|
||||||
|
|
||||||
/** Percent-escape characters in a UTF-8 string intended to be part of a URL. */
|
/** Percent-escape characters in a UTF-8 string intended to be part of a URL. */
|
||||||
std::string urlencode(const std::string &str);
|
std::string urlencode(std::string_view str);
|
||||||
|
|
||||||
/** Surround the string 'str' with double quotes. */
|
/** Surround the string 'str' with double quotes. */
|
||||||
inline std::string quote(const std::string &str)
|
inline std::string quote(std::string_view str)
|
||||||
{
|
{
|
||||||
return '"' + str + '"';
|
return '"' + std::string(str) + '"';
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Convert no, false, off, 0, 0.0 to false, empty to def, and others to true */
|
/** Convert no, false, off, 0, 0.0 to false, empty to def, and others to true */
|
||||||
|
@ -47,7 +47,7 @@ static int byte_size_from_utf8_first(const unsigned char ch)
|
|||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string lowercase(const std::string& s)
|
std::string lowercase(std::string_view s)
|
||||||
{
|
{
|
||||||
if(!s.empty()) {
|
if(!s.empty()) {
|
||||||
utf8::iterator itor(s);
|
utf8::iterator itor(s);
|
||||||
@ -64,10 +64,10 @@ std::string lowercase(const std::string& s)
|
|||||||
res.append(itor.substr().second, s.end());
|
res.append(itor.substr().second, s.end());
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
return s;
|
return std::string();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::size_t index(const std::string& str, const std::size_t index)
|
std::size_t index(std::string_view str, const std::size_t index)
|
||||||
{
|
{
|
||||||
// chr counts characters, i is the codepoint index
|
// chr counts characters, i is the codepoint index
|
||||||
// remark: several functions rely on the fallback to str.length()
|
// remark: several functions rely on the fallback to str.length()
|
||||||
@ -82,7 +82,7 @@ std::size_t index(const std::string& str, const std::size_t index)
|
|||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::size_t size(const std::string& str)
|
std::size_t size(std::string_view str)
|
||||||
{
|
{
|
||||||
unsigned int chr, i = 0, len = str.size();
|
unsigned int chr, i = 0, len = str.size();
|
||||||
try {
|
try {
|
||||||
|
@ -38,20 +38,20 @@ namespace utf16 {
|
|||||||
* back and forth.
|
* back and forth.
|
||||||
*/
|
*/
|
||||||
namespace utf8 {
|
namespace utf8 {
|
||||||
typedef ucs4::iterator_base<std::string, ucs4_convert_impl::convert_impl<char>::type> iterator;
|
typedef ucs4::iterator_base<std::string_view, ucs4_convert_impl::convert_impl<char>::type> iterator;
|
||||||
|
|
||||||
/** Returns a lowercased version of the string. */
|
/** Returns a lowercased version of the string. */
|
||||||
std::string lowercase(const std::string& s);
|
std::string lowercase(std::string_view s);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Codepoint index corresponding to the nth character in a UTF-8 string.
|
* Codepoint index corresponding to the nth character in a UTF-8 string.
|
||||||
*
|
*
|
||||||
* @return str.length() if there are less than @p index characters.
|
* @return str.length() if there are less than @p index characters.
|
||||||
*/
|
*/
|
||||||
std::size_t index(const std::string& str, const std::size_t index);
|
std::size_t index(std::string_view str, const std::size_t index);
|
||||||
|
|
||||||
/** Length in characters of a UTF-8 string. */
|
/** Length in characters of a UTF-8 string. */
|
||||||
std::size_t size(const std::string& str);
|
std::size_t size(std::string_view str);
|
||||||
|
|
||||||
/** Insert a UTF-8 string at the specified position. */
|
/** Insert a UTF-8 string at the specified position. */
|
||||||
std::string& insert(std::string& str, const std::size_t pos, const std::string& insert) ;
|
std::string& insert(std::string& str, const std::size_t pos, const std::string& insert) ;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user