From 13379198056ce8d5dde91bc6d5ff7e9dcb4a4756 Mon Sep 17 00:00:00 2001 From: Eric Wasylishen Date: Sun, 17 Nov 2024 21:52:10 -0700 Subject: [PATCH] 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/ --- common/bspfile.cc | 8 ++++---- common/cmdlib.cc | 6 +++--- common/entdata.cc | 20 ++++++++++---------- common/imglib.cc | 18 +++++++++--------- common/mapfile.cc | 2 +- common/parser.cc | 2 +- common/settings.cc | 4 ++-- include/common/bspfile_common.hh | 4 ++-- include/common/cmdlib.hh | 6 +++--- include/common/entdata.h | 20 ++++++++++---------- include/common/imglib.hh | 16 ++++++++-------- include/common/mapfile.hh | 2 +- include/common/parser.hh | 2 +- include/common/settings.hh | 4 ++-- include/qbsp/map.hh | 2 +- qbsp/map.cc | 2 +- 16 files changed, 59 insertions(+), 59 deletions(-) diff --git a/common/bspfile.cc b/common/bspfile.cc index 8146abf5..99584df8 100644 --- a/common/bspfile.cc +++ b/common/bspfile.cc @@ -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])) { diff --git a/common/cmdlib.cc b/common/cmdlib.cc index f90d9b22..468f5f46 100644 --- a/common/cmdlib.cc +++ b/common/cmdlib.cc @@ -45,7 +45,7 @@ #include #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) diff --git a/common/entdata.cc b/common/entdata.cc index a38f05b6..0263674c 100644 --- a/common/entdata.cc +++ b/common/entdata.cc @@ -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(); } diff --git a/common/imglib.cc b/common/imglib.cc index 1084bb5b..c4594470 100644 --- a/common/imglib.cc +++ b/common/imglib.cc @@ -126,7 +126,7 @@ struct q2_miptex_t }; std::optional 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; @@ -167,7 +167,7 @@ Quake/Half Life MIP */ std::optional 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; @@ -256,7 +256,7 @@ std::optional load_mip( } std::optional 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 load_stb( // texture cache std::unordered_map 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 &pixels) return avg /= n; } -std::tuple, fs::resolve_result, fs::data> load_texture(const std::string_view &name, +std::tuple, 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, fs::resolve_result, fs::data> load_textu return {std::nullopt, {}, {}}; } -std::optional load_wal_meta(const std::string_view &name, const fs::data &file, const gamedef_t *game) +std::optional 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 load_wal_meta(const std::string_view &name, const fs // see .wal_json section in qbsp.rst for format documentation std::optional 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 load_wal_json_meta( } std::tuple, 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; diff --git a/common/mapfile.cc b/common/mapfile.cc index bf697300..ee08372c 100644 --- a/common/mapfile.cc +++ b/common/mapfile.cc @@ -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); diff --git a/common/parser.cc b/common/parser.cc index c3df3e41..2b3c1fa7 100644 --- a/common/parser.cc +++ b/common/parser.cc @@ -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) { } diff --git a/common/settings.cc b/common/settings.cc index dd3be518..f21639df 100644 --- a/common/settings.cc +++ b/common/settings.cc @@ -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) diff --git a/include/common/bspfile_common.hh b/include/common/bspfile_common.hh index 7620b91c..cb5ceb8e 100644 --- a/include/common/bspfile_common.hh +++ b/include/common/bspfile_common.hh @@ -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; diff --git a/include/common/cmdlib.hh b/include/common/cmdlib.hh index aa53902b..11b3f9af 100644 --- a/include/common/cmdlib.hh +++ b/include/common/cmdlib.hh @@ -30,9 +30,9 @@ #include #include // 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 { diff --git a/include/common/entdata.h b/include/common/entdata.h index 8ddc0b45..683d0615 100644 --- a/include/common/entdata.h +++ b/include/common/entdata.h @@ -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(); } diff --git a/include/common/imglib.hh b/include/common/imglib.hh index 42e1dd19..0d1142ff 100644 --- a/include/common/imglib.hh +++ b/include/common/imglib.hh @@ -89,19 +89,19 @@ void clear(); qvec3b calculate_average(const std::vector &pixels); -const texture *find(const std::string_view &str); +const texture *find(std::string_view str); // Load wal std::optional 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 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 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, fs::resolve_result, fs::data> load_texture(const std::string_view &name, +std::tuple, 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 load_wal_meta(const std::string_view &name, const fs::data &file, const gamedef_t *game); +std::optional load_wal_meta(std::string_view name, const fs::data &file, const gamedef_t *game); std::optional 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, 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); diff --git a/include/common/mapfile.hh b/include/common/mapfile.hh index f6e0a6ef..381d573a 100644 --- a/include/common/mapfile.hh +++ b/include/common/mapfile.hh @@ -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 diff --git a/include/common/parser.hh b/include/common/parser.hh index e882ae8d..b415c6eb 100644 --- a/include/common/parser.hh +++ b/include/common/parser.hh @@ -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() diff --git a/include/common/settings.hh b/include/common/settings.hh index f2b709f2..83c8638e 100644 --- a/include/common/settings.hh +++ b/include/common/settings.hh @@ -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\" ", const setting_group *group = nullptr, + std::string_view format = "\"str\" ", const setting_group *group = nullptr, const char *description = ""); const std::unordered_set &values() const; diff --git a/include/qbsp/map.hh b/include/qbsp/map.hh index 621817a6..75473af3 100644 --- a/include/qbsp/map.hh +++ b/include/qbsp/map.hh @@ -254,7 +254,7 @@ struct mapdata_t // Small cache for image meta in the current map std::unordered_map> meta_cache; // load or fetch image meta associated with the specified name - const std::optional &load_image_meta(const std::string_view &name); + const std::optional &load_image_meta(std::string_view name); // whether we had attempted loading texture stuff bool textures_loaded = false; diff --git a/qbsp/map.cc b/qbsp/map.cc index a2bfbf84..86f6dd85 100644 --- a/qbsp/map.cc +++ b/qbsp/map.cc @@ -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 &mapdata_t::load_image_meta(const std::string_view &name) +const std::optional &mapdata_t::load_image_meta(std::string_view name) { static std::optional nullmeta = std::nullopt; auto it = meta_cache.find(name.data());