cleanup: don't pass std::string_view by reference

by value is the same on MSVC, may be marginally better elsewhere

see:
https://quuxplusone.github.io/blog/2021/11/09/pass-string-view-by-value/
https://quuxplusone.github.io/blog/2021/11/19/string-view-by-value-ps/
This commit is contained in:
Eric Wasylishen 2024-11-17 21:52:10 -07:00
parent 9c016b0e3a
commit 1337919805
16 changed files with 59 additions and 59 deletions

View File

@ -185,7 +185,7 @@ public:
return (a.native & TEX_SPECIAL) == (b.native & TEX_SPECIAL);
}
int32_t surfflags_from_string(const std::string_view &str) const override
int32_t surfflags_from_string(std::string_view str) const override
{
if (string_iequals(str, "special")) {
return TEX_SPECIAL;
@ -405,7 +405,7 @@ public:
return true;
}
int32_t contents_from_string(const std::string_view &str) const override
int32_t contents_from_string(std::string_view str) const override
{
// Q1 doesn't get contents from files
return 0;
@ -829,7 +829,7 @@ struct gamedef_q2_t : public gamedef_t
"FLOWING", "NODRAW", "HINT", "512", "1024", "2048", "4096", "8192", "16384", "32768", "65536", "131072",
"262144", "524288", "1048576", "2097152", "4194304", "8388608", "16777216", "ALPHATEST"};
int32_t surfflags_from_string(const std::string_view &str) const override
int32_t surfflags_from_string(std::string_view str) const override
{
for (size_t i = 0; i < std::size(surf_bitflag_names); i++) {
if (string_iequals(str, surf_bitflag_names[i])) {
@ -1129,7 +1129,7 @@ struct gamedef_q2_t : public gamedef_t
"CURRENT_90", "CURRENT_180", "CURRENT_270", "CURRENT_UP", "CURRENT_DOWN", "ORIGIN", "MONSTER", "DEADMONSTER",
"DETAIL", "TRANSLUCENT", "LADDER", "1073741824", "2147483648"};
int32_t contents_from_string(const std::string_view &str) const override
int32_t contents_from_string(std::string_view str) const override
{
for (size_t i = 0; i < std::size(bitflag_names); i++) {
if (string_iequals(str, bitflag_names[i])) {

View File

@ -45,7 +45,7 @@
#include <strings.h>
#endif
int32_t Q_strncasecmp(const std::string_view &a, const std::string_view &b, size_t maxcount)
int32_t Q_strncasecmp(std::string_view a, std::string_view b, size_t maxcount)
{
return
#ifdef _WIN32
@ -58,7 +58,7 @@ int32_t Q_strncasecmp(const std::string_view &a, const std::string_view &b, size
(a.data(), b.data(), maxcount);
}
int32_t Q_strcasecmp(const std::string_view &a, const std::string_view &b)
int32_t Q_strcasecmp(std::string_view a, std::string_view b)
{
return
#ifdef _WIN32
@ -86,7 +86,7 @@ string_replaceall(std::string &str, const std::string &from, const std::string &
}
bool // mxd
string_iequals(const std::string_view &a, const std::string_view &b)
string_iequals(std::string_view a, std::string_view b)
{
size_t sz = a.size();
if (b.size() != sz)

View File

@ -38,7 +38,7 @@ entdict_t::entdict_t(parser_base_t &parser)
parse(parser);
}
const std::string &entdict_t::get(const std::string_view &key) const
const std::string &entdict_t::get(std::string_view key) const
{
if (auto it = find(key); it != keyvalues.end()) {
return it->second;
@ -48,7 +48,7 @@ const std::string &entdict_t::get(const std::string_view &key) const
return empty;
}
double entdict_t::get_float(const std::string_view &key) const
double entdict_t::get_float(std::string_view key) const
{
const std::string &s = get(key);
@ -59,7 +59,7 @@ double entdict_t::get_float(const std::string_view &key) const
return atof(s.data());
}
int32_t entdict_t::get_int(const std::string_view &key) const
int32_t entdict_t::get_int(std::string_view key) const
{
const std::string &s = get(key);
@ -70,7 +70,7 @@ int32_t entdict_t::get_int(const std::string_view &key) const
return atoi(s.data());
}
int32_t entdict_t::get_vector(const std::string_view &key, qvec3f &vec) const
int32_t entdict_t::get_vector(std::string_view key, qvec3f &vec) const
{
std::string value = get(key);
@ -83,7 +83,7 @@ int32_t entdict_t::get_vector(const std::string_view &key, qvec3f &vec) const
return sscanf(value.data(), "%f %f %f", &vec[0], &vec[1], &vec[2]);
}
void entdict_t::set(const std::string_view &key, const std::string_view &value)
void entdict_t::set(std::string_view key, std::string_view value)
{
// search for existing key to update
if (auto it = find(key); it != keyvalues.end()) {
@ -96,14 +96,14 @@ void entdict_t::set(const std::string_view &key, const std::string_view &value)
keyvalues.emplace_back(key, value);
}
void entdict_t::remove(const std::string_view &key)
void entdict_t::remove(std::string_view key)
{
if (auto it = find(key); it != keyvalues.end()) {
keyvalues.erase(it);
}
}
void entdict_t::rename(const std::string_view &from, const std::string_view &to)
void entdict_t::rename(std::string_view from, std::string_view to)
{
const auto it = find(from);
if (it != end()) {
@ -113,7 +113,7 @@ void entdict_t::rename(const std::string_view &from, const std::string_view &to)
}
}
keyvalues_t::iterator entdict_t::find(const std::string_view &key)
keyvalues_t::iterator entdict_t::find(std::string_view key)
{
auto existingIt = keyvalues.end();
for (auto it = keyvalues.begin(); it != keyvalues.end(); ++it) {
@ -125,7 +125,7 @@ keyvalues_t::iterator entdict_t::find(const std::string_view &key)
return existingIt;
}
keyvalues_t::const_iterator entdict_t::find(const std::string_view &key) const
keyvalues_t::const_iterator entdict_t::find(std::string_view key) const
{
auto existingIt = keyvalues.end();
for (auto it = keyvalues.begin(); it != keyvalues.end(); ++it) {
@ -137,7 +137,7 @@ keyvalues_t::const_iterator entdict_t::find(const std::string_view &key) const
return existingIt;
}
bool entdict_t::has(const std::string_view &key) const
bool entdict_t::has(std::string_view key) const
{
return find(key) != end();
}

View File

@ -126,7 +126,7 @@ struct q2_miptex_t
};
std::optional<texture> load_wal(
const std::string_view &name, const fs::data &file, bool meta_only, const gamedef_t *game)
std::string_view name, const fs::data &file, bool meta_only, const gamedef_t *game)
{
imemstream stream(file->data(), file->size(), std::ios_base::in | std::ios_base::binary);
stream >> endianness<std::endian::little>;
@ -167,7 +167,7 @@ Quake/Half Life MIP
*/
std::optional<texture> load_mip(
const std::string_view &name, const fs::data &file, bool meta_only, const gamedef_t *game)
std::string_view name, const fs::data &file, bool meta_only, const gamedef_t *game)
{
imemstream stream(file->data(), file->size());
stream >> endianness<std::endian::little>;
@ -256,7 +256,7 @@ std::optional<texture> load_mip(
}
std::optional<texture> load_stb(
const std::string_view &name, const fs::data &file, bool meta_only, const gamedef_t *game)
std::string_view name, const fs::data &file, bool meta_only, const gamedef_t *game)
{
int x, y, channels_in_file;
stbi_uc *rgba_data = stbi_load_from_memory(file->data(), file->size(), &x, &y, &channels_in_file, 4);
@ -294,7 +294,7 @@ std::optional<texture> load_stb(
// texture cache
std::unordered_map<std::string, texture, case_insensitive_hash, case_insensitive_equal> textures;
const texture *find(const std::string_view &str)
const texture *find(std::string_view str)
{
auto it = textures.find(str.data());
@ -326,7 +326,7 @@ qvec3b calculate_average(const std::vector<qvec4b> &pixels)
return avg /= n;
}
std::tuple<std::optional<img::texture>, fs::resolve_result, fs::data> load_texture(const std::string_view &name,
std::tuple<std::optional<img::texture>, fs::resolve_result, fs::data> load_texture(std::string_view name,
bool meta_only, const gamedef_t *game, const settings::common_settings &options, bool no_prefix, bool mip_only)
{
fs::path prefix{"textures"};
@ -360,7 +360,7 @@ std::tuple<std::optional<img::texture>, fs::resolve_result, fs::data> load_textu
return {std::nullopt, {}, {}};
}
std::optional<texture_meta> load_wal_meta(const std::string_view &name, const fs::data &file, const gamedef_t *game)
std::optional<texture_meta> load_wal_meta(std::string_view name, const fs::data &file, const gamedef_t *game)
{
if (auto tex = load_wal(name, file, true, game)) {
return tex->meta;
@ -371,7 +371,7 @@ std::optional<texture_meta> load_wal_meta(const std::string_view &name, const fs
// see .wal_json section in qbsp.rst for format documentation
std::optional<texture_meta> load_wal_json_meta(
const std::string_view &name, const fs::data &file, const gamedef_t *game)
std::string_view name, const fs::data &file, const gamedef_t *game)
{
try {
auto json = json::parse(file->begin(), file->end());
@ -459,7 +459,7 @@ std::optional<texture_meta> load_wal_json_meta(
}
std::tuple<std::optional<img::texture_meta>, fs::resolve_result, fs::data> load_texture_meta(
const std::string_view &name, const gamedef_t *game, const settings::common_settings &options)
std::string_view name, const gamedef_t *game, const settings::common_settings &options)
{
fs::path prefix;
@ -535,7 +535,7 @@ static qvec3b increase_saturation(const qvec3b &color)
// Load the specified texture from the BSP
static void AddTextureName(
const std::string_view &textureName, const mbsp_t *bsp, const settings::common_settings &options)
std::string_view textureName, const mbsp_t *bsp, const settings::common_settings &options)
{
if (img::find(textureName)) {
return;

View File

@ -1234,7 +1234,7 @@ void map_file_t::convert_to(texcoord_style_t style, const gamedef_t *game, const
}
}
map_file_t parse(const std::string_view &view, parser_source_location base_location)
map_file_t parse(std::string_view view, parser_source_location base_location)
{
parser_t parser(view, base_location);

View File

@ -67,7 +67,7 @@ parser_t::parser_t(const void *start, size_t length, parser_source_location base
{
}
parser_t::parser_t(const std::string_view &view, parser_source_location base_location)
parser_t::parser_t(std::string_view view, parser_source_location base_location)
: parser_t(&view.front(), view.size(), base_location)
{
}

View File

@ -259,7 +259,7 @@ const char *setting_base::sourceString() const
// setting_string
setting_string::setting_string(setting_container *dictionary, const nameset &names, std::string v,
const std::string_view &format, const setting_group *group, const char *description)
std::string_view format, const setting_group *group, const char *description)
: setting_value(dictionary, names, v, group, description),
_format(format)
{
@ -316,7 +316,7 @@ std::string setting_path::format() const
// setting_set
setting_set::setting_set(setting_container *dictionary, const nameset &names, const std::string_view &format,
setting_set::setting_set(setting_container *dictionary, const nameset &names, std::string_view format,
const setting_group *group, const char *description)
: setting_base(dictionary, names, group, description),
_format(format)

View File

@ -388,7 +388,7 @@ struct gamedef_t
* e.g. warping and non-warping
*/
virtual bool surfflags_may_phong(const surfflags_t &a, const surfflags_t &b) const = 0;
virtual int32_t surfflags_from_string(const std::string_view &str) const = 0;
virtual int32_t surfflags_from_string(std::string_view str) const = 0;
// FIXME: fix so that we don't have to pass a name here
virtual bool texinfo_is_hintskip(const surfflags_t &flags, const std::string &name) const = 0;
virtual contentflags_t create_contents_from_native(int32_t native) const = 0;
@ -418,7 +418,7 @@ struct gamedef_t
virtual bool contents_are_sky(contentflags_t contents) const = 0;
virtual bool contents_are_liquid(contentflags_t contents) const = 0;
virtual bool contents_are_valid(contentflags_t contents, bool strict = true) const = 0;
virtual int32_t contents_from_string(const std::string_view &str) const = 0;
virtual int32_t contents_from_string(std::string_view str) const = 0;
virtual bool portal_can_see_through(
contentflags_t contents0, contentflags_t contents1, bool transwater) const = 0;
virtual bool contents_seals_map(contentflags_t contents) const = 0;

View File

@ -30,9 +30,9 @@
#include <ostream>
#include <tuple> // for std::apply()
int32_t Q_strncasecmp(const std::string_view &a, const std::string_view &b, size_t maxcount);
int32_t Q_strcasecmp(const std::string_view &a, const std::string_view &b);
bool string_iequals(const std::string_view &a, const std::string_view &b); // mxd
int32_t Q_strncasecmp(std::string_view a, std::string_view b, size_t maxcount);
int32_t Q_strcasecmp(std::string_view a, std::string_view b);
bool string_iequals(std::string_view a, std::string_view b); // mxd
struct case_insensitive_hash
{

View File

@ -42,19 +42,19 @@ public:
entdict_t();
entdict_t(parser_base_t &parser);
const std::string &get(const std::string_view &key) const;
double get_float(const std::string_view &key) const;
int32_t get_int(const std::string_view &key) const;
const std::string &get(std::string_view key) const;
double get_float(std::string_view key) const;
int32_t get_int(std::string_view key) const;
// returns number of vector components read
int32_t get_vector(const std::string_view &key, qvec3f &out) const;
void set(const std::string_view &key, const std::string_view &value);
void remove(const std::string_view &key);
void rename(const std::string_view &from, const std::string_view &to);
int32_t get_vector(std::string_view key, qvec3f &out) const;
void set(std::string_view key, std::string_view value);
void remove(std::string_view key);
void rename(std::string_view from, std::string_view to);
keyvalues_t::iterator find(const std::string_view &key);
keyvalues_t::const_iterator find(const std::string_view &key) const;
keyvalues_t::iterator find(std::string_view key);
keyvalues_t::const_iterator find(std::string_view key) const;
bool has(const std::string_view &key) const;
bool has(std::string_view key) const;
inline keyvalues_t::const_iterator begin() const { return keyvalues.begin(); }
inline keyvalues_t::const_iterator end() const { return keyvalues.end(); }

View File

@ -89,19 +89,19 @@ void clear();
qvec3b calculate_average(const std::vector<qvec4b> &pixels);
const texture *find(const std::string_view &str);
const texture *find(std::string_view str);
// Load wal
std::optional<texture> load_wal(
const std::string_view &name, const fs::data &file, bool meta_only, const gamedef_t *game);
std::string_view name, const fs::data &file, bool meta_only, const gamedef_t *game);
// Load Quake/Half Life mip (raw data)
std::optional<texture> load_mip(
const std::string_view &name, const fs::data &file, bool meta_only, const gamedef_t *game);
std::string_view name, const fs::data &file, bool meta_only, const gamedef_t *game);
// stb_image.h loaders
std::optional<texture> load_stb(
const std::string_view &name, const fs::data &file, bool meta_only, const gamedef_t *game);
std::string_view name, const fs::data &file, bool meta_only, const gamedef_t *game);
// list of supported extensions and their loaders
struct extension_info_t
@ -115,7 +115,7 @@ constexpr extension_info_t extension_list[] = {{".png", ext::STB, load_stb}, {".
{".wal", ext::WAL, load_wal}, {".mip", ext::MIP, load_mip}, {"", ext::MIP, load_mip}};
// Attempt to load a texture from the specified name.
std::tuple<std::optional<texture>, fs::resolve_result, fs::data> load_texture(const std::string_view &name,
std::tuple<std::optional<texture>, fs::resolve_result, fs::data> load_texture(std::string_view name,
bool meta_only, const gamedef_t *game, const settings::common_settings &options, bool no_prefix = false,
bool mip_only = false);
@ -126,10 +126,10 @@ enum class meta_ext
};
// Load wal
std::optional<texture_meta> load_wal_meta(const std::string_view &name, const fs::data &file, const gamedef_t *game);
std::optional<texture_meta> load_wal_meta(std::string_view name, const fs::data &file, const gamedef_t *game);
std::optional<texture_meta> load_wal_json_meta(
const std::string_view &name, const fs::data &file, const gamedef_t *game);
std::string_view name, const fs::data &file, const gamedef_t *game);
// list of supported meta extensions and their loaders
constexpr struct
@ -142,7 +142,7 @@ constexpr struct
// Attempt to load a texture meta from the specified name.
std::tuple<std::optional<texture_meta>, fs::resolve_result, fs::data> load_texture_meta(
const std::string_view &name, const gamedef_t *game, const settings::common_settings &options);
std::string_view name, const gamedef_t *game, const settings::common_settings &options);
// Loads textures referenced by the bsp into the texture cache.
void load_textures(const mbsp_t *bsp, const settings::common_settings &options);

View File

@ -210,6 +210,6 @@ struct map_file_t
void convert_to(texcoord_style_t style, const gamedef_t *game, const settings::common_settings &options);
};
map_file_t parse(const std::string_view &view, parser_source_location base_location);
map_file_t parse(std::string_view view, parser_source_location base_location);
} // namespace mapfile

View File

@ -149,7 +149,7 @@ struct parser_t : parser_base_t
// pull from string_view; note that the string view must live for the entire
// duration of the parser's life time
parser_t(const std::string_view &view, parser_source_location base_location);
parser_t(std::string_view view, parser_source_location base_location);
// pull from fs::data; note that the data must live for the entire
// duration of the parser's life time, and must has_value()

View File

@ -408,7 +408,7 @@ private:
public:
setting_string(setting_container *dictionary, const nameset &names, std::string v,
const std::string_view &format = "\"str\"", const setting_group *group = nullptr, const char *description = "");
std::string_view format = "\"str\"", const setting_group *group = nullptr, const char *description = "");
bool parse(const std::string &setting_name, parser_base_t &parser, source source) override;
[[deprecated("use value()")]] std::string string_value() const override;
std::string format() const override;
@ -432,7 +432,7 @@ private:
public:
setting_set(setting_container *dictionary, const nameset &names,
const std::string_view &format = "\"str\" <multiple allowed>", const setting_group *group = nullptr,
std::string_view format = "\"str\" <multiple allowed>", const setting_group *group = nullptr,
const char *description = "");
const std::unordered_set<std::string> &values() const;

View File

@ -254,7 +254,7 @@ struct mapdata_t
// Small cache for image meta in the current map
std::unordered_map<std::string, std::optional<img::texture_meta>> meta_cache;
// load or fetch image meta associated with the specified name
const std::optional<img::texture_meta> &load_image_meta(const std::string_view &name);
const std::optional<img::texture_meta> &load_image_meta(std::string_view name);
// whether we had attempted loading texture stuff
bool textures_loaded = false;

View File

@ -172,7 +172,7 @@ void mapdata_t::add_hash_edge(size_t v1, size_t v2, int64_t edge_index, const fa
.has_been_reused = false});
}
const std::optional<img::texture_meta> &mapdata_t::load_image_meta(const std::string_view &name)
const std::optional<img::texture_meta> &mapdata_t::load_image_meta(std::string_view name)
{
static std::optional<img::texture_meta> nullmeta = std::nullopt;
auto it = meta_cache.find(name.data());