Rev 4408: Move self._update_tail into a macro for UPDATE_TAIL. in lp:///~jameinel/bzr/bencode_serializer

John Arbash Meinel john at arbash-meinel.com
Thu Jun 4 16:54:39 BST 2009


At lp:///~jameinel/bzr/bencode_serializer

------------------------------------------------------------
revno: 4408
revision-id: john at arbash-meinel.com-20090604155421-wdxp4le2q49jvg2q
parent: john at arbash-meinel.com-20090603195150-69dnt7zs996nwdp9
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: bencode_serializer
timestamp: Thu 2009-06-04 10:54:21 -0500
message:
  Move self._update_tail into a macro for UPDATE_TAIL.
-------------- next part --------------
=== added file 'bzrlib/_bencode_pyx.h'
--- a/bzrlib/_bencode_pyx.h	1970-01-01 00:00:00 +0000
+++ b/bzrlib/_bencode_pyx.h	2009-06-04 15:54:21 +0000
@@ -0,0 +1,23 @@
+/* Copyright (C) 2009 Canonical Ltd
+ * 
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+/* Simple header providing some macro definitions for _bencode_pyx.pyx
+ */
+
+#define UPDATE_TAIL(coder, n) (((coder)->size -= (n), (coder)->tail += (n)))
+
+

=== modified file 'bzrlib/_bencode_pyx.pyx'
--- a/bzrlib/_bencode_pyx.pyx	2009-06-03 14:14:31 +0000
+++ b/bzrlib/_bencode_pyx.pyx	2009-06-04 15:54:21 +0000
@@ -49,12 +49,21 @@
 cdef extern from "python-compat.h":
     int snprintf(char* buffer, size_t nsize, char* fmt, ...)
 
-
-cdef class Decoder:
+cdef class Coder:
+    """Base class for both Decoder and Encoder"""
+
+    cdef readonly char *tail
+    cdef readonly int size
+
+cdef extern from "_bencode_pyx.h":
+    void UPDATE_TAIL(Coder, int n)
+    int MAX_INT_AS_STR
+
+
+
+cdef class Decoder(Coder):
     """Bencode decoder"""
 
-    cdef readonly char *tail
-    cdef readonly int size
     cdef readonly int _yield_tuples
     cdef object text
 
@@ -87,15 +96,15 @@
         try:
             ch = self.tail[0]
             if ch == c'i':
-                self._update_tail(1)
+                UPDATE_TAIL(self, 1)
                 return self._decode_int()
             elif c'0' <= ch <= c'9':
                 return self._decode_string()
             elif ch == c'l':
-                self._update_tail(1)
+                UPDATE_TAIL(self, 1)
                 return self._decode_list()
             elif ch == c'd':
-                self._update_tail(1)
+                UPDATE_TAIL(self, 1)
                 return self._decode_dict()
             else:
                 raise ValueError('unknown object type identifier %r' % ch)
@@ -105,7 +114,7 @@
     cdef void _update_tail(self, int n):
         """Update tail pointer and resulting size by n characters"""
         self.size = self.size - n
-        self.tail = &self.tail[n]
+        self.tail = self.tail + n
 
     cdef int _read_digits(self, char stop_char) except -1:
         cdef int i
@@ -133,14 +142,14 @@
             ret = PyInt_FromString(self.tail, NULL, 10)
         finally:
             self.tail[i] = c'e'
-        self._update_tail(i+1)
+        UPDATE_TAIL(self, i+1)
         return ret
 
     cdef object _decode_string(self):
         cdef int n, i
         i = self._read_digits(c':')
         n = strtol(self.tail, NULL, 10)
-        self._update_tail(i+1)
+        UPDATE_TAIL(self, i+1)
         if n == 0:
             return ''
         if n > self.size:
@@ -149,7 +158,7 @@
             raise ValueError('string size below zero: %d' % n)
 
         result = PyString_FromStringAndSize(self.tail, n)
-        self._update_tail(n)
+        UPDATE_TAIL(self, n)
         return result
 
     cdef object _decode_list(self):
@@ -157,7 +166,7 @@
 
         while self.size > 0:
             if self.tail[0] == c'e':
-                self._update_tail(1)
+                UPDATE_TAIL(self, 1)
                 if self._yield_tuples:
                     return tuple(result)
                 else:
@@ -176,7 +185,7 @@
         while self.size > 0:
             ch = self.tail[0]
             if ch == c'e':
-                self._update_tail(1)
+                UPDATE_TAIL(self, 1)
                 return result
             else:
                 # keys should be strings only
@@ -213,13 +222,11 @@
     INT_BUF_SIZE = 32
 
 
-cdef class Encoder:
+cdef class Encoder(Coder):
     """Bencode encoder"""
 
     cdef readonly char *buffer
     cdef readonly int maxsize
-    cdef readonly char *tail
-    cdef readonly int size
 
     def __init__(self, int maxsize=INITSIZE):
         """Initialize encoder engine
@@ -287,7 +294,7 @@
         n = snprintf(self.tail, INT_BUF_SIZE, "i%de", x)
         if n < 0:
             raise MemoryError('int %d too big to encode' % x)
-        self._update_tail(n)
+        UPDATE_TAIL(self, n)
         return 1
 
     cdef int _encode_long(self, x) except 0:
@@ -296,36 +303,36 @@
     cdef int _append_string(self, s) except 0:
         self._ensure_buffer(PyString_GET_SIZE(s))
         memcpy(self.tail, PyString_AS_STRING(s), PyString_GET_SIZE(s))
-        self._update_tail(PyString_GET_SIZE(s))
+        UPDATE_TAIL(self, PyString_GET_SIZE(s))
         return 1
 
     cdef int _encode_string(self, x) except 0:
         cdef int n
-        self._ensure_buffer(PyString_GET_SIZE(x) + 32)
-        n = snprintf(self.tail, 32, '%d:', PyString_GET_SIZE(x))
+        self._ensure_buffer(PyString_GET_SIZE(x) + INT_BUF_SIZE)
+        n = snprintf(self.tail, INT_BUF_SIZE, '%d:', PyString_GET_SIZE(x))
         if n < 0:
             raise MemoryError('string %s too big to encode' % x)
         memcpy(<void *>(self.tail+n), PyString_AS_STRING(x),
                PyString_GET_SIZE(x))
-        self._update_tail(n+PyString_GET_SIZE(x))
+        UPDATE_TAIL(self, n+PyString_GET_SIZE(x))
         return 1
 
     cdef int _encode_list(self, x) except 0:
         self._ensure_buffer(2)
         self.tail[0] = c'l'
-        self._update_tail(1)
+        UPDATE_TAIL(self, 1)
 
         for i in x:
             self.process(i)
 
         self.tail[0] = c'e'
-        self._update_tail(1)
+        UPDATE_TAIL(self, 1)
         return 1
 
     cdef int _encode_dict(self, x) except 0:
         self._ensure_buffer(2)
         self.tail[0] = c'd'
-        self._update_tail(1)
+        UPDATE_TAIL(self, 1)
 
         keys = x.keys()
         keys.sort()
@@ -336,7 +343,7 @@
             self.process(x[k])
 
         self.tail[0] = c'e'
-        self._update_tail(1)
+        UPDATE_TAIL(self, 1)
         return 1
 
     def process(self, object x):



More information about the bazaar-commits mailing list