Skip to content

Commit af3cdaa

Browse files
committed
added pystring::mul
Add the __mul__ function, the string multiplication operator.
1 parent 1076a67 commit af3cdaa

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

pystring.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <cctype>
3838
#include <cstring>
3939
#include <iostream>
40+
#include <sstream>
4041

4142
namespace pystring
4243
{
@@ -1048,7 +1049,24 @@ namespace pystring
10481049

10491050
return str.substr( startp, endp - startp );
10501051
}
1051-
1052+
1053+
1054+
//////////////////////////////////////////////////////////////////////////////////////////////
1055+
///
1056+
///
1057+
std::string mul( const std::string & str, int n )
1058+
{
1059+
// Early exits
1060+
if (n <= 0) return "";
1061+
if (n == 1) return str;
1062+
1063+
std::ostringstream os;
1064+
for(int i=0; i<n; ++i)
1065+
{
1066+
os << str;
1067+
}
1068+
return os.str();
1069+
}
10521070

10531071

10541072
}//namespace pystring

pystring.h

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

168+
//////////////////////////////////////////////////////////////////////////////////////////////
169+
/// @brief Return a copy of the string, concatenated N times, together.
170+
/// Corresponds to the __mul__ operator.
171+
///
172+
std::string mul( const std::string & str, int n);
173+
168174
//////////////////////////////////////////////////////////////////////////////////////////////
169175
/// @brief Split the string around first occurance of sep.
170176
/// Three strings will always placed into result. If sep is found, the strings will

0 commit comments

Comments
 (0)