I noticed an extra null when trying to grep dfhack.run ls output.
std::string manages null byte at end(). Pre C++11 didn't require null
termination for std::string but C++11 add the null termination to make
c_str() and data() truely const. It is undefined behavior to modify the
internal null termination but this modification sets the null to null.
vsnprintf man page claims:
"If an output error is encountered, a negative value is returned."
That means we has to call vsnprintf twice at most to have whole output
written to a string. But in case of error we return an empty string.
The code also optimizes an expected common case of outputting single
line with a small stack allocated buffer. If the stack buffer is too
small then it uses std::string::resize to allocate exactly enough memory
and writes directly to std::string.
Second call could use vsprintf because memory is known to be large
enough. But I think that difference isn't detectable outside micro
benchmarks.
The idea is to make ui handling more modular, dispensing with
huge functions that switch or if/else on lots of variables.
For now, used to split up functions in the sort plugin.
For more flexibility, the base api is split into 3 phases:
alloc, setSize, and construct. No support for non-actual
buildings like stockpiles and activity zones at the moment.