mirror of
https://github.com/NixOS/nix.git
synced 2025-11-09 12:06:01 +01: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
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue