Skip to content

Commit

Permalink
Merge pull request NixOS#11093 from NixOS/fix-concatStringsSep
Browse files Browse the repository at this point in the history
Fix C++ `concatStringsSep`
  • Loading branch information
roberth committed Jul 14, 2024
2 parents b1effc9 + 7e604f7 commit 9d7397c
Show file tree
Hide file tree
Showing 46 changed files with 275 additions and 25 deletions.
1 change: 1 addition & 0 deletions src/build-remote/build-remote.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "serialise.hh"
#include "build-result.hh"
#include "store-api.hh"
#include "strings.hh"
#include "derivations.hh"
#include "local-store.hh"
#include "legacy.hh"
Expand Down
5 changes: 3 additions & 2 deletions src/libcmd/command.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <nlohmann/json.hpp>

#include "command.hh"
#include "markdown.hh"
#include "store-api.hh"
Expand All @@ -6,8 +8,7 @@
#include "nixexpr.hh"
#include "profiles.hh"
#include "repl.hh"

#include <nlohmann/json.hpp>
#include "strings.hh"

extern char * * environ __attribute__((weak));

Expand Down
3 changes: 3 additions & 0 deletions src/libcmd/installables.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

#include <nlohmann/json.hpp>

#include "strings-inline.hh"

namespace nix {

void completeFlakeInputPath(
Expand Down Expand Up @@ -374,6 +376,7 @@ void completeFlakeRefWithFragment(
auto attrPath2 = (*attr)->getAttrPath(attr2);
/* Strip the attrpath prefix. */
attrPath2.erase(attrPath2.begin(), attrPath2.begin() + attrPathPrefix.size());
// FIXME: handle names with dots
completions.add(flakeRefS + "#" + prefixRoot + concatStringsSep(".", evalState->symbols.resolve(attrPath2)));
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/libcmd/repl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
#include <gc/gc_cpp.h>
#endif

#include "strings.hh"

namespace nix {

/**
Expand Down
6 changes: 3 additions & 3 deletions src/libexpr/eval-cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ struct AttrDb
(key.first)
(symbols[key.second])
(AttrType::ListOfStrings)
(concatStringsSep("\t", l)).exec();
(dropEmptyInitThenConcatStringsSep("\t", l)).exec();

return state->db.getLastInsertedRowId();
});
Expand Down Expand Up @@ -435,12 +435,12 @@ std::vector<Symbol> AttrCursor::getAttrPath(Symbol name) const

std::string AttrCursor::getAttrPathStr() const
{
return concatStringsSep(".", root->state.symbols.resolve(getAttrPath()));
return dropEmptyInitThenConcatStringsSep(".", root->state.symbols.resolve(getAttrPath()));
}

std::string AttrCursor::getAttrPathStr(Symbol name) const
{
return concatStringsSep(".", root->state.symbols.resolve(getAttrPath(name)));
return dropEmptyInitThenConcatStringsSep(".", root->state.symbols.resolve(getAttrPath(name)));
}

Value & AttrCursor::forceValue()
Expand Down
2 changes: 2 additions & 0 deletions src/libexpr/eval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@

#endif

#include "strings-inline.hh"

using json = nlohmann::json;

namespace nix {
Expand Down
2 changes: 2 additions & 0 deletions src/libexpr/nixexpr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <cstdlib>
#include <sstream>

#include "strings-inline.hh"

namespace nix {

unsigned long Expr::nrExprs = 0;
Expand Down
5 changes: 5 additions & 0 deletions src/libexpr/symbol-table.hh
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public:
}

friend std::ostream & operator <<(std::ostream & os, const SymbolStr & symbol);

bool empty() const
{
return s->empty();
}
};

/**
Expand Down
2 changes: 1 addition & 1 deletion src/libflake/flake/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void ConfigFile::apply(const Settings & flakeSettings)
else if (auto* b = std::get_if<Explicit<bool>>(&value))
valueS = b->t ? "true" : "false";
else if (auto ss = std::get_if<std::vector<std::string>>(&value))
valueS = concatStringsSep(" ", *ss); // FIXME: evil
valueS = dropEmptyInitThenConcatStringsSep(" ", *ss); // FIXME: evil
else
assert(false);

Expand Down
2 changes: 2 additions & 0 deletions src/libflake/flake/lockfile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <iterator>
#include <nlohmann/json.hpp>

#include "strings.hh"

namespace nix::flake {

static FlakeRef getFlakeRef(
Expand Down
1 change: 1 addition & 0 deletions src/libmain/shared.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <openssl/crypto.h>

#include "exit.hh"
#include "strings.hh"

namespace nix {

Expand Down
2 changes: 2 additions & 0 deletions src/libstore/build/derivation-goal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

#include <nlohmann/json.hpp>

#include "strings.hh"

namespace nix {

DerivationGoal::DerivationGoal(const StorePath & drvPath,
Expand Down
1 change: 1 addition & 0 deletions src/libstore/build/entry-points.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# include "derivation-goal.hh"
#endif
#include "local-store.hh"
#include "strings.hh"

namespace nix {

Expand Down
2 changes: 2 additions & 0 deletions src/libstore/derivations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <boost/container/small_vector.hpp>
#include <nlohmann/json.hpp>

#include "strings-inline.hh"

namespace nix {

std::optional<StorePath> DerivationOutput::path(const StoreDirConfig & store, std::string_view drvName, OutputNameView outputName) const
Expand Down
4 changes: 3 additions & 1 deletion src/libstore/globals.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
#include <sys/sysctl.h>
#endif

#include "strings.hh"

namespace nix {


Expand Down Expand Up @@ -82,7 +84,7 @@ Settings::Settings()
Strings ss;
for (auto & p : tokenizeString<Strings>(*s, ":"))
ss.push_back("@" + p);
builders = concatStringsSep(" ", ss);
builders = concatStringsSep("\n", ss);
}

#if defined(__linux__) && defined(SANDBOX_SHELL)
Expand Down
2 changes: 2 additions & 0 deletions src/libstore/local-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@

#include <sqlite3.h>

#include "strings.hh"


namespace nix {

Expand Down
1 change: 1 addition & 0 deletions src/libstore/misc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "callback.hh"
#include "closure.hh"
#include "filetransfer.hh"
#include "strings.hh"

namespace nix {

Expand Down
2 changes: 2 additions & 0 deletions src/libstore/nar-info-disk-cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <sqlite3.h>
#include <nlohmann/json.hpp>

#include "strings.hh"

namespace nix {

static const char * schema = R"sql(
Expand Down
1 change: 1 addition & 0 deletions src/libstore/nar-info.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "globals.hh"
#include "nar-info.hh"
#include "store-api.hh"
#include "strings.hh"

namespace nix {

Expand Down
1 change: 1 addition & 0 deletions src/libstore/outputs-spec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "regex-combinators.hh"
#include "outputs-spec.hh"
#include "path-regex.hh"
#include "strings-inline.hh"

namespace nix {

Expand Down
1 change: 1 addition & 0 deletions src/libstore/path-info.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "store-api.hh"
#include "json-utils.hh"
#include "comparator.hh"
#include "strings.hh"

namespace nix {

Expand Down
3 changes: 3 additions & 0 deletions src/libstore/path-info.hh
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ struct ValidPathInfo : UnkeyedValidPathInfo {
*/
bool checkSignature(const Store & store, const PublicKeys & publicKeys, const std::string & sig) const;

/**
* References as store path basenames, including a self reference if it has one.
*/
Strings shortRefs() const;

ValidPathInfo(const ValidPathInfo & other) = default;
Expand Down
4 changes: 3 additions & 1 deletion src/libstore/path-with-outputs.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include <regex>

#include "path-with-outputs.hh"
#include "store-api.hh"
#include "strings.hh"

#include <regex>

namespace nix {

Expand Down
2 changes: 2 additions & 0 deletions src/libstore/store-api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include <filesystem>
#include <nlohmann/json.hpp>

#include "strings.hh"

using json = nlohmann::json;

namespace nix {
Expand Down
1 change: 1 addition & 0 deletions src/libstore/unix/build/hook-instance.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "hook-instance.hh"
#include "file-system.hh"
#include "child.hh"
#include "strings.hh"

namespace nix {

Expand Down
2 changes: 2 additions & 0 deletions src/libstore/unix/build/local-derivation-goal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
#include <grp.h>
#include <iostream>

#include "strings.hh"

namespace nix {

void handleDiffHook(
Expand Down
1 change: 1 addition & 0 deletions src/libutil/canon-path.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "canon-path.hh"
#include "util.hh"
#include "file-path-impl.hh"
#include "strings-inline.hh"

namespace nix {

Expand Down
8 changes: 5 additions & 3 deletions src/libutil/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#include <nlohmann/json.hpp>

#include "strings.hh"

namespace nix {

Config::Config(StringMap initials)
Expand Down Expand Up @@ -114,7 +116,7 @@ static void parseConfigFiles(const std::string & contents, const std::string & p
if (tokens.empty()) continue;

if (tokens.size() < 2)
throw UsageError("illegal configuration line '%1%' in '%2%'", line, path);
throw UsageError("syntax error in configuration line '%1%' in '%2%'", line, path);

auto include = false;
auto ignoreMissing = false;
Expand All @@ -127,7 +129,7 @@ static void parseConfigFiles(const std::string & contents, const std::string & p

if (include) {
if (tokens.size() != 2)
throw UsageError("illegal configuration line '%1%' in '%2%'", line, path);
throw UsageError("syntax error in configuration line '%1%' in '%2%'", line, path);
auto p = absPath(tokens[1], dirOf(path));
if (pathExists(p)) {
try {
Expand All @@ -143,7 +145,7 @@ static void parseConfigFiles(const std::string & contents, const std::string & p
}

if (tokens[1] != "=")
throw UsageError("illegal configuration line '%1%' in '%2%'", line, path);
throw UsageError("syntax error in configuration line '%1%' in '%2%'", line, path);

std::string name = std::move(tokens[0]);

Expand Down
2 changes: 2 additions & 0 deletions src/libutil/file-system.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
# include <io.h>
#endif

#include "strings-inline.hh"

namespace fs = std::filesystem;

namespace nix {
Expand Down
3 changes: 3 additions & 0 deletions src/libutil/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ sources = files(
'signature/signer.cc',
'source-accessor.cc',
'source-path.cc',
'strings.cc',
'suggestions.cc',
'tarfile.cc',
'terminal.cc',
Expand Down Expand Up @@ -215,6 +216,8 @@ headers = [config_h] + files(
'source-accessor.hh',
'source-path.hh',
'split.hh',
'strings.hh',
'strings-inline.hh',
'suggestions.hh',
'sync.hh',
'tarfile.hh',
Expand Down
31 changes: 31 additions & 0 deletions src/libutil/strings-inline.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include "strings.hh"

namespace nix {

template<class C>
std::string concatStringsSep(const std::string_view sep, const C & ss)
{
size_t size = 0;
bool tail = false;
// need a cast to string_view since this is also called with Symbols
for (const auto & s : ss) {
if (tail)
size += sep.size();
size += std::string_view(s).size();
tail = true;
}
std::string s;
s.reserve(size);
tail = false;
for (auto & i : ss) {
if (tail)
s += sep;
s += i;
tail = true;
}
return s;
}

} // namespace nix
19 changes: 19 additions & 0 deletions src/libutil/strings.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <string>

#include "strings-inline.hh"
#include "util.hh"

namespace nix {

template std::string concatStringsSep(std::string_view, const Strings &);
template std::string concatStringsSep(std::string_view, const StringSet &);
template std::string concatStringsSep(std::string_view, const std::vector<std::string> &);

typedef std::string_view strings_2[2];
template std::string concatStringsSep(std::string_view, const strings_2 &);
typedef std::string_view strings_3[3];
template std::string concatStringsSep(std::string_view, const strings_3 &);
typedef std::string_view strings_4[4];
template std::string concatStringsSep(std::string_view, const strings_4 &);

} // namespace nix
Loading

0 comments on commit 9d7397c

Please sign in to comment.