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 
00046 /*
00047  * split method and family
00048  */
00049 
00050 Array Pcre::_split(const string& piece, int limit, int start_offset, int end_offset) {
00051   Array Splitted;
00052   /* _expression will be used as delimiter */
00053   if(_expression.length() == 1) {
00054     /* use the plain c++ way, ignore the pre-compiled p_pcre */
00055     string buffer, _delimiter, _piece;
00056     char z;
00057     if(case_t) {
00058       z = toupper(_expression[0]);
00059       for(size_t pos=0; pos < piece.length(); pos++) {
00060         _piece += (char)toupper(piece[pos]);
00061       }
00062     }
00063     else {
00064       z = _expression[0];
00065       _piece = piece;
00066     }
00067     for(size_t pos=0; pos<piece.length(); pos++) {
00068       if(_piece[pos] == z) {
00069         Splitted.push_back(buffer);
00070         buffer = "";
00071       }
00072       else {
00073         buffer += piece[pos];
00074       }
00075     }
00076     if(buffer != "") {
00077       Splitted.push_back(buffer);
00078     }
00079   }
00080   else {
00081     /* use the regex way */
00082     if(_expression[0] != '(' && _expression[ _expression.length() - 1 ] != ')' ) {
00083       /* oh, oh - the pre-compiled expression does not contain brackets */
00084       pcre_free(p_pcre);
00085       pcre_free(p_pcre_extra);
00086       
00087       pcre       *_p = NULL;
00088       pcre_extra *_e = NULL;;
00089 
00090       p_pcre = _p;
00091       p_pcre_extra = _e;
00092 
00093       _expression = "(" + _expression + ")";
00094       Compile(_flags);
00095     }
00096     int num_pieces=0, pos=0, piece_end = 0, piece_start = 0;
00097     for(;;) {
00098       if(search(piece, pos) == true) {
00099         if(matches() > 0) {
00100           piece_end   = get_match_start(0) - 1;
00101           piece_start = pos;
00102           pos = piece_end + 1 + get_match_length(0);
00103           string junk(piece, piece_start, (piece_end - piece_start)+1);
00104           num_pieces++;
00105           if( (limit != 0 && num_pieces < limit) || limit == 0) {
00106             if( (start_offset != 0 && num_pieces >= start_offset) || start_offset == 0) {
00107               if( (end_offset != 0 && num_pieces <= end_offset) || end_offset == 0) {
00108                 /* we are within the allowed range, so just add the grab */
00109                 Splitted.push_back(junk);
00110               }
00111             }
00112           }
00113         }
00114       }
00115       else {
00116         /* the rest of the string, there are no more delimiters */
00117         string junk(piece, pos, (piece.length() - pos));
00118         num_pieces++;
00119         if( (limit != 0 && num_pieces < limit) || limit == 0) {
00120           if( (start_offset != 0 && num_pieces >= start_offset) || start_offset == 0) {
00121             if( (end_offset != 0 && num_pieces <= end_offset) || end_offset == 0) {
00122               /* we are within the allowed range, so just add the grab */
00123               Splitted.push_back(junk);
00124             }
00125           }
00126         }
00127         break;
00128       }
00129     } // for()
00130   } // if(_expression.length()
00131   return Splitted;
00132 }
00133 
00134 Array Pcre::split(const string& piece) {
00135   return _split(piece, 0, 0, 0);
00136 }
00137 
00138 Array Pcre::split(const string& piece, int limit) {
00139   return _split(piece, limit, 0, 0);
00140 }
00141 
00142 Array Pcre::split(const string& piece, int limit, int start_offset) {
00143   return _split(piece, limit, start_offset, 0);
00144 }
00145 
00146 Array Pcre::split(const string& piece, int limit, int start_offset, int end_offset) {
00147   return _split(piece, limit, start_offset, end_offset);
00148 }
00149 
00150 Array Pcre::split(const string& piece, vector<int> positions) {
00151   Array PreSplitted = _split(piece, 0, 0, 0);
00152   Array Splitted;
00153   for(vector<int>::iterator vecIt=positions.begin(); vecIt != positions.end(); ++vecIt) {
00154     Splitted.push_back(PreSplitted[*vecIt]);
00155   }
00156   return Splitted;
00157 }

Generated on Wed Jun 25 00:39:01 2003 for PCRE++ by doxygen1.3-rc3