Screenshot instructions:
Windows
Mac
Red Hat Linux
Ubuntu
Click URL instructions:
Right-click on ad, choose "Copy Link", then paste here →
(This may not be possible with some types of ads)
From: <pjenvey@us...> - 2008-05-30 00:35:42
|
Revision: 4481 http://jython.svn.sourceforge.net/jython/?rev=4481&view=rev Author: pjenvey Date: 2008-05-29 17:35:40 -0700 (Thu, 29 May 2008) Log Message: ----------- add str/unicode partition/rpartition fixes #1796272, 1797751 thanks ukeshav Modified Paths: -------------- trunk/jython/Lib/test/string_tests.py trunk/jython/Lib/test/test_set.py trunk/jython/src/org/python/core/PyString.java trunk/jython/src/org/python/core/PyUnicode.java Modified: trunk/jython/Lib/test/string_tests.py =================================================================== --- trunk/jython/Lib/test/string_tests.py 2008-05-30 00:34:42 UTC (rev 4480) +++ trunk/jython/Lib/test/string_tests.py 2008-05-30 00:35:40 UTC (rev 4481) @@ -590,6 +590,34 @@ else: self.checkcall(format, "__mod__", value) + def test_partition(self): + self.checkequal(('this is the par', 'ti', 'tion method'), + 'this is the partition method', 'partition', 'ti') + + # from raymond's original specification + S = 'http://www.python.org'; + self.checkequal(('http', '://', 'www.python.org'), S, 'partition', '://') + self.checkequal(('http://www.python.org';, '', ''), S, 'partition', '?') + self.checkequal(('', 'http://';, 'www.python.org'), S, 'partition', 'http://';) + self.checkequal(('http://www.python.';, 'org', ''), S, 'partition', 'org') + + self.checkraises(ValueError, S, 'partition', '') + self.checkraises(TypeError, S, 'partition', None) + + def test_rpartition(self): + self.checkequal(('this is the rparti', 'ti', 'on method'), + 'this is the rpartition method', 'rpartition', 'ti') + + # from raymond's original specification + S = 'http://www.python.org'; + self.checkequal(('http', '://', 'www.python.org'), S, 'rpartition', '://') + self.checkequal(('', '', 'http://www.python.org';), S, 'rpartition', '?') + self.checkequal(('', 'http://';, 'www.python.org'), S, 'rpartition', 'http://';) + self.checkequal(('http://www.python.';, 'org', ''), S, 'rpartition', 'org') + + self.checkraises(ValueError, S, 'rpartition', '') + self.checkraises(TypeError, S, 'rpartition', None) + class MixinStrStringUserStringTest: # Additional tests for 8bit strings, i.e. str, UserString and # the string module Modified: trunk/jython/Lib/test/test_set.py =================================================================== --- trunk/jython/Lib/test/test_set.py 2008-05-30 00:34:42 UTC (rev 4480) +++ trunk/jython/Lib/test/test_set.py 2008-05-30 00:35:40 UTC (rev 4481) @@ -261,10 +261,7 @@ w = ReprWrapper() s = self.thetype([w]) w.value = s - # XXX: Jython doesn't have str.partition yet - #name = repr(s).partition('(')[0] # strip class name from repr string - srepr = repr(s) - name = srepr[0:srepr.find('(')] + name = repr(s).partition('(')[0] # strip class name from repr string self.assertEqual(repr(s), '%s([%s(...)])' % (name, name)) def test_cyclical_print(self): Modified: trunk/jython/src/org/python/core/PyString.java =================================================================== --- trunk/jython/src/org/python/core/PyString.java 2008-05-30 00:34:42 UTC (rev 4480) +++ trunk/jython/src/org/python/core/PyString.java 2008-05-30 00:35:40 UTC (rev 4481) @@ -1063,6 +1063,108 @@ return list; } + public PyTuple partition(PyObject sepObj) { + return str_partition(sepObj); + } + + @ExposedMethod + final PyTuple str_partition(PyObject sepObj) { + String sep; + + if (sepObj instanceof PyUnicode) { + return unicodePartition(sepObj); + } else if (sepObj instanceof PyString) { + sep = ((PyString)sepObj).string; + } else { + throw Py.TypeError("expected a character buffer object"); + } + + if (sep.length() == 0) { + throw Py.ValueError("empty separator"); + } + + int index = string.indexOf(sep); + if (index != -1) { + return new PyTuple(fromSubstring(0, index), sepObj, + fromSubstring(index + sep.length(), string.length())); + } else { + return new PyTuple(this, Py.EmptyString, Py.EmptyString); + } + } + + final PyTuple unicodePartition(PyObject sepObj) { + PyUnicode strObj = __unicode__(); + String str = strObj.string; + + // Will throw a TypeError if not a basestring + String sep = sepObj.asString(); + sepObj = sepObj.__unicode__(); + + if (sep.length() == 0) { + throw Py.ValueError("empty separator"); + } + + int index = str.indexOf(sep); + if (index != -1) { + return new PyTuple(strObj.fromSubstring(0, index), sepObj, + strObj.fromSubstring(index + sep.length(), str.length())); + } else { + PyUnicode emptyUnicode = Py.newUnicode(""); + return new PyTuple(this, emptyUnicode, emptyUnicode); + } + } + + public PyTuple rpartition(PyObject sepObj) { + return str_rpartition(sepObj); + } + + @ExposedMethod + final PyTuple str_rpartition(PyObject sepObj) { + String sep; + + if (sepObj instanceof PyUnicode) { + return unicodePartition(sepObj); + } else if (sepObj instanceof PyString) { + sep = ((PyString)sepObj).string; + } else { + throw Py.TypeError("expected a character buffer object"); + } + + if (sep.length() == 0) { + throw Py.ValueError("empty separator"); + } + + int index = string.lastIndexOf(sep); + if (index != -1) { + return new PyTuple(fromSubstring(0, index), sepObj, + fromSubstring(index + sep.length(), string.length())); + } else { + return new PyTuple(Py.EmptyString, Py.EmptyString, this); + } + } + + final PyTuple unicodeRpartition(PyObject sepObj) { + PyUnicode strObj = __unicode__(); + String str = strObj.string; + + // Will throw a TypeError if not a basestring + String sep = sepObj.asString(); + sepObj = sepObj.__unicode__(); + + if (sep.length() == 0) { + throw Py.ValueError("empty separator"); + } + + int index = str.lastIndexOf(sep); + if (index != -1) { + return new PyTuple(strObj.fromSubstring(0, index), sepObj, + strObj.fromSubstring(index + sep.length(), str.length())); + } else { + PyUnicode emptyUnicode = Py.newUnicode(""); + return new PyTuple(emptyUnicode, emptyUnicode, this); + } + } + private PyList splitfields(String sep, int maxsplit) { PyList list = new PyList(); Modified: trunk/jython/src/org/python/core/PyUnicode.java =================================================================== --- trunk/jython/src/org/python/core/PyUnicode.java 2008-05-30 00:34:42 UTC (rev 4480) +++ trunk/jython/src/org/python/core/PyUnicode.java 2008-05-30 00:35:40 UTC (rev 4481) @@ -206,7 +206,16 @@ return new PyUnicode(str_rstrip(sep)); } + @ExposedMethod + final PyTuple unicode_partition(PyObject sep) { + return unicodePartition(sep); + } + @ExposedMethod + final PyTuple unicode_rpartition(PyObject sep) { + return unicodeRpartition(sep); + } + @ExposedMethod(defaults = {"null", "-1"}) final PyList unicode_split(String sep, int maxsplit) { return str_split(sep, maxsplit); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |