[pypy-svn] r48719 - in pypy/branch/ropes-unicode/pypy/objspace/std: . test
cfbolz at codespeak.net
cfbolz at codespeak.net
Thu Nov 15 22:19:10 CET 2007
Author: cfbolz
Date: Thu Nov 15 22:19:08 2007
New Revision: 48719
Modified:
pypy/branch/ropes-unicode/pypy/objspace/std/rope.py
pypy/branch/ropes-unicode/pypy/objspace/std/test/test_rope.py
Log:
some more functionality for ropes
Modified: pypy/branch/ropes-unicode/pypy/objspace/std/rope.py
==============================================================================
--- pypy/branch/ropes-unicode/pypy/objspace/std/rope.py (original)
+++ pypy/branch/ropes-unicode/pypy/objspace/std/rope.py Thu Nov 15 22:19:08 2007
@@ -80,6 +80,9 @@
def getint(self, index):
raise NotImplementedError("abstract base class")
+ def getrope(self, index):
+ raise NotImplementedError("abstract base class")
+
def getslice(self, start, stop):
raise NotImplementedError("abstract base class")
@@ -153,6 +156,9 @@
def getint(self, index):
return ord(self.s[index])
+ def getrope(self, index):
+ return LiteralStringNode.PREBUILT[ord(self.s[index])]
+
def getslice(self, start, stop):
assert 0 <= start <= stop
return LiteralStringNode(self.s[start:stop])
@@ -225,6 +231,14 @@
def getint(self, index):
return ord(self.u[index])
+ def getrope(self, index):
+ ch = ord(self.u[index])
+ if ch < 256:
+ return LiteralStringNode.PREBUILT[ord(self.s[index])]
+ if len(self.u) == 1:
+ return self
+ return LiteralUnicodeNode(unichr(ch))
+
def getslice(self, start, stop):
assert 0 <= start <= stop
return LiteralUnicodeNode(self.u[start:stop])
@@ -256,6 +270,7 @@
id(self), len(self.u),
repr(addinfo).replace('"', '').replace("\\", "\\\\")))
+
class BinaryConcatNode(StringNode):
def __init__(self, left, right):
self.left = left
@@ -318,6 +333,13 @@
else:
return self.left.getint(index)
+ def getrope(self, index):
+ llen = self.left.length()
+ if index >= llen:
+ return self.right.getrope(index - llen)
+ else:
+ return self.left.getrope(index)
+
def flatten_string(self):
f = fringe(self)
return "".join([node.flatten_string() for node in f])
@@ -590,6 +612,9 @@
if chunk:
nodelist.append(LiteralStringNode("".join(chunk)))
return rebalance(nodelist, length)
+rope_from_unicharlist._annspecialcase_ = "specialize:argtype(0)"
+
+rope_from_unicode = rope_from_unicharlist
# __________________________________________________________________________
# searching
@@ -1152,6 +1177,25 @@
return False
return True
+def strip(node, left=True, right=True, predicate=lambda i: chr(i).isspace()):
+ length = node.length()
+
+ lpos = 0
+ rpos = length
+
+ if left:
+ iter = ItemIterator(node)
+ while lpos < rpos and predicate(iter.nextint()):
+ lpos += 1
+
+ if right:
+ iter = ReverseItemIterator(node)
+ while rpos > lpos and predicate(iter.nextint()):
+ rpos -= 1
+
+ assert rpos >= lpos
+ return getslice_one(node, lpos, rpos)
+strip._annspecialcase_ = "specialize:arg(3)"
# __________________________________________________________________________
# misc
@@ -1165,3 +1209,4 @@
x ^= rope.getint(0)
x ^= rope.length()
return intmask(x)
+
Modified: pypy/branch/ropes-unicode/pypy/objspace/std/test/test_rope.py
==============================================================================
--- pypy/branch/ropes-unicode/pypy/objspace/std/test/test_rope.py (original)
+++ pypy/branch/ropes-unicode/pypy/objspace/std/test/test_rope.py Thu Nov 15 22:19:08 2007
@@ -663,3 +663,35 @@
c = iter.nextchar(); assert c == "a"
c = iter.nextchar(); assert c == "b"
c = iter.nextchar(); assert c == "c"
+
+def test_strip():
+ node = BinaryConcatNode(LiteralStringNode(" \t\n abc "),
+ LiteralStringNode("def "))
+ r = strip(node)
+ assert r.flatten_string() == "abc def"
+ r = strip(node, left=False)
+ assert r.flatten_string() == " \t\n abc def"
+ r = strip(node, right=False)
+ assert r.flatten_string() == "abc def "
+
+ node = BinaryConcatNode(LiteralStringNode("aaaYYYYa"),
+ LiteralStringNode("abab"))
+ predicate = lambda i: chr(i) in "ab"
+ r = strip(node, predicate=predicate)
+ assert r.flatten_string() == "YYYY"
+ r = strip(node, left=False, predicate=predicate)
+ assert r.flatten_string() == "aaaYYYY"
+ r = strip(node, right=False, predicate=predicate)
+ assert r.flatten_string() == "YYYYaabab"
+
+def test_getrope():
+ s1, result = make_random_string(200, unicode=True)
+ s2 = s1.rebalance()
+ for i in range(len(result)):
+ for s in [s1, s2]:
+ r = s.getrope(i)
+ assert r.length() == 1
+ assert r.getint(0) == s.getint(i)
+ assert isinstance(r, LiteralNode)
+ assert r.getint(0) >= 128 or isinstance(r, LiteralStringNode)
+ assert r.getrope(0) is r
More information about the Pypy-commit
mailing list