-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
#cython: language_level=3 | ||
from charfbuzz cimport * | ||
from cpython.unicode cimport PyUnicode_AS_UNICODE, PyUnicode_GET_SIZE | ||
from libc.stdint cimport uint16_t, uint32_t | ||
from libc.stdlib cimport free, malloc | ||
from libc.string cimport const_char | ||
from typing import Callable, Dict, List, Tuple | ||
import array | ||
from cpython cimport array | ||
|
||
|
||
cdef bint PY_NARROW_UNICODE = sizeof(Py_UNICODE) != 4 | ||
|
@@ -181,29 +181,24 @@ cdef class Buffer: | |
|
||
def add_str(self, text: str, | ||
item_offset: int = None, item_length: int = None) -> None: | ||
cdef array.array packed | ||
# handle both "wide" and "narrow" python builds; strip the BOM | ||
if PY_NARROW_UNICODE: | ||
packed = array.array("H", text.encode("UTF-16")[2:]) | ||
else: | ||
packed = array.array("I", text.encode("UTF-32")[4:]) | ||
cdef unsigned int size = len(packed) | ||
cdef Py_UNICODE* array = PyUnicode_AS_UNICODE(text) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
anthrotype
Author
Member
|
||
cdef Py_ssize_t size = PyUnicode_GET_SIZE(text) | ||
if item_offset is None: | ||
item_offset = 0 | ||
if item_length is None: | ||
item_length = size | ||
if PY_NARROW_UNICODE: | ||
hb_buffer_add_utf16( | ||
self._hb_buffer, | ||
packed.data.as_ushorts, | ||
<uint16_t*>array, | ||
size, | ||
item_offset, | ||
item_length, | ||
) | ||
else: | ||
hb_buffer_add_utf32( | ||
self._hb_buffer, | ||
packed.data.as_uints, | ||
<uint32_t*>array, | ||
size, | ||
item_offset, | ||
item_length, | ||
|
PyUnicode_AS_UNICODE()
is actually deprecated and can also returnNULL
, see https://docs.python.org/3/c-api/unicode.html#c.PyUnicode_AS_UNICODE.