/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4763.2.4 by John Arbash Meinel
merge bzr.2.1 in preparation for NEWS entry.
1
# Copyright (C) 2009, 2010 Canonical Ltd
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
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
4241.6.4 by Robert Collins
Really build on old pyrex.
18
#python2.4 support
19
cdef extern from "python-compat.h":
4679.9.14 by John Arbash Meinel
Revert previous change.
20
    pass
4241.6.4 by Robert Collins
Really build on old pyrex.
21
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
22
cdef extern from *:
23
    ctypedef unsigned int size_t
24
    int memcmp(void *, void*, size_t)
25
    void memcpy(void *, void*, size_t)
26
    void *memchr(void *s, int c, size_t len)
27
    long strtol(char *, char **, int)
28
    void sprintf(char *, char *, ...)
29
30
cdef extern from "Python.h":
4265.1.3 by John Arbash Meinel
restore the old Py_ssize_t import in the pyrex files.
31
    ctypedef int Py_ssize_t # Required for older pyrex versions
4679.9.12 by John Arbash Meinel
Tweak some of the internals of _chk_map_pyx.pyx
32
    ctypedef struct PyObject:
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
33
        pass
34
    int PyTuple_CheckExact(object p)
35
    Py_ssize_t PyTuple_GET_SIZE(object t)
36
    int PyString_CheckExact(object)
37
    char *PyString_AS_STRING(object s)
5218.2.1 by John Arbash Meinel
Implement a compiled extension for parsing the text key out of a CHKInventory value.
38
    PyObject *PyString_FromStringAndSize_ptr "PyString_FromStringAndSize" (char *, Py_ssize_t)
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
39
    Py_ssize_t PyString_GET_SIZE(object)
5218.2.1 by John Arbash Meinel
Implement a compiled extension for parsing the text key out of a CHKInventory value.
40
    void PyString_InternInPlace(PyObject **)
5165.1.2 by John Arbash Meinel
Simplify further, if this isn't going to be hot enough to matter, make the
41
    unsigned long PyInt_AsUnsignedLongMask(object) except? -1
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
42
43
    int PyDict_SetItem(object d, object k, object v) except -1
44
45
    object PyTuple_New(Py_ssize_t count)
46
    void PyTuple_SET_ITEM(object t, Py_ssize_t offset, object)
47
48
    void Py_INCREF(object)
5218.2.1 by John Arbash Meinel
Implement a compiled extension for parsing the text key out of a CHKInventory value.
49
    void Py_DECREF_ptr "Py_DECREF" (PyObject *)
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
50
51
    PyObject * PyTuple_GET_ITEM_ptr "PyTuple_GET_ITEM" (object t,
52
                                                        Py_ssize_t offset)
53
    int PyString_CheckExact_ptr "PyString_CheckExact" (PyObject *p)
54
    Py_ssize_t PyString_GET_SIZE_ptr "PyString_GET_SIZE" (PyObject *s)
55
    char *PyString_AS_STRING_ptr "PyString_AS_STRING" (PyObject *s)
56
    object PyString_FromStringAndSize(char*, Py_ssize_t)
57
4679.9.16 by John Arbash Meinel
More cleanups and clarifications.
58
# cimport all of the definitions we will need to access
4679.9.12 by John Arbash Meinel
Tweak some of the internals of _chk_map_pyx.pyx
59
from _static_tuple_c cimport StaticTuple,\
60
    import_static_tuple_c, StaticTuple_New, \
5223.1.1 by John Arbash Meinel
Some small tweaks to the chk_map code.
61
    StaticTuple_Intern, StaticTuple_SET_ITEM, StaticTuple_CheckExact, \
62
    StaticTuple_GET_SIZE
4679.9.12 by John Arbash Meinel
Tweak some of the internals of _chk_map_pyx.pyx
63
64
cdef extern from "_static_tuple_c.h":
4679.9.16 by John Arbash Meinel
More cleanups and clarifications.
65
    # Defined explicitly rather than cimport-ing. Trying to use cimport, the
66
    # type for PyObject is a different class that happens to have the same
67
    # name...
4679.9.12 by John Arbash Meinel
Tweak some of the internals of _chk_map_pyx.pyx
68
    PyObject * StaticTuple_GET_ITEM_ptr "StaticTuple_GET_ITEM" (StaticTuple,
69
                                                                Py_ssize_t)
70
5165.1.1 by Martin
Use python builtin crc32 function rather than zlib C api for _chk_map_pyx
71
cdef object crc32
5165.1.4 by John Arbash Meinel
zlib.crc32 seems to be quite a bit faster than binascii.crc32
72
from zlib import crc32
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
73
4679.9.1 by John Arbash Meinel
Merge in the static-tuple-no-use branch, and bring back the chk_map use.
74
4679.9.16 by John Arbash Meinel
More cleanups and clarifications.
75
# Set up the StaticTuple C_API functionality
4679.9.1 by John Arbash Meinel
Merge in the static-tuple-no-use branch, and bring back the chk_map use.
76
import_static_tuple_c()
77
78
cdef object _LeafNode
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
79
_LeafNode = None
4679.9.1 by John Arbash Meinel
Merge in the static-tuple-no-use branch, and bring back the chk_map use.
80
cdef object _InternalNode
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
81
_InternalNode = None
4679.9.1 by John Arbash Meinel
Merge in the static-tuple-no-use branch, and bring back the chk_map use.
82
cdef object _unknown
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
83
_unknown = None
84
4459.2.1 by Vincent Ladeuil
Use a consistent scheme for naming pyrex source files.
85
# We shouldn't just copy this from _dirstate_helpers_pyx
4634.117.10 by John Arbash Meinel
Change 'no except' to 'cannot_raise'
86
cdef void* _my_memrchr(void *s, int c, size_t n): # cannot_raise
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
87
    # memrchr seems to be a GNU extension, so we have to implement it ourselves
88
    cdef char *pos
89
    cdef char *start
90
91
    start = <char*>s
92
    pos = start + n - 1
93
    while pos >= start:
94
        if pos[0] == c:
95
            return <void*>pos
96
        pos = pos - 1
97
    return NULL
98
99
5218.2.1 by John Arbash Meinel
Implement a compiled extension for parsing the text key out of a CHKInventory value.
100
cdef object safe_interned_string_from_size(char *s, Py_ssize_t size):
101
    cdef PyObject *py_str
102
    if size < 0:
103
        raise AssertionError(
104
            'tried to create a string with an invalid size: %d @0x%x'
105
            % (size, <int>s))
106
    py_str = PyString_FromStringAndSize_ptr(s, size)
107
    PyString_InternInPlace(&py_str)
108
    result = <object>py_str
109
    # Casting a PyObject* to an <object> triggers an INCREF from Pyrex, so we
110
    # DECREF it to avoid geting immortal strings
111
    Py_DECREF_ptr(py_str)
112
    return result
113
114
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
115
def _search_key_16(key):
116
    """See chk_map._search_key_16."""
117
    cdef Py_ssize_t num_bits
118
    cdef Py_ssize_t i, j
119
    cdef Py_ssize_t num_out_bytes
5165.1.1 by Martin
Use python builtin crc32 function rather than zlib C api for _chk_map_pyx
120
    cdef unsigned long crc_val
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
121
    cdef Py_ssize_t out_off
122
    cdef char *c_out
123
4679.9.1 by John Arbash Meinel
Merge in the static-tuple-no-use branch, and bring back the chk_map use.
124
    num_bits = len(key)
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
125
    # 4 bytes per crc32, and another 1 byte between bits
126
    num_out_bytes = (9 * num_bits) - 1
127
    out = PyString_FromStringAndSize(NULL, num_out_bytes)
128
    c_out = PyString_AS_STRING(out)
129
    for i from 0 <= i < num_bits:
130
        if i > 0:
131
            c_out[0] = c'\x00'
132
            c_out = c_out + 1
5165.1.2 by John Arbash Meinel
Simplify further, if this isn't going to be hot enough to matter, make the
133
        crc_val = PyInt_AsUnsignedLongMask(crc32(key[i]))
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
134
        # Hex(val) order
135
        sprintf(c_out, '%08X', crc_val)
136
        c_out = c_out + 8
137
    return out
138
139
140
def _search_key_255(key):
141
    """See chk_map._search_key_255."""
142
    cdef Py_ssize_t num_bits
143
    cdef Py_ssize_t i, j
144
    cdef Py_ssize_t num_out_bytes
5165.1.1 by Martin
Use python builtin crc32 function rather than zlib C api for _chk_map_pyx
145
    cdef unsigned long crc_val
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
146
    cdef Py_ssize_t out_off
147
    cdef char *c_out
148
4679.9.1 by John Arbash Meinel
Merge in the static-tuple-no-use branch, and bring back the chk_map use.
149
    num_bits = len(key)
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
150
    # 4 bytes per crc32, and another 1 byte between bits
151
    num_out_bytes = (5 * num_bits) - 1
152
    out = PyString_FromStringAndSize(NULL, num_out_bytes)
153
    c_out = PyString_AS_STRING(out)
154
    for i from 0 <= i < num_bits:
155
        if i > 0:
156
            c_out[0] = c'\x00'
157
            c_out = c_out + 1
5165.1.2 by John Arbash Meinel
Simplify further, if this isn't going to be hot enough to matter, make the
158
        crc_val = PyInt_AsUnsignedLongMask(crc32(key[i]))
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
159
        # MSB order
160
        c_out[0] = (crc_val >> 24) & 0xFF
161
        c_out[1] = (crc_val >> 16) & 0xFF
162
        c_out[2] = (crc_val >> 8) & 0xFF
163
        c_out[3] = (crc_val >> 0) & 0xFF
164
        for j from 0 <= j < 4:
165
            if c_out[j] == c'\n':
166
                c_out[j] = c'_'
167
        c_out = c_out + 4
168
    return out
169
170
171
cdef int _get_int_from_line(char **cur, char *end, char *message) except -1:
172
    """Read a positive integer from the data stream.
173
174
    :param cur: The start of the data, this will be moved to after the
175
        trailing newline when done.
176
    :param end: Do not parse any data past this byte.
177
    :return: The integer stored in those bytes
178
    """
179
    cdef int value
180
    cdef char *next_line, *next
181
182
    next_line = <char *>memchr(cur[0], c'\n', end - cur[0])
183
    if next_line == NULL:
184
        raise ValueError("Missing %s line\n" % message)
185
186
    value = strtol(cur[0], &next, 10)
187
    if next != next_line:
188
        raise ValueError("%s line not a proper int\n" % message)
189
    cur[0] = next_line + 1
190
    return value
191
192
5223.1.1 by John Arbash Meinel
Some small tweaks to the chk_map code.
193
cdef _import_globals():
194
    """Set the global attributes. Done lazy to avoid recursive import loops."""
195
    global _LeafNode, _InternalNode, _unknown
196
197
    from bzrlib import chk_map
198
    _LeafNode = chk_map.LeafNode
199
    _InternalNode = chk_map.InternalNode
200
    _unknown = chk_map._unknown
201
202
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
203
def _deserialise_leaf_node(bytes, key, search_key_func=None):
204
    """Deserialise bytes, with key key, into a LeafNode.
205
206
    :param bytes: The bytes of the node.
207
    :param key: The key that the serialised node has.
208
    """
209
    cdef char *c_bytes, *cur, *next, *end
210
    cdef char *next_line
211
    cdef Py_ssize_t c_bytes_len, prefix_length, items_length
212
    cdef int maximum_size, width, length, i, prefix_tail_len
213
    cdef int num_value_lines, num_prefix_bits
214
    cdef char *prefix, *value_start, *prefix_tail
215
    cdef char *next_null, *last_null, *line_start
216
    cdef char *c_entry, *entry_start
4679.9.1 by John Arbash Meinel
Merge in the static-tuple-no-use branch, and bring back the chk_map use.
217
    cdef StaticTuple entry_bits
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
218
219
    if _LeafNode is None:
5223.1.1 by John Arbash Meinel
Some small tweaks to the chk_map code.
220
        _import_globals()
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
221
222
    result = _LeafNode(search_key_func=search_key_func)
223
    # Splitlines can split on '\r' so don't use it, split('\n') adds an
224
    # extra '' if the bytes ends in a final newline.
225
    if not PyString_CheckExact(bytes):
226
        raise TypeError('bytes must be a plain string not %s' % (type(bytes),))
227
228
    c_bytes = PyString_AS_STRING(bytes)
229
    c_bytes_len = PyString_GET_SIZE(bytes)
230
231
    if c_bytes_len < 9 or memcmp(c_bytes, "chkleaf:\n", 9) != 0:
232
        raise ValueError("not a serialised leaf node: %r" % bytes)
233
    if c_bytes[c_bytes_len - 1] != c'\n':
234
        raise ValueError("bytes does not end in a newline")
235
236
    end = c_bytes + c_bytes_len
237
    cur = c_bytes + 9
238
    maximum_size = _get_int_from_line(&cur, end, "maximum_size")
239
    width = _get_int_from_line(&cur, end, "width")
240
    length = _get_int_from_line(&cur, end, "length")
241
242
    next_line = <char *>memchr(cur, c'\n', end - cur)
243
    if next_line == NULL:
244
        raise ValueError('Missing the prefix line\n')
245
    prefix = cur
246
    prefix_length = next_line - cur
247
    cur = next_line + 1
248
249
    prefix_bits = []
250
    prefix_tail = prefix
251
    num_prefix_bits = 0
252
    next_null = <char *>memchr(prefix, c'\0', prefix_length)
253
    while next_null != NULL:
254
        num_prefix_bits = num_prefix_bits + 1
255
        prefix_bits.append(
256
            PyString_FromStringAndSize(prefix_tail, next_null - prefix_tail))
257
        prefix_tail = next_null + 1
258
        next_null = <char *>memchr(prefix_tail, c'\0', next_line - prefix_tail)
259
    prefix_tail_len = next_line - prefix_tail
260
261
    if num_prefix_bits >= width:
262
        raise ValueError('Prefix has too many nulls versus width')
263
264
    items_length = end - cur
265
    items = {}
266
    while cur < end:
267
        line_start = cur
268
        next_line = <char *>memchr(cur, c'\n', end - cur)
269
        if next_line == NULL:
270
            raise ValueError('null line\n')
271
        last_null = <char *>_my_memrchr(cur, c'\0', next_line - cur)
272
        if last_null == NULL:
273
            raise ValueError('fail to find the num value lines null')
274
        next_null = last_null + 1 # move past NULL
275
        num_value_lines = _get_int_from_line(&next_null, next_line + 1,
276
                                             "num value lines")
277
        cur = next_line + 1
278
        value_start = cur
279
        # Walk num_value_lines forward
280
        for i from 0 <= i < num_value_lines:
281
            next_line = <char *>memchr(cur, c'\n', end - cur)
282
            if next_line == NULL:
283
                raise ValueError('missing trailing newline')
284
            cur = next_line + 1
4679.9.1 by John Arbash Meinel
Merge in the static-tuple-no-use branch, and bring back the chk_map use.
285
        entry_bits = StaticTuple_New(width)
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
286
        for i from 0 <= i < num_prefix_bits:
4679.9.4 by John Arbash Meinel
A bit broken, but getting there.
287
            # TODO: Use PyList_GetItem, or turn prefix_bits into a
288
            #       tuple/StaticTuple
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
289
            entry = prefix_bits[i]
290
            # SET_ITEM 'steals' a reference
291
            Py_INCREF(entry)
4679.9.1 by John Arbash Meinel
Merge in the static-tuple-no-use branch, and bring back the chk_map use.
292
            StaticTuple_SET_ITEM(entry_bits, i, entry)
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
293
        value = PyString_FromStringAndSize(value_start, next_line - value_start)
294
        # The next entry bit needs the 'tail' from the prefix, and first part
295
        # of the line
296
        entry_start = line_start
297
        next_null = <char *>memchr(entry_start, c'\0',
298
                                   last_null - entry_start + 1)
299
        if next_null == NULL:
300
            raise ValueError('bad no null, bad')
301
        entry = PyString_FromStringAndSize(NULL,
302
                    prefix_tail_len + next_null - line_start)
303
        c_entry = PyString_AS_STRING(entry)
304
        if prefix_tail_len > 0:
305
            memcpy(c_entry, prefix_tail, prefix_tail_len)
306
        if next_null - line_start > 0:
307
            memcpy(c_entry + prefix_tail_len, line_start, next_null - line_start)
308
        Py_INCREF(entry)
309
        i = num_prefix_bits
4679.9.1 by John Arbash Meinel
Merge in the static-tuple-no-use branch, and bring back the chk_map use.
310
        StaticTuple_SET_ITEM(entry_bits, i, entry)
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
311
        while next_null != last_null: # We have remaining bits
312
            i = i + 1
313
            if i > width:
314
                raise ValueError("Too many bits for entry")
315
            entry_start = next_null + 1
316
            next_null = <char *>memchr(entry_start, c'\0',
317
                                       last_null - entry_start + 1)
318
            if next_null == NULL:
319
                raise ValueError('bad no null')
320
            entry = PyString_FromStringAndSize(entry_start,
321
                                               next_null - entry_start)
322
            Py_INCREF(entry)
4679.9.1 by John Arbash Meinel
Merge in the static-tuple-no-use branch, and bring back the chk_map use.
323
            StaticTuple_SET_ITEM(entry_bits, i, entry)
5223.1.1 by John Arbash Meinel
Some small tweaks to the chk_map code.
324
        if StaticTuple_GET_SIZE(entry_bits) != width:
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
325
            raise AssertionError(
326
                'Incorrect number of elements (%d vs %d)'
327
                % (len(entry_bits)+1, width + 1))
4679.9.1 by John Arbash Meinel
Merge in the static-tuple-no-use branch, and bring back the chk_map use.
328
        entry_bits = StaticTuple_Intern(entry_bits)
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
329
        PyDict_SetItem(items, entry_bits, value)
330
    if len(items) != length:
331
        raise ValueError("item count (%d) mismatch for key %s,"
332
                         " bytes %r" % (length, entry_bits, bytes))
333
    result._items = items
334
    result._len = length
335
    result._maximum_size = maximum_size
336
    result._key = key
337
    result._key_width = width
338
    result._raw_size = items_length + length * prefix_length
339
    if length == 0:
340
        result._search_prefix = None
341
        result._common_serialised_prefix = None
342
    else:
343
        result._search_prefix = _unknown
344
        result._common_serialised_prefix = PyString_FromStringAndSize(prefix,
345
                                                prefix_length)
346
    if c_bytes_len != result._current_size():
347
        raise AssertionError('_current_size computed incorrectly %d != %d',
348
            c_bytes_len, result._current_size())
349
    return result
350
351
352
def _deserialise_internal_node(bytes, key, search_key_func=None):
353
    cdef char *c_bytes, *cur, *next, *end
354
    cdef char *next_line
355
    cdef Py_ssize_t c_bytes_len, prefix_length
356
    cdef int maximum_size, width, length, i, prefix_tail_len
357
    cdef char *prefix, *line_prefix, *next_null, *c_item_prefix
358
359
    if _InternalNode is None:
5223.1.1 by John Arbash Meinel
Some small tweaks to the chk_map code.
360
        _import_globals()
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
361
    result = _InternalNode(search_key_func=search_key_func)
362
4679.9.4 by John Arbash Meinel
A bit broken, but getting there.
363
    if not StaticTuple_CheckExact(key):
364
        raise TypeError('key %r is not a StaticTuple' % (key,))
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
365
    if not PyString_CheckExact(bytes):
366
        raise TypeError('bytes must be a plain string not %s' % (type(bytes),))
367
368
    c_bytes = PyString_AS_STRING(bytes)
369
    c_bytes_len = PyString_GET_SIZE(bytes)
370
371
    if c_bytes_len < 9 or memcmp(c_bytes, "chknode:\n", 9) != 0:
372
        raise ValueError("not a serialised internal node: %r" % bytes)
373
    if c_bytes[c_bytes_len - 1] != c'\n':
374
        raise ValueError("bytes does not end in a newline")
375
376
    items = {}
377
    cur = c_bytes + 9
378
    end = c_bytes + c_bytes_len
379
    maximum_size = _get_int_from_line(&cur, end, "maximum_size")
380
    width = _get_int_from_line(&cur, end, "width")
381
    length = _get_int_from_line(&cur, end, "length")
382
383
    next_line = <char *>memchr(cur, c'\n', end - cur)
384
    if next_line == NULL:
385
        raise ValueError('Missing the prefix line\n')
386
    prefix = cur
387
    prefix_length = next_line - cur
388
    cur = next_line + 1
389
390
    while cur < end:
391
        # Find the null separator
392
        next_line = <char *>memchr(cur, c'\n', end - cur)
393
        if next_line == NULL:
394
            raise ValueError('missing trailing newline')
395
        next_null = <char *>_my_memrchr(cur, c'\0', next_line - cur)
396
        if next_null == NULL:
397
            raise ValueError('bad no null')
398
        item_prefix = PyString_FromStringAndSize(NULL,
399
            prefix_length + next_null - cur)
400
        c_item_prefix = PyString_AS_STRING(item_prefix)
401
        if prefix_length:
402
            memcpy(c_item_prefix, prefix, prefix_length)
403
        memcpy(c_item_prefix + prefix_length, cur, next_null - cur)
404
        flat_key = PyString_FromStringAndSize(next_null + 1,
405
                                              next_line - next_null - 1)
4679.9.1 by John Arbash Meinel
Merge in the static-tuple-no-use branch, and bring back the chk_map use.
406
        flat_key = StaticTuple(flat_key).intern()
407
        PyDict_SetItem(items, item_prefix, flat_key)
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
408
        cur = next_line + 1
409
    assert len(items) > 0
410
    result._items = items
411
    result._len = length
412
    result._maximum_size = maximum_size
413
    result._key = key
414
    result._key_width = width
415
    # XXX: InternalNodes don't really care about their size, and this will
416
    #      change if we add prefix compression
417
    result._raw_size = None # len(bytes)
418
    result._node_width = len(item_prefix)
419
    result._search_prefix = PyString_FromStringAndSize(prefix, prefix_length)
420
    return result
5218.2.1 by John Arbash Meinel
Implement a compiled extension for parsing the text key out of a CHKInventory value.
421
422
423
def _bytes_to_text_key(bytes):
424
    """Take a CHKInventory value string and return a (file_id, rev_id) tuple"""
425
    cdef StaticTuple key
426
    cdef char *byte_str, *cur_end, *file_id_str, *byte_end
427
    cdef char *revision_str
428
    cdef Py_ssize_t byte_size, pos, file_id_len
429
430
    if not PyString_CheckExact(bytes):
431
        raise TypeError('bytes must be a string')
432
    byte_str = PyString_AS_STRING(bytes)
433
    byte_size = PyString_GET_SIZE(bytes)
434
    byte_end = byte_str + byte_size
435
    cur_end = <char*>memchr(byte_str, c':', byte_size)
436
    if cur_end == NULL:
437
        raise ValueError('No kind section found.')
5218.2.3 by John Arbash Meinel
Trivial fix of a string => character.
438
    if cur_end[1] != c' ':
5218.2.1 by John Arbash Meinel
Implement a compiled extension for parsing the text key out of a CHKInventory value.
439
        raise ValueError('Kind section should end with ": "')
440
    file_id_str = cur_end + 2
441
    # file_id is now the data up until the next newline
442
    cur_end = <char*>memchr(file_id_str, c'\n', byte_end - file_id_str)
443
    if cur_end == NULL:
444
        raise ValueError('no newline after file-id')
445
    file_id = safe_interned_string_from_size(file_id_str,
446
                                             cur_end - file_id_str)
447
    # this is the end of the parent_str
448
    cur_end = <char*>memchr(cur_end + 1, c'\n', byte_end - cur_end - 1)
449
    if cur_end == NULL:
450
        raise ValueError('no newline after parent_str')
451
    # end of the name str
452
    cur_end = <char*>memchr(cur_end + 1, c'\n', byte_end - cur_end - 1)
453
    if cur_end == NULL:
454
        raise ValueError('no newline after name str')
455
    # the next section is the revision info
456
    revision_str = cur_end + 1
457
    cur_end = <char*>memchr(cur_end + 1, c'\n', byte_end - cur_end - 1)
458
    if cur_end == NULL:
459
        # This is probably a dir: entry, which has revision as the last item
460
        cur_end = byte_end
461
    revision = safe_interned_string_from_size(revision_str,
462
        cur_end - revision_str)
463
    key = StaticTuple_New(2)
464
    Py_INCREF(file_id)
465
    StaticTuple_SET_ITEM(key, 0, file_id) 
466
    Py_INCREF(revision)
467
    StaticTuple_SET_ITEM(key, 1, revision) 
468
    return StaticTuple_Intern(key)