Skip to content

Commit

Permalink
Use faster Python unicode API for unquoter (#1292)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Oct 16, 2024
1 parent e776a70 commit 4533b3c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGES/1292.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improved performance of unquoting strings -- by :user:`bdraco`.
15 changes: 10 additions & 5 deletions yarl/_quoting_c.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -332,23 +332,28 @@ cdef class _Unquoter:
return self._do_unquote(<str>val)

cdef str _do_unquote(self, str val):
if len(val) == 0:
cdef Py_ssize_t length = PyUnicode_GET_LENGTH(val)
if length == 0:
return val

cdef list ret = []
cdef char buffer[4]
cdef Py_ssize_t buflen = 0
cdef Py_ssize_t consumed
cdef str unquoted
cdef Py_UCS4 ch = 0
cdef Py_ssize_t idx = 0
cdef Py_ssize_t length = len(val)
cdef Py_ssize_t start_pct

cdef int kind = PyUnicode_KIND(val)
cdef const void *data = PyUnicode_DATA(val)
while idx < length:
ch = val[idx]
ch = PyUnicode_READ(kind, data, idx)
idx += 1
if ch == '%' and idx <= length - 2:
ch = _restore_ch(val[idx], val[idx + 1])
ch = _restore_ch(
PyUnicode_READ(kind, data, idx),
PyUnicode_READ(kind, data, idx + 1)
)
if ch != <Py_UCS4>-1:
idx += 2
assert buflen < 4
Expand Down

0 comments on commit 4533b3c

Please sign in to comment.