Skip to content

Commit 59c7e44

Browse files
committed
add partition and rpartition
git-svn-id: http://pystring.googlecode.com/svn/trunk@11 e90ac086-db91-11dd-a3d5-f9b94b98161c
1 parent a4d4d59 commit 59c7e44

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

pystring.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,48 @@ namespace pystring
303303
}
304304

305305
}
306+
307+
//////////////////////////////////////////////////////////////////////////////////////////////
308+
///
309+
///
310+
void partition( const std::string & str, const std::string & sep, std::vector< std::string > & result )
311+
{
312+
result.resize(3);
313+
int index = find( str, sep );
314+
if ( index < 0 )
315+
{
316+
result[0] = str;
317+
result[1] = "";
318+
result[2] = "";
319+
}
320+
else
321+
{
322+
result[0] = str.substr( 0, index );
323+
result[1] = sep;
324+
result[2] = str.substr( index + sep.size(), str.size() );
325+
}
326+
}
327+
328+
//////////////////////////////////////////////////////////////////////////////////////////////
329+
///
330+
///
331+
void rpartition( const std::string & str, const std::string & sep, std::vector< std::string > & result )
332+
{
333+
result.resize(3);
334+
int index = rfind( str, sep );
335+
if ( index < 0 )
336+
{
337+
result[0] = "";
338+
result[1] = "";
339+
result[2] = str;
340+
}
341+
else
342+
{
343+
result[0] = str.substr( 0, index );
344+
result[1] = sep;
345+
result[2] = str.substr( index + sep.size(), str.size() );
346+
}
347+
}
306348

307349
//////////////////////////////////////////////////////////////////////////////////////////////
308350
///

pystring.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,14 @@ namespace pystring
165165
///
166166
std::string lstrip( const std::string & str, const std::string & chars = "" );
167167

168+
//////////////////////////////////////////////////////////////////////////////////////////////
169+
/// @brief Split the string around first occurance of sep.
170+
/// Three strings will always placed into result. If sep is found, the strings will
171+
/// be the text before sep, sep itself, and the remaining text. If sep is
172+
/// not found, the original string will be returned with two empty strings.
173+
///
174+
void partition( const std::string & str, const std::string & sep, std::vector< std::string > & result );
175+
168176
//////////////////////////////////////////////////////////////////////////////////////////////
169177
/// @brief Return a copy of the string with all occurrences of substring old replaced by new. If
170178
/// the optional argument count is given, only the first count occurrences are replaced.
@@ -190,6 +198,14 @@ namespace pystring
190198
///
191199
std::string rjust( const std::string & str, int width);
192200

201+
//////////////////////////////////////////////////////////////////////////////////////////////
202+
/// @brief Split the string around last occurance of sep.
203+
/// Three strings will always placed into result. If sep is found, the strings will
204+
/// be the text before sep, sep itself, and the remaining text. If sep is
205+
/// not found, the original string will be returned with two empty strings.
206+
///
207+
void rpartition( const std::string & str, const std::string & sep, std::vector< std::string > & result );
208+
193209
//////////////////////////////////////////////////////////////////////////////////////////////
194210
/// @brief Return a copy of the string with trailing characters removed. If chars is "", whitespace
195211
/// characters are removed. If not "", the characters in the string will be stripped from the

0 commit comments

Comments
 (0)