Skip to content

Commit e86429c

Browse files
committed
add rsplit function
git-svn-id: http://pystring.googlecode.com/svn/trunk@3 e90ac086-db91-11dd-a3d5-f9b94b98161c
1 parent 51b608b commit e86429c

File tree

2 files changed

+142
-37
lines changed

2 files changed

+142
-37
lines changed

pystring.cpp

Lines changed: 132 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -31,48 +31,99 @@
3131
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3232
///////////////////////////////////////////////////////////////////////////////
3333

34-
#include <pystring.h>
3534
#include <ctype.h>
3635
#include <string.h>
3736
#include <iostream>
37+
#include <algorithm>
38+
#include "pystring.h"
3839

3940
namespace pystring
4041
{
41-
42-
//////////////////////////////////////////////////////////////////////////////////////////////
43-
///
44-
///
45-
void split_whitespace( const std::string & str, std::vector< std::string > & result, int maxsplit )
46-
{
47-
std::string::size_type i, j, len = str.size();
48-
for (i = j = 0; i < len; )
49-
{
50-
51-
while ( i < len && ::isspace( str[i] ) ) i++;
52-
j = i;
53-
54-
while ( i < len && ! ::isspace( str[i]) ) i++;
55-
56-
57-
58-
if (j < i)
59-
{
60-
if ( maxsplit-- <= 0 ) break;
61-
62-
result.push_back( str.substr( j, i - j ));
63-
64-
while ( i < len && ::isspace( str[i])) i++;
65-
j = i;
66-
}
67-
}
68-
if (j < len)
69-
{
70-
71-
result.push_back( str.substr( j, len - j ));
72-
}
73-
}
74-
75-
42+
43+
namespace {
44+
45+
//////////////////////////////////////////////////////////////////////////////////////////////
46+
/// why doesn't the std::reverse work?
47+
///
48+
void reverse_strings( std::vector< std::string > & result)
49+
{
50+
for (std::vector< std::string >::size_type i = 0; i < result.size() / 2; i++ )
51+
{
52+
std::swap(result[i], result[result.size() - 1 - i]);
53+
}
54+
}
55+
56+
//////////////////////////////////////////////////////////////////////////////////////////////
57+
///
58+
///
59+
void split_whitespace( const std::string & str, std::vector< std::string > & result, int maxsplit )
60+
{
61+
std::string::size_type i, j, len = str.size();
62+
for (i = j = 0; i < len; )
63+
{
64+
65+
while ( i < len && ::isspace( str[i] ) ) i++;
66+
j = i;
67+
68+
while ( i < len && ! ::isspace( str[i]) ) i++;
69+
70+
71+
72+
if (j < i)
73+
{
74+
if ( maxsplit-- <= 0 ) break;
75+
76+
result.push_back( str.substr( j, i - j ));
77+
78+
while ( i < len && ::isspace( str[i])) i++;
79+
j = i;
80+
}
81+
}
82+
if (j < len)
83+
{
84+
result.push_back( str.substr( j, len - j ));
85+
}
86+
}
87+
88+
89+
//////////////////////////////////////////////////////////////////////////////////////////////
90+
///
91+
///
92+
void rsplit_whitespace( const std::string & str, std::vector< std::string > & result, int maxsplit )
93+
{
94+
std::string::size_type len = str.size();
95+
std::string::size_type i, j;
96+
for (i = j = len; i > 0; )
97+
{
98+
99+
while ( i > 0 && ::isspace( str[i - 1] ) ) i--;
100+
j = i;
101+
102+
while ( i > 0 && ! ::isspace( str[i - 1]) ) i--;
103+
104+
105+
106+
if (j > i)
107+
{
108+
if ( maxsplit-- <= 0 ) break;
109+
110+
result.push_back( str.substr( i, j - i ));
111+
112+
while ( i > 0 && ::isspace( str[i - 1])) i--;
113+
j = i;
114+
}
115+
}
116+
if (j > 0)
117+
{
118+
result.push_back( str.substr( 0, j ));
119+
}
120+
//std::reverse( result, result.begin(), result.end() );
121+
reverse_strings( result );
122+
}
123+
124+
} //anonymous namespace
125+
126+
76127
//////////////////////////////////////////////////////////////////////////////////////////////
77128
///
78129
///
@@ -111,6 +162,51 @@ namespace pystring
111162
result.push_back( str.substr( j, len-j ) );
112163
}
113164

165+
//////////////////////////////////////////////////////////////////////////////////////////////
166+
///
167+
///
168+
void rsplit( const std::string & str, std::vector< std::string > & result, const std::string & sep, int maxsplit )
169+
{
170+
if ( maxsplit == 0 )
171+
{
172+
split( str, result, sep, 0 );
173+
return;
174+
}
175+
176+
result.clear();
177+
178+
if ( maxsplit < 0 ) maxsplit = MAX_32BIT_INT;//result.max_size();
179+
180+
181+
if ( sep.size() == 0 )
182+
{
183+
rsplit_whitespace( str, result, maxsplit );
184+
return;
185+
}
186+
187+
std::string::size_type i,j, len = str.size(), n = sep.size();
188+
189+
i = j = len;
190+
191+
while ( i > n )
192+
{
193+
if ( str[i - 1] == sep[n - 1] && str.substr( i - n, n ) == sep )
194+
{
195+
if ( maxsplit-- <= 0 ) break;
196+
197+
result.push_back( str.substr( i, j - i ) );
198+
i = j = i - n;
199+
}
200+
else
201+
{
202+
i--;
203+
}
204+
}
205+
206+
result.push_back( str.substr( 0, j ) );
207+
reverse_strings( result );
208+
}
209+
114210
//////////////////////////////////////////////////////////////////////////////////////////////
115211
///
116212
///

pystring.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,16 @@ namespace pystring
203203
/// any whitespace string is a separator.
204204
///
205205
void split( const std::string & str, std::vector< std::string > & result, const std::string & sep = "", int maxsplit = -1);
206-
206+
207+
//////////////////////////////////////////////////////////////////////////////////////////////
208+
/// @brief Fills the "result" list with the words in the string, using sep as the delimiter string.
209+
/// Does a number of splits starting at the end of the string, the result still has the
210+
/// split strings in their original order.
211+
/// If maxsplit is > -1, at most maxsplit splits are done. If sep is "",
212+
/// any whitespace string is a separator.
213+
///
214+
void rsplit( const std::string & str, std::vector< std::string > & result, const std::string & sep = "", int maxsplit = -1);
215+
207216
//////////////////////////////////////////////////////////////////////////////////////////////
208217
/// @brief Return a list of the lines in the string, breaking at line boundaries. Line breaks
209218
/// are not included in the resulting list unless keepends is given and true.

0 commit comments

Comments
 (0)