CMS 3D CMS Logo

split.h

Go to the documentation of this file.
00001 #ifndef ParameterSet_split_h
00002 #define ParameterSet_split_h
00003 
00004 // ----------------------------------------------------------------------
00005 // $Id: split.h,v 1.7 2006/12/19 00:37:03 wmtan Exp $
00006 //
00007 // definition of split() and related templates
00008 // ----------------------------------------------------------------------
00009 
00010 
00011 // ----------------------------------------------------------------------
00012 // prolog
00013 
00014 
00015 // ----------------------------------------------------------------------
00016 // prerequisite source files and headers
00017 
00018 #include <string>
00019 
00020 
00021 // ----------------------------------------------------------------------
00022 // contents
00023 
00024 namespace edm  {
00025 
00026   template< class FwdIter >
00027   FwdIter
00028     contextual_find(FwdIter b, FwdIter e
00029                    , char first, char sep, char last);
00030 
00031   template< class FwdIter >
00032   FwdIter
00033     contextual_find_not(FwdIter b, FwdIter e
00034                        , char first, char sep, char last);
00035 
00036   template< class OutIter >
00037   bool
00038     split(OutIter result
00039          , std::string const& string_to_split
00040          , char first, char sep, char last);
00041 
00042 }  // namespace edm
00043 
00044 
00045 // ----------------------------------------------------------------------
00046 // contextual_find
00047 
00048 template< class FwdIter >
00049 FwdIter
00050   edm::contextual_find(FwdIter b, FwdIter e
00051                       , char first, char sep, char last)
00052 {
00053   for(int  nested = 0;  b != e;  ++b)  {
00054     if(*b == first)
00055       ++nested;
00056     else if(*b == last)
00057       --nested;
00058     else if(*b == sep  &&  nested == 0)
00059       return b;
00060   }
00061 
00062   return e;
00063 
00064 }  // contextual_find()
00065 
00066 
00067 // ----------------------------------------------------------------------
00068 // contextual_find_not
00069 
00070 template< class FwdIter >
00071 FwdIter
00072   edm::contextual_find_not(FwdIter b, FwdIter e
00073                           , char /* first */ , char sep, char /* last */)
00074 {
00075   for(;  b != e;  ++b)  {
00076     if(*b != sep)
00077       return b;
00078   }
00079 
00080   return e;
00081 
00082 }  // contextual_find_not()
00083 
00084 
00085 // ----------------------------------------------------------------------
00086 // split()
00087 
00088 template< class OutIter >
00089 bool
00090   edm::split(OutIter dest, std::string const& s
00091             , char first, char sep, char last)
00092 {
00093   typedef  std::string::const_iterator  str_c_iter;
00094   str_c_iter  b = s.begin()
00095            ,  e = s.end();
00096 
00097   if(static_cast<unsigned int>(e - b) < 2u)  return false;
00098 
00099   if(*b == first)  ++b;
00100   else               return false;
00101 
00102   if(*--e != last)  return false;
00103 
00104   // invariant:  we've found all items in [b..boi)
00105   for(str_c_iter  //boi = std::find_if(b, e, is_not_a(sep))
00106                    boi = contextual_find_not(b, e, first, sep, last)
00107                 ,  eoi
00108      ; boi != e
00109      //; boi = std::find_if(eoi, e, is_not_a(sep))
00110      ; boi = contextual_find_not(eoi, e, first, sep, last))
00111   {
00112     // find end of current item:
00113     //eoi = std::find_if(boi, e, is_a(sep));
00114     eoi = contextual_find(boi, e, first, sep, last);
00115 
00116     // copy the item formed from characters in [boi..eoi):
00117     *dest++ = std::string(boi, eoi);
00118   }  // for
00119 
00120   return true;
00121 }  // split< >()
00122 
00123 
00124 // ----------------------------------------------------------------------
00125 // epilog
00126 
00127 #endif

Generated on Tue Jun 9 17:36:26 2009 for CMSSW by  doxygen 1.5.4