From: <pj...@us...> - 2008-08-20 23:01:44
|
Revision: 5227 http://jython.svn.sourceforge.net/jython/?rev=5227&view=rev Author: pjenvey Date: 2008-08-20 23:01:42 +0000 (Wed, 20 Aug 2008) Log Message: ----------- rewrite the subx joiner yet again: this time exactly as CPython does it, with __getslice__ and join. fixes a string subclass corner case Modified Paths: -------------- trunk/jython/Lib/test/test_re_jy.py trunk/jython/src/org/python/modules/sre/PatternObject.java Modified: trunk/jython/Lib/test/test_re_jy.py =================================================================== --- trunk/jython/Lib/test/test_re_jy.py 2008-08-20 20:59:44 UTC (rev 5226) +++ trunk/jython/Lib/test/test_re_jy.py 2008-08-20 23:01:42 UTC (rev 5227) @@ -8,6 +8,23 @@ result = re.sub('', lambda match : None, 'foo') self.assertEqual(result, 'foo') self.assert_(isinstance(result, str)) + + def test_sub_with_subclasses(self): + class Foo(unicode): + def join(self, items): + return Foo(unicode.join(self, items)) + result = re.sub('bar', 'baz', Foo('bar')) + self.assertEqual(result, u'baz') + self.assertEqual(type(result), unicode) + + class Foo2(unicode): + def join(self, items): + return Foo2(unicode.join(self, items)) + def __getslice__(self, start, stop): + return Foo2(unicode.__getslice__(self, start, stop)) + result = re.sub('bar', 'baz', Foo2('bar')) + self.assertEqual(result, Foo2('baz')) + self.assert_(isinstance(result, Foo2)) def test_unkown_groupname(self): self.assertRaises(IndexError, Modified: trunk/jython/src/org/python/modules/sre/PatternObject.java =================================================================== --- trunk/jython/src/org/python/modules/sre/PatternObject.java 2008-08-20 20:59:44 UTC (rev 5226) +++ trunk/jython/src/org/python/modules/sre/PatternObject.java 2008-08-20 23:01:42 UTC (rev 5227) @@ -118,7 +118,7 @@ SRE_STATE state = new SRE_STATE(string, 0, Integer.MAX_VALUE, flags); - StringBuilder buf = new StringBuilder(); + PyList list = new PyList(); int n = 0; int i = 0; @@ -137,7 +137,7 @@ if (i < b) { /* get segment before this match */ - buf.append(string.substring(i, b)); + list.append(string.__getslice__(Py.newInteger(i), Py.newInteger(b))); } if (! (i == b && i == e && n > 0)) { PyObject item; @@ -150,7 +150,7 @@ } if (item != Py.None) { - buf.append(item.toString()); + list.append(item); } i = e; n++; @@ -163,25 +163,23 @@ state.start = state.ptr; } if (i < state.endpos) { - buf.append(string.substring(i, state.endpos)); + list.append(string.__getslice__(Py.newInteger(i), Py.newInteger(state.endpos))); } - // Follows rules enumerated in test_re.test_bug_1140 - PyString outstring; - if (buf.length() == 0) { - outstring = instring.createInstance(buf.toString()); - } else if (template instanceof PyUnicode || instring instanceof PyUnicode) { - outstring = Py.newUnicode(buf.toString()); - } else { - outstring = Py.newString(buf.toString()); - } - + PyObject outstring = join_list(list, string); if (subn) { return new PyTuple(outstring, Py.newInteger(n)); } return outstring; } + private PyObject join_list(PyList list, PyString string) { + PyObject joiner = string.__getslice__(Py.Zero, Py.Zero); + if (list.size() == 0) { + return joiner; + } + return joiner.__getattr__("join").__call__(list); + } public PyObject split(PyObject[] args, String[] kws) { ArgParser ap = new ArgParser("split", args, kws, This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |