/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/_rio_pyx.pyx

  • Committer: Jelmer Vernooij
  • Date: 2017-06-10 16:40:42 UTC
  • mfrom: (6653.6.7 rename-controldir)
  • mto: This revision was merged to the branch mainline in revision 6690.
  • Revision ID: jelmer@jelmer.uk-20170610164042-zrxqgy2htyduvke2
MergeĀ rename-controldirĀ branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2009, 2010 Canonical Ltd
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Pyrex implementation of _read_stanza_*."""
 
18
 
 
19
from __future__ import absolute_import
 
20
 
 
21
 
 
22
cdef extern from "python-compat.h":
 
23
    pass
 
24
 
 
25
cdef extern from "stdlib.h":
 
26
    void *malloc(int)
 
27
    void *realloc(void *, int)
 
28
    void free(void *)
 
29
 
 
30
cdef extern from "Python.h":
 
31
    ctypedef int Py_ssize_t # Required for older pyrex versions
 
32
    ctypedef int Py_UNICODE
 
33
    char *PyString_AS_STRING(object s)
 
34
    Py_ssize_t PyString_GET_SIZE(object t) except -1
 
35
    object PyUnicode_DecodeUTF8(char *string, Py_ssize_t length, char *errors)
 
36
    object PyString_FromStringAndSize(char *s, Py_ssize_t len)
 
37
    int PyString_CheckExact(object)
 
38
    int PyUnicode_CheckExact(object)
 
39
    object PyUnicode_Join(object, object)
 
40
    object PyUnicode_EncodeASCII(Py_UNICODE *, int, char *)
 
41
    Py_UNICODE *PyUnicode_AS_UNICODE(object)
 
42
    Py_UNICODE *PyUnicode_AsUnicode(object)
 
43
    Py_ssize_t PyUnicode_GET_SIZE(object) except -1
 
44
    int PyList_Append(object, object) except -1
 
45
    int Py_UNICODE_ISLINEBREAK(Py_UNICODE)
 
46
    object PyUnicode_FromUnicode(Py_UNICODE *, int)
 
47
    void *Py_UNICODE_COPY(Py_UNICODE *, Py_UNICODE *, int)
 
48
 
 
49
cdef extern from "string.h":
 
50
    void *memcpy(void *, void *, int)
 
51
 
 
52
from .rio import Stanza
 
53
 
 
54
cdef int _valid_tag_char(char c): # cannot_raise
 
55
    return (c == c'_' or c == c'-' or
 
56
            (c >= c'a' and c <= c'z') or
 
57
            (c >= c'A' and c <= c'Z') or
 
58
            (c >= c'0' and c <= c'9'))
 
59
 
 
60
 
 
61
def _valid_tag(tag):
 
62
    cdef char *c_tag
 
63
    cdef Py_ssize_t c_len
 
64
    cdef int i
 
65
    if not PyString_CheckExact(tag):
 
66
        raise TypeError(tag)
 
67
    c_tag = PyString_AS_STRING(tag)
 
68
    c_len = PyString_GET_SIZE(tag)
 
69
    if c_len < 1:
 
70
        return False
 
71
    for i from 0 <= i < c_len:
 
72
        if not _valid_tag_char(c_tag[i]):
 
73
            return False
 
74
    return True
 
75
 
 
76
 
 
77
cdef object _split_first_line_utf8(char *line, int len,
 
78
                                   char *value, Py_ssize_t *value_len):
 
79
    cdef int i
 
80
    for i from 0 <= i < len:
 
81
        if line[i] == c':':
 
82
            if line[i+1] != c' ':
 
83
                raise ValueError("invalid tag in line %r" % line)
 
84
            memcpy(value, line+i+2, len-i-2)
 
85
            value_len[0] = len-i-2
 
86
            return PyString_FromStringAndSize(line, i)
 
87
    raise ValueError('tag/value separator not found in line %r' % line)
 
88
 
 
89
 
 
90
cdef object _split_first_line_unicode(Py_UNICODE *line, int len,
 
91
                                      Py_UNICODE *value, Py_ssize_t *value_len):
 
92
    cdef int i
 
93
    for i from 0 <= i < len:
 
94
        if line[i] == c':':
 
95
            if line[i+1] != c' ':
 
96
                raise ValueError("invalid tag in line %r" %
 
97
                                 PyUnicode_FromUnicode(line, len))
 
98
            memcpy(value, &line[i+2], (len-i-2) * sizeof(Py_UNICODE))
 
99
            value_len[0] = len-i-2
 
100
            return PyUnicode_EncodeASCII(line, i, "strict")
 
101
    raise ValueError("tag/value separator not found in line %r" %
 
102
                     PyUnicode_FromUnicode(line, len))
 
103
 
 
104
 
 
105
def _read_stanza_utf8(line_iter):
 
106
    cdef char *c_line
 
107
    cdef Py_ssize_t c_len
 
108
    cdef char *accum_value, *new_accum_value
 
109
    cdef Py_ssize_t accum_len, accum_size
 
110
    pairs = []
 
111
    tag = None
 
112
    accum_len = 0
 
113
    accum_size = 4096
 
114
    accum_value = <char *>malloc(accum_size)
 
115
    if accum_value == NULL:
 
116
        raise MemoryError
 
117
    try:
 
118
        for line in line_iter:
 
119
            if line is None:
 
120
                break # end of file
 
121
            if not PyString_CheckExact(line):
 
122
                raise TypeError("%r is not a plain string" % line)
 
123
            c_line = PyString_AS_STRING(line)
 
124
            c_len = PyString_GET_SIZE(line)
 
125
            if c_len < 1:
 
126
                break       # end of file
 
127
            if c_len == 1 and c_line[0] == c"\n":
 
128
                break       # end of stanza
 
129
            if accum_len + c_len > accum_size:
 
130
                accum_size = (accum_len + c_len)
 
131
                new_accum_value = <char *>realloc(accum_value, accum_size)
 
132
                if new_accum_value == NULL:
 
133
                    raise MemoryError
 
134
                else:
 
135
                    accum_value = new_accum_value
 
136
            if c_line[0] == c'\t': # continues previous value
 
137
                if tag is None:
 
138
                    raise ValueError('invalid continuation line %r' % line)
 
139
                memcpy(accum_value+accum_len, c_line+1, c_len-1)
 
140
                accum_len = accum_len + c_len-1
 
141
            else: # new tag:value line
 
142
                if tag is not None:
 
143
                    PyList_Append(pairs,
 
144
                        (tag, PyUnicode_DecodeUTF8(accum_value, accum_len-1,
 
145
                                                   "strict")))
 
146
                tag = _split_first_line_utf8(c_line, c_len, accum_value,
 
147
                                             &accum_len)
 
148
                if not _valid_tag(tag):
 
149
                    raise ValueError("invalid rio tag %r" % (tag,))
 
150
        if tag is not None: # add last tag-value
 
151
            PyList_Append(pairs,
 
152
                (tag, PyUnicode_DecodeUTF8(accum_value, accum_len-1, "strict")))
 
153
            return Stanza.from_pairs(pairs)
 
154
        else:     # didn't see any content
 
155
            return None
 
156
    finally:
 
157
        free(accum_value)
 
158
 
 
159
 
 
160
def _read_stanza_unicode(unicode_iter):
 
161
    cdef Py_UNICODE *c_line
 
162
    cdef int c_len
 
163
    cdef Py_UNICODE *accum_value, *new_accum_value
 
164
    cdef Py_ssize_t accum_len, accum_size
 
165
    pairs = []
 
166
    tag = None
 
167
    accum_len = 0
 
168
    accum_size = 4096
 
169
    accum_value = <Py_UNICODE *>malloc(accum_size*sizeof(Py_UNICODE))
 
170
    if accum_value == NULL:
 
171
        raise MemoryError
 
172
    try:
 
173
        for line in unicode_iter:
 
174
            if line is None:
 
175
                break       # end of file
 
176
            if not PyUnicode_CheckExact(line):
 
177
                raise TypeError("%r is not a unicode string" % line)
 
178
            c_line = PyUnicode_AS_UNICODE(line)
 
179
            c_len = PyUnicode_GET_SIZE(line)
 
180
            if c_len < 1:
 
181
                break        # end of file
 
182
            if Py_UNICODE_ISLINEBREAK(c_line[0]):
 
183
                break       # end of stanza
 
184
            if accum_len + c_len > accum_size:
 
185
                accum_size = accum_len + c_len
 
186
                new_accum_value = <Py_UNICODE *>realloc(accum_value,
 
187
                    accum_size*sizeof(Py_UNICODE))
 
188
                if new_accum_value == NULL:
 
189
                    raise MemoryError
 
190
                else:
 
191
                    accum_value = new_accum_value
 
192
            if c_line[0] == c'\t': # continues previous value,
 
193
                if tag is None:
 
194
                    raise ValueError('invalid continuation line %r' % line)
 
195
                memcpy(&accum_value[accum_len], &c_line[1],
 
196
                    (c_len-1)*sizeof(Py_UNICODE))
 
197
                accum_len = accum_len + (c_len-1)
 
198
            else: # new tag:value line
 
199
                if tag is not None:
 
200
                    PyList_Append(pairs,
 
201
                        (tag, PyUnicode_FromUnicode(accum_value, accum_len-1)))
 
202
                tag = _split_first_line_unicode(c_line, c_len, accum_value,
 
203
                                                &accum_len)
 
204
                if not _valid_tag(tag):
 
205
                    raise ValueError("invalid rio tag %r" % (tag,))
 
206
        if tag is not None: # add last tag-value
 
207
            PyList_Append(pairs,
 
208
                    (tag, PyUnicode_FromUnicode(accum_value, accum_len-1)))
 
209
            return Stanza.from_pairs(pairs)
 
210
        else:     # didn't see any content
 
211
            return None
 
212
    finally:
 
213
        free(accum_value)