1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>split.cc Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.3-rc3 -->
<center>
<a class="qindex" href="index.html">Main Page</a> <a class="qindex" href="namespaces.html">Namespace List</a> <a class="qindex" href="annotated.html">Compound List</a> <a class="qindex" href="files.html">File List</a> <a class="qindex" href="functions.html">Compound Members</a> <a class="qindex" href="globals.html">File Members</a> </center>
<hr><h1>split.cc</h1><a href="split_8cc.html">Go to the documentation of this file.</a><div class="fragment"><pre>00001 <span class="comment">/*</span>
00002 <span class="comment"> *</span>
00003 <span class="comment"> * This file is part of the PCRE++ Class Library.</span>
00004 <span class="comment"> *</span>
00005 <span class="comment"> * By accessing this software, PCRE++, you are duly informed</span>
00006 <span class="comment"> * of and agree to be bound by the conditions described below</span>
00007 <span class="comment"> * in this notice:</span>
00008 <span class="comment"> *</span>
00009 <span class="comment"> * This software product, PCRE++, is developed by Thomas Linden</span>
00010 <span class="comment"> * and copyrighted (C) 2002-2003 by Thomas Linden,with all rights </span>
00011 <span class="comment"> * reserved.</span>
00012 <span class="comment"> *</span>
00013 <span class="comment"> * There is no charge for PCRE++ software. You can redistribute</span>
00014 <span class="comment"> * it and/or modify it under the terms of the GNU Lesser General</span>
00015 <span class="comment"> * Public License, which is incorporated by reference herein.</span>
00016 <span class="comment"> *</span>
00017 <span class="comment"> * PCRE++ is distributed WITHOUT ANY WARRANTY, IMPLIED OR EXPRESS,</span>
00018 <span class="comment"> * OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE or that</span>
00019 <span class="comment"> * the use of it will not infringe on any third party's intellec-</span>
00020 <span class="comment"> * tual property rights.</span>
00021 <span class="comment"> *</span>
00022 <span class="comment"> * You should have received a copy of the GNU Lesser General Public</span>
00023 <span class="comment"> * License along with PCRE++. Copies can also be obtained from:</span>
00024 <span class="comment"> *</span>
00025 <span class="comment"> * http://www.gnu.org/licenses/lgpl.txt</span>
00026 <span class="comment"> *</span>
00027 <span class="comment"> * or by writing to:</span>
00028 <span class="comment"> *</span>
00029 <span class="comment"> * Free Software Foundation, Inc.</span>
00030 <span class="comment"> * 59 Temple Place, Suite 330</span>
00031 <span class="comment"> * Boston, MA 02111-1307</span>
00032 <span class="comment"> * USA</span>
00033 <span class="comment"> *</span>
00034 <span class="comment"> * Or contact:</span>
00035 <span class="comment"> *</span>
00036 <span class="comment"> * "Thomas Linden" <tom@daemon.de></span>
00037 <span class="comment"> *</span>
00038 <span class="comment"> *</span>
00039 <span class="comment"> */</span>
00040
00041
00042 <span class="preprocessor">#include "<a class="code" href="pcre++_8h.html">pcre++.h</a>"</span>
00043
00044 <span class="keyword">using</span> <span class="keyword">namespace </span>std;
00045 <span class="keyword">using</span> <span class="keyword">namespace </span>pcrepp;
00046
00047 <span class="comment">/*</span>
00048 <span class="comment"> * split method and family</span>
00049 <span class="comment"> */</span>
00050
00051 vector<string> Pcre::_split(<span class="keyword">const</span> string& piece, <span class="keywordtype">int</span> limit, <span class="keywordtype">int</span> start_offset, <span class="keywordtype">int</span> end_offset) {
00052 vector<string> Splitted;
00053 <span class="comment">/* _expression will be used as delimiter */</span>
00054 <span class="keywordflow">if</span>(_expression.length() == 1) {
00055 <span class="comment">/* use the plain c++ way, ignore the pre-compiled p_pcre */</span>
00056 string buffer, _delimiter, _piece;
00057 <span class="keywordtype">char</span> z;
00058 <span class="keywordflow">if</span>(case_t) {
00059 z = toupper(_expression[0]);
00060 <span class="keywordflow">for</span>(size_t pos=0; pos < piece.length(); pos++) {
00061 _piece += (char)toupper(piece[pos]);
00062 }
00063 }
00064 <span class="keywordflow">else</span> {
00065 z = _expression[0];
00066 _piece = piece;
00067 }
00068 <span class="keywordflow">for</span>(size_t pos=0; pos<piece.length(); pos++) {
00069 <span class="keywordflow">if</span>(_piece[pos] == z) {
00070 Splitted.push_back(buffer);
00071 buffer = <span class="stringliteral">""</span>;
00072 }
00073 <span class="keywordflow">else</span> {
00074 buffer += piece[pos];
00075 }
00076 }
00077 <span class="keywordflow">if</span>(buffer != <span class="stringliteral">""</span>) {
00078 Splitted.push_back(buffer);
00079 }
00080 }
00081 <span class="keywordflow">else</span> {
00082 <span class="comment">/* use the regex way */</span>
00083 <span class="keywordflow">if</span>(_expression[0] != <span class="charliteral">'('</span> && _expression[ _expression.length() - 1 ] != <span class="charliteral">')'</span> ) {
00084 <span class="comment">/* oh, oh - the pre-compiled expression does not contain brackets */</span>
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 = <span class="stringliteral">"("</span> + _expression + <span class="stringliteral">")"</span>;
00095 Compile(_flags);
00096 }
00097 <span class="keywordtype">int</span> num_pieces=0, pos=0, piece_end = 0, piece_start = 0;
00098 <span class="keywordflow">for</span>(;;) {
00099 <span class="keywordflow">if</span>(<a class="code" href="classpcrepp_1_1Pcre.html#a8">search</a>(piece, pos) == <span class="keyword">true</span>) {
00100 <span class="keywordflow">if</span>(<a class="code" href="classpcrepp_1_1Pcre.html#a18">matches</a>() > 0) {
00101 piece_end = <a class="code" href="classpcrepp_1_1Pcre.html#a14">get_match_start</a>(0) - 1;
00102 piece_start = pos;
00103 pos = piece_end + 1 + <a class="code" href="classpcrepp_1_1Pcre.html#a16">get_match_length</a>(0);
00104 string junk(piece, piece_start, (piece_end - piece_start)+1);
00105 num_pieces++;
00106 <span class="keywordflow">if</span>( (limit != 0 && num_pieces < limit) || limit == 0) {
00107 <span class="keywordflow">if</span>( (start_offset != 0 && num_pieces >= start_offset) || start_offset == 0) {
00108 <span class="keywordflow">if</span>( (end_offset != 0 && num_pieces <= end_offset) || end_offset == 0) {
00109 <span class="comment">/* we are within the allowed range, so just add the grab */</span>
00110 Splitted.push_back(junk);
00111 }
00112 }
00113 }
00114 }
00115 }
00116 <span class="keywordflow">else</span> {
00117 <span class="comment">/* the rest of the string, there are no more delimiters */</span>
00118 string junk(piece, pos, (piece.length() - pos));
00119 num_pieces++;
00120 <span class="keywordflow">if</span>( (limit != 0 && num_pieces < limit) || limit == 0) {
00121 <span class="keywordflow">if</span>( (start_offset != 0 && num_pieces >= start_offset) || start_offset == 0) {
00122 <span class="keywordflow">if</span>( (end_offset != 0 && num_pieces <= end_offset) || end_offset == 0) {
00123 <span class="comment">/* we are within the allowed range, so just add the grab */</span>
00124 Splitted.push_back(junk);
00125 }
00126 }
00127 }
00128 <span class="keywordflow">break</span>;
00129 }
00130 } <span class="comment">// for()</span>
00131 } <span class="comment">// if(_expression.length()</span>
00132 <span class="keywordflow">return</span> Splitted;
00133 }
00134
<a name="l00135"></a><a class="code" href="classpcrepp_1_1Pcre.html#a19">00135</a> vector<string> Pcre::split(<span class="keyword">const</span> string& piece) {
00136 <span class="keywordflow">return</span> _split(piece, 0, 0, 0);
00137 }
00138
<a name="l00139"></a><a class="code" href="classpcrepp_1_1Pcre.html#a20">00139</a> vector<string> Pcre::split(<span class="keyword">const</span> string& piece, <span class="keywordtype">int</span> limit) {
00140 <span class="keywordflow">return</span> _split(piece, limit, 0, 0);
00141 }
00142
<a name="l00143"></a><a class="code" href="classpcrepp_1_1Pcre.html#a21">00143</a> vector<string> Pcre::split(<span class="keyword">const</span> string& piece, <span class="keywordtype">int</span> limit, <span class="keywordtype">int</span> start_offset) {
00144 <span class="keywordflow">return</span> _split(piece, limit, start_offset, 0);
00145 }
00146
<a name="l00147"></a><a class="code" href="classpcrepp_1_1Pcre.html#a22">00147</a> vector<string> Pcre::split(<span class="keyword">const</span> string& piece, <span class="keywordtype">int</span> limit, <span class="keywordtype">int</span> start_offset, <span class="keywordtype">int</span> end_offset) {
00148 <span class="keywordflow">return</span> _split(piece, limit, start_offset, end_offset);
00149 }
00150
<a name="l00151"></a><a class="code" href="classpcrepp_1_1Pcre.html#a23">00151</a> vector<string> Pcre::split(<span class="keyword">const</span> string& piece, vector<int> positions) {
00152 vector<string> PreSplitted = _split(piece, 0, 0, 0);
00153 vector<string> Splitted;
00154 <span class="keywordflow">for</span>(vector<int>::iterator vecIt=positions.begin(); vecIt != positions.end(); ++vecIt) {
00155 Splitted.push_back(PreSplitted[*vecIt]);
00156 }
00157 <span class="keywordflow">return</span> Splitted;
00158 }
</pre></div><hr><address style="align: right;"><small>Generated on Wed Aug 25 01:38:04 2004 for PCRE++ by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border=0
width=110 height=53></a>1.3-rc3 </small></address>
</body>
</html>
|