mirror of https://github.com/python/cpython
parent
3704644acb
commit
f207277167
|
@ -5,14 +5,14 @@
|
||||||
Note: string objects have grown methods in Python 1.6
|
Note: string objects have grown methods in Python 1.6
|
||||||
This module requires Python 1.6 or later.
|
This module requires Python 1.6 or later.
|
||||||
"""
|
"""
|
||||||
from types import StringType, UnicodeType
|
from types import StringTypes
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
__all__ = ["UserString","MutableString"]
|
__all__ = ["UserString","MutableString"]
|
||||||
|
|
||||||
class UserString:
|
class UserString:
|
||||||
def __init__(self, seq):
|
def __init__(self, seq):
|
||||||
if isinstance(seq, StringType) or isinstance(seq, UnicodeType):
|
if isinstance(seq, StringTypes):
|
||||||
self.data = seq
|
self.data = seq
|
||||||
elif isinstance(seq, UserString):
|
elif isinstance(seq, UserString):
|
||||||
self.data = seq.data[:]
|
self.data = seq.data[:]
|
||||||
|
@ -43,19 +43,19 @@ class UserString:
|
||||||
def __add__(self, other):
|
def __add__(self, other):
|
||||||
if isinstance(other, UserString):
|
if isinstance(other, UserString):
|
||||||
return self.__class__(self.data + other.data)
|
return self.__class__(self.data + other.data)
|
||||||
elif isinstance(other, StringType) or isinstance(other, UnicodeType):
|
elif isinstance(other, StringTypes):
|
||||||
return self.__class__(self.data + other)
|
return self.__class__(self.data + other)
|
||||||
else:
|
else:
|
||||||
return self.__class__(self.data + str(other))
|
return self.__class__(self.data + str(other))
|
||||||
def __radd__(self, other):
|
def __radd__(self, other):
|
||||||
if isinstance(other, StringType) or isinstance(other, UnicodeType):
|
if isinstance(other, StringTypes):
|
||||||
return self.__class__(other + self.data)
|
return self.__class__(other + self.data)
|
||||||
else:
|
else:
|
||||||
return self.__class__(str(other) + self.data)
|
return self.__class__(str(other) + self.data)
|
||||||
def __iadd__(self, other):
|
def __iadd__(self, other):
|
||||||
if isinstance(other, UserString):
|
if isinstance(other, UserString):
|
||||||
self.data += other.data
|
self.data += other.data
|
||||||
elif isinstance(other, StringType) or isinstance(other, UnicodeType):
|
elif isinstance(other, StringTypes):
|
||||||
self.data += other
|
self.data += other
|
||||||
else:
|
else:
|
||||||
self.data += str(other)
|
self.data += str(other)
|
||||||
|
@ -159,7 +159,7 @@ class MutableString(UserString):
|
||||||
start = max(start, 0); end = max(end, 0)
|
start = max(start, 0); end = max(end, 0)
|
||||||
if isinstance(sub, UserString):
|
if isinstance(sub, UserString):
|
||||||
self.data = self.data[:start]+sub.data+self.data[end:]
|
self.data = self.data[:start]+sub.data+self.data[end:]
|
||||||
elif isinstance(sub, StringType) or isinstance(sub, UnicodeType):
|
elif isinstance(sub, StringTypes):
|
||||||
self.data = self.data[:start]+sub+self.data[end:]
|
self.data = self.data[:start]+sub+self.data[end:]
|
||||||
else:
|
else:
|
||||||
self.data = self.data[:start]+str(sub)+self.data[end:]
|
self.data = self.data[:start]+str(sub)+self.data[end:]
|
||||||
|
|
|
@ -176,10 +176,15 @@ def run_method_tests(test):
|
||||||
test('strip', 'hello', 'hello', 'xyz')
|
test('strip', 'hello', 'hello', 'xyz')
|
||||||
|
|
||||||
# strip/lstrip/rstrip with unicode arg
|
# strip/lstrip/rstrip with unicode arg
|
||||||
test('strip', 'xyzzyhelloxyzzy', u'hello', u'xyz')
|
if have_unicode:
|
||||||
test('lstrip', 'xyzzyhelloxyzzy', u'helloxyzzy', u'xyz')
|
test('strip', 'xyzzyhelloxyzzy',
|
||||||
test('rstrip', 'xyzzyhelloxyzzy', u'xyzzyhello', u'xyz')
|
unicode('hello', 'ascii'), unicode('xyz', 'ascii'))
|
||||||
test('strip', 'hello', u'hello', u'xyz')
|
test('lstrip', 'xyzzyhelloxyzzy',
|
||||||
|
unicode('helloxyzzy', 'ascii'), unicode('xyz', 'ascii'))
|
||||||
|
test('rstrip', 'xyzzyhelloxyzzy',
|
||||||
|
unicode('xyzzyhello', 'ascii'), unicode('xyz', 'ascii'))
|
||||||
|
test('strip', 'hello',
|
||||||
|
unicode('hello', 'ascii'), unicode('xyz', 'ascii'))
|
||||||
|
|
||||||
test('swapcase', 'HeLLo cOmpUteRs', 'hEllO CoMPuTErS')
|
test('swapcase', 'HeLLo cOmpUteRs', 'hEllO CoMPuTErS')
|
||||||
test('translate', 'xyzabcdef', 'xyzxyz', transtable, 'def')
|
test('translate', 'xyzabcdef', 'xyzxyz', transtable, 'def')
|
||||||
|
|
Loading…
Reference in New Issue