Chris@16: // (C) Copyright Jeremy Siek 2004 Chris@16: // Distributed under the Boost Software License, Version 1.0. (See Chris@16: // accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: #ifndef BOOST_STRINGTOK_HPP Chris@16: #define BOOST_STRINGTOK_HPP Chris@16: Chris@16: /* Chris@16: * stringtok.hpp -- Breaks a string into tokens. This is an example for lib3. Chris@16: * Chris@16: * Template function looks like this: Chris@16: * Chris@16: * template Chris@16: * void stringtok (Container &l, Chris@16: * string const &s, Chris@16: * char const * const ws = " \t\n"); Chris@16: * Chris@16: * A nondestructive version of strtok() that handles its own memory and can Chris@16: * be broken up by any character(s). Does all the work at once rather than Chris@16: * in an invocation loop like strtok() requires. Chris@16: * Chris@16: * Container is any type that supports push_back(a_string), although using Chris@16: * list and deque are indicated due to their O(1) push_back. Chris@16: * (I prefer deque<> because op[]/at() is available as well.) The first Chris@16: * parameter references an existing Container. Chris@16: * Chris@16: * s is the string to be tokenized. From the parameter declaration, it can Chris@16: * be seen that s is not affected. Since references-to-const may refer to Chris@16: * temporaries, you could use stringtok(some_container, readline("")) when Chris@16: * using the GNU readline library. Chris@16: * Chris@16: * The final parameter is an array of characters that serve as whitespace. Chris@16: * Whitespace characters default to one or more of tab, space, and newline, Chris@16: * in any combination. Chris@16: * Chris@16: * 'l' need not be empty on entry. On return, 'l' will have the token Chris@16: * strings appended. Chris@16: * Chris@16: * Chris@16: * [Example: Chris@16: * list ls; Chris@16: * stringtok (ls, " this \t is\t\n a test "); Chris@16: * for (list::const_iterator i = ls.begin(); Chris@16: * i != ls.end(); ++i) Chris@16: * { Chris@16: * cerr << ':' << (*i) << ":\n"; Chris@16: * } Chris@16: * Chris@16: * would print Chris@16: * :this: Chris@16: * :is: Chris@16: * :a: Chris@16: * :test: Chris@16: * -end example] Chris@16: * Chris@16: * pedwards@jaj.com May 1999 Chris@16: */ Chris@16: Chris@16: Chris@16: #include Chris@16: #include // for strchr Chris@16: Chris@16: Chris@16: /***************************************************************** Chris@16: * This is the only part of the implementation that I don't like. Chris@16: * It can probably be improved upon by the reader... Chris@16: */ Chris@16: Chris@16: inline bool Chris@16: isws (char c, char const * const wstr) Chris@16: { Chris@16: using namespace std; Chris@16: return (strchr(wstr,c) != NULL); Chris@16: } Chris@16: Chris@16: Chris@16: namespace boost { Chris@16: Chris@16: /***************************************************************** Chris@16: * Simplistic and quite Standard, but a bit slow. This should be Chris@16: * templatized on basic_string instead, or on a more generic StringT Chris@16: * that just happens to support ::size_type, .substr(), and so on. Chris@16: * I had hoped that "whitespace" would be a trait, but it isn't, so Chris@16: * the user must supply it. Enh, this lets them break up strings on Chris@16: * different things easier than traits would anyhow. Chris@16: */ Chris@16: template Chris@16: void Chris@16: stringtok (Container &l, std::string const &s, char const * const ws = " \t\n") Chris@16: { Chris@16: typedef std::string::size_type size_type; Chris@16: const size_type S = s.size(); Chris@16: size_type i = 0; Chris@16: Chris@16: while (i < S) { Chris@16: // eat leading whitespace Chris@16: while ((i < S) && (isws(s[i],ws))) ++i; Chris@16: if (i == S) return; // nothing left but WS Chris@16: Chris@16: // find end of word Chris@16: size_type j = i+1; Chris@16: while ((j < S) && (!isws(s[j],ws))) ++j; Chris@16: Chris@16: // add word Chris@16: l.push_back(s.substr(i,j-i)); Chris@16: Chris@16: // set up for next loop Chris@16: i = j+1; Chris@16: } Chris@16: } Chris@16: Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #endif // BOOST_STRINGTOK_HPP