Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:28:59

0001 #ifndef ParameterSet_split_h
0002 #define ParameterSet_split_h
0003 
0004 // ----------------------------------------------------------------------
0005 // definition of split() and related templates
0006 // ----------------------------------------------------------------------
0007 
0008 // ----------------------------------------------------------------------
0009 // prolog
0010 
0011 // ----------------------------------------------------------------------
0012 // prerequisite source files and headers
0013 
0014 #include <string>
0015 
0016 // ----------------------------------------------------------------------
0017 // contents
0018 
0019 namespace edm {
0020 
0021   template <class FwdIter>
0022   FwdIter contextual_find(FwdIter b, FwdIter e, char first, char sep, char last);
0023 
0024   template <class FwdIter>
0025   FwdIter contextual_find_not(FwdIter b, FwdIter e, char first, char sep, char last);
0026 
0027   template <class OutIter>
0028   bool split(OutIter result, std::string const& string_to_split, char first, char sep, char last);
0029 
0030 }  // namespace edm
0031 
0032 // ----------------------------------------------------------------------
0033 // contextual_find
0034 
0035 template <class FwdIter>
0036 FwdIter edm::contextual_find(FwdIter b, FwdIter e, char first, char sep, char last) {
0037   for (int nested = 0; b != e; ++b) {
0038     if (*b == first)
0039       ++nested;
0040     else if (*b == last)
0041       --nested;
0042     else if (*b == sep && nested == 0)
0043       return b;
0044   }
0045 
0046   return e;
0047 
0048 }  // contextual_find()
0049 
0050 // ----------------------------------------------------------------------
0051 // contextual_find_not
0052 
0053 template <class FwdIter>
0054 FwdIter edm::contextual_find_not(FwdIter b, FwdIter e, char /* first */, char sep, char /* last */) {
0055   for (; b != e; ++b) {
0056     if (*b != sep)
0057       return b;
0058   }
0059 
0060   return e;
0061 
0062 }  // contextual_find_not()
0063 
0064 // ----------------------------------------------------------------------
0065 // split()
0066 
0067 template <class OutIter>
0068 bool edm::split(OutIter dest, std::string const& s, char first, char sep, char last) {
0069   typedef std::string::const_iterator str_c_iter;
0070   str_c_iter b = s.begin(), e = s.end();
0071 
0072   if (static_cast<unsigned int>(e - b) < 2u)
0073     return false;
0074 
0075   if (*b == first)
0076     ++b;
0077   else
0078     return false;
0079 
0080   if (*--e != last)
0081     return false;
0082 
0083   // invariant:  we've found all items in [b..boi)
0084   for (str_c_iter  //boi = std::find_if(b, e, is_not_a(sep))
0085            boi = contextual_find_not(b, e, first, sep, last),
0086            eoi;
0087        boi != e
0088        //; boi = std::find_if(eoi, e, is_not_a(sep))
0089        ;
0090        boi = contextual_find_not(eoi, e, first, sep, last)) {
0091     // find end of current item:
0092     //eoi = std::find_if(boi, e, is_a(sep));
0093     eoi = contextual_find(boi, e, first, sep, last);
0094 
0095     // copy the item formed from characters in [boi..eoi):
0096     *dest++ = std::string(boi, eoi);
0097   }  // for
0098 
0099   return true;
0100 }  // split< >()
0101 
0102 // ----------------------------------------------------------------------
0103 // epilog
0104 
0105 #endif