1
# Copyright (C) 2007-2010 Canonical Ltd
2
# Copyright (C) 2018 Breezy developers
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
"""Trivial string helpers for use in other cython modules."""
20
from __future__ import absolute_import
22
cdef extern from "python-compat.h":
23
object PyBytes_FromStringAndSize (char *, Py_ssize_t)
24
object PyBytes_InternFromStringAndSize (char *, Py_ssize_t)
27
cdef inline void* _my_memrchr(void *s, int c, size_t n): # cannot_raise
28
# memrchr seems to be a GNU extension, so we have to implement it ourselves
41
cdef inline object safe_string_from_size(char *s, Py_ssize_t size):
44
'tried to create a string with an invalid size: %d' % size)
45
return PyBytes_FromStringAndSize(s, size)
48
cdef inline object safe_interned_string_from_size(char *s, Py_ssize_t size):
51
'tried to create a string with an invalid size: %d' % size)
52
return PyBytes_InternFromStringAndSize(s, size)