CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 // ----------------------------------------------------------------------
10 // prolog
11 
12 
13 // ----------------------------------------------------------------------
14 // prerequisite source files and headers
15 
16 #include <string>
17 
18 
19 // ----------------------------------------------------------------------
20 // contents
21 
22 namespace edm {
23 
24  template< class FwdIter >
25  FwdIter
26  contextual_find(FwdIter b, FwdIter e
27  , char first, char sep, char last);
28 
29  template< class FwdIter >
30  FwdIter
31  contextual_find_not(FwdIter b, FwdIter e
32  , char first, char sep, char last);
33 
34  template< class OutIter >
35  bool
36  split(OutIter result
37  , std::string const& string_to_split
38  , char first, char sep, char last);
39 
40 } // namespace edm
41 
42 
43 // ----------------------------------------------------------------------
44 // contextual_find
45 
46 template< class FwdIter >
47 FwdIter
48  edm::contextual_find(FwdIter b, FwdIter e
49  , char first, char sep, char last)
50 {
51  for(int nested = 0; b != e; ++b) {
52  if(*b == first)
53  ++nested;
54  else if(*b == last)
55  --nested;
56  else if(*b == sep && nested == 0)
57  return b;
58  }
59 
60  return e;
61 
62 } // contextual_find()
63 
64 
65 // ----------------------------------------------------------------------
66 // contextual_find_not
67 
68 template< class FwdIter >
69 FwdIter
70  edm::contextual_find_not(FwdIter b, FwdIter e
71  , char /* first */ , char sep, char /* last */)
72 {
73  for(; b != e; ++b) {
74  if(*b != sep)
75  return b;
76  }
77 
78  return e;
79 
80 } // contextual_find_not()
81 
82 
83 // ----------------------------------------------------------------------
84 // split()
85 
86 template< class OutIter >
87 bool
88  edm::split(OutIter dest, std::string const& s
89  , char first, char sep, char last)
90 {
91  typedef std::string::const_iterator str_c_iter;
92  str_c_iter b = s.begin()
93  , e = s.end();
94 
95  if(static_cast<unsigned int>(e - b) < 2u) return false;
96 
97  if(*b == first) ++b;
98  else return false;
99 
100  if(*--e != last) return false;
101 
102  // invariant: we've found all items in [b..boi)
103  for(str_c_iter //boi = std::find_if(b, e, is_not_a(sep))
104  boi = contextual_find_not(b, e, first, sep, last)
105  , eoi
106  ; boi != e
107  //; boi = std::find_if(eoi, e, is_not_a(sep))
108  ; boi = contextual_find_not(eoi, e, first, sep, last))
109  {
110  // find end of current item:
111  //eoi = std::find_if(boi, e, is_a(sep));
112  eoi = contextual_find(boi, e, first, sep, last);
113 
114  // copy the item formed from characters in [boi..eoi):
115  *dest++ = std::string(boi, eoi);
116  } // for
117 
118  return true;
119 } // split< >()
120 
121 
122 // ----------------------------------------------------------------------
123 // epilog
124 
125 #endif
tuple result
Definition: query.py:137
list nested
Definition: CommonUtil.py:281
bool split(OutIter result, std::string const &string_to_split, char first, char sep, char last)
Definition: split.h:88
bool first
Definition: L1TdeRCT.cc:94
FwdIter contextual_find_not(FwdIter b, FwdIter e, char first, char sep, char last)
Definition: split.h:70
double b
Definition: hdecay.h:120
FwdIter contextual_find(FwdIter b, FwdIter e, char first, char sep, char last)
Definition: split.h:48
T first(std::pair< T, U > const &p)