generated from dopt-python/py311-cython
string holder test
This commit is contained in:
parent
2608715bfe
commit
a7e9bc9bc1
File diff suppressed because it is too large
Load Diff
@ -1 +1,45 @@
|
|||||||
"""placeholder module for compilation"""
|
"""placeholder module for compilation"""
|
||||||
|
|
||||||
|
import typing as t
|
||||||
|
|
||||||
|
import cython
|
||||||
|
from cython.cimports.cpython.ref import Py_INCREF
|
||||||
|
|
||||||
|
|
||||||
|
@cython.cclass
|
||||||
|
class StringHolder:
|
||||||
|
_data = cython.declare(cython.p_char, visibility="private")
|
||||||
|
_length = cython.declare(cython.Py_ssize_t, visibility="readonly")
|
||||||
|
_py_cache = cython.declare(object, visibility="private")
|
||||||
|
|
||||||
|
def __cinit__(self, data: str) -> None:
|
||||||
|
"""accepting Python str
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
data : str
|
||||||
|
_description_
|
||||||
|
"""
|
||||||
|
if not isinstance(data, str):
|
||||||
|
raise TypeError("Data not 'str'")
|
||||||
|
|
||||||
|
tmp = data.encode("utf-8")
|
||||||
|
Py_INCREF(tmp)
|
||||||
|
self._data = tmp
|
||||||
|
self._length = len(tmp)
|
||||||
|
self._py_cache = None
|
||||||
|
|
||||||
|
@cython.cfunc
|
||||||
|
def _bytes_to_str(self) -> str:
|
||||||
|
tmp = t.cast(bytes, self._data[: self._length])
|
||||||
|
return tmp.decode("utf-8")
|
||||||
|
|
||||||
|
@property
|
||||||
|
def py_string(self) -> str:
|
||||||
|
if self._py_cache is None:
|
||||||
|
self._py_cache = self._bytes_to_str()
|
||||||
|
return t.cast(str, self._py_cache)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def py_bytes(self) -> bytes:
|
||||||
|
return t.cast(bytes, self._data[: self._length])
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user