1
0
Fork 0

writing variable length strings gets a null terminator

This commit is contained in:
Kevin Matz 2023-05-13 11:24:31 -04:00
parent 5f98768a8a
commit 798fe44ca4
1 changed files with 3 additions and 3 deletions

View File

@ -390,18 +390,18 @@ void bufferstream::readString(std::string &str, const int fixed_length, const bo
* @param str
* @param fixed_length write this length to the stream, padding with null
* if str is shorter than fixed_length. If 0, will write the unrestricted
* contents of str.
* contents of str, including it's null termination.
* @param terminated If true, the last byte of fixed_length is guaranteed to
* be a 0 (null) byte.
*/
void bufferstream::writeString(const std::string &str, const size_t fixed_length,
const bool terminated)
const bool terminated)
{
// fixed_length == 0 means dynamic length.
bool fixed = fixed_length > 0 ? true : false;
// fixed length strings restricted to fixed_lengh characters
size_t maxlen = fixed ? fixed_length : str.size();
size_t maxlen = fixed ? fixed_length : str.size() + 1; // null terminated
// write no more of the string than is available
size_t wrtlen = str.size() < maxlen ? str.size() : maxlen;