1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-24 22:11:15 +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:
John Ericson 2025-05-19 17:55:23 -04:00 committed by GitHub
commit fe41bdefd7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
27 changed files with 56 additions and 39 deletions

View file

@ -336,7 +336,7 @@ struct MixEnvironment : virtual Args
StringSet keepVars;
StringSet unsetVars;
std::map<std::string, std::string> setVars;
StringMap setVars;
bool ignoreEnvironment;
MixEnvironment();

View file

@ -89,9 +89,9 @@ bool getBoolAttr(const Attrs & attrs, const std::string & name)
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) {
if (auto v = std::get_if<uint64_t>(&attr.second)) {
query.insert_or_assign(attr.first, fmt("%d", *v));

View file

@ -134,7 +134,7 @@ ParsedURL Input::toURL() const
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();
for (auto & attr : extraQuery)

View file

@ -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)$"))
throw Error("iterating over .gitmodules: %s", git_error_last()->message);
std::map<std::string, std::string> entries;
StringMap entries;
while (true) {
git_config_entry * entry = nullptr;

View file

@ -37,7 +37,7 @@ std::optional<bool> maybeGetBoolAttr(const Attrs & attrs, const std::string & na
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);

View file

@ -71,7 +71,7 @@ public:
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;

View file

@ -15,7 +15,7 @@ const static std::string subDirRegex = subDirElemRegex + "(?:/" + subDirElemRege
std::string FlakeRef::to_string() const
{
std::map<std::string, std::string> extraQuery;
StringMap extraQuery;
if (subdir != "")
extraQuery.insert_or_assign("dir", subdir);
return input.toURLString(extraQuery);

View file

@ -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<std::string, std::string> hookEnvironment = getEnv();
StringMap hookEnvironment = getEnv();
hookEnvironment.emplace("DRV_PATH", store.printStorePath(drvPath));
hookEnvironment.emplace("OUT_PATHS", chomp(concatStringsSep(" ", store.printStorePathSet(outputPaths))));

View file

@ -125,7 +125,7 @@ struct StoreDirConfigBase : Config
*/
struct StoreDirConfig : StoreDirConfigBase, MixStoreDirMethods
{
using Params = std::map<std::string, std::string>;
using Params = StringMap;
StoreDirConfig(const Params & params);

View file

@ -41,7 +41,7 @@ namespace nix {
*/
struct StoreReference
{
using Params = std::map<std::string, std::string>;
using Params = StringMap;
/**
* Special store reference `""` or `"auto"`

View file

@ -96,7 +96,7 @@ Realisation Realisation::fromJSON(
std::map <DrvOutput, StorePath> dependentRealisations;
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)});
return Realisation{

View file

@ -83,7 +83,7 @@ bool SSHMaster::isMasterRunning() {
Strings createSSHEnv()
{
// 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
// $SHELL. To keep things simple and avoid potential issues with other

View file

@ -187,7 +187,7 @@ private:
typedef std::map<Path, ChrootPath> PathsInChroot; // maps target path to source path
PathsInChroot pathsInChroot;
typedef std::map<std::string, std::string> Environment;
typedef StringMap Environment;
Environment env;
/**

View file

@ -106,7 +106,7 @@ TEST(concatMapStringsSep, two)
TEST(concatMapStringsSep, map)
{
std::map<std::string, std::string> strings;
StringMap strings;
strings["this"] = "that";
strings["1"] = "one";

View file

@ -5,8 +5,8 @@ namespace nix {
/* ----------- tests for url.hh --------------------------------------------------*/
std::string print_map(std::map<std::string, std::string> m) {
std::map<std::string, std::string>::iterator it;
std::string print_map(StringMap m) {
StringMap::iterator it;
std::string s = "{ ";
for (it = m.begin(); it != m.end(); ++it) {
s += "{ ";

View file

@ -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<std::string, std::string> unhacked;
StringMap unhacked;
for (auto & i : readDirectory(path))
if (archiveSettings.useCaseHack) {
std::string name(i.first);

View file

@ -21,9 +21,9 @@ std::optional<std::string> getEnvNonEmpty(const std::string & key)
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) {
auto s = environ[i];
auto eq = strchr(s, '=');
@ -41,7 +41,7 @@ void clearEnv()
unsetenv(name.first.c_str());
}
void replaceEnv(const std::map<std::string, std::string> & newEnv)
void replaceEnv(const StringMap & newEnv)
{
clearEnv();
for (auto & newEnvVar : newEnv)

View file

@ -34,7 +34,7 @@ std::optional<std::string> getEnvNonEmpty(const std::string & key);
/**
* Get the entire environment.
*/
std::map<std::string, std::string> getEnv();
StringMap getEnv();
#ifdef _WIN32
/**
@ -64,6 +64,6 @@ void clearEnv();
/**
* Replace the entire environment with the given one.
*/
void replaceEnv(const std::map<std::string, std::string> & newEnv);
void replaceEnv(const StringMap & newEnv);
}

View file

@ -103,7 +103,7 @@ struct RunOptions
std::optional<uid_t> gid;
#endif
std::optional<Path> chdir;
std::optional<std::map<std::string, std::string>> environment;
std::optional<StringMap> environment;
std::optional<std::string> input;
Source * standardIn = nullptr;
Sink * standardOut = nullptr;

View file

@ -12,8 +12,25 @@
namespace nix {
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.

View file

@ -10,7 +10,7 @@ struct ParsedURL
std::string scheme;
std::optional<std::string> authority;
std::string path;
std::map<std::string, std::string> 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<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);

View file

@ -10,7 +10,7 @@
namespace nix {
typedef std::map<std::string, std::string> XMLAttrs;
typedef std::map<std::string, std::string, std::less<>> XMLAttrs;
class XMLWriter

View file

@ -31,9 +31,9 @@ std::optional<Path> getCgroupFS()
}
// 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")) {
static std::regex regex("([0-9]+):([^:]*):(.*)");

View file

@ -10,7 +10,7 @@ namespace nix {
std::optional<Path> getCgroupFS();
std::map<std::string, std::string> getCgroups(const Path & cgroupFile);
StringMap getCgroups(const Path & cgroupFile);
struct CgroupStats
{

View file

@ -70,9 +70,9 @@ std::string percentDecode(std::string_view in)
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, "&")) {
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<std::string, std::string> & ss)
std::string encodeQuery(const StringMap & ss)
{
std::string res;
bool first = true;

View file

@ -18,7 +18,7 @@
using namespace nix;
typedef std::map<std::string, std::string> Channels;
typedef StringMap Channels;
static Channels channels;
static std::filesystem::path channelsList;

View file

@ -55,12 +55,12 @@ struct BuildEnvironment
using Array = std::vector<std::string>;
using Associative = std::map<std::string, std::string>;
using Associative = StringMap;
using Value = std::variant<String, Array, Associative>;
std::map<std::string, Value> vars;
std::map<std::string, std::string> bashFunctions;
StringMap bashFunctions;
std::optional<std::pair<std::string, std::string>> structuredAttrs;
static BuildEnvironment fromJSON(const nlohmann::json & json)