2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libstdc++-v3 / docs / html / 21_strings / stringtok_std_h.txt
blob2f3d7e073684aef9fc676fb7eedd165253d43826
1 /*
2  * Same as stringtok_h.txt, but doesn't (visiably) use C functions.
3 */
5 #include <string>
7 // The std:: prefix is not used here, for readability, and a line like
8 // "using namespace std;" is dangerous to have in a header file.
10 template <typename Container>
11 void
12 stringtok (Container &container, string const &in,
13            const char * const delimiters = " \t\n")
15     const string::size_type len = in.length();
16           string::size_type i = 0;
18     while ( i < len )
19     {
20         // eat leading whitespace
21         i = in.find_first_not_of (delimiters, i);
22         if (i == string::npos)
23             return;   // nothing left but white space
25         // find the end of the token
26         string::size_type j = in.find_first_of (delimiters, i);
28         // push token
29         if (j == string::npos) {
30             container.push_back (in.substr(i));
31             return;
32         } else
33             container.push_back (in.substr(i, j-i));
35         // set up for next loop
36         i = j + 1;
37     }