CMS 3D CMS Logo

split.h
Go to the documentation of this file.
1 #ifndef ParameterSet_split_h
2 #define ParameterSet_split_h
3 
4 // ----------------------------------------------------------------------
5 // definition of split() and related templates
6 // ----------------------------------------------------------------------
7 
8 // ----------------------------------------------------------------------
9 // prolog
10 
11 // ----------------------------------------------------------------------
12 // prerequisite source files and headers
13 
14 #include <string>
15 
16 // ----------------------------------------------------------------------
17 // contents
18 
19 namespace edm {
20 
21  template <class FwdIter>
22  FwdIter contextual_find(FwdIter b, FwdIter e, char first, char sep, char last);
23 
24  template <class FwdIter>
25  FwdIter contextual_find_not(FwdIter b, FwdIter e, char first, char sep, char last);
26 
27  template <class OutIter>
28  bool split(OutIter result, std::string const& string_to_split, char first, char sep, char last);
29 
30 } // namespace edm
31 
32 // ----------------------------------------------------------------------
33 // contextual_find
34 
35 template <class FwdIter>
36 FwdIter edm::contextual_find(FwdIter b, FwdIter e, char first, char sep, char last) {
37  for (int nested = 0; b != e; ++b) {
38  if (*b == first)
39  ++nested;
40  else if (*b == last)
41  --nested;
42  else if (*b == sep && nested == 0)
43  return b;
44  }
45 
46  return e;
47 
48 } // contextual_find()
49 
50 // ----------------------------------------------------------------------
51 // contextual_find_not
52 
53 template <class FwdIter>
54 FwdIter edm::contextual_find_not(FwdIter b, FwdIter e, char /* first */, char sep, char /* last */) {
55  for (; b != e; ++b) {
56  if (*b != sep)
57  return b;
58  }
59 
60  return e;
61 
62 } // contextual_find_not()
63 
64 // ----------------------------------------------------------------------
65 // split()
66 
67 template <class OutIter>
68 bool edm::split(OutIter dest, std::string const& s, char first, char sep, char last) {
69  typedef std::string::const_iterator str_c_iter;
70  str_c_iter b = s.begin(), e = s.end();
71 
72  if (static_cast<unsigned int>(e - b) < 2u)
73  return false;
74 
75  if (*b == first)
76  ++b;
77  else
78  return false;
79 
80  if (*--e != last)
81  return false;
82 
83  // invariant: we've found all items in [b..boi)
84  for (str_c_iter //boi = std::find_if(b, e, is_not_a(sep))
85  boi = contextual_find_not(b, e, first, sep, last),
86  eoi;
87  boi != e
88  //; boi = std::find_if(eoi, e, is_not_a(sep))
89  ;
90  boi = contextual_find_not(eoi, e, first, sep, last)) {
91  // find end of current item:
92  //eoi = std::find_if(boi, e, is_a(sep));
93  eoi = contextual_find(boi, e, first, sep, last);
94 
95  // copy the item formed from characters in [boi..eoi):
96  *dest++ = std::string(boi, eoi);
97  } // for
98 
99  return true;
100 } // split< >()
101 
102 // ----------------------------------------------------------------------
103 // epilog
104 
105 #endif
bool split(OutIter result, std::string const &string_to_split, char first, char sep, char last)
Definition: split.h:68
FwdIter contextual_find_not(FwdIter b, FwdIter e, char first, char sep, char last)
Definition: split.h:54
double b
Definition: hdecay.h:120
HLT enums.
FwdIter contextual_find(FwdIter b, FwdIter e, char first, char sep, char last)
Definition: split.h:36
T first(std::pair< T, U > const &p)