Chapter 37. A C++-ish strtok(), and join()

Two template functions implement strtok()-like functionality that's more C++-like than the standard C library version:

#include <x/strtok.H>

std::string str;

std::list<std::string> container;

x::strtok_str(str, " \t\r\n", container);

strtok_str() works with either narrow or wide character strings. The second argument sets the list of delimiter characters. Consecutive sequences of characters in the first parameter are delimited by characters in the delimiter character list. The sequences extracted and added to the container passed as the third argument. The third argument can be any container that implements push_back(). The above example extracts words from the string delimited by whitespace characters, and appends them to the container.

#include <x/strtok.H>

std::string str;

std::list<std::string> container;

x::strtok_if(str,
             [] (char c)
             {
                return c == ' '|| c == '\t' || c == '\n' ||
                    c == '\r';
             }, container);

The strtok_if() function uses a functor or a predicate rather than a literal string, to define delimiter characters. The functor takes one parameter, a single character, and returns true if the character should be considered a delimiter character.

x::join() is the opposite of strtok(), and combines a collection of strings together, with a separator string sandwiched in between:

#include <x/join.H>
#include <x/dir.H>
#include <iostream>
#include <iterator>

int main()
{
    std::set<std::string> files;

    x::dir dir(".");

    std::copy(dir.begin(), dir.end(),
          std::insert_iterator<std::set<std::string> >(files,
                                   files.end()));

    std::cout << x::join(files, "\n") << std::endl;
    return 0;
}

x::join() is heavily overloaded. The collection of strings can be specified as a container, or as an input sequence defined by a beginning and an ending iterator. The separator string can be a string object or a literal string. The resulting joined string can be written to an output iterator, or returned as a single string.