split.h
Go to the documentation of this file.
1 #ifndef cetlib_split_h
2 #define cetlib_split_h
3 
4 // ======================================================================
5 //
6 // split: Obtain substrings at a string's specified boundaries.
7 //
8 // Examples of splitting with ':' as the separator:
9 // input result
10 // "" [ ] # Different behavior than split_by_regex.h
11 // "a" ["a"]
12 // "a:" ["a"]
13 // ":boo" ["boo"] # Different behavior than split_by_regex.h
14 // "a:b" ["a","b"]
15 // "a::b" ["a","b"] // N.B.! # Different behavior than split_by_regex.h
16 //
17 // ======================================================================
18 
19 #include <algorithm>
20 #include <functional>
21 #include <string>
22 
23 namespace cet {
24  template <class OutIter>
25  void split(std::string const& s, char c, OutIter dest);
26 
27  template <class Pred, class OutIter>
28  void split_if(std::string const& s, Pred is_sep, OutIter dest);
29 }
30 
31 // ======================================================================
32 
33 template <class OutIter>
34 void
35 cet::split(std::string const& s, char c, OutIter dest)
36 {
37  split_if(s, [c](char x) { return x == c; }, dest);
38 }
39 
40 // ======================================================================
41 
42 template <class Pred, class OutIter>
43 void
44 cet::split_if(std::string const& s, Pred is_sep, OutIter dest)
45 {
46  auto const e = s.cend();
47  auto const is_not_sep = [is_sep](auto const c) { return !is_sep(c); };
48  auto const new_boi = [is_not_sep, &e](auto const si) {
49  return std::find_if(si, e, is_not_sep);
50  };
51  // invariant: we've found all items in [b..boi)
52  // e is an arbitrary value to use as the initializer
53  for (typename std::remove_const<decltype(e)>::type boi = new_boi(s.cbegin()),
54  eoi = e;
55  boi != e;
56  boi = new_boi(eoi)) // advance to next non-separator
57  {
58  // find end of current item:
59  eoi = std::find_if(boi, e, is_sep);
60 
61  // copy the item formed from characters in [boi..eoi):
62  *dest = std::string(boi, eoi);
63  ++dest;
64  } // for
65 
66 } // split_if<>()
67 
68 // ======================================================================
69 
70 #endif /* cetlib_split_h */
71 
72 // Local variables:
73 // mode:c++
74 // End:
std::string string
Definition: nybbler.cc:12
void split_if(std::string const &s, Pred is_sep, OutIter dest)
Definition: split.h:44
const double e
list x
Definition: train.py:276
void split(std::string const &s, char c, OutIter dest)
Definition: split.h:35
static QCString * s
Definition: config.cpp:1042