~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/_btree_serializer_pyx.pyx

  • Committer: John Arbash Meinel
  • Date: 2010-08-23 19:10:35 UTC
  • mto: This revision was merged to the branch mainline in revision 5390.
  • Revision ID: john@arbash-meinel.com-20100823191035-57bojnmqw54nutsz
switch 'x += 1' to 'x = x + 1' to deal with brain-damaged old versions of pyrex.

Show diffs side-by-side

added added

removed removed

Lines of Context:
370
370
    j = 0
371
371
    for i from 0 <= i < 20:
372
372
        top = _unhexbuf[<unsigned char>(as_hex[j])]
373
 
        j += 1
 
373
        j = j + 1
374
374
        bot = _unhexbuf[<unsigned char>(as_hex[j])]
375
 
        j += 1
 
375
        j = j + 1
376
376
        if top == -1 or bot == -1:
377
377
            return 0
378
378
        as_bin[i] = <unsigned char>((top << 4) + bot);
397
397
    for i from 0 <= i < 20:
398
398
        c = as_bin[i]
399
399
        as_hex[j] = _hexbuf[(c>>4)&0xf]
400
 
        j += 1
 
400
        j = j + 1
401
401
        as_hex[j] = _hexbuf[(c)&0xf]
402
 
        j += 1
 
402
        j = j + 1
403
403
 
404
404
 
405
405
def _py_hexlify(as_bin):
670
670
            PyList_Append(result, item)
671
671
        return result
672
672
 
 
673
    cdef int _count_records(self, char *c_content, char *c_end):
 
674
        """Count how many records are in this section."""
 
675
        cdef char *c_cur
 
676
        cdef int num_records
 
677
 
 
678
        c_cur = c_content
 
679
        num_records = 0
 
680
        while c_cur != NULL and c_cur < c_end:
 
681
            c_cur = <char *>memchr(c_cur, c'\n', c_end - c_cur);
 
682
            if c_cur == NULL:
 
683
                break
 
684
            c_cur = c_cur + 1
 
685
            num_records = num_records + 1
 
686
        return num_records
 
687
 
673
688
    cdef _parse_bytes(self, bytes):
674
689
        """Parse the string 'bytes' into content."""
675
690
        cdef char *c_bytes
676
 
        cdef char *c_content
677
691
        cdef char *c_cur
678
692
        cdef char *c_end
679
693
        cdef Py_ssize_t n_bytes
690
704
        if strncmp(c_bytes, 'type=leaf\n', 10):
691
705
            raise ValueError("bytes did not start with 'type=leaf\\n': %r"
692
706
                             % (bytes[:10],))
693
 
        c_content = c_bytes + 10
694
 
        c_cur = c_content
695
 
        num_records = 0
696
 
        while c_cur != NULL and c_cur < c_end:
697
 
            c_cur = <char *>memchr(c_cur, c'\n', c_end - c_cur);
698
 
            if c_cur == NULL:
699
 
                break
700
 
            c_cur += 1
701
 
            num_records += 1
 
707
        c_cur = c_bytes + 10
 
708
        num_records = self._count_records(c_cur, c_end)
702
709
        # Now allocate the memory for these items, and go to town
703
 
        # We allocate both the offsets and the records in the same malloc. we
704
 
        # should probably pay a bit closer attention to alignment
705
710
        self.records = <gc_chk_sha1_record*>PyMem_Malloc(num_records *
706
711
            (sizeof(unsigned short) + sizeof(gc_chk_sha1_record)))
707
712
        self.num_records = num_records
708
 
        c_cur = c_content
709
713
        cur_record = self.records
710
714
        entry = 0
711
715
        while c_cur != NULL and c_cur < c_end and entry < num_records:
712
716
            c_cur = self._parse_one_entry(c_cur, c_end, cur_record)
713
 
            cur_record += 1
714
 
            entry += 1
 
717
            cur_record = cur_record + 1
 
718
            entry = entry + 1
715
719
        if (entry != self.num_records
716
720
            or c_cur != c_end
717
721
            or cur_record != self.records + self.num_records):
729
733
        if strncmp(c_cur, 'sha1:', 5):
730
734
            raise ValueError('line did not start with sha1: %r'
731
735
                % (safe_string_from_size(c_cur, 10),))
732
 
        c_cur += 5
 
736
        c_cur = c_cur + 5
733
737
        c_next = <char *>memchr(c_cur, c'\0', c_end - c_cur)
734
738
        if c_next == NULL or (c_next - c_cur != 40):
735
739
            raise ValueError('Line did not contain 40 hex bytes')
738
742
        c_cur = c_next + 1
739
743
        if c_cur[0] != c'\0':
740
744
            raise ValueError('only 1 null, not 2 as expected')
741
 
        c_cur += 1
 
745
        c_cur = c_cur + 1
742
746
        cur_record.block_offset = strtoll(c_cur, &c_next, 10)
743
747
        if c_cur == c_next or c_next[0] != c' ':
744
748
            raise ValueError('Failed to parse block offset')
795
799
            common_shift = 24
796
800
            while common_mask & 0x80000000 and common_shift > 0:
797
801
                common_mask = common_mask << 1
798
 
                common_shift -= 1
 
802
                common_shift = common_shift - 1
799
803
            self.common_shift = common_shift
800
804
        offset = 0
801
805
        max_offset = self.num_records
809
813
            this_offset = self._offset_for_sha1(self.records[i].sha1)
810
814
            while offset <= this_offset:
811
815
                self.offsets[offset] = i
812
 
                offset += 1
 
816
                offset = offset + 1
813
817
        while offset < 257:
814
818
            self.offsets[offset] = max_offset
815
 
            offset += 1
 
819
            offset = offset + 1
816
820
 
817
821
    def _get_offsets(self):
818
822
        cdef int i