split.cc Source File

Main Page Namespace List Compound List File List Compound Members File Members

# split.cc

Go to the documentation of this file.

    00001 /*
    00002  *
    00003  *  This file  is part of the PCRE++ Class Library.
    00004  *
    00005  *  By  accessing  this software,  PCRE++, you  are  duly informed
    00006  *  of and agree to be  bound  by the  conditions  described below
    00007  *  in this notice:
    00008  *
    00009  *  This software product,  PCRE++,  is developed by Thomas Linden
    00010  *  and copyrighted (C) 2002-2003 by Thomas Linden,with all rights
    00011  *  reserved.
    00012  *
    00013  *  There  is no charge for PCRE++ software.  You can redistribute
    00014  *  it and/or modify it under the terms of the GNU  Lesser General
    00015  *  Public License, which is incorporated by reference herein.
    00016  *
    00017  *  PCRE++ is distributed WITHOUT ANY WARRANTY, IMPLIED OR EXPRESS,
    00018  *  OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE or that
    00019  *  the use of it will not infringe on any third party's intellec-
    00020  *  tual property rights.
    00021  *
    00022  *  You should have received a copy of the GNU Lesser General Public
    00023  *  License along with PCRE++.  Copies can also be obtained from:
    00024  *
    00025  *    http://www.gnu.org/licenses/lgpl.txt
    00026  *
    00027  *  or by writing to:
    00028  *
    00029  *  Free Software Foundation, Inc.
    00030  *  59 Temple Place, Suite 330
    00031  *  Boston, MA 02111-1307
    00032  *  USA
    00033  *
    00034  *  Or contact:
    00035  *
    00036  *   "Thomas Linden" <tom@daemon.de>
    00037  *
    00038  *
    00039  */
    00040
    00041
    00042 #include "pcre++.h"
    00043
    00044 using namespace std;
    00045 using namespace pcrepp;
    00046
    00047 /*
    00048  * split method and family
    00049  */
    00050
    00051 vector<string> Pcre::_split(const string& piece, int limit, int start_offset, int end_offset) {
    00052   vector<string> Splitted;
    00053   /* _expression will be used as delimiter */
    00054   if(_expression.length() == 1) {
    00055     /* use the plain c++ way, ignore the pre-compiled p_pcre */
    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     /* use the regex way */
    00083     if(_expression[0] != '(' && _expression[ _expression.length() - 1 ] != ')' ) {
    00084       /* oh, oh - the pre-compiled expression does not contain brackets */
    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                 /* we are within the allowed range, so just add the grab */
    00110                 Splitted.push_back(junk);
    00111               }
    00112             }
    00113           }
    00114         }
    00115       }
    00116       else {
    00117         /* the rest of the string, there are no more delimiters */
    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               /* we are within the allowed range, so just add the grab */
    00124               Splitted.push_back(junk);
    00125             }
    00126           }
    00127         }
    00128         break;
    00129       }
    00130     } // for()
    00131   } // if(_expression.length()
    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 }