| CODENOTIFIER | HelpYou are not signed inSign in |
Project: Jython
Revision: 5227
Author: pjenvey
Date: 20 Aug 2008 19:01:42
Changes:rewrite the subx joiner yet again: this time exactly as CPython does it, with
__getslice__ and join. fixes a string subclass corner case
| ... | ...@@ -8,6 +8,23 @@ | |
| 8 | 8 | result = re.sub('', lambda match : None, 'foo') |
| 9 | 9 | self.assertEqual(result, 'foo') |
| 10 | 10 | self.assert_(isinstance(result, str)) |
| 11 | ||
| 12 | def test_sub_with_subclasses(self): | |
| 13 | class Foo(unicode): | |
| 14 | def join(self, items): | |
| 15 | return Foo(unicode.join(self, items)) | |
| 16 | result = re.sub('bar', 'baz', Foo('bar')) | |
| 17 | self.assertEqual(result, u'baz') | |
| 18 | self.assertEqual(type(result), unicode) | |
| 19 | ||
| 20 | class Foo2(unicode): | |
| 21 | def join(self, items): | |
| 22 | return Foo2(unicode.join(self, items)) | |
| 23 | def __getslice__(self, start, stop): | |
| 24 | return Foo2(unicode.__getslice__(self, start, stop)) | |
| 25 | result = re.sub('bar', 'baz', Foo2('bar')) | |
| 26 | self.assertEqual(result, Foo2('baz')) | |
| 27 | self.assert_(isinstance(result, Foo2)) | |
| 11 | 28 | |
| 12 | 29 | def test_unkown_groupname(self): |
| 13 | 30 | self.assertRaises(IndexError, |
| ... | ...@@ -118,7 +118,7 @@ | |
| 118 | 118 | |
| 119 | 119 | SRE_STATE state = new SRE_STATE(string, 0, Integer.MAX_VALUE, flags); |
| 120 | 120 | |
| 121 | StringBuilder buf = new StringBuilder(); | |
| 121 | PyList list = new PyList(); | |
| 122 | 122 | |
| 123 | 123 | int n = 0; |
| 124 | 124 | int i = 0; |
| ... | ...@@ -137,7 +137,7 @@ | |
| 137 | 137 | |
| 138 | 138 | if (i < b) { |
| 139 | 139 | /* get segment before this match */ |
| 140 | buf.append(string.substring(i, b)); | |
| 140 | list.append(string.__getslice__(Py.newInteger(i), Py.newInteger(b))); | |
| 141 | 141 | } |
| 142 | 142 | if (! (i == b && i == e && n > 0)) { |
| 143 | 143 | PyObject item; |
| ... | ...@@ -150,7 +150,7 @@ | |
| 150 | 150 | } |
| 151 | 151 | |
| 152 | 152 | if (item != Py.None) { |
| 153 | buf.append(item.toString()); | |
| 153 | list.append(item); | |
| 154 | 154 | } |
| 155 | 155 | i = e; |
| 156 | 156 | n++; |
| ... | ...@@ -163,25 +163,23 @@ | |
| 163 | 163 | state.start = state.ptr; |
| 164 | 164 | } |
| 165 | 165 | if (i < state.endpos) { |
| 166 | buf.append(string.substring(i, state.endpos)); | |
| 167 | } | |
| 168 | ||
| 169 | // Follows rules enumerated in test_re.test_bug_1140 | |
| 170 | PyString outstring; | |
| 171 | if (buf.length() == 0) { | |
| 172 | outstring = instring.createInstance(buf.toString()); | |
| 173 | } else if (template instanceof PyUnicode || instring instanceof PyUnicode) { | |
| 174 | outstring = Py.newUnicode(buf.toString()); | |
| 175 | } else { | |
| 176 | outstring = Py.newString(buf.toString()); | |
| 166 | list.append(string.__getslice__(Py.newInteger(i), Py.newInteger(state.endpos))); | |
| 177 | 167 | } |
| 178 | 168 | |
| 169 | PyObject outstring = join_list(list, string); | |
| 179 | 170 | if (subn) { |
| 180 | 171 | return new PyTuple(outstring, Py.newInteger(n)); |
| 181 | 172 | } |
| 182 | 173 | return outstring; |
| 183 | 174 | } |
| 184 | 175 | |
| 176 | private PyObject join_list(PyList list, PyString string) { | |
| 177 | PyObject joiner = string.__getslice__(Py.Zero, Py.Zero); | |
| 178 | if (list.size() == 0) { | |
| 179 | return joiner; | |
| 180 | } | |
| 181 | return joiner.__getattr__("join").__call__(list); | |
| 182 | } | |
| 185 | 183 | |
| 186 | 184 | public PyObject split(PyObject[] args, String[] kws) { |
| 187 | 185 | ArgParser ap = new ArgParser("split", args, kws, |