Use std::string_view instead of const char* internally where possible

DescribeGameType could return a std::string_view, but I'd rather consistently avoid returning std::string_view to avoid confusion over lifetime issues (even though they're not present in that case).
This commit is contained in:
Oliver Hamlet
2025-04-04 18:45:34 +01:00
parent 0c8df973aa
commit 58dbff885c
6 changed files with 33 additions and 33 deletions
+2 -2
View File
@@ -31,8 +31,8 @@
#include <vector>
namespace loot {
inline constexpr const char* BSA_FILE_EXTENSION = ".bsa";
inline constexpr const char* BA2_FILE_EXTENSION = ".ba2";
inline constexpr std::string_view BSA_FILE_EXTENSION = ".bsa";
inline constexpr std::string_view BA2_FILE_EXTENSION = ".ba2";
std::map<uint64_t, std::set<uint64_t>> GetAssetsInBethesdaArchive(
const std::filesystem::path& archivePath);
+7 -7
View File
@@ -56,19 +56,19 @@ using loot::GameType;
// game's install path. These directories have fixed paths relative to the
// game install path (renaming them causes the game launch to fail, or not
// find the DLC files).
constexpr const char* MS_FO4_AUTOMATRON_DATA_PATH =
constexpr std::string_view MS_FO4_AUTOMATRON_DATA_PATH =
"../../Fallout 4- Automatron (PC)/Content/Data";
constexpr const char* MS_FO4_CONTRAPTIONS_DATA_PATH =
constexpr std::string_view MS_FO4_CONTRAPTIONS_DATA_PATH =
"../../Fallout 4- Contraptions Workshop (PC)/Content/Data";
constexpr const char* MS_FO4_FAR_HARBOR_DATA_PATH =
constexpr std::string_view MS_FO4_FAR_HARBOR_DATA_PATH =
"../../Fallout 4- Far Harbor (PC)/Content/Data";
constexpr const char* MS_FO4_TEXTURE_PACK_DATA_PATH =
constexpr std::string_view MS_FO4_TEXTURE_PACK_DATA_PATH =
"../../Fallout 4- High Resolution Texture Pack/Content/Data";
constexpr const char* MS_FO4_NUKA_WORLD_DATA_PATH =
constexpr std::string_view MS_FO4_NUKA_WORLD_DATA_PATH =
"../../Fallout 4- Nuka-World (PC)/Content/Data";
constexpr const char* MS_FO4_VAULT_TEC_DATA_PATH =
constexpr std::string_view MS_FO4_VAULT_TEC_DATA_PATH =
"../../Fallout 4- Vault-Tec Workshop (PC)/Content/Data";
constexpr const char* MS_FO4_WASTELAND_DATA_PATH =
constexpr std::string_view MS_FO4_WASTELAND_DATA_PATH =
"../../Fallout 4- Wasteland Workshop (PC)/Content/Data";
bool IsMicrosoftStoreInstall(const GameType gameType,
+5 -3
View File
@@ -24,12 +24,14 @@
#include "api/helpers/logging.h"
#include <string_view>
#include <spdlog/sinks/base_sink.h>
namespace {
using loot::LogLevel;
constexpr const char* LOGGER_NAME = "loot_api_logger";
constexpr std::string_view LOGGER_NAME = "loot_api_logger";
LogLevel mapFromSpdlog(spdlog::level::level_enum severity) {
using spdlog::level::level_enum;
@@ -93,12 +95,12 @@ private:
}
namespace loot {
std::shared_ptr<spdlog::logger> getLogger() { return spdlog::get(LOGGER_NAME); }
std::shared_ptr<spdlog::logger> getLogger() { return spdlog::get(std::string(LOGGER_NAME)); }
std::shared_ptr<spdlog::logger> createLogger(
std::function<void(LogLevel, const char*)> callback) {
auto sink = std::make_shared<SpdLoggingSink>(callback);
auto logger = std::make_shared<spdlog::logger>(LOGGER_NAME, sink);
auto logger = std::make_shared<spdlog::logger>(std::string(LOGGER_NAME), sink);
logger->set_level(spdlog::level::level_enum::trace);
return logger;
+15 -15
View File
@@ -37,14 +37,14 @@ namespace loot {
/* The string below matches timestamps that use forwardslashes for date
separators. However, Pseudosem v1.0.1 will only compare the first
two digits as it does not recognise forwardslashes as separators. */
constexpr const char* dateRegex =
constexpr std::string_view dateRegex =
R"((\d{1,2}/\d{1,2}/\d{1,4} \d{1,2}:\d{1,2}:\d{1,2}))";
/* The string below matches the range of version strings supported by
Pseudosem v1.0.1, excluding space separators, as they make version
extraction from inside sentences very tricky and have not been
seen "in the wild". */
constexpr const char* pseudosemVersionRegex =
constexpr std::string_view pseudosemVersionRegex =
R"((\d+(?:\.\d+)+(?:[-._:]?[A-Za-z0-9]+)*))"
// The string below prevents version numbers followed by a comma from
// matching.
@@ -53,21 +53,19 @@ constexpr const char* pseudosemVersionRegex =
/* The string below matches a number containing one or more
digits found at the start of the search string or preceded by
'v' or 'version:. */
constexpr const char* digitsVersionRegex = R"((?:^|v|version:\s*)(\d+))";
constexpr std::string_view digitsVersionRegex = R"((?:^|v|version:\s*)(\d+))";
std::vector<Tag> ExtractBashTags(std::string_view description) {
std::vector<Tag> tags;
static constexpr const char* BASH_TAGS_OPENER = "{{BASH:";
static constexpr std::size_t BASH_TAGS_OPENER_LENGTH =
std::char_traits<char>::length(BASH_TAGS_OPENER);
static constexpr std::string_view BASH_TAGS_OPENER = "{{BASH:";
size_t startPos = description.find("{{BASH:");
if (startPos == std::string::npos ||
startPos + BASH_TAGS_OPENER_LENGTH >= description.length()) {
startPos + BASH_TAGS_OPENER.length() >= description.length()) {
return tags;
}
startPos += BASH_TAGS_OPENER_LENGTH;
startPos += BASH_TAGS_OPENER.length();
const size_t endPos = description.find("}}", startPos);
if (endPos == std::string::npos) {
@@ -94,12 +92,15 @@ std::optional<std::string> ExtractVersion(std::string_view text) {
together, and in order to extract the correct one, they must be searched
for in order of priority. */
static const std::vector<regex> versionRegexes({
regex(dateRegex, regex::ECMAScript | regex::icase),
regex(std::string(R"(version:?\s)") + pseudosemVersionRegex,
regex(
dateRegex.begin(), dateRegex.end(), regex::ECMAScript | regex::icase),
regex(R"(version:?\s)" + std::string(pseudosemVersionRegex),
regex::ECMAScript | regex::icase),
regex(std::string(R"((?:^|v|\s))") + pseudosemVersionRegex,
regex(R"((?:^|v|\s))" + std::string(pseudosemVersionRegex),
regex::ECMAScript | regex::icase),
regex(digitsVersionRegex.begin(),
digitsVersionRegex.end(),
regex::ECMAScript | regex::icase),
regex(digitsVersionRegex, regex::ECMAScript | regex::icase),
});
std::match_results<std::string_view::iterator> what;
@@ -198,8 +199,7 @@ int CompareFilenames(const ComparableFilename& lhs,
// Use CompareStringOrdinal as that will perform case conversion
// using the operating system uppercase table information, which (I think)
// will give results that match the filesystem, and is not locale-dependent.
int result = CompareStringOrdinal(
lhs.c_str(), -1, rhs.c_str(), -1, true);
int result = CompareStringOrdinal(lhs.c_str(), -1, rhs.c_str(), -1, true);
switch (result) {
case CSTR_LESS_THAN:
return -1;
@@ -238,7 +238,7 @@ std::string NormalizeFilename(std::string_view 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);
return filename.substr(0, filename.length() - GHOST_FILE_EXTENSION.length());
}
return filename;
+1 -3
View File
@@ -38,9 +38,7 @@
#endif
namespace loot {
inline constexpr const char* GHOST_FILE_EXTENSION = ".ghost";
inline constexpr std::size_t GHOST_FILE_EXTENSION_LENGTH =
std::char_traits<char>::length(GHOST_FILE_EXTENSION);
inline constexpr std::string_view GHOST_FILE_EXTENSION = ".ghost";
#ifdef _WIN32
typedef std::wstring ComparableFilename;
+3 -3
View File
@@ -625,9 +625,9 @@ Plugin::GetPluginsMetadata(const std::vector<const Plugin*>& plugins) {
std::string GetArchiveFileExtension(const GameType gameType) {
if (gameType == GameType::fo4 || gameType == GameType::fo4vr ||
gameType == GameType::starfield)
return BA2_FILE_EXTENSION;
return std::string(BA2_FILE_EXTENSION);
else
return BSA_FILE_EXTENSION;
return std::string(BSA_FILE_EXTENSION);
}
unsigned int Plugin::GetEspluginGameId(GameType gameType) {
@@ -660,7 +660,7 @@ bool hasPluginFileExtension(std::string_view filename, GameType gameType) {
if (gameType != GameType::openmw &&
boost::iends_with(filename, GHOST_FILE_EXTENSION)) {
filename =
filename.substr(0, filename.length() - GHOST_FILE_EXTENSION_LENGTH);
filename.substr(0, filename.length() - GHOST_FILE_EXTENSION.length());
}
if (boost::iends_with(filename, ".esp") ||