Convert some internal functions to take std::string_view

This commit is contained in:
Oliver Hamlet
2025-04-04 18:45:22 +01:00
parent 0b5a8b5767
commit 0c8df973aa
22 changed files with 96 additions and 88 deletions
+1 -1
View File
@@ -139,7 +139,7 @@ std::filesystem::path ResolvePluginPath(
std::vector<std::filesystem::path> FindArchives(
const std::filesystem::path& parentPath,
const std::string& archiveFileExtension) {
std::string_view archiveFileExtension) {
if (!std::filesystem::is_directory(parentPath)) {
return {};
}
+1 -1
View File
@@ -36,7 +36,7 @@ std::vector<const Plugin*> GameCache::GetPlugins() const {
return output;
}
const Plugin* GameCache::GetPlugin(const std::string& pluginName) const {
const Plugin* GameCache::GetPlugin(std::string_view pluginName) const {
const auto it = plugins_.find(NormalizeFilename(pluginName));
if (it != end(plugins_))
return it->second.get();
+2 -1
View File
@@ -26,6 +26,7 @@
#define LOOT_API_GAME_GAME_CACHE
#include <string>
#include <string_view>
#include <unordered_map>
#include "api/plugin.h"
@@ -34,7 +35,7 @@ namespace loot {
class GameCache {
public:
std::vector<const Plugin*> GetPlugins() const;
const Plugin* GetPlugin(const std::string& pluginName) const;
const Plugin* GetPlugin(std::string_view pluginName) const;
void AddPlugin(Plugin&& plugin);
std::vector<const Plugin*> GetPluginsWithReplacements(
+8 -6
View File
@@ -210,7 +210,8 @@ std::filesystem::path LoadOrderHandler::GetActivePluginsFilePath() const {
return filePath;
}
std::vector<std::filesystem::path> LoadOrderHandler::GetAdditionalDataPaths() const {
std::vector<std::filesystem::path> LoadOrderHandler::GetAdditionalDataPaths()
const {
const auto logger = getLogger();
if (logger) {
logger->trace("Getting additional data paths.");
@@ -219,7 +220,8 @@ std::vector<std::filesystem::path> LoadOrderHandler::GetAdditionalDataPaths() co
char** pathArr = nullptr;
size_t pathArrSize = 0;
const unsigned int ret = lo_get_additional_plugins_directories(gh_.get(), &pathArr, &pathArrSize);
const unsigned int ret =
lo_get_additional_plugins_directories(gh_.get(), &pathArr, &pathArrSize);
HandleError("get additional data paths", ret);
@@ -288,7 +290,7 @@ void LoadOrderHandler::SetAdditionalDataPaths(
}
}
void LoadOrderHandler::HandleError(const std::string& operation,
void LoadOrderHandler::HandleError(std::string_view operation,
unsigned int returnCode) const {
if (returnCode == LIBLO_OK || returnCode == LIBLO_WARN_LO_MISMATCH) {
return;
@@ -298,10 +300,10 @@ void LoadOrderHandler::HandleError(const std::string& operation,
std::string err;
lo_get_error_message(&e);
if (e == nullptr) {
err = "libloadorder failed to " + operation +
". Details could not be fetched.";
err = fmt::format(
"libloadorder failed to {}. Details could not be fetched.", operation);
} else {
err = "libloadorder failed to " + operation + ". Details: " + e;
err = fmt::format("libloadorder failed to {}. Details: {}", operation, e);
}
auto logger = getLogger();
+2 -1
View File
@@ -28,6 +28,7 @@
#include <filesystem>
#include <libloadorder.hpp>
#include <string>
#include <string_view>
#include <vector>
#include "loot/enum/game_type.h"
@@ -61,7 +62,7 @@ public:
const std::vector<std::filesystem::path>& dataPaths) const;
private:
void HandleError(const std::string& operation, unsigned int returnCode) const;
void HandleError(std::string_view operation, unsigned int returnCode) const;
std::unique_ptr<std::remove_pointer<lo_game_handle>::type,
decltype(&lo_destroy_handle)>
+11 -11
View File
@@ -55,7 +55,7 @@ constexpr const char* pseudosemVersionRegex =
'v' or 'version:. */
constexpr const char* digitsVersionRegex = R"((?:^|v|version:\s*)(\d+))";
std::vector<Tag> ExtractBashTags(const std::string& description) {
std::vector<Tag> ExtractBashTags(std::string_view description) {
std::vector<Tag> tags;
static constexpr const char* BASH_TAGS_OPENER = "{{BASH:";
@@ -87,7 +87,7 @@ std::vector<Tag> ExtractBashTags(const std::string& description) {
return tags;
}
std::optional<std::string> ExtractVersion(const std::string& text) {
std::optional<std::string> ExtractVersion(std::string_view text) {
using std::regex;
/* There are a few different version formats that can appear in strings
@@ -102,9 +102,9 @@ std::optional<std::string> ExtractVersion(const std::string& text) {
regex(digitsVersionRegex, regex::ECMAScript | regex::icase),
});
std::smatch what;
std::match_results<std::string_view::iterator> what;
for (const auto& versionRegex : versionRegexes) {
if (std::regex_search(text, what, versionRegex)) {
if (std::regex_search(text.begin(), text.end(), what, versionRegex)) {
for (auto it = next(begin(what)); it != end(what); ++it) {
if (it->str().empty())
continue;
@@ -135,9 +135,9 @@ int narrow(size_t value) {
return castValue;
}
std::wstring ToWinWide(const std::string& str) {
std::wstring ToWinWide(std::string_view str) {
const size_t len = MultiByteToWideChar(
CP_UTF8, 0, str.c_str(), static_cast<int>(str.length()), 0, 0);
CP_UTF8, 0, str.data(), static_cast<int>(str.length()), 0, 0);
if (len == 0) {
return std::wstring();
@@ -146,7 +146,7 @@ std::wstring ToWinWide(const std::string& str) {
std::wstring wstr(len, 0);
MultiByteToWideChar(CP_UTF8,
0,
str.c_str(),
str.data(),
narrow(str.length()),
wstr.data(),
narrow(wstr.length()));
@@ -180,7 +180,7 @@ std::string FromWinWide(const std::wstring& wstr) {
}
#endif
ComparableFilename ToComparableFilename(const std::string filename) {
ComparableFilename ToComparableFilename(std::string_view filename) {
#ifdef _WIN32
return ToWinWide(filename);
#else
@@ -188,7 +188,7 @@ ComparableFilename ToComparableFilename(const std::string filename) {
#endif
}
int CompareFilenames(const std::string& lhs, const std::string& rhs) {
int CompareFilenames(std::string_view lhs, std::string_view rhs) {
return CompareFilenames(ToComparableFilename(lhs), ToComparableFilename(rhs));
}
@@ -216,7 +216,7 @@ int CompareFilenames(const ComparableFilename& lhs,
#endif
}
std::string NormalizeFilename(const std::string& filename) {
std::string NormalizeFilename(std::string_view filename) {
#ifdef _WIN32
auto wideString = ToWinWide(filename);
@@ -235,7 +235,7 @@ std::string NormalizeFilename(const std::string& filename) {
#endif
}
std::string TrimDotGhostExtension(const std::string& filename) {
std::string TrimDotGhostExtension(std::string&& filename) {
// If the name passed ends in '.ghost', that should be trimmed.
if (boost::iends_with(filename, GHOST_FILE_EXTENSION)) {
return filename.substr(0, filename.length() - GHOST_FILE_EXTENSION_LENGTH);
+7 -6
View File
@@ -27,6 +27,7 @@
#include <optional>
#include <string>
#include <string_view>
#include <vector>
#include "loot/metadata/tag.h"
@@ -47,17 +48,17 @@ typedef std::wstring ComparableFilename;
typedef icu::UnicodeString ComparableFilename;
#endif
std::vector<Tag> ExtractBashTags(const std::string& description);
std::vector<Tag> ExtractBashTags(std::string_view description);
std::optional<std::string> ExtractVersion(const std::string& text);
std::optional<std::string> ExtractVersion(std::string_view text);
ComparableFilename ToComparableFilename(const std::string filename);
ComparableFilename ToComparableFilename(std::string_view filename);
// Compare strings as if they're filenames, respecting filesystem case
// insensitivity on Windows. Returns -1 if lhs < rhs, 0 if lhs == rhs, and 1 if
// lhs > rhs. The comparison may give different results on Linux, but is still
// locale-invariant.
int CompareFilenames(const std::string& lhs, const std::string& rhs);
int CompareFilenames(std::string_view lhs, std::string_view rhs);
int CompareFilenames(const ComparableFilename& lhs,
const ComparableFilename& rhs);
@@ -68,9 +69,9 @@ int CompareFilenames(const ComparableFilename& lhs,
// different but hopefully still consistent enough with the behaviour on Windows
// that the normalized filenames distinguish characters in a similar way to the
// Windows filesystem.
std::string NormalizeFilename(const std::string& filename);
std::string NormalizeFilename(std::string_view filename);
std::string TrimDotGhostExtension(const std::string& filename);
std::string TrimDotGhostExtension(std::string&& filename);
}
#endif
+7 -7
View File
@@ -32,18 +32,18 @@
#include "loot/exception/error_categories.h"
namespace loot {
void HandleError(const std::string operation, int returnCode) {
void HandleError(std::string_view operation, int returnCode) {
if (returnCode == LCI_OK) {
return;
}
const char* message = nullptr;
std::string err = "Failed to " + operation + ". ";
std::string err;
lci_get_error_message(&message);
if (message == nullptr) {
err += "Error code: " + std::to_string(returnCode);
err = fmt::format("Failed to {}. Error code: {}", operation, returnCode);
} else {
err += "Details: " + std::string(message);
err = fmt::format("Failed to {}. Details: {}", operation, message);
}
auto logger = getLogger();
@@ -260,12 +260,12 @@ void ConditionEvaluator::SetAdditionalDataPaths(
}
bool ConditionEvaluator::Evaluate(const PluginCleaningData& cleaningData,
const std::string& pluginName) {
std::string_view pluginName) {
if (pluginName.empty())
return false;
return Evaluate("checksum(\"" + pluginName + "\", " +
CrcToString(cleaningData.GetCRC()) + ")");
return Evaluate(fmt::format(
"checksum(\"{}\", {})", pluginName, CrcToString(cleaningData.GetCRC())));
}
void ParseCondition(const std::string& condition) {
+2 -1
View File
@@ -30,6 +30,7 @@
#include <filesystem>
#include <memory>
#include <string>
#include <string_view>
#include "loot/enum/game_type.h"
#include "loot/metadata/plugin_cleaning_data.h"
@@ -56,7 +57,7 @@ public:
private:
bool Evaluate(const PluginCleaningData& cleaningData,
const std::string& pluginName);
std::string_view pluginName);
std::unique_ptr<lci_state, decltype(&lci_state_destroy)> lciState_;
};
+3 -4
View File
@@ -50,10 +50,9 @@ std::vector<T> mergeVectors(std::vector<T> first,
}
namespace loot {
PluginMetadata::PluginMetadata(const std::string& n) : name_(n) {
// If the name passed ends in '.ghost', that should be trimmed.
name_ = TrimDotGhostExtension(n);
// If the name passed ends in '.ghost', that should be trimmed.
PluginMetadata::PluginMetadata(const std::string& n) :
name_(TrimDotGhostExtension(std::string(n))) {
if (IsRegexPlugin()) {
nameRegex_ = std::regex(name_, std::regex::ECMAScript | std::regex::icase);
}
+4 -7
View File
@@ -56,7 +56,7 @@ std::string read_to_string(const std::filesystem::path& filePath) {
}
std::optional<std::pair<size_t, size_t>> FindPreludeBounds(
const std::string& masterlist) {
std::string_view masterlist) {
size_t startOfPrelude = std::string::npos;
size_t endOfPrelude = std::string::npos;
@@ -118,7 +118,7 @@ std::string IndentPrelude(const std::string& prelude) {
}
std::string ReplaceMetadataListPrelude(const std::string& prelude,
const std::string& masterlist) {
std::string&& masterlist) {
auto preludeBounds = FindPreludeBounds(masterlist);
if (!preludeBounds.has_value()) {
@@ -163,11 +163,8 @@ void MetadataList::LoadWithPrelude(const std::filesystem::path& filePath,
// retained.
// As such, replacing the prelude needs to happen before parsing,
// which means reading the files and performing string manipulation.
auto prelude_content = read_to_string(preludePath);
auto masterlist_content = read_to_string(filePath);
masterlist_content =
ReplaceMetadataListPrelude(prelude_content, masterlist_content);
auto masterlist_content = ReplaceMetadataListPrelude(
read_to_string(preludePath), read_to_string(filePath));
auto stream = std::istringstream(masterlist_content);
this->Load(stream, filePath);
+2 -1
View File
@@ -28,6 +28,7 @@
#include <filesystem>
#include <optional>
#include <string>
#include <string_view>
#include <unordered_map>
#include <vector>
@@ -51,7 +52,7 @@ namespace loot {
// YAML's block style (at least up to the end of the prelude in the
// latter). This is true for all official files.
std::string ReplaceMetadataListPrelude(const std::string& prelude,
const std::string& masterlist);
std::string&& masterlist);
class MetadataList {
public:
+14 -13
View File
@@ -37,17 +37,20 @@
#include "loot/exception/file_access_error.h"
namespace loot {
// Intentionally takes a copy of the first parameter.
std::filesystem::path ReplaceExtension(std::filesystem::path path,
const std::string& newExtension) {
std::string_view newExtension) {
return path.replace_extension(std::filesystem::u8path(newExtension));
}
// Intentionally takes a copy of the first parameter.
std::filesystem::path GetSuffixedArchivePath(std::filesystem::path pluginPath,
const std::string& suffix,
const std::string& newExtension) {
std::string_view suffix,
std::string_view newExtension) {
// replace_extension() with no argument just removes the existing extension.
pluginPath.replace_extension();
pluginPath += suffix + newExtension;
pluginPath += suffix;
pluginPath += newExtension;
return pluginPath;
}
@@ -89,7 +92,7 @@ std::vector<std::filesystem::path> FindAssociatedArchive(
std::vector<std::filesystem::path> FindAssociatedArchivesWithSuffixes(
const std::filesystem::path& pluginPath,
const std::string& archiveExtension,
std::string_view archiveExtension,
const std::vector<std::string>& supportedSuffixes) {
std::vector<std::filesystem::path> paths;
@@ -181,14 +184,12 @@ std::vector<std::filesystem::path> FindAssociatedArchives(
}
}
void HandleEspluginError(unsigned int returnCode,
const std::string& operation) {
void HandleEspluginError(unsigned int returnCode, std::string_view operation) {
if (returnCode == ESP_OK) {
return;
}
auto err = "esplugin failed to " + operation +
". Error code: " + std::to_string(returnCode);
auto err = fmt::format("esplugin failed to {}. Error code: {}", operation, returnCode);
const char* e = nullptr;
esp_get_error_message(&e);
@@ -207,8 +208,8 @@ void HandleEspluginError(unsigned int returnCode,
}
void HandleEspluginError(unsigned int returnCode,
const std::string& message,
const std::string& args...) {
std::string_view message,
std::string_view args...) {
if (returnCode == ESP_OK) {
return;
}
@@ -228,7 +229,7 @@ void HandleEspluginError(unsigned int returnCode,
Plugin::Plugin(const GameType gameType,
const GameCache& gameCache,
std::filesystem::path pluginPath,
const std::filesystem::path& pluginPath,
const bool headerOnly) :
name_(gameType == GameType::openmw
? pluginPath.filename().u8string()
@@ -655,7 +656,7 @@ unsigned int Plugin::GetEspluginGameId(GameType gameType) {
}
}
bool hasPluginFileExtension(std::string filename, GameType gameType) {
bool hasPluginFileExtension(std::string_view filename, GameType gameType) {
if (gameType != GameType::openmw &&
boost::iends_with(filename, GHOST_FILE_EXTENSION)) {
filename =
+3 -2
View File
@@ -30,6 +30,7 @@
#include <map>
#include <set>
#include <string>
#include <string_view>
#include <vector>
#include "api/game/load_order_handler.h"
@@ -53,7 +54,7 @@ class Plugin final : public PluginSortingInterface {
public:
explicit Plugin(const GameType gameType,
const GameCache& gameCache,
std::filesystem::path pluginPath,
const std::filesystem::path& pluginPath,
const bool headerOnly);
void ResolveRecordIds(Vec_PluginMetadata* pluginsMetadata) const;
@@ -116,7 +117,7 @@ private:
std::string GetArchiveFileExtension(const GameType gameType);
bool hasPluginFileExtension(std::string filename, GameType gameType);
bool hasPluginFileExtension(std::string_view filename, GameType gameType);
bool equivalent(const std::filesystem::path& path1,
const std::filesystem::path& path2);
+4 -4
View File
@@ -171,7 +171,7 @@ GroupGraph BuildGroupGraph(const std::vector<Group>& masterlistGroups,
return graph;
}
vertex_t GetVertexByName(const GroupGraph& graph, const std::string& name) {
vertex_t GetVertexByName(const GroupGraph& graph, std::string_view name) {
for (const auto& vertex :
boost::make_iterator_range(boost::vertices(graph))) {
if (graph[vertex] == name) {
@@ -184,12 +184,12 @@ vertex_t GetVertexByName(const GroupGraph& graph, const std::string& name) {
logger->error("Can't find group with name \"{}\"", name);
}
throw std::invalid_argument("Can't find group with name \"" + name + "\"");
throw std::invalid_argument("Can't find group with name \"" + std::string(name) + "\"");
}
std::vector<Vertex> GetGroupsPath(const GroupGraph& graph,
const std::string& fromGroupName,
const std::string& toGroupName) {
std::string_view fromGroupName,
std::string_view toGroupName) {
auto logger = getLogger();
auto fromVertex = GetVertexByName(graph, fromGroupName);
+3 -2
View File
@@ -27,6 +27,7 @@
#include <boost/graph/adjacency_list.hpp>
#include <string>
#include <string_view>
#include <unordered_map>
#include <vector>
@@ -45,7 +46,7 @@ GroupGraph BuildGroupGraph(const std::vector<Group>& masterlistGroups,
const std::vector<Group>& userGroups);
std::vector<Vertex> GetGroupsPath(const GroupGraph& groupGraph,
const std::string& fromGroupName,
const std::string& toGroupName);
std::string_view fromGroupName,
std::string_view toGroupName);
}
#endif
+3 -2
View File
@@ -33,6 +33,7 @@
#include <boost/graph/iteration_macros.hpp>
#include <boost/graph/topological_sort.hpp>
#include <queue>
#include <string_view>
#include "api/helpers/logging.h"
#include "api/helpers/text.h"
@@ -918,11 +919,11 @@ void PluginGraph::AddHardcodedPluginEdges(
// Build the vertex map for implicitly active plugins and record the vertices
// for other plugins.
for (const auto& vertex : boost::make_iterator_range(GetVertices())) {
const auto pluginName = GetPlugin(vertex).GetName();
const auto& pluginName = GetPlugin(vertex).GetName();
const auto it =
std::find_if(hardcodedPlugins.begin(),
hardcodedPlugins.end(),
[&](const std::string& name) {
[&](std::string_view name) {
return CompareFilenames(name, pluginName) == 0;
});
+5 -5
View File
@@ -48,8 +48,8 @@ std::vector<PluginSortingData> GetPluginsSortingData(
const auto userMetadata = db.GetPluginUserMetadata(pluginFilename, true)
.value_or(PluginMetadata(pluginFilename));
const auto pluginSortingData = PluginSortingData(
plugin, masterlistMetadata, userMetadata, i);
const auto pluginSortingData =
PluginSortingData(plugin, masterlistMetadata, userMetadata, i);
pluginsSortingData.push_back(pluginSortingData);
i += 1;
@@ -77,7 +77,7 @@ void ValidatePluginGroups(const std::vector<PluginSortingData>& plugins,
bool IsInRange(const std::vector<PluginSortingData>::const_iterator& begin,
const std::vector<PluginSortingData>::const_iterator& end,
const std::string& name) {
std::string_view name) {
return std::any_of(begin, end, [&](const PluginSortingData& plugin) {
return CompareFilenames(plugin.GetName(), name) == 0;
});
@@ -91,10 +91,10 @@ void ValidateSpecificAndHardcodedEdges(
const std::vector<std::string>& hardcodedPlugins) {
const auto logger = getLogger();
const auto isNonMaster = [&](const std::string& name) {
const auto isNonMaster = [&](std::string_view name) {
return IsInRange(firstNonMaster, end, name);
};
const auto isBlueprintMaster = [&](const std::string& name) {
const auto isBlueprintMaster = [&](std::string_view name) {
return IsInRange(firstBlueprintMaster, firstNonMaster, name);
};
+1
View File
@@ -26,6 +26,7 @@
#define LOOT_API_SORTING_PLUGIN_SORT
#include <string>
#include <string_view>
#include <vector>
#include "api/game/game.h"
+10 -10
View File
@@ -452,7 +452,7 @@ TEST(ReplaceMetadataListPrelude, shouldReturnAnEmptyStringIfGivenEmptyStrings) {
std::string prelude = "";
std::string masterlist = "";
auto result = ReplaceMetadataListPrelude(prelude, masterlist);
auto result = ReplaceMetadataListPrelude(prelude, std::string(masterlist));
EXPECT_EQ(masterlist, result);
}
@@ -466,7 +466,7 @@ TEST(ReplaceMetadataListPrelude, shouldNotChangeAMasterlistWithNoPrelude) {
- name: a.esp
)";
auto result = ReplaceMetadataListPrelude(prelude, masterlist);
auto result = ReplaceMetadataListPrelude(prelude, std::string(masterlist));
EXPECT_EQ(masterlist, result);
}
@@ -484,7 +484,7 @@ plugins:
- name: a.esp
)";
auto result = ReplaceMetadataListPrelude(prelude, masterlist);
auto result = ReplaceMetadataListPrelude(prelude, std::move(masterlist));
auto expectedResult = R"(prelude:
globals:
@@ -510,7 +510,7 @@ prelude:
)";
auto result = ReplaceMetadataListPrelude(prelude, masterlist);
auto result = ReplaceMetadataListPrelude(prelude, std::move(masterlist));
auto expectedResult = R"(plugins:
- name: a.esp
@@ -540,7 +540,7 @@ plugins:
- name: a.esp
)";
auto result = ReplaceMetadataListPrelude(prelude, masterlist);
auto result = ReplaceMetadataListPrelude(prelude, std::move(masterlist));
auto expectedResult = R"(
common:
@@ -573,7 +573,7 @@ plugins:
- name: a.esp
)";
auto result = ReplaceMetadataListPrelude(prelude, masterlist);
auto result = ReplaceMetadataListPrelude(prelude, std::move(masterlist));
auto expectedResult = R"(prelude:
globals:
@@ -597,7 +597,7 @@ plugins:
- name: a.esp
)";
auto result = ReplaceMetadataListPrelude(prelude, masterlist);
auto result = ReplaceMetadataListPrelude(prelude, std::move(masterlist));
auto expectedResult = R"(prelude:
globals: [{type: note, content: A message.}]
@@ -612,7 +612,7 @@ TEST(ReplaceMetadataListPrelude, doesNotChangeAFlowStyleMasterlist) {
std::string prelude = "globals: [{type: note, content: A message.}]";
std::string masterlist = "{prelude: {}, plugins: [{name: a.esp}]}";
auto result = ReplaceMetadataListPrelude(prelude, masterlist);
auto result = ReplaceMetadataListPrelude(prelude, std::string(masterlist));
EXPECT_EQ(masterlist, result);
}
@@ -631,7 +631,7 @@ plugins:
- name: a.esp
)";
auto result = ReplaceMetadataListPrelude(prelude, masterlist);
auto result = ReplaceMetadataListPrelude(prelude, std::move(masterlist));
auto expectedResult = R"(prelude:
globals:
@@ -658,7 +658,7 @@ plugins:
- name: a.esp
)";
auto result = ReplaceMetadataListPrelude(prelude, masterlist);
auto result = ReplaceMetadataListPrelude(prelude, std::move(masterlist));
auto expectedResult = R"(prelude:
globals:

Some files were not shown because too many files have changed in this diff Show More