1
# Copyright (C) 2008 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
"""Pyrex extensions for converting chunks to lines."""
21
cdef extern from "python-compat.h":
24
cdef extern from "stdlib.h":
25
ctypedef unsigned size_t
27
cdef extern from "Python.h":
28
ctypedef int Py_ssize_t # Required for older pyrex versions
29
ctypedef struct PyObject:
31
int PyList_Append(object lst, object item) except -1
33
char *PyString_AsString(object p) except NULL
34
int PyString_AsStringAndSize(object s, char **buf, Py_ssize_t *len) except -1
36
cdef extern from "string.h":
37
void *memchr(void *s, int c, size_t n)
40
def chunks_to_lines(chunks):
44
cdef Py_ssize_t the_len
46
# Check to see if the chunks are already lines
48
PyString_AsStringAndSize(chunk, &c_str, &the_len)
51
c_last = c_str + the_len - 1
52
newline = <char *>memchr(c_str, c'\n', the_len)
53
if newline == NULL or newline != c_last:
58
from bzrlib import osutils
59
return osutils.split_lines(''.join(chunks))