summaryrefslogtreecommitdiff
path: root/Lib/UserString.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/UserString.py')
-rwxr-xr-xLib/UserString.py58
1 files changed, 46 insertions, 12 deletions
diff --git a/Lib/UserString.py b/Lib/UserString.py
index 60dc34bc4b..726b3f7d3c 100755
--- a/Lib/UserString.py
+++ b/Lib/UserString.py
@@ -6,10 +6,11 @@ Note: string objects have grown methods in Python 1.6
This module requires Python 1.6 or later.
"""
import sys
+import collections
__all__ = ["UserString","MutableString"]
-class UserString:
+class UserString(collections.Sequence):
def __init__(self, seq):
if isinstance(seq, basestring):
self.data = seq
@@ -129,7 +130,7 @@ class UserString:
def upper(self): return self.__class__(self.data.upper())
def zfill(self, width): return self.__class__(self.data.zfill(width))
-class MutableString(UserString):
+class MutableString(UserString, collections.MutableSequence):
"""mutable string objects
Python strings are immutable objects. This has the advantage, that
@@ -145,19 +146,50 @@ class MutableString(UserString):
A faster and better solution is to rewrite your program using lists."""
def __init__(self, string=""):
+ from warnings import warnpy3k
+ warnpy3k('the class UserString.MutableString has been removed in '
+ 'Python 3.0', stacklevel=2)
self.data = string
- def __hash__(self):
- raise TypeError, "unhashable type (it is mutable)"
+
+ # We inherit object.__hash__, so we must deny this explicitly
+ __hash__ = None
+
def __setitem__(self, index, sub):
- if index < 0:
- index += len(self.data)
- if index < 0 or index >= len(self.data): raise IndexError
- self.data = self.data[:index] + sub + self.data[index+1:]
+ if isinstance(index, slice):
+ if isinstance(sub, UserString):
+ sub = sub.data
+ elif not isinstance(sub, basestring):
+ sub = str(sub)
+ start, stop, step = index.indices(len(self.data))
+ if step == -1:
+ start, stop = stop+1, start+1
+ sub = sub[::-1]
+ elif step != 1:
+ # XXX(twouters): I guess we should be reimplementing
+ # the extended slice assignment/deletion algorithm here...
+ raise TypeError, "invalid step in slicing assignment"
+ start = min(start, stop)
+ self.data = self.data[:start] + sub + self.data[stop:]
+ else:
+ if index < 0:
+ index += len(self.data)
+ if index < 0 or index >= len(self.data): raise IndexError
+ self.data = self.data[:index] + sub + self.data[index+1:]
def __delitem__(self, index):
- if index < 0:
- index += len(self.data)
- if index < 0 or index >= len(self.data): raise IndexError
- self.data = self.data[:index] + self.data[index+1:]
+ if isinstance(index, slice):
+ start, stop, step = index.indices(len(self.data))
+ if step == -1:
+ start, stop = stop+1, start+1
+ elif step != 1:
+ # XXX(twouters): see same block in __setitem__
+ raise TypeError, "invalid step in slicing deletion"
+ start = min(start, stop)
+ self.data = self.data[:start] + self.data[stop:]
+ else:
+ if index < 0:
+ index += len(self.data)
+ if index < 0 or index >= len(self.data): raise IndexError
+ self.data = self.data[:index] + self.data[index+1:]
def __setslice__(self, start, end, sub):
start = max(start, 0); end = max(end, 0)
if isinstance(sub, UserString):
@@ -182,6 +214,8 @@ class MutableString(UserString):
def __imul__(self, n):
self.data *= n
return self
+ def insert(self, index, value):
+ self[index:index] = value
if __name__ == "__main__":
# execute the regression test to stdout, if called as a script: