From: <pj...@us...> - 2008-07-11 20:17:08
|
Revision: 4893 http://jython.svn.sourceforge.net/jython/?rev=4893&view=rev Author: pjenvey Date: 2008-07-11 13:16:53 -0700 (Fri, 11 Jul 2008) Log Message: ----------- o re-integrate workarounds for string_tests o add rsplit fixes #1023 thanks Nimish Telang Modified Paths: -------------- branches/asm/Lib/test/string_tests.py branches/asm/src/org/python/core/PyString.java Modified: branches/asm/Lib/test/string_tests.py =================================================================== --- branches/asm/Lib/test/string_tests.py 2008-07-11 20:10:32 UTC (rev 4892) +++ branches/asm/Lib/test/string_tests.py 2008-07-11 20:16:53 UTC (rev 4893) @@ -936,7 +936,12 @@ self.checkequal('abc', 'abc', '__mul__', 1) self.checkequal('abcabcabc', 'abc', '__mul__', 3) self.checkraises(TypeError, 'abc', '__mul__') - self.checkraises(TypeError, 'abc', '__mul__', '') + # CPython specific; pypy tests via the operator module instead + if not test_support.is_jython: + self.checkraises(TypeError, 'abc', '__mul__', '') + else: + import operator + self.checkraises(TypeError, operator, '__mul__', 'abc', '') # XXX: on a 64-bit system, this doesn't raise an overflow error, # but either raises a MemoryError, or succeeds (if you have 54TiB) #self.checkraises(OverflowError, 10000*'abc', '__mul__', 2000000000) @@ -1027,7 +1032,7 @@ # unicodeobject.c uses a 120 byte buffer and switches from # 'f' formatting to 'g' at precision 50, so we expect # OverflowErrors for the ranges x < 50 and prec >= 67. - if x < 50 and prec >= 67: + if not test_support.is_jython and x < 50 and prec >= 67: self.checkraises(OverflowError, format, "__mod__", value) else: self.checkcall(format, "__mod__", value) Modified: branches/asm/src/org/python/core/PyString.java =================================================================== --- branches/asm/src/org/python/core/PyString.java 2008-07-11 20:10:32 UTC (rev 4892) +++ branches/asm/src/org/python/core/PyString.java 2008-07-11 20:16:53 UTC (rev 4893) @@ -1072,6 +1072,76 @@ return list; } + public PyList rsplit() { + return str_rsplit(null, -1); + } + + public PyList rsplit(String sep) { + return str_rsplit(sep, -1); + } + + public PyList rsplit(String sep, int maxsplit) { + return str_rsplit(sep, maxsplit); + } + + @ExposedMethod(defaults = {"null", "-1"}) + final PyList str_rsplit(String sep, int maxsplit) { + if (sep != null) { + if (sep.length() == 0) { + throw Py.ValueError("empty separator"); + } + PyList list = rsplitfields(sep, maxsplit); + list.reverse(); + return list; + } + + PyList list = new PyList(); + char[] chars = string.toCharArray(); + + if (maxsplit < 0) { + maxsplit = chars.length; + } + + int splits = 0; + int i = chars.length - 1; + + while (i > -1 && Character.isWhitespace(chars[i])) { + i--; + } + if (i == -1) { + return list; + } + + while (splits < maxsplit) { + while (i > -1 && Character.isWhitespace(chars[i])) { + i--; + } + if (i == -1) { + break; + } + + int nextWsChar = i; + while (nextWsChar > -1 && !Character.isWhitespace(chars[nextWsChar])) { + nextWsChar--; + } + if (nextWsChar == -1) { + break; + } + + splits++; + list.add(fromSubstring(nextWsChar + 1, i + 1)); + i = nextWsChar; + } + while (i > -1 && Character.isWhitespace(chars[i])) { + i--; + } + if (i > -1) { + list.add(fromSubstring(0,i+1)); + } + list.reverse(); + return list; + } + public PyTuple partition(PyObject sepObj) { return str_partition(sepObj); } @@ -1206,6 +1276,37 @@ return list; } + private PyList rsplitfields(String sep, int maxsplit) { + PyList list = new PyList(); + + int length = string.length(); + if (maxsplit < 0) { + maxsplit = length + 1; + } + + int lastbreak = length; + int splits = 0; + int index = length; + int sepLength = sep.length(); + + while (index > 0 && splits < maxsplit) { + int i = string.lastIndexOf(sep, index - sepLength); + if (i == index) { + i -= sepLength; + } + if (i < 0) { + break; + } + splits++; + list.append(fromSubstring(i + sepLength, lastbreak)); + lastbreak = i; + index = i; + + } + list.append(fromSubstring(0, lastbreak)); + return list; + } + public PyList splitlines() { return str_splitlines(false); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |