1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/ossrv_pub/boost_apis/boost/pending/stringtok.hpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,111 @@
1.4 +// (C) Copyright Jeremy Siek 2004
1.5 +// Distributed under the Boost Software License, Version 1.0. (See
1.6 +// accompanying file LICENSE_1_0.txt or copy at
1.7 +// http://www.boost.org/LICENSE_1_0.txt)
1.8 +
1.9 +/*
1.10 + * stringtok.hpp -- Breaks a string into tokens. This is an example for lib3.
1.11 + *
1.12 + * Template function looks like this:
1.13 + *
1.14 + * template <typename Container>
1.15 + * void stringtok (Container &l,
1.16 + * string const &s,
1.17 + * char const * const ws = " \t\n");
1.18 + *
1.19 + * A nondestructive version of strtok() that handles its own memory and can
1.20 + * be broken up by any character(s). Does all the work at once rather than
1.21 + * in an invocation loop like strtok() requires.
1.22 + *
1.23 + * Container is any type that supports push_back(a_string), although using
1.24 + * list<string> and deque<string> are indicated due to their O(1) push_back.
1.25 + * (I prefer deque<> because op[]/at() is available as well.) The first
1.26 + * parameter references an existing Container.
1.27 + *
1.28 + * s is the string to be tokenized. From the parameter declaration, it can
1.29 + * be seen that s is not affected. Since references-to-const may refer to
1.30 + * temporaries, you could use stringtok(some_container, readline("")) when
1.31 + * using the GNU readline library.
1.32 + *
1.33 + * The final parameter is an array of characters that serve as whitespace.
1.34 + * Whitespace characters default to one or more of tab, space, and newline,
1.35 + * in any combination.
1.36 + *
1.37 + * 'l' need not be empty on entry. On return, 'l' will have the token
1.38 + * strings appended.
1.39 + *
1.40 + *
1.41 + * [Example:
1.42 + * list<string> ls;
1.43 + * stringtok (ls, " this \t is\t\n a test ");
1.44 + * for (list<string>::const_iterator i = ls.begin();
1.45 + * i != ls.end(); ++i)
1.46 + * {
1.47 + * cerr << ':' << (*i) << ":\n";
1.48 + * }
1.49 + *
1.50 + * would print
1.51 + * :this:
1.52 + * :is:
1.53 + * :a:
1.54 + * :test:
1.55 + * -end example]
1.56 + *
1.57 + * pedwards@jaj.com May 1999
1.58 +*/
1.59 +
1.60 +
1.61 +#include <string>
1.62 +#include <cstring> // for strchr
1.63 +
1.64 +
1.65 +/*****************************************************************
1.66 + * This is the only part of the implementation that I don't like.
1.67 + * It can probably be improved upon by the reader...
1.68 +*/
1.69 +
1.70 + inline bool
1.71 + isws (char c, char const * const wstr)
1.72 + {
1.73 + using namespace std;
1.74 + return (strchr(wstr,c) != NULL);
1.75 + }
1.76 +
1.77 +
1.78 +namespace boost {
1.79 +
1.80 +/*****************************************************************
1.81 + * Simplistic and quite Standard, but a bit slow. This should be
1.82 + * templatized on basic_string instead, or on a more generic StringT
1.83 + * that just happens to support ::size_type, .substr(), and so on.
1.84 + * I had hoped that "whitespace" would be a trait, but it isn't, so
1.85 + * the user must supply it. Enh, this lets them break up strings on
1.86 + * different things easier than traits would anyhow.
1.87 +*/
1.88 +template <typename Container>
1.89 +void
1.90 +stringtok (Container &l, std::string const &s, char const * const ws = " \t\n")
1.91 +{
1.92 + typedef std::string::size_type size_type;
1.93 + const size_type S = s.size();
1.94 + size_type i = 0;
1.95 +
1.96 + while (i < S) {
1.97 + // eat leading whitespace
1.98 + while ((i < S) && (isws(s[i],ws))) ++i;
1.99 + if (i == S) return; // nothing left but WS
1.100 +
1.101 + // find end of word
1.102 + size_type j = i+1;
1.103 + while ((j < S) && (!isws(s[j],ws))) ++j;
1.104 +
1.105 + // add word
1.106 + l.push_back(s.substr(i,j-i));
1.107 +
1.108 + // set up for next loop
1.109 + i = j+1;
1.110 + }
1.111 +}
1.112 +
1.113 +
1.114 +} // namespace boost