~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/_bencode_c.pyx

  • Committer: Jelmer Vernooij
  • Date: 2009-05-26 10:40:17 UTC
  • mto: (4398.5.1 bencode_serializer)
  • mto: This revision was merged to the branch mainline in revision 4410.
  • Revision ID: jelmer@samba.org-20090526104017-ejhpx119bltk85w0
use C character constants rather than a custom enum.

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
    int snprintf(char* buffer, size_t nsize, char* fmt, ...)
47
47
 
48
48
 
49
 
cdef enum:  # Codes for used characters
50
 
    MINUS   = 0x2D      # ord(-)
51
 
    CHAR_0  = 0x30      # ord(0)
52
 
    CHAR_1  = 0x31      # ord(1)
53
 
    CHAR_2  = 0x32      # ord(2)
54
 
    CHAR_3  = 0x33      # ord(3)
55
 
    CHAR_4  = 0x34      # ord(4)
56
 
    CHAR_5  = 0x35      # ord(5)
57
 
    CHAR_6  = 0x36      # ord(6)
58
 
    CHAR_7  = 0x37      # ord(7)
59
 
    CHAR_8  = 0x38      # ord(8)
60
 
    CHAR_9  = 0x39      # ord(9)
61
 
    COLON   = 0x3A      # ord(:)
62
 
    SMALL_D = 0x64      # ord(d)
63
 
    SMALL_E = 0x65      # ord(e)
64
 
    SMALL_I = 0x69      # ord(i)
65
 
    SMALL_L = 0x6c      # ord(l)
66
 
 
67
 
 
68
49
cdef class Decoder:
69
50
    """Bencode decoder"""
70
51
 
118
99
 
119
100
        ch = self.tail[0]
120
101
 
121
 
        if ch == SMALL_I:
 
102
        if ch == c'i':
122
103
            self._update_tail(1)
123
104
            return self._decode_int()
124
 
        elif CHAR_0 <= ch <= CHAR_9:
 
105
        elif c'0' <= ch <= c'9':
125
106
            return self._decode_string()
126
 
        elif ch == SMALL_L:
 
107
        elif ch == c'l':
127
108
            self._update_tail(1)
128
109
            return self._decode_list()
129
 
        elif ch == SMALL_D:
 
110
        elif ch == c'd':
130
111
            self._update_tail(1)
131
112
            return self._decode_dict()
132
113
 
139
120
 
140
121
    cdef object _decode_int(self):
141
122
        cdef int result
142
 
        result = self._decode_int_until(SMALL_E)
 
123
        result = self._decode_int_until(c'e')
143
124
        if result != self._MAXINT:
144
125
            return result
145
126
        else:
160
141
            raise ValueError
161
142
 
162
143
        sign = 0
163
 
        if MINUS == self.tail[0]:
 
144
        if c'-' == self.tail[0]:
164
145
            sign = 1
165
146
 
166
147
        if n-sign == 0:
167
148
            raise ValueError    # ie / i-e
168
149
 
169
 
        if self.tail[sign] == CHAR_0:   # special check for zero
 
150
        if self.tail[sign] == c'0':   # special check for zero
170
151
            if sign:
171
152
                raise ValueError    # i-0e
172
153
            if n > 1:
179
160
            result = 0
180
161
            for i from sign <= i < n:
181
162
                digit = self.tail[i]
182
 
                if CHAR_0 <= digit <= CHAR_9:
183
 
                    result = result * 10 + (digit - CHAR_0)
 
163
                if c'0' <= digit <= c'9':
 
164
                    result = result * 10 + (digit - c'0')
184
165
                else:
185
166
                    raise ValueError
186
167
            if sign:
203
184
    cdef object _decode_string(self):
204
185
        cdef int n
205
186
 
206
 
        n = self._decode_int_until(COLON)
 
187
        n = self._decode_int_until(c':')
207
188
        if n == 0:
208
189
            return ''
209
190
        if n == self._MAXINT:
220
201
        result = []
221
202
 
222
203
        while self.size > 0:
223
 
            if self.tail[0] == SMALL_E:
 
204
            if self.tail[0] == c'e':
224
205
                self._update_tail(1)
225
206
                if self._yield_tuples:
226
207
                    return tuple(result)
239
220
 
240
221
        while self.size > 0:
241
222
            ch = self.tail[0]
242
 
            if ch == SMALL_E:
 
223
            if ch == c'e':
243
224
                self._update_tail(1)
244
225
                return result
245
 
            elif CHAR_0 <= ch <= CHAR_9:
 
226
            elif c'0' <= ch <= c'9':
246
227
                # keys should be strings only
247
228
                key = self._decode_string()
248
229
                if lastkey >= key:
387
368
 
388
369
    cdef int _encode_list(self, x) except 0:
389
370
        self._ensure_buffer(2)
390
 
        self.tail[0] = SMALL_L
 
371
        self.tail[0] = c'l'
391
372
        self._update_tail(1)
392
373
 
393
374
        for i in x:
394
375
            self.process(i)
395
376
 
396
 
        self.tail[0] = SMALL_E
 
377
        self.tail[0] = c'e'
397
378
        self._update_tail(1)
398
379
        return 1
399
380
 
400
381
    cdef int _encode_dict(self, x) except 0:
401
382
        self._ensure_buffer(2)
402
 
        self.tail[0] = SMALL_D
 
383
        self.tail[0] = c'd'
403
384
        self._update_tail(1)
404
385
 
405
386
        keys = x.keys()
410
391
            self._encode_string(k)
411
392
            self.process(x[k])
412
393
 
413
 
        self.tail[0] = SMALL_E
 
394
        self.tail[0] = c'e'
414
395
        self._update_tail(1)
415
396
        return 1
416
397