mirror of
https://github.com/NixOS/nix
synced 2025-06-25 10:41:16 +02:00
Merge pull request #13234 from xokdvium/transparent-comparator-map
Use C++14 N3657 transparent comparator for `StringMap` and `StringPairs` (NFC)
This commit is contained in:
commit
fe41bdefd7
27 changed files with 56 additions and 39 deletions
|
@ -336,7 +336,7 @@ struct MixEnvironment : virtual Args
|
||||||
|
|
||||||
StringSet keepVars;
|
StringSet keepVars;
|
||||||
StringSet unsetVars;
|
StringSet unsetVars;
|
||||||
std::map<std::string, std::string> setVars;
|
StringMap setVars;
|
||||||
bool ignoreEnvironment;
|
bool ignoreEnvironment;
|
||||||
|
|
||||||
MixEnvironment();
|
MixEnvironment();
|
||||||
|
|
|
@ -89,9 +89,9 @@ bool getBoolAttr(const Attrs & attrs, const std::string & name)
|
||||||
return *s;
|
return *s;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::map<std::string, std::string> attrsToQuery(const Attrs & attrs)
|
StringMap attrsToQuery(const Attrs & attrs)
|
||||||
{
|
{
|
||||||
std::map<std::string, std::string> query;
|
StringMap query;
|
||||||
for (auto & attr : attrs) {
|
for (auto & attr : attrs) {
|
||||||
if (auto v = std::get_if<uint64_t>(&attr.second)) {
|
if (auto v = std::get_if<uint64_t>(&attr.second)) {
|
||||||
query.insert_or_assign(attr.first, fmt("%d", *v));
|
query.insert_or_assign(attr.first, fmt("%d", *v));
|
||||||
|
|
|
@ -134,7 +134,7 @@ ParsedURL Input::toURL() const
|
||||||
return scheme->toURL(*this);
|
return scheme->toURL(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Input::toURLString(const std::map<std::string, std::string> & extraQuery) const
|
std::string Input::toURLString(const StringMap & extraQuery) const
|
||||||
{
|
{
|
||||||
auto url = toURL();
|
auto url = toURL();
|
||||||
for (auto & attr : extraQuery)
|
for (auto & attr : extraQuery)
|
||||||
|
|
|
@ -368,7 +368,7 @@ struct GitRepoImpl : GitRepo, std::enable_shared_from_this<GitRepoImpl>
|
||||||
if (git_config_iterator_glob_new(Setter(it), config.get(), "^submodule\\..*\\.(path|url|branch)$"))
|
if (git_config_iterator_glob_new(Setter(it), config.get(), "^submodule\\..*\\.(path|url|branch)$"))
|
||||||
throw Error("iterating over .gitmodules: %s", git_error_last()->message);
|
throw Error("iterating over .gitmodules: %s", git_error_last()->message);
|
||||||
|
|
||||||
std::map<std::string, std::string> entries;
|
StringMap entries;
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
git_config_entry * entry = nullptr;
|
git_config_entry * entry = nullptr;
|
||||||
|
|
|
@ -37,7 +37,7 @@ std::optional<bool> maybeGetBoolAttr(const Attrs & attrs, const std::string & na
|
||||||
|
|
||||||
bool getBoolAttr(const Attrs & attrs, const std::string & name);
|
bool getBoolAttr(const Attrs & attrs, const std::string & name);
|
||||||
|
|
||||||
std::map<std::string, std::string> attrsToQuery(const Attrs & attrs);
|
StringMap attrsToQuery(const Attrs & attrs);
|
||||||
|
|
||||||
Hash getRevAttr(const Attrs & attrs, const std::string & name);
|
Hash getRevAttr(const Attrs & attrs, const std::string & name);
|
||||||
|
|
||||||
|
|
|
@ -71,7 +71,7 @@ public:
|
||||||
|
|
||||||
ParsedURL toURL() const;
|
ParsedURL toURL() const;
|
||||||
|
|
||||||
std::string toURLString(const std::map<std::string, std::string> & extraQuery = {}) const;
|
std::string toURLString(const StringMap & extraQuery = {}) const;
|
||||||
|
|
||||||
std::string to_string() const;
|
std::string to_string() const;
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,7 @@ const static std::string subDirRegex = subDirElemRegex + "(?:/" + subDirElemRege
|
||||||
|
|
||||||
std::string FlakeRef::to_string() const
|
std::string FlakeRef::to_string() const
|
||||||
{
|
{
|
||||||
std::map<std::string, std::string> extraQuery;
|
StringMap extraQuery;
|
||||||
if (subdir != "")
|
if (subdir != "")
|
||||||
extraQuery.insert_or_assign("dir", subdir);
|
extraQuery.insert_or_assign("dir", subdir);
|
||||||
return input.toURLString(extraQuery);
|
return input.toURLString(extraQuery);
|
||||||
|
|
|
@ -974,7 +974,7 @@ void runPostBuildHook(
|
||||||
fmt("running post-build-hook '%s'", settings.postBuildHook),
|
fmt("running post-build-hook '%s'", settings.postBuildHook),
|
||||||
Logger::Fields{store.printStorePath(drvPath)});
|
Logger::Fields{store.printStorePath(drvPath)});
|
||||||
PushActivity pact(act.id);
|
PushActivity pact(act.id);
|
||||||
std::map<std::string, std::string> hookEnvironment = getEnv();
|
StringMap hookEnvironment = getEnv();
|
||||||
|
|
||||||
hookEnvironment.emplace("DRV_PATH", store.printStorePath(drvPath));
|
hookEnvironment.emplace("DRV_PATH", store.printStorePath(drvPath));
|
||||||
hookEnvironment.emplace("OUT_PATHS", chomp(concatStringsSep(" ", store.printStorePathSet(outputPaths))));
|
hookEnvironment.emplace("OUT_PATHS", chomp(concatStringsSep(" ", store.printStorePathSet(outputPaths))));
|
||||||
|
|
|
@ -125,7 +125,7 @@ struct StoreDirConfigBase : Config
|
||||||
*/
|
*/
|
||||||
struct StoreDirConfig : StoreDirConfigBase, MixStoreDirMethods
|
struct StoreDirConfig : StoreDirConfigBase, MixStoreDirMethods
|
||||||
{
|
{
|
||||||
using Params = std::map<std::string, std::string>;
|
using Params = StringMap;
|
||||||
|
|
||||||
StoreDirConfig(const Params & params);
|
StoreDirConfig(const Params & params);
|
||||||
|
|
||||||
|
|
|
@ -41,7 +41,7 @@ namespace nix {
|
||||||
*/
|
*/
|
||||||
struct StoreReference
|
struct StoreReference
|
||||||
{
|
{
|
||||||
using Params = std::map<std::string, std::string>;
|
using Params = StringMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Special store reference `""` or `"auto"`
|
* Special store reference `""` or `"auto"`
|
||||||
|
|
|
@ -96,7 +96,7 @@ Realisation Realisation::fromJSON(
|
||||||
|
|
||||||
std::map <DrvOutput, StorePath> dependentRealisations;
|
std::map <DrvOutput, StorePath> dependentRealisations;
|
||||||
if (auto jsonDependencies = json.find("dependentRealisations"); jsonDependencies != json.end())
|
if (auto jsonDependencies = json.find("dependentRealisations"); jsonDependencies != json.end())
|
||||||
for (auto & [jsonDepId, jsonDepOutPath] : jsonDependencies->get<std::map<std::string, std::string>>())
|
for (auto & [jsonDepId, jsonDepOutPath] : jsonDependencies->get<StringMap>())
|
||||||
dependentRealisations.insert({DrvOutput::parse(jsonDepId), StorePath(jsonDepOutPath)});
|
dependentRealisations.insert({DrvOutput::parse(jsonDepId), StorePath(jsonDepOutPath)});
|
||||||
|
|
||||||
return Realisation{
|
return Realisation{
|
||||||
|
|
|
@ -83,7 +83,7 @@ bool SSHMaster::isMasterRunning() {
|
||||||
Strings createSSHEnv()
|
Strings createSSHEnv()
|
||||||
{
|
{
|
||||||
// Copy the environment and set SHELL=/bin/sh
|
// Copy the environment and set SHELL=/bin/sh
|
||||||
std::map<std::string, std::string> env = getEnv();
|
StringMap env = getEnv();
|
||||||
|
|
||||||
// SSH will invoke the "user" shell for -oLocalCommand, but that means
|
// SSH will invoke the "user" shell for -oLocalCommand, but that means
|
||||||
// $SHELL. To keep things simple and avoid potential issues with other
|
// $SHELL. To keep things simple and avoid potential issues with other
|
||||||
|
|
|
@ -187,7 +187,7 @@ private:
|
||||||
typedef std::map<Path, ChrootPath> PathsInChroot; // maps target path to source path
|
typedef std::map<Path, ChrootPath> PathsInChroot; // maps target path to source path
|
||||||
PathsInChroot pathsInChroot;
|
PathsInChroot pathsInChroot;
|
||||||
|
|
||||||
typedef std::map<std::string, std::string> Environment;
|
typedef StringMap Environment;
|
||||||
Environment env;
|
Environment env;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -106,7 +106,7 @@ TEST(concatMapStringsSep, two)
|
||||||
|
|
||||||
TEST(concatMapStringsSep, map)
|
TEST(concatMapStringsSep, map)
|
||||||
{
|
{
|
||||||
std::map<std::string, std::string> strings;
|
StringMap strings;
|
||||||
strings["this"] = "that";
|
strings["this"] = "that";
|
||||||
strings["1"] = "one";
|
strings["1"] = "one";
|
||||||
|
|
||||||
|
|
|
@ -5,8 +5,8 @@ namespace nix {
|
||||||
|
|
||||||
/* ----------- tests for url.hh --------------------------------------------------*/
|
/* ----------- tests for url.hh --------------------------------------------------*/
|
||||||
|
|
||||||
std::string print_map(std::map<std::string, std::string> m) {
|
std::string print_map(StringMap m) {
|
||||||
std::map<std::string, std::string>::iterator it;
|
StringMap::iterator it;
|
||||||
std::string s = "{ ";
|
std::string s = "{ ";
|
||||||
for (it = m.begin(); it != m.end(); ++it) {
|
for (it = m.begin(); it != m.end(); ++it) {
|
||||||
s += "{ ";
|
s += "{ ";
|
||||||
|
|
|
@ -72,7 +72,7 @@ void SourceAccessor::dumpPath(
|
||||||
|
|
||||||
/* If we're on a case-insensitive system like macOS, undo
|
/* If we're on a case-insensitive system like macOS, undo
|
||||||
the case hack applied by restorePath(). */
|
the case hack applied by restorePath(). */
|
||||||
std::map<std::string, std::string> unhacked;
|
StringMap unhacked;
|
||||||
for (auto & i : readDirectory(path))
|
for (auto & i : readDirectory(path))
|
||||||
if (archiveSettings.useCaseHack) {
|
if (archiveSettings.useCaseHack) {
|
||||||
std::string name(i.first);
|
std::string name(i.first);
|
||||||
|
|
|
@ -21,9 +21,9 @@ std::optional<std::string> getEnvNonEmpty(const std::string & key)
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::map<std::string, std::string> getEnv()
|
StringMap getEnv()
|
||||||
{
|
{
|
||||||
std::map<std::string, std::string> env;
|
StringMap env;
|
||||||
for (size_t i = 0; environ[i]; ++i) {
|
for (size_t i = 0; environ[i]; ++i) {
|
||||||
auto s = environ[i];
|
auto s = environ[i];
|
||||||
auto eq = strchr(s, '=');
|
auto eq = strchr(s, '=');
|
||||||
|
@ -41,7 +41,7 @@ void clearEnv()
|
||||||
unsetenv(name.first.c_str());
|
unsetenv(name.first.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void replaceEnv(const std::map<std::string, std::string> & newEnv)
|
void replaceEnv(const StringMap & newEnv)
|
||||||
{
|
{
|
||||||
clearEnv();
|
clearEnv();
|
||||||
for (auto & newEnvVar : newEnv)
|
for (auto & newEnvVar : newEnv)
|
||||||
|
|
|
@ -34,7 +34,7 @@ std::optional<std::string> getEnvNonEmpty(const std::string & key);
|
||||||
/**
|
/**
|
||||||
* Get the entire environment.
|
* Get the entire environment.
|
||||||
*/
|
*/
|
||||||
std::map<std::string, std::string> getEnv();
|
StringMap getEnv();
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
/**
|
/**
|
||||||
|
@ -64,6 +64,6 @@ void clearEnv();
|
||||||
/**
|
/**
|
||||||
* Replace the entire environment with the given one.
|
* Replace the entire environment with the given one.
|
||||||
*/
|
*/
|
||||||
void replaceEnv(const std::map<std::string, std::string> & newEnv);
|
void replaceEnv(const StringMap & newEnv);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -103,7 +103,7 @@ struct RunOptions
|
||||||
std::optional<uid_t> gid;
|
std::optional<uid_t> gid;
|
||||||
#endif
|
#endif
|
||||||
std::optional<Path> chdir;
|
std::optional<Path> chdir;
|
||||||
std::optional<std::map<std::string, std::string>> environment;
|
std::optional<StringMap> environment;
|
||||||
std::optional<std::string> input;
|
std::optional<std::string> input;
|
||||||
Source * standardIn = nullptr;
|
Source * standardIn = nullptr;
|
||||||
Sink * standardOut = nullptr;
|
Sink * standardOut = nullptr;
|
||||||
|
|
|
@ -12,8 +12,25 @@
|
||||||
namespace nix {
|
namespace nix {
|
||||||
|
|
||||||
typedef std::list<std::string> Strings;
|
typedef std::list<std::string> Strings;
|
||||||
typedef std::map<std::string, std::string> StringMap;
|
|
||||||
typedef std::map<std::string, std::string> StringPairs;
|
/**
|
||||||
|
* Alias to ordered std::string -> std::string map container with transparent comparator.
|
||||||
|
*
|
||||||
|
* Used instead of std::map<std::string, std::string> to use C++14 N3657 [1]
|
||||||
|
* heterogenous lookup consistently across the whole codebase.
|
||||||
|
* Transparent comparators get rid of creation of unnecessary
|
||||||
|
* temporary variables when looking up keys by `std::string_view`
|
||||||
|
* or C-style `const char *` strings.
|
||||||
|
*
|
||||||
|
* [1]: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3657.htm
|
||||||
|
*/
|
||||||
|
using StringMap = std::map<std::string, std::string, std::less<>>;
|
||||||
|
/**
|
||||||
|
* Alias to an ordered map of std::string -> std::string. Uses transparent comparator.
|
||||||
|
*
|
||||||
|
* @see StringMap
|
||||||
|
*/
|
||||||
|
using StringPairs = StringMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Alias to ordered set container with transparent comparator.
|
* Alias to ordered set container with transparent comparator.
|
||||||
|
|
|
@ -10,7 +10,7 @@ struct ParsedURL
|
||||||
std::string scheme;
|
std::string scheme;
|
||||||
std::optional<std::string> authority;
|
std::optional<std::string> authority;
|
||||||
std::string path;
|
std::string path;
|
||||||
std::map<std::string, std::string> query;
|
StringMap query;
|
||||||
std::string fragment;
|
std::string fragment;
|
||||||
|
|
||||||
std::string to_string() const;
|
std::string to_string() const;
|
||||||
|
@ -30,9 +30,9 @@ MakeError(BadURL, Error);
|
||||||
std::string percentDecode(std::string_view in);
|
std::string percentDecode(std::string_view in);
|
||||||
std::string percentEncode(std::string_view s, std::string_view keep="");
|
std::string percentEncode(std::string_view s, std::string_view keep="");
|
||||||
|
|
||||||
std::map<std::string, std::string> decodeQuery(const std::string & query);
|
StringMap decodeQuery(const std::string & query);
|
||||||
|
|
||||||
std::string encodeQuery(const std::map<std::string, std::string> & query);
|
std::string encodeQuery(const StringMap & query);
|
||||||
|
|
||||||
ParsedURL parseURL(const std::string & url);
|
ParsedURL parseURL(const std::string & url);
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,7 @@
|
||||||
namespace nix {
|
namespace nix {
|
||||||
|
|
||||||
|
|
||||||
typedef std::map<std::string, std::string> XMLAttrs;
|
typedef std::map<std::string, std::string, std::less<>> XMLAttrs;
|
||||||
|
|
||||||
|
|
||||||
class XMLWriter
|
class XMLWriter
|
||||||
|
|
|
@ -31,9 +31,9 @@ std::optional<Path> getCgroupFS()
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: obsolete, check for cgroup2
|
// FIXME: obsolete, check for cgroup2
|
||||||
std::map<std::string, std::string> getCgroups(const Path & cgroupFile)
|
StringMap getCgroups(const Path & cgroupFile)
|
||||||
{
|
{
|
||||||
std::map<std::string, std::string> cgroups;
|
StringMap cgroups;
|
||||||
|
|
||||||
for (auto & line : tokenizeString<std::vector<std::string>>(readFile(cgroupFile), "\n")) {
|
for (auto & line : tokenizeString<std::vector<std::string>>(readFile(cgroupFile), "\n")) {
|
||||||
static std::regex regex("([0-9]+):([^:]*):(.*)");
|
static std::regex regex("([0-9]+):([^:]*):(.*)");
|
||||||
|
|
|
@ -10,7 +10,7 @@ namespace nix {
|
||||||
|
|
||||||
std::optional<Path> getCgroupFS();
|
std::optional<Path> getCgroupFS();
|
||||||
|
|
||||||
std::map<std::string, std::string> getCgroups(const Path & cgroupFile);
|
StringMap getCgroups(const Path & cgroupFile);
|
||||||
|
|
||||||
struct CgroupStats
|
struct CgroupStats
|
||||||
{
|
{
|
||||||
|
|
|
@ -70,9 +70,9 @@ std::string percentDecode(std::string_view in)
|
||||||
return decoded;
|
return decoded;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::map<std::string, std::string> decodeQuery(const std::string & query)
|
StringMap decodeQuery(const std::string & query)
|
||||||
{
|
{
|
||||||
std::map<std::string, std::string> result;
|
StringMap result;
|
||||||
|
|
||||||
for (const auto & s : tokenizeString<Strings>(query, "&")) {
|
for (const auto & s : tokenizeString<Strings>(query, "&")) {
|
||||||
auto e = s.find('=');
|
auto e = s.find('=');
|
||||||
|
@ -108,7 +108,7 @@ std::string percentEncode(std::string_view s, std::string_view keep)
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string encodeQuery(const std::map<std::string, std::string> & ss)
|
std::string encodeQuery(const StringMap & ss)
|
||||||
{
|
{
|
||||||
std::string res;
|
std::string res;
|
||||||
bool first = true;
|
bool first = true;
|
||||||
|
|
|
@ -18,7 +18,7 @@
|
||||||
|
|
||||||
using namespace nix;
|
using namespace nix;
|
||||||
|
|
||||||
typedef std::map<std::string, std::string> Channels;
|
typedef StringMap Channels;
|
||||||
|
|
||||||
static Channels channels;
|
static Channels channels;
|
||||||
static std::filesystem::path channelsList;
|
static std::filesystem::path channelsList;
|
||||||
|
|
|
@ -55,12 +55,12 @@ struct BuildEnvironment
|
||||||
|
|
||||||
using Array = std::vector<std::string>;
|
using Array = std::vector<std::string>;
|
||||||
|
|
||||||
using Associative = std::map<std::string, std::string>;
|
using Associative = StringMap;
|
||||||
|
|
||||||
using Value = std::variant<String, Array, Associative>;
|
using Value = std::variant<String, Array, Associative>;
|
||||||
|
|
||||||
std::map<std::string, Value> vars;
|
std::map<std::string, Value> vars;
|
||||||
std::map<std::string, std::string> bashFunctions;
|
StringMap bashFunctions;
|
||||||
std::optional<std::pair<std::string, std::string>> structuredAttrs;
|
std::optional<std::pair<std::string, std::string>> structuredAttrs;
|
||||||
|
|
||||||
static BuildEnvironment fromJSON(const nlohmann::json & json)
|
static BuildEnvironment fromJSON(const nlohmann::json & json)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue