diff options
Diffstat (limited to 'libstdc++-v3/docs/html/21_strings/stringtok_std_h.txt')
-rw-r--r-- | libstdc++-v3/docs/html/21_strings/stringtok_std_h.txt | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/libstdc++-v3/docs/html/21_strings/stringtok_std_h.txt b/libstdc++-v3/docs/html/21_strings/stringtok_std_h.txt new file mode 100644 index 00000000000..2f3d7e07368 --- /dev/null +++ b/libstdc++-v3/docs/html/21_strings/stringtok_std_h.txt @@ -0,0 +1,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; + } +} + |