# HG changeset patch # User Paul Boddie # Date 1481127879 -3600 # Node ID d9053172e0ffd6967bc710f77f052d747e10f9cf # Parent 120b54816bd4e666036d29d0fc1a82040a9cfed2 Implemented the join method for strings. diff -r 120b54816bd4 -r d9053172e0ff lib/__builtins__/str.py --- a/lib/__builtins__/str.py Wed Dec 07 17:15:12 2016 +0100 +++ b/lib/__builtins__/str.py Wed Dec 07 17:24:39 2016 +0100 @@ -139,7 +139,30 @@ def endswith(self, s): pass def find(self, sub, start=None, end=None): pass def index(self, sub, start=None, end=None): pass - def join(self, l): pass + + def join(self, l): + + "Join the elements in 'l' with this string." + + # Empty strings just cause the list elements to be concatenated. + + if not self.__bool__(): + return str(buffer(l)) + + # Non-empty strings join the elements together in a buffer. + + b = buffer() + first = True + + for s in l: + if first: + first = False + else: + b.append(self) + b.append(s) + + return str(b) + def lower(self): pass def lstrip(self, chars=None): pass def replace(self, old, new, count=None): pass diff -r 120b54816bd4 -r d9053172e0ff tests/string.py --- a/tests/string.py Wed Dec 07 17:15:12 2016 +0100 +++ b/tests/string.py Wed Dec 07 17:24:39 2016 +0100 @@ -21,4 +21,14 @@ except ValueError, exc: print "ord(s): value is not appropriate", exc.value +l = ["Hello", "world!"] +s3 = " ".join(l) +print s3 # Hello world! + +s4 = "".join(l) +print s4 # Helloworld! + print hash(s) +print hash(s2) +print hash(s3) +print hash(s4)