mirror of
https://github.com/NixOS/nix
synced 2025-07-05 16:31:47 +02:00
Add fresh concatStringsSep without bug
The buggy version was previously renamed to dropEmptyInitThenConcatStringsSep
This commit is contained in:
parent
79eb0adf9d
commit
a681d354e7
5 changed files with 150 additions and 0 deletions
|
@ -148,6 +148,7 @@ sources = files(
|
|||
'signature/signer.cc',
|
||||
'source-accessor.cc',
|
||||
'source-path.cc',
|
||||
'strings.cc',
|
||||
'suggestions.cc',
|
||||
'tarfile.cc',
|
||||
'terminal.cc',
|
||||
|
@ -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',
|
||||
|
|
31
src/libutil/strings-inline.hh
Normal file
31
src/libutil/strings-inline.hh
Normal file
|
@ -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
|
12
src/libutil/strings.cc
Normal file
12
src/libutil/strings.cc
Normal file
|
@ -0,0 +1,12 @@
|
|||
#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> &);
|
||||
|
||||
} // namespace nix
|
21
src/libutil/strings.hh
Normal file
21
src/libutil/strings.hh
Normal file
|
@ -0,0 +1,21 @@
|
|||
#pragma once
|
||||
|
||||
#include <list>
|
||||
#include <set>
|
||||
#include <string_view>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace nix {
|
||||
|
||||
/**
|
||||
* Concatenate the given strings with a separator between the elements.
|
||||
*/
|
||||
template<class C>
|
||||
std::string concatStringsSep(const std::string_view sep, const C & ss);
|
||||
|
||||
extern template std::string concatStringsSep(std::string_view, const std::list<std::string> &);
|
||||
extern template std::string concatStringsSep(std::string_view, const std::set<std::string> &);
|
||||
extern template std::string concatStringsSep(std::string_view, const std::vector<std::string> &);
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue