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
00046
00047
00048
00049
00050 Array Pcre::_split(const string& piece, int limit, int start_offset, int end_offset) {
00051 Array Splitted;
00052
00053 if(_expression.length() == 1) {
00054
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
00082 if(_expression[0] != '(' && _expression[ _expression.length() - 1 ] != ')' ) {
00083
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
00109 Splitted.push_back(junk);
00110 }
00111 }
00112 }
00113 }
00114 }
00115 else {
00116
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
00123 Splitted.push_back(junk);
00124 }
00125 }
00126 }
00127 break;
00128 }
00129 }
00130 }
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 }