# HG changeset patch # User Paul Boddie # Date 1485639461 -3600 # Node ID 29349f755d4d9b9662298b0f8c0646e118fede6a # Parent 5fad8f7bcd90b4a68c46750cef0a59392c6b2a73 Added string multiplication. diff -r 5fad8f7bcd90 -r 29349f755d4d lib/__builtins__/str.py --- a/lib/__builtins__/str.py Sat Jan 28 13:27:31 2017 +0100 +++ b/lib/__builtins__/str.py Sat Jan 28 22:37:41 2017 +0100 @@ -3,7 +3,7 @@ """ String objects. -Copyright (C) 2015, 2016 Paul Boddie +Copyright (C) 2015, 2016, 2017 Paul Boddie This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -102,8 +102,20 @@ return self._binary_op_rev(str_add, other) - def __mul__(self, other): pass - def __rmul__(self, other): pass + def __mul__(self, other): + + "Multiply the string by 'other'." + + b = buffer() + + while other > 0: + b.append(self) + other -= 1 + + return str(b) + + __rmul__ = __mul__ + def __mod__(self, other): pass def __rmod__(self, other): pass diff -r 5fad8f7bcd90 -r 29349f755d4d tests/string.py --- a/tests/string.py Sat Jan 28 13:27:31 2017 +0100 +++ b/tests/string.py Sat Jan 28 22:37:41 2017 +0100 @@ -64,3 +64,15 @@ print hash(s4) print "# hash(s5):", print hash(s5) + +# Test multiplication of strings. + +s6 = "abc" +print s6 * -1 # +print s6 * 0 # +print s6 * 1 # abc +print s6 * 2 # abcabc +print -1 * s6 # +print 0 * s6 # +print 1 * s6 # abc +print 2 * s6 # abcabc