Tag Archives: terminated

Parsing null terminated strings from a stream

During a project this week, my project partner and I ran into a problem that is somewhat interesting: how do you parse a string whose delimiters are null characters (‘\0’)? For example, how do you separate something like this:

This\0is\0a\0null\0character\0separated\0string

…into something like this:

This
is
a
null
character
separated
string

The problem is, of course, that you can’t put this into a std::string and iterate over the string to separate it out, because std::string terminates on the null character. So doing something like this:

std::string str = std::string("This\0is\0a\0null\0character\0separated\0string");

…results in str returning only "This" when it is used, and iterating would stop at the first null character.

So what can we do? Assuming our null terminated string is of type char *, we can actually use this string constructor behavior to our advantage by pointing at the beginning of each part of the string, then moving forward past the null terminator until we’ve reached the end of our character stream.

std::vector<std::string> splitOnNullTerminator(char *str, int length) {
    const char* p = str;
    std::vector<std::string> vector;
 
    do {
        vector.push_back(std::string(p));
        p += vector.back().size() + 1;
    } while (p - str < length);
    return vector;
}

This solution assumes, of course, that str is contiguous in memory (i.e. it’s a char array). But it is elegant, don’t you think?