55 lines
1.5 KiB
Cython
55 lines
1.5 KiB
Cython
#cython: language_level=3
|
|
|
|
cdef bytes str2bytes(str string):
|
|
"""
|
|
Short cut to convert ascii encoded python string (str) to bytes
|
|
which can be easily converted to C-strings.
|
|
|
|
@param string: the python string to be converted.
|
|
@type string: str
|
|
@return a transcoded string
|
|
@rtype: bytes
|
|
"""
|
|
return string.encode('ascii')
|
|
|
|
cdef str bytes2str(bytes string):
|
|
"""
|
|
Short cut to convert bytes (C-strings) to ascii encoded python string (str).
|
|
|
|
@param string: the binary (C-string) string to be converted.
|
|
@type string: bytes
|
|
@return an ascii transcoded string
|
|
@rtype: str
|
|
"""
|
|
return string.decode('ascii')
|
|
|
|
cdef bytes tobytes(object string):
|
|
"""
|
|
Short cut to convert ascii encoded string (str or bytes) to bytes
|
|
which can be easily converted to C-strings.
|
|
|
|
@param string: the python string to be converted.
|
|
@type string: bytes or str
|
|
@return a transcoded string
|
|
@rtype: bytes
|
|
"""
|
|
if isinstance(string, bytes):
|
|
return string
|
|
return str2bytes(string)
|
|
|
|
|
|
cdef str tostr(object string):
|
|
"""
|
|
Short cut to convert ascii encoded string (str or bytes) to bytes
|
|
which can be easily converted to C-strings.
|
|
|
|
@param string: the python string to be converted.
|
|
@type string: bytes or str
|
|
@return a transcoded string
|
|
@rtype: bytes
|
|
"""
|
|
if isinstance(string, str):
|
|
return string
|
|
return bytes2str(string)
|
|
|