Exemple #1
0
def test_string_buffer():
    buf = StringBuffer('hello world')
    assert buf.getitem(4) == 'o'
    assert buf.getitem(4) == buf[4]
    assert buf.getlength() == 11
    assert buf.getlength() == len(buf)
    assert buf.getslice(1, 1, 5) == 'ello '
    assert buf.getslice(1, 1, 5) == buf[1:6]
    assert buf.getslice(1, 2, 3) == 'el '
    assert buf.as_str() == 'hello world'
Exemple #2
0
class MasterReader(object):
    def __init__(self, s):
        self.inputbuf = StringBuffer(s)
        self.length = len(s)
        self.inputpos = 0

    def can_advance(self, count):
        end = self.inputpos + count
        return end <= self.length

    def advance(self, count):
        if not self.can_advance(count):
            raise StructError("unpack str size too short for format")
        self.inputpos += count

    def read(self, count):
        curpos = self.inputpos
        self.advance(count) # raise if we are out of bound
        return self.inputbuf.getslice(curpos, 1, count)

    def align(self, mask):
        self.inputpos = (self.inputpos + mask) & ~mask