SymbolStr: Remove std::string conversion

This refactoring allows the symbol table to be stored as something
other than std::strings.
This commit is contained in:
Eelco Dolstra 2024-06-06 16:33:41 +02:00
parent 0363dbf2b9
commit 61080554ab
15 changed files with 37 additions and 37 deletions

View File

@ -289,10 +289,10 @@ void SourceExprCommand::completeInstallable(AddCompletions & completions, std::s
if (v2.type() == nAttrs) {
for (auto & i : *v2.attrs()) {
std::string name = state->symbols[i.name];
std::string_view name = state->symbols[i.name];
if (name.find(searchWord) == 0) {
if (prefix_ == "")
completions.add(name);
completions.add(std::string(name));
else
completions.add(prefix_ + "." + name);
}

View File

@ -383,7 +383,7 @@ nix_value * nix_get_attr_byidx(
try {
auto & v = check_value_in(value);
const nix::Attr & a = (*v.attrs())[i];
*name = ((const std::string &) (state->state.symbols[a.name])).c_str();
*name = state->state.symbols[a.name].c_str();
nix_gc_incref(nullptr, a.value);
state->state.forceValue(*a.value, nix::noPos);
return as_nix_value_ptr(a.value);
@ -399,7 +399,7 @@ nix_get_attr_name_byidx(nix_c_context * context, const nix_value * value, EvalSt
try {
auto & v = check_value_in(value);
const nix::Attr & a = (*v.attrs())[i];
return ((const std::string &) (state->state.symbols[a.name])).c_str();
return state->state.symbols[a.name].c_str();
}
NIXC_CATCH_ERRS_NULL
}

View File

@ -76,7 +76,7 @@ std::pair<Value *, PosIdx> findAlongAttrPath(EvalState & state, const std::strin
if (!a) {
std::set<std::string> attrNames;
for (auto & attr : *v->attrs())
attrNames.insert(state.symbols[attr.name]);
attrNames.insert(std::string(state.symbols[attr.name]));
auto suggestions = Suggestions::bestMatches(attrNames, attr);
throw AttrPathNotFound(suggestions, "attribute '%1%' in selection path '%2%' not found", attr, attrPath);

View File

@ -484,7 +484,7 @@ Suggestions AttrCursor::getSuggestionsForAttr(Symbol name)
auto attrNames = getAttrs();
std::set<std::string> strAttrNames;
for (auto & name : attrNames)
strAttrNames.insert(root->state.symbols[name]);
strAttrNames.insert(std::string(root->state.symbols[name]));
return Suggestions::bestMatches(strAttrNames, root->state.symbols[name]);
}

View File

@ -633,11 +633,11 @@ void mapStaticEnvBindings(const SymbolTable & st, const StaticEnv & se, const En
if (se.isWith && !env.values[0]->isThunk()) {
// add 'with' bindings.
for (auto & j : *env.values[0]->attrs())
vm[st[j.name]] = j.value;
vm.insert_or_assign(std::string(st[j.name]), j.value);
} else {
// iterate through staticenv bindings and add them.
for (auto & i : se.vars)
vm[st[i.first]] = env.values[i.second];
vm.insert_or_assign(std::string(st[i.first]), env.values[i.second]);
}
}
}
@ -1338,7 +1338,7 @@ void ExprSelect::eval(EvalState & state, Env & env, Value & v)
if (!(j = vAttrs->attrs()->get(name))) {
std::set<std::string> allAttrNames;
for (auto & attr : *vAttrs->attrs())
allAttrNames.insert(state.symbols[attr.name]);
allAttrNames.insert(std::string(state.symbols[attr.name]));
auto suggestions = Suggestions::bestMatches(allAttrNames, state.symbols[name]);
state.error<EvalError>("attribute '%1%' missing", state.symbols[name])
.atPos(pos).withSuggestions(suggestions).withFrame(env, *this).debugThrow();
@ -1496,7 +1496,7 @@ void EvalState::callFunction(Value & fun, size_t nrArgs, Value * * args, Value &
if (!lambda.formals->has(i.name)) {
std::set<std::string> formalNames;
for (auto & formal : lambda.formals->formals)
formalNames.insert(symbols[formal.name]);
formalNames.insert(std::string(symbols[formal.name]));
auto suggestions = Suggestions::bestMatches(formalNames, symbols[i.name]);
error<TypeError>("function '%1%' called with unexpected argument '%2%'",
(lambda.name ? std::string(symbols[lambda.name]) : "anonymous lambda"),

View File

@ -342,9 +342,9 @@ std::optional<PackageInfo> getDerivation(EvalState & state, Value & v,
}
static std::string addToPath(const std::string & s1, const std::string & s2)
static std::string addToPath(const std::string & s1, std::string_view s2)
{
return s1.empty() ? s2 : s1 + "." + s2;
return s1.empty() ? std::string(s2) : s1 + "." + s2;
}

View File

@ -1225,7 +1225,7 @@ static void derivationStrictInternal(
for (auto & i : attrs->lexicographicOrder(state.symbols)) {
if (i->name == state.sIgnoreNulls) continue;
const std::string & key = state.symbols[i->name];
auto key = state.symbols[i->name];
vomit("processing attribute '%1%'", key);
auto handleHashMode = [&](const std::string_view s) {
@ -1309,7 +1309,7 @@ static void derivationStrictInternal(
if (i->name == state.sStructuredAttrs) continue;
(*jsonObject)[key] = printValueAsJSON(state, true, *i->value, pos, context);
jsonObject->emplace(key, printValueAsJSON(state, true, *i->value, pos, context));
if (i->name == state.sBuilder)
drv.builder = state.forceString(*i->value, context, pos, context_below);

View File

@ -30,9 +30,9 @@ public:
return *s == s2;
}
operator const std::string & () const
const char * c_str() const
{
return *s;
return s->c_str();
}
operator const std::string_view () const

View File

@ -58,7 +58,7 @@ json printValueAsJSON(EvalState & state, bool strict,
out = json::object();
for (auto & a : v.attrs()->lexicographicOrder(state.symbols)) {
try {
out[state.symbols[a->name]] = printValueAsJSON(state, strict, *a->value, a->pos, context, copyToStore);
out.emplace(state.symbols[a->name], printValueAsJSON(state, strict, *a->value, a->pos, context, copyToStore));
} catch (Error & e) {
e.addTrace(state.positions[a->pos],
HintFmt("while evaluating attribute '%1%'", state.symbols[a->name]));

View File

@ -9,7 +9,7 @@
namespace nix {
static XMLAttrs singletonAttrs(const std::string & name, const std::string & value)
static XMLAttrs singletonAttrs(const std::string & name, std::string_view value)
{
XMLAttrs attrs;
attrs[name] = value;

View File

@ -98,7 +98,7 @@ static std::map<FlakeId, FlakeInput> parseFlakeInputs(
const std::optional<Path> & baseDir, InputPath lockRootPath);
static FlakeInput parseFlakeInput(EvalState & state,
const std::string & inputName, Value * value, const PosIdx pos,
std::string_view inputName, Value * value, const PosIdx pos,
const std::optional<Path> & baseDir, InputPath lockRootPath)
{
expectType(state, nAttrs, *value, pos);
@ -178,7 +178,7 @@ static FlakeInput parseFlakeInput(EvalState & state,
}
if (!input.follows && !input.ref)
input.ref = FlakeRef::fromAttrs({{"type", "indirect"}, {"id", inputName}});
input.ref = FlakeRef::fromAttrs({{"type", "indirect"}, {"id", std::string(inputName)}});
return input;
}
@ -244,7 +244,7 @@ static Flake readFlake(
for (auto & formal : outputs->value->payload.lambda.fun->formals->formals) {
if (formal.name != state.sSelf)
flake.inputs.emplace(state.symbols[formal.name], FlakeInput {
.ref = parseFlakeRef(state.symbols[formal.name])
.ref = parseFlakeRef(std::string(state.symbols[formal.name]))
});
}
}

View File

@ -38,8 +38,8 @@ int levenshteinDistance(std::string_view first, std::string_view second)
}
Suggestions Suggestions::bestMatches (
std::set<std::string> allMatches,
std::string query)
const std::set<std::string> & allMatches,
std::string_view query)
{
std::set<Suggestion> res;
for (const auto & possibleMatch : allMatches) {

View File

@ -35,8 +35,8 @@ public:
) const;
static Suggestions bestMatches (
std::set<std::string> allMatches,
std::string query
const std::set<std::string> & allMatches,
std::string_view query
);
Suggestions& operator+=(const Suggestions & other);

View File

@ -165,7 +165,7 @@ struct CmdFlakeLock : FlakeCommand
};
static void enumerateOutputs(EvalState & state, Value & vFlake,
std::function<void(const std::string & name, Value & vProvide, const PosIdx pos)> callback)
std::function<void(std::string_view name, Value & vProvide, const PosIdx pos)> callback)
{
auto pos = vFlake.determinePos(noPos);
state.forceAttrs(vFlake, pos, "while evaluating a flake to get its outputs");
@ -393,15 +393,15 @@ struct CmdFlakeCheck : FlakeCommand
|| (hasPrefix(name, "_") && name.substr(1) == expected);
};
auto checkSystemName = [&](const std::string & system, const PosIdx pos) {
auto checkSystemName = [&](std::string_view system, const PosIdx pos) {
// FIXME: what's the format of "system"?
if (system.find('-') == std::string::npos)
reportError(Error("'%s' is not a valid system type, at %s", system, resolve(pos)));
};
auto checkSystemType = [&](const std::string & system, const PosIdx pos) {
auto checkSystemType = [&](std::string_view system, const PosIdx pos) {
if (!checkAllSystems && system != localSystem) {
omittedSystems.insert(system);
omittedSystems.insert(std::string(system));
return false;
} else {
return true;
@ -450,7 +450,7 @@ struct CmdFlakeCheck : FlakeCommand
}
};
auto checkOverlay = [&](const std::string & attrPath, Value & v, const PosIdx pos) {
auto checkOverlay = [&](std::string_view attrPath, Value & v, const PosIdx pos) {
try {
Activity act(*logger, lvlInfo, actUnknown,
fmt("checking overlay '%s'", attrPath));
@ -469,7 +469,7 @@ struct CmdFlakeCheck : FlakeCommand
}
};
auto checkModule = [&](const std::string & attrPath, Value & v, const PosIdx pos) {
auto checkModule = [&](std::string_view attrPath, Value & v, const PosIdx pos) {
try {
Activity act(*logger, lvlInfo, actUnknown,
fmt("checking NixOS module '%s'", attrPath));
@ -480,9 +480,9 @@ struct CmdFlakeCheck : FlakeCommand
}
};
std::function<void(const std::string & attrPath, Value & v, const PosIdx pos)> checkHydraJobs;
std::function<void(std::string_view attrPath, Value & v, const PosIdx pos)> checkHydraJobs;
checkHydraJobs = [&](const std::string & attrPath, Value & v, const PosIdx pos) {
checkHydraJobs = [&](std::string_view attrPath, Value & v, const PosIdx pos) {
try {
Activity act(*logger, lvlInfo, actUnknown,
fmt("checking Hydra job '%s'", attrPath));
@ -523,7 +523,7 @@ struct CmdFlakeCheck : FlakeCommand
}
};
auto checkTemplate = [&](const std::string & attrPath, Value & v, const PosIdx pos) {
auto checkTemplate = [&](std::string_view attrPath, Value & v, const PosIdx pos) {
try {
Activity act(*logger, lvlInfo, actUnknown,
fmt("checking template '%s'", attrPath));
@ -579,7 +579,7 @@ struct CmdFlakeCheck : FlakeCommand
enumerateOutputs(*state,
*vFlake,
[&](const std::string & name, Value & vOutput, const PosIdx pos) {
[&](std::string_view name, Value & vOutput, const PosIdx pos) {
Activity act(*logger, lvlInfo, actUnknown,
fmt("checking flake output '%s'", name));
@ -603,7 +603,7 @@ struct CmdFlakeCheck : FlakeCommand
if (name == "checks") {
state->forceAttrs(vOutput, pos, "");
for (auto & attr : *vOutput.attrs()) {
const auto & attr_name = state->symbols[attr.name];
std::string_view attr_name = state->symbols[attr.name];
checkSystemName(attr_name, attr.pos);
if (checkSystemType(attr_name, attr.pos)) {
state->forceAttrs(*attr.value, attr.pos, "");

View File

@ -429,7 +429,7 @@ void mainWrapped(int argc, char * * argv)
b["doc"] = trim(stripIndentation(primOp->doc));
if (primOp->experimentalFeature)
b["experimental-feature"] = primOp->experimentalFeature;
builtinsJson[state.symbols[builtin.name]] = std::move(b);
builtinsJson.emplace(state.symbols[builtin.name], std::move(b));
}
for (auto & [name, info] : state.constantInfos) {
auto b = nlohmann::json::object();