The purpose of this function has little to do with immutability. Value's strings are never mutated, and the point of this function is to singleton empty strings. Change-Id: Ifd41dd952409d54e4d3de9ab59064e6928b0e480
24 lines
380 B
C++
24 lines
380 B
C++
#include "gc-alloc.hh"
|
|
|
|
#include <cstring>
|
|
#include <string_view>
|
|
|
|
namespace nix
|
|
{
|
|
|
|
char const * gcCopyStringIfNeeded(std::string_view toCopyFrom)
|
|
{
|
|
if (toCopyFrom.empty()) {
|
|
return "";
|
|
}
|
|
|
|
size_t const size = toCopyFrom.size();
|
|
char * cstr = gcAllocString(size + 1);
|
|
memcpy(cstr, toCopyFrom.data(), size);
|
|
cstr[size] = '\0';
|
|
|
|
return cstr;
|
|
}
|
|
|
|
}
|