1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-07-06 21:41:48 +02:00

Add withBuffer

... to avoid non-standard, unidiomatic alloca.
This commit is contained in:
Robert Hensing 2022-01-06 14:31:23 +01:00
parent d038a67bd3
commit 55c58580be
2 changed files with 21 additions and 18 deletions

View file

@ -272,25 +272,19 @@ Derivation parseDerivation(const Store & store, std::string && s, std::string_vi
static void printString(string & res, std::string_view s)
{
char * buf;
size_t bufSize = s.size() * 2 + 2;
std::unique_ptr<char[]> dynBuf;
if (bufSize < 0x10000) {
buf = (char *)alloca(bufSize);
} else {
dynBuf = decltype(dynBuf)(new char[bufSize]);
buf = dynBuf.get();
}
char * p = buf;
*p++ = '"';
for (auto c : s)
if (c == '\"' || c == '\\') { *p++ = '\\'; *p++ = c; }
else if (c == '\n') { *p++ = '\\'; *p++ = 'n'; }
else if (c == '\r') { *p++ = '\\'; *p++ = 'r'; }
else if (c == '\t') { *p++ = '\\'; *p++ = 't'; }
else *p++ = c;
*p++ = '"';
res.append(buf, p - buf);
withBuffer<void, char>(bufSize, [&](char buf[]) {
char * p = buf;
*p++ = '"';
for (auto c : s)
if (c == '\"' || c == '\\') { *p++ = '\\'; *p++ = c; }
else if (c == '\n') { *p++ = '\\'; *p++ = 'n'; }
else if (c == '\r') { *p++ = '\\'; *p++ = 'r'; }
else if (c == '\t') { *p++ = '\\'; *p++ = 't'; }
else *p++ = c;
*p++ = '"';
res.append(buf, p - buf);
});
}