00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 
00038 
00039 
00040 
00041 
00042 #include "pcre++.h"
00043 
00044 using namespace std;
00045 using namespace pcrepp;
00046 
00047 
00048 
00049 
00050 
00051 vector<string> Pcre::_split(const string& piece, int limit, int start_offset, int end_offset) {
00052   vector<string> Splitted;
00053   
00054   if(_expression.length() == 1) {
00055     
00056     string buffer, _delimiter, _piece;
00057     char z;
00058     if(case_t) {
00059       z = toupper(_expression[0]);
00060       for(size_t pos=0; pos < piece.length(); pos++) {
00061         _piece += (char)toupper(piece[pos]);
00062       }
00063     }
00064     else {
00065       z = _expression[0];
00066       _piece = piece;
00067     }
00068     for(size_t pos=0; pos<piece.length(); pos++) {
00069       if(_piece[pos] == z) {
00070         Splitted.push_back(buffer);
00071         buffer = "";
00072       }
00073       else {
00074         buffer += piece[pos];
00075       }
00076     }
00077     if(buffer != "") {
00078       Splitted.push_back(buffer);
00079     }
00080   }
00081   else {
00082     
00083     if(_expression[0] != '(' && _expression[ _expression.length() - 1 ] != ')' ) {
00084       
00085       pcre_free(p_pcre);
00086       pcre_free(p_pcre_extra);
00087       
00088       pcre       *_p = NULL;
00089       pcre_extra *_e = NULL;;
00090 
00091       p_pcre = _p;
00092       p_pcre_extra = _e;
00093 
00094       _expression = "(" + _expression + ")";
00095       Compile(_flags);
00096     }
00097     int num_pieces=0, pos=0, piece_end = 0, piece_start = 0;
00098     for(;;) {
00099       if(search(piece, pos) == true) {
00100         if(matches() > 0) {
00101           piece_end   = get_match_start(0) - 1;
00102           piece_start = pos;
00103           pos = piece_end + 1 + get_match_length(0);
00104           string junk(piece, piece_start, (piece_end - piece_start)+1);
00105           num_pieces++;
00106           if( (limit != 0 && num_pieces < limit) || limit == 0) {
00107             if( (start_offset != 0 && num_pieces >= start_offset) || start_offset == 0) {
00108               if( (end_offset != 0 && num_pieces <= end_offset) || end_offset == 0) {
00109                 
00110                 Splitted.push_back(junk);
00111               }
00112             }
00113           }
00114         }
00115       }
00116       else {
00117         
00118         string junk(piece, pos, (piece.length() - pos));
00119         num_pieces++;
00120         if( (limit != 0 && num_pieces < limit) || limit == 0) {
00121           if( (start_offset != 0 && num_pieces >= start_offset) || start_offset == 0) {
00122             if( (end_offset != 0 && num_pieces <= end_offset) || end_offset == 0) {
00123               
00124               Splitted.push_back(junk);
00125             }
00126           }
00127         }
00128         break;
00129       }
00130     } 
00131   } 
00132   return Splitted;
00133 }
00134 
00135 vector<string> Pcre::split(const string& piece) {
00136   return _split(piece, 0, 0, 0);
00137 }
00138 
00139 vector<string> Pcre::split(const string& piece, int limit) {
00140   return _split(piece, limit, 0, 0);
00141 }
00142 
00143 vector<string> Pcre::split(const string& piece, int limit, int start_offset) {
00144   return _split(piece, limit, start_offset, 0);
00145 }
00146 
00147 vector<string> Pcre::split(const string& piece, int limit, int start_offset, int end_offset) {
00148   return _split(piece, limit, start_offset, end_offset);
00149 }
00150 
00151 vector<string> Pcre::split(const string& piece, vector<int> positions) {
00152   vector<string> PreSplitted = _split(piece, 0, 0, 0);
00153   vector<string> Splitted;
00154   for(vector<int>::iterator vecIt=positions.begin(); vecIt != positions.end(); ++vecIt) {
00155     Splitted.push_back(PreSplitted[*vecIt]);
00156   }
00157   return Splitted;
00158 }