File: stringtok_std_h.txt

package info (click to toggle)
gcc-h8300-hms 1%3A3.4.6%2Bdfsg2-4
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 94,508 kB
  • ctags: 79,901
  • sloc: ansic: 627,399; cpp: 89,017; makefile: 24,796; asm: 21,058; sh: 16,616; yacc: 3,740; perl: 718; xml: 692; lex: 587; exp: 298; awk: 223; pascal: 86; lisp: 59; sed: 37
file content (39 lines) | stat: -rw-r--r-- 1,006 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
 * Same as stringtok_h.txt, but doesn't (visiably) use C functions.
*/

#include <string>

// The std:: prefix is not used here, for readability, and a line like
// "using namespace std;" is dangerous to have in a header file.

template <typename Container>
void
stringtok (Container &container, string const &in,
           const char * const delimiters = " \t\n")
{
    const string::size_type len = in.length();
          string::size_type i = 0;

    while ( i < len )
    {
        // eat leading whitespace
        i = in.find_first_not_of (delimiters, i);
        if (i == string::npos)
            return;   // nothing left but white space

        // find the end of the token
        string::size_type j = in.find_first_of (delimiters, i);

        // push token
        if (j == string::npos) {
            container.push_back (in.substr(i));
            return;
        } else
            container.push_back (in.substr(i, j-i));

        // set up for next loop
        i = j + 1;
    }
}