diff --git a/src/libcmd/include/nix/cmd/command.hh b/src/libcmd/include/nix/cmd/command.hh index cb436cfdb..20cd1abc1 100644 --- a/src/libcmd/include/nix/cmd/command.hh +++ b/src/libcmd/include/nix/cmd/command.hh @@ -336,7 +336,7 @@ struct MixEnvironment : virtual Args StringSet keepVars; StringSet unsetVars; - std::map setVars; + StringMap setVars; bool ignoreEnvironment; MixEnvironment(); diff --git a/src/libfetchers/attrs.cc b/src/libfetchers/attrs.cc index 47f6aa8c5..6808e8af1 100644 --- a/src/libfetchers/attrs.cc +++ b/src/libfetchers/attrs.cc @@ -89,9 +89,9 @@ bool getBoolAttr(const Attrs & attrs, const std::string & name) return *s; } -std::map attrsToQuery(const Attrs & attrs) +StringMap attrsToQuery(const Attrs & attrs) { - std::map query; + StringMap query; for (auto & attr : attrs) { if (auto v = std::get_if(&attr.second)) { query.insert_or_assign(attr.first, fmt("%d", *v)); diff --git a/src/libfetchers/fetchers.cc b/src/libfetchers/fetchers.cc index 35201e8ae..9cb896601 100644 --- a/src/libfetchers/fetchers.cc +++ b/src/libfetchers/fetchers.cc @@ -134,7 +134,7 @@ ParsedURL Input::toURL() const return scheme->toURL(*this); } -std::string Input::toURLString(const std::map & extraQuery) const +std::string Input::toURLString(const StringMap & extraQuery) const { auto url = toURL(); for (auto & attr : extraQuery) diff --git a/src/libfetchers/git-utils.cc b/src/libfetchers/git-utils.cc index 60d8edee0..09508c602 100644 --- a/src/libfetchers/git-utils.cc +++ b/src/libfetchers/git-utils.cc @@ -368,7 +368,7 @@ struct GitRepoImpl : GitRepo, std::enable_shared_from_this if (git_config_iterator_glob_new(Setter(it), config.get(), "^submodule\\..*\\.(path|url|branch)$")) throw Error("iterating over .gitmodules: %s", git_error_last()->message); - std::map entries; + StringMap entries; while (true) { git_config_entry * entry = nullptr; diff --git a/src/libfetchers/include/nix/fetchers/attrs.hh b/src/libfetchers/include/nix/fetchers/attrs.hh index 1b757d712..582abd144 100644 --- a/src/libfetchers/include/nix/fetchers/attrs.hh +++ b/src/libfetchers/include/nix/fetchers/attrs.hh @@ -37,7 +37,7 @@ std::optional maybeGetBoolAttr(const Attrs & attrs, const std::string & na bool getBoolAttr(const Attrs & attrs, const std::string & name); -std::map attrsToQuery(const Attrs & attrs); +StringMap attrsToQuery(const Attrs & attrs); Hash getRevAttr(const Attrs & attrs, const std::string & name); diff --git a/src/libfetchers/include/nix/fetchers/fetchers.hh b/src/libfetchers/include/nix/fetchers/fetchers.hh index 3288ecc5e..1f8f6bdac 100644 --- a/src/libfetchers/include/nix/fetchers/fetchers.hh +++ b/src/libfetchers/include/nix/fetchers/fetchers.hh @@ -71,7 +71,7 @@ public: ParsedURL toURL() const; - std::string toURLString(const std::map & extraQuery = {}) const; + std::string toURLString(const StringMap & extraQuery = {}) const; std::string to_string() const; diff --git a/src/libflake/flakeref.cc b/src/libflake/flakeref.cc index 12bddf578..d56f2858f 100644 --- a/src/libflake/flakeref.cc +++ b/src/libflake/flakeref.cc @@ -15,7 +15,7 @@ const static std::string subDirRegex = subDirElemRegex + "(?:/" + subDirElemRege std::string FlakeRef::to_string() const { - std::map extraQuery; + StringMap extraQuery; if (subdir != "") extraQuery.insert_or_assign("dir", subdir); return input.toURLString(extraQuery); diff --git a/src/libstore/build/derivation-goal.cc b/src/libstore/build/derivation-goal.cc index 393157938..ee848ec84 100644 --- a/src/libstore/build/derivation-goal.cc +++ b/src/libstore/build/derivation-goal.cc @@ -974,7 +974,7 @@ void runPostBuildHook( fmt("running post-build-hook '%s'", settings.postBuildHook), Logger::Fields{store.printStorePath(drvPath)}); PushActivity pact(act.id); - std::map hookEnvironment = getEnv(); + StringMap hookEnvironment = getEnv(); hookEnvironment.emplace("DRV_PATH", store.printStorePath(drvPath)); hookEnvironment.emplace("OUT_PATHS", chomp(concatStringsSep(" ", store.printStorePathSet(outputPaths)))); diff --git a/src/libstore/include/nix/store/store-dir-config.hh b/src/libstore/include/nix/store/store-dir-config.hh index 6bf9ebf14..ea0161d79 100644 --- a/src/libstore/include/nix/store/store-dir-config.hh +++ b/src/libstore/include/nix/store/store-dir-config.hh @@ -125,7 +125,7 @@ struct StoreDirConfigBase : Config */ struct StoreDirConfig : StoreDirConfigBase, MixStoreDirMethods { - using Params = std::map; + using Params = StringMap; StoreDirConfig(const Params & params); diff --git a/src/libstore/include/nix/store/store-reference.hh b/src/libstore/include/nix/store/store-reference.hh index 433a347aa..750bd8275 100644 --- a/src/libstore/include/nix/store/store-reference.hh +++ b/src/libstore/include/nix/store/store-reference.hh @@ -41,7 +41,7 @@ namespace nix { */ struct StoreReference { - using Params = std::map; + using Params = StringMap; /** * Special store reference `""` or `"auto"` diff --git a/src/libstore/realisation.cc b/src/libstore/realisation.cc index 635fb6946..9a72422eb 100644 --- a/src/libstore/realisation.cc +++ b/src/libstore/realisation.cc @@ -96,7 +96,7 @@ Realisation Realisation::fromJSON( std::map dependentRealisations; if (auto jsonDependencies = json.find("dependentRealisations"); jsonDependencies != json.end()) - for (auto & [jsonDepId, jsonDepOutPath] : jsonDependencies->get>()) + for (auto & [jsonDepId, jsonDepOutPath] : jsonDependencies->get()) dependentRealisations.insert({DrvOutput::parse(jsonDepId), StorePath(jsonDepOutPath)}); return Realisation{ diff --git a/src/libstore/ssh.cc b/src/libstore/ssh.cc index 97b75cba1..d29734a3e 100644 --- a/src/libstore/ssh.cc +++ b/src/libstore/ssh.cc @@ -83,7 +83,7 @@ bool SSHMaster::isMasterRunning() { Strings createSSHEnv() { // Copy the environment and set SHELL=/bin/sh - std::map env = getEnv(); + StringMap env = getEnv(); // SSH will invoke the "user" shell for -oLocalCommand, but that means // $SHELL. To keep things simple and avoid potential issues with other diff --git a/src/libstore/unix/build/derivation-builder.cc b/src/libstore/unix/build/derivation-builder.cc index 58e8d8ba6..f3847e604 100644 --- a/src/libstore/unix/build/derivation-builder.cc +++ b/src/libstore/unix/build/derivation-builder.cc @@ -187,7 +187,7 @@ private: typedef std::map PathsInChroot; // maps target path to source path PathsInChroot pathsInChroot; - typedef std::map Environment; + typedef StringMap Environment; Environment env; /** diff --git a/src/libutil-tests/strings.cc b/src/libutil-tests/strings.cc index f5af4e0ff..bf1f66025 100644 --- a/src/libutil-tests/strings.cc +++ b/src/libutil-tests/strings.cc @@ -106,7 +106,7 @@ TEST(concatMapStringsSep, two) TEST(concatMapStringsSep, map) { - std::map strings; + StringMap strings; strings["this"] = "that"; strings["1"] = "one"; diff --git a/src/libutil-tests/url.cc b/src/libutil-tests/url.cc index 4c089c106..c93a96d84 100644 --- a/src/libutil-tests/url.cc +++ b/src/libutil-tests/url.cc @@ -5,8 +5,8 @@ namespace nix { /* ----------- tests for url.hh --------------------------------------------------*/ - std::string print_map(std::map m) { - std::map::iterator it; + std::string print_map(StringMap m) { + StringMap::iterator it; std::string s = "{ "; for (it = m.begin(); it != m.end(); ++it) { s += "{ "; diff --git a/src/libutil/archive.cc b/src/libutil/archive.cc index 487873ce6..9069e4b49 100644 --- a/src/libutil/archive.cc +++ b/src/libutil/archive.cc @@ -72,7 +72,7 @@ void SourceAccessor::dumpPath( /* If we're on a case-insensitive system like macOS, undo the case hack applied by restorePath(). */ - std::map unhacked; + StringMap unhacked; for (auto & i : readDirectory(path)) if (archiveSettings.useCaseHack) { std::string name(i.first); diff --git a/src/libutil/environment-variables.cc b/src/libutil/environment-variables.cc index 0b668f125..adae17734 100644 --- a/src/libutil/environment-variables.cc +++ b/src/libutil/environment-variables.cc @@ -21,9 +21,9 @@ std::optional getEnvNonEmpty(const std::string & key) return value; } -std::map getEnv() +StringMap getEnv() { - std::map env; + StringMap env; for (size_t i = 0; environ[i]; ++i) { auto s = environ[i]; auto eq = strchr(s, '='); @@ -41,7 +41,7 @@ void clearEnv() unsetenv(name.first.c_str()); } -void replaceEnv(const std::map & newEnv) +void replaceEnv(const StringMap & newEnv) { clearEnv(); for (auto & newEnvVar : newEnv) diff --git a/src/libutil/include/nix/util/environment-variables.hh b/src/libutil/include/nix/util/environment-variables.hh index d6c7472fc..9b2fab4f4 100644 --- a/src/libutil/include/nix/util/environment-variables.hh +++ b/src/libutil/include/nix/util/environment-variables.hh @@ -34,7 +34,7 @@ std::optional getEnvNonEmpty(const std::string & key); /** * Get the entire environment. */ -std::map getEnv(); +StringMap getEnv(); #ifdef _WIN32 /** @@ -64,6 +64,6 @@ void clearEnv(); /** * Replace the entire environment with the given one. */ -void replaceEnv(const std::map & newEnv); +void replaceEnv(const StringMap & newEnv); } diff --git a/src/libutil/include/nix/util/processes.hh b/src/libutil/include/nix/util/processes.hh index ef7bddf2f..ab5f23e49 100644 --- a/src/libutil/include/nix/util/processes.hh +++ b/src/libutil/include/nix/util/processes.hh @@ -103,7 +103,7 @@ struct RunOptions std::optional gid; #endif std::optional chdir; - std::optional> environment; + std::optional environment; std::optional input; Source * standardIn = nullptr; Sink * standardOut = nullptr; diff --git a/src/libutil/include/nix/util/types.hh b/src/libutil/include/nix/util/types.hh index 5139256ca..edb34f5e2 100644 --- a/src/libutil/include/nix/util/types.hh +++ b/src/libutil/include/nix/util/types.hh @@ -12,8 +12,25 @@ namespace nix { typedef std::list Strings; -typedef std::map StringMap; -typedef std::map StringPairs; + +/** + * Alias to ordered std::string -> std::string map container with transparent comparator. + * + * Used instead of std::map 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>; +/** + * 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. diff --git a/src/libutil/include/nix/util/url.hh b/src/libutil/include/nix/util/url.hh index ced846787..a509f06da 100644 --- a/src/libutil/include/nix/util/url.hh +++ b/src/libutil/include/nix/util/url.hh @@ -10,7 +10,7 @@ struct ParsedURL std::string scheme; std::optional authority; std::string path; - std::map query; + StringMap query; std::string fragment; std::string to_string() const; @@ -30,9 +30,9 @@ MakeError(BadURL, Error); std::string percentDecode(std::string_view in); std::string percentEncode(std::string_view s, std::string_view keep=""); -std::map decodeQuery(const std::string & query); +StringMap decodeQuery(const std::string & query); -std::string encodeQuery(const std::map & query); +std::string encodeQuery(const StringMap & query); ParsedURL parseURL(const std::string & url); diff --git a/src/libutil/include/nix/util/xml-writer.hh b/src/libutil/include/nix/util/xml-writer.hh index 74f53b7ca..ae5a6ced7 100644 --- a/src/libutil/include/nix/util/xml-writer.hh +++ b/src/libutil/include/nix/util/xml-writer.hh @@ -10,7 +10,7 @@ namespace nix { -typedef std::map XMLAttrs; +typedef std::map> XMLAttrs; class XMLWriter diff --git a/src/libutil/linux/cgroup.cc b/src/libutil/linux/cgroup.cc index 4acfe82f1..c82fdc11c 100644 --- a/src/libutil/linux/cgroup.cc +++ b/src/libutil/linux/cgroup.cc @@ -31,9 +31,9 @@ std::optional getCgroupFS() } // FIXME: obsolete, check for cgroup2 -std::map getCgroups(const Path & cgroupFile) +StringMap getCgroups(const Path & cgroupFile) { - std::map cgroups; + StringMap cgroups; for (auto & line : tokenizeString>(readFile(cgroupFile), "\n")) { static std::regex regex("([0-9]+):([^:]*):(.*)"); diff --git a/src/libutil/linux/include/nix/util/cgroup.hh b/src/libutil/linux/include/nix/util/cgroup.hh index 6a41c6b44..eb49c3419 100644 --- a/src/libutil/linux/include/nix/util/cgroup.hh +++ b/src/libutil/linux/include/nix/util/cgroup.hh @@ -10,7 +10,7 @@ namespace nix { std::optional getCgroupFS(); -std::map getCgroups(const Path & cgroupFile); +StringMap getCgroups(const Path & cgroupFile); struct CgroupStats { diff --git a/src/libutil/url.cc b/src/libutil/url.cc index eaa2b0682..b7286072d 100644 --- a/src/libutil/url.cc +++ b/src/libutil/url.cc @@ -70,9 +70,9 @@ std::string percentDecode(std::string_view in) return decoded; } -std::map decodeQuery(const std::string & query) +StringMap decodeQuery(const std::string & query) { - std::map result; + StringMap result; for (const auto & s : tokenizeString(query, "&")) { auto e = s.find('='); @@ -108,7 +108,7 @@ std::string percentEncode(std::string_view s, std::string_view keep) return res; } -std::string encodeQuery(const std::map & ss) +std::string encodeQuery(const StringMap & ss) { std::string res; bool first = true; diff --git a/src/nix-channel/nix-channel.cc b/src/nix-channel/nix-channel.cc index bc1164a43..c4a058658 100644 --- a/src/nix-channel/nix-channel.cc +++ b/src/nix-channel/nix-channel.cc @@ -18,7 +18,7 @@ using namespace nix; -typedef std::map Channels; +typedef StringMap Channels; static Channels channels; static std::filesystem::path channelsList; diff --git a/src/nix/develop.cc b/src/nix/develop.cc index b525e5de3..37bce6ca0 100644 --- a/src/nix/develop.cc +++ b/src/nix/develop.cc @@ -55,12 +55,12 @@ struct BuildEnvironment using Array = std::vector; - using Associative = std::map; + using Associative = StringMap; using Value = std::variant; std::map vars; - std::map bashFunctions; + StringMap bashFunctions; std::optional> structuredAttrs; static BuildEnvironment fromJSON(const nlohmann::json & json)