This pattern of checking if there is something in the buffer, if yes append a separator and append more data is used in several places. Optionally you could make a function to handle this or append to a std::vector<std::string>.