string holder test

This commit is contained in:
Florian Förster 2025-11-27 16:45:08 +01:00
parent 2608715bfe
commit a7e9bc9bc1
3 changed files with 7615 additions and 360 deletions

1909
pdm.lock generated

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1 +1,45 @@
"""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])