Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:59:37

0001 //  (C) Copyright Jeremy Siek 2004
0002 //  Distributed under the Boost Software License, Version 1.0. (See
0003 //  accompanying file LICENSE_1_0.txt or copy at
0004 //  http://www.boost.org/LICENSE_1_0.txt)
0005 
0006 #ifndef BOOST_STRINGTOK_HPP
0007 #define BOOST_STRINGTOK_HPP
0008 
0009 /*
0010  * stringtok.hpp -- Breaks a string into tokens.  This is an example for lib3.
0011  *
0012  * Template function looks like this:
0013  *
0014  *    template <typename Container>
0015  *    void stringtok (Container &l,
0016  *                    string const &s,
0017  *                    char const * const ws = " \t\n");
0018  *
0019  * A nondestructive version of strtok() that handles its own memory and can
0020  * be broken up by any character(s).  Does all the work at once rather than
0021  * in an invocation loop like strtok() requires.
0022  *
0023  * Container is any type that supports push_back(a_string), although using
0024  * list<string> and deque<string> are indicated due to their O(1) push_back.
0025  * (I prefer deque<> because op[]/at() is available as well.)  The first
0026  * parameter references an existing Container.
0027  *
0028  * s is the string to be tokenized.  From the parameter declaration, it can
0029  * be seen that s is not affected.  Since references-to-const may refer to
0030  * temporaries, you could use stringtok(some_container, readline("")) when
0031  * using the GNU readline library.
0032  *
0033  * The final parameter is an array of characters that serve as whitespace.
0034  * Whitespace characters default to one or more of tab, space, and newline,
0035  * in any combination.
0036  *
0037  * 'l' need not be empty on entry.  On return, 'l' will have the token
0038  * strings appended.
0039  *
0040  *
0041  * [Example:
0042  *       list<string>       ls;
0043  *       stringtok (ls, " this  \t is\t\n  a test  ");
0044  *       for (list<string>::const_iterator i = ls.begin();
0045  *            i != ls.end(); ++i)
0046  *       {
0047  *            cerr << ':' << (*i) << ":\n";
0048  *       }
0049  *
0050  *  would print
0051  *       :this:
0052  *       :is:
0053  *       :a:
0054  *       :test:
0055  * -end example]
0056  *
0057  * pedwards@jaj.com  May 1999
0058  */
0059 
0060 #include <string>
0061 #include <cstring> // for strchr
0062 
0063 /*****************************************************************
0064  * This is the only part of the implementation that I don't like.
0065  * It can probably be improved upon by the reader...
0066  */
0067 
0068 inline bool isws(char c, char const* const wstr)
0069 {
0070     using namespace std;
0071     return (strchr(wstr, c) != NULL);
0072 }
0073 
0074 namespace boost
0075 {
0076 
0077 /*****************************************************************
0078  * Simplistic and quite Standard, but a bit slow.  This should be
0079  * templatized on basic_string instead, or on a more generic StringT
0080  * that just happens to support ::size_type, .substr(), and so on.
0081  * I had hoped that "whitespace" would be a trait, but it isn't, so
0082  * the user must supply it.  Enh, this lets them break up strings on
0083  * different things easier than traits would anyhow.
0084  */
0085 template < typename Container >
0086 void stringtok(
0087     Container& l, std::string const& s, char const* const ws = " \t\n")
0088 {
0089     typedef std::string::size_type size_type;
0090     const size_type S = s.size();
0091     size_type i = 0;
0092 
0093     while (i < S)
0094     {
0095         // eat leading whitespace
0096         while ((i < S) && (isws(s[i], ws)))
0097             ++i;
0098         if (i == S)
0099             return; // nothing left but WS
0100 
0101         // find end of word
0102         size_type j = i + 1;
0103         while ((j < S) && (!isws(s[j], ws)))
0104             ++j;
0105 
0106         // add word
0107         l.push_back(s.substr(i, j - i));
0108 
0109         // set up for next loop
0110         i = j + 1;
0111     }
0112 }
0113 
0114 } // namespace boost
0115 
0116 #endif // BOOST_STRINGTOK_HPP