Author: Armin Rigo <ar...@tunes.org> Branch: py3.5 Changeset: r92471:8b9aafe3487b Date: 2017-09-26 08:40 +0200 http://bitbucket.org/pypy/pypy/changeset/8b9aafe3487b/
Log: PyUnicode_Substring() diff --git a/pypy/module/cpyext/test/test_unicodeobject.py b/pypy/module/cpyext/test/test_unicodeobject.py --- a/pypy/module/cpyext/test/test_unicodeobject.py +++ b/pypy/module/cpyext/test/test_unicodeobject.py @@ -851,6 +851,17 @@ assert r"['a\n', 'b\n', 'c\n', 'd']" == space.unwrap(space.repr( PyUnicode_Splitlines(space, w_str, 1))) + def test_substring_api(self, space): + w_str = space.wrap(u"abcd") + assert space.unwrap(PyUnicode_Substring(space, w_str, 1, 3)) == u"bc" + assert space.unwrap(PyUnicode_Substring(space, w_str, 0, 4)) == u"abcd" + assert space.unwrap(PyUnicode_Substring(space, w_str, 0, 9)) == u"abcd" + assert space.unwrap(PyUnicode_Substring(space, w_str, 1, 4)) == u"bcd" + assert space.unwrap(PyUnicode_Substring(space, w_str, 2, 2)) == u"" + assert space.unwrap(PyUnicode_Substring(space, w_str, 5, 4)) == u"" + assert space.unwrap(PyUnicode_Substring(space, w_str, 5, 3)) == u"" + assert space.unwrap(PyUnicode_Substring(space, w_str, 4, 3)) == u"" + def test_Ready(self, space): w_str = space.wrap(u'abc') # ASCII py_str = as_pyobj(space, w_str) diff --git a/pypy/module/cpyext/unicodeobject.py b/pypy/module/cpyext/unicodeobject.py --- a/pypy/module/cpyext/unicodeobject.py +++ b/pypy/module/cpyext/unicodeobject.py @@ -1043,3 +1043,17 @@ resulting strings.""" w_keepend = space.newbool(bool(rffi.cast(lltype.Signed, keepend))) return space.call_method(w_str, "splitlines", w_keepend) + +@cpython_api([PyObject, Py_ssize_t, Py_ssize_t], PyObject) +def PyUnicode_Substring(space, w_str, start, end): + usrc = space.unicode_w(w_str) + length = len(usrc) + if start < 0 or end < 0: + raise oefmt(space.w_IndexError, "string index out of range") + if start >= length or end < start: + result = u'' + else: + if end > length: + end = length + result = usrc[start:end] + return space.newunicode(result) _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit