monotone

monotone Mtn Source Tree

Root/stringtok.hh

1// copyright (C) 2005 Christof Petig <christof@petig-baender.de>
2// all rights reserved.
3// licensed to the public under the terms of the GNU GPL (>= 2)
4// see the file COPYING for details
5
6// inspired by code from Marcelo E. Magallon and the libstdc++ doku
7
8#include <string>
9
10template <typename Container>
11void
12stringtok (Container &container, std::string const &in,
13 const char * const delimiters = " \t\n")
14{
15 const std::string::size_type len = in.length();
16 std::string::size_type i = 0;
17
18 while ( i < len )
19 {
20 // find the end of the token
21 std::string::size_type j = in.find_first_of (delimiters, i);
22
23 // push token
24 if (j == std::string::npos) {
25 container.push_back (in.substr(i));
26 return;
27 } else
28 container.push_back (in.substr(i, j-i));
29
30 // set up for next loop
31 i = j + 1;
32 }
33}
34
35// an adaptor giving push_back on insert providing containers (sets)
36template <typename Container>
37 class push_back2insert
38{ Container &c;
39public:
40 push_back2insert(Container &_c) : c(_c) {}
41 template <typename T>
42 void push_back(const T &t) const { c.insert(t); }
43};
44

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status