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
int PyString_CheckExact(object p)
34
char *PyString_AS_STRING(object p)
35
Py_ssize_t PyString_GET_SIZE(object p)
36
object PyString_FromStringAndSize(char *c_str, Py_ssize_t len)
38
cdef extern from "string.h":
39
void *memchr(void *s, int c, size_t n)
42
def chunks_to_lines(chunks):
43
"""Re-split chunks into simple lines.
45
Each entry in the result should contain a single newline at the end. Except
46
for the last entry which may not have a final newline. If chunks is already
47
a simple list of lines, we return it directly.
49
:param chunks: An list/tuple of strings. If chunks is already a list of
50
lines, then we will return it as-is.
51
:return: A list of strings.
56
cdef Py_ssize_t the_len
57
cdef int last_no_newline
59
# Check to see if the chunks are already lines
63
# We have a chunk which followed a chunk without a newline, so this
64
# is not a simple list of lines.
66
# Switching from PyString_AsStringAndSize to PyString_CheckExact and
67
# then the macros GET_SIZE and AS_STRING saved us 40us / 470us.
68
# It seems PyString_AsStringAndSize can actually trigger a conversion,
69
# which we don't want anyway.
70
if not PyString_CheckExact(chunk):
71
raise TypeError('chunk is not a string')
72
the_len = PyString_GET_SIZE(chunk)
74
# An empty string is never a valid line
76
c_str = PyString_AS_STRING(chunk)
77
c_last = c_str + the_len - 1
78
newline = <char *>memchr(c_str, c'\n', the_len)
81
# Missing a newline. Only valid as the last line
84
# There is a newline in the middle, we must resplit
87
# Everything was already a list of lines
90
# We know we need to create a new list of lines
92
tail = None # Any remainder from the previous chunk
97
if not PyString_CheckExact(chunk):
98
raise TypeError('chunk is not a string')
99
the_len = PyString_GET_SIZE(chunk)
101
# An empty string is never a valid line, and we don't need to
104
c_str = PyString_AS_STRING(chunk)
105
c_last = c_str + the_len - 1
106
newline = <char *>memchr(c_str, c'\n', the_len)
107
if newline == c_last:
109
PyList_Append(lines, chunk)
110
elif newline == NULL:
111
# A chunk without a newline, if this is the last entry, then we
115
# We have a newline in the middle, loop until we've consumed all
117
while newline != NULL:
118
line = PyString_FromStringAndSize(c_str, newline - c_str + 1)
119
PyList_Append(lines, line)
121
if c_str > c_last: # We are done
123
the_len = c_last - c_str + 1
124
newline = <char *>memchr(c_str, c'\n', the_len)
126
tail = PyString_FromStringAndSize(c_str, the_len)
129
PyList_Append(lines, tail)