netarch/gapstr.py

247 lines
6.1 KiB
Python
Raw Permalink Normal View History

2008-01-18 18:57:23 -07:00
#! /usr/bin/python
2008-07-08 17:52:28 -06:00
## 2008 Massive Blowout
2009-08-18 12:23:13 -06:00
"""Functions to treat a list as a byte array with gaps.
2008-01-18 18:57:23 -07:00
2009-08-18 12:23:13 -06:00
Lists should have only byte and numeric items.
2008-01-18 18:57:23 -07:00
"""
import __init__
import sys
class GapString:
2008-01-18 19:16:36 -07:00
def __init__(self, init=None, drop='?'):
2008-01-18 18:57:23 -07:00
self.contents = []
self.length = 0
self.drop = drop
2011-07-02 15:04:55 -06:00
2008-01-18 19:16:36 -07:00
if init:
self.append(init)
2008-01-18 18:57:23 -07:00
def __len__(self):
return int(self.length)
2008-01-18 18:57:23 -07:00
2011-07-02 15:04:55 -06:00
def loss(self):
ret = 0
for i in self.contents:
try:
ret += i
except TypeError:
pass
return ret
2008-01-18 18:57:23 -07:00
def __repr__(self):
return '<GapString of length %d>' % self.length
def append(self, i):
try:
2008-01-18 18:57:23 -07:00
self.length += len(i)
self.contents.append(i)
except TypeError:
self.length += i
self.contents.append(i)
2008-01-18 18:57:23 -07:00
2011-07-02 15:04:55 -06:00
def pop(self, idx=-1):
item = self.contents.pop(idx)
try:
self.length -= item
except TypeError:
self.length -= len(item)
return GapString(item)
2008-01-18 18:57:23 -07:00
def __str__(self):
ret = []
for i in self.contents:
try:
2008-01-18 18:57:23 -07:00
ret.append(self.drop * i)
except TypeError:
2008-01-18 18:57:23 -07:00
ret.append(i)
return ''.join(ret)
def __iter__(self):
for i in self.contents:
try:
for c in i:
yield c
except TypeError:
for j in range(i):
yield self.drop
def __nonzero__(self):
return self.length > 0
2008-07-21 17:52:35 -06:00
def hasgaps(self):
for i in self.contents:
2009-08-18 12:23:13 -06:00
try:
len(i)
except TypeError:
2008-07-21 17:52:35 -06:00
return True
return False
2008-01-18 18:57:23 -07:00
def hexdump(self, fd=sys.stdout):
offset = 0
d = __init__.HexDumper(fd)
for i in self.contents:
try:
2009-08-18 12:23:13 -06:00
for j in xrange(i):
2008-01-18 18:57:23 -07:00
d.dump_drop()
except TypeError:
2008-01-18 18:57:23 -07:00
for c in i:
d.dump_chr(c)
2008-01-18 19:09:09 -07:00
d.finish()
2008-01-18 18:57:23 -07:00
def extend(self, other):
self.contents += other.contents
self.length += other.length
def __getslice__(self, start, end):
end = min(self.length, end)
2008-01-18 19:09:09 -07:00
start = min(self.length, start)
2008-01-18 18:57:23 -07:00
2008-01-18 19:16:36 -07:00
new = self.__class__(drop=self.drop)
new.length = max(end - start, 0)
2008-01-18 19:09:09 -07:00
if new.length == 0:
2008-06-18 21:35:27 -06:00
new.contents = []
2008-01-18 19:09:09 -07:00
return new
2008-06-18 21:35:27 -06:00
new.contents = self.contents[:]
2008-01-18 19:09:09 -07:00
2008-01-18 18:57:23 -07:00
l = self.length - new.length - start
# Trim off the beginning
while start >= 0:
i = new.contents.pop(0)
try:
2008-01-18 18:57:23 -07:00
start -= i
if start < 0:
new.contents.insert(0, -start)
except TypeError:
2008-01-18 18:57:23 -07:00
start -= len(i)
if start < 0:
new.contents.insert(0, i[start:])
# Trim off the end
while l >= 0:
i = new.contents.pop()
try:
2008-01-18 18:57:23 -07:00
l -= i
if l < 0:
new.contents.append(-l)
except TypeError:
2008-01-18 18:57:23 -07:00
l -= len(i)
if l < 0:
new.contents.append(i[:-l])
return new
2008-06-18 21:35:27 -06:00
def __getitem__(self, idx):
2009-08-18 12:23:13 -06:00
if False:
c = self[idx:idx+1]
if c.hasgaps():
return self.drop[0]
else:
return c.contents[0][0]
else:
l = 0
for i in self.contents:
try:
l += len(i)
except TypeError:
l += i
if l > idx:
offs = idx - l
try:
return i[offs]
except:
return self.drop[0]
raise IndexError('Out of bounds')
2008-06-18 21:35:27 -06:00
2008-01-18 18:57:23 -07:00
def __add__(self, other):
if isinstance(other, str):
self.append(other)
else:
2008-01-18 19:27:37 -07:00
new = self.__class__(drop=self.drop)
2008-01-18 18:57:23 -07:00
new.extend(self)
new.extend(other)
return new
def __xor__(self, mask):
try:
2008-06-18 21:35:27 -06:00
mask = [ord(c) for c in mask]
except TypeError:
2009-08-18 12:23:13 -06:00
pass
try:
masklen = len(mask)
except TypeError:
masklen = 1
mask = [mask]
2008-01-18 18:57:23 -07:00
2008-01-18 19:27:37 -07:00
new = self.__class__(drop=self.drop)
2008-01-18 18:57:23 -07:00
for i in self.contents:
try:
2008-01-18 18:57:23 -07:00
r = []
offset = len(new) % masklen
for c in i:
o = ord(c)
r.append(chr(o ^ mask[offset]))
offset = (offset + 1) % masklen
new.append(''.join(r))
except TypeError:
new.append(i)
2008-01-18 18:57:23 -07:00
return new
def index(self, needle):
pos = 0
for i in self.contents:
try:
return pos + i.index(needle)
except AttributeError:
pos += i
except ValueError:
pos += len(i)
raise ValueError('substring not found')
def split(self, pivot=' ', times=None):
ret = []
cur = self
2011-07-02 15:04:55 -06:00
while (not times) or (len(ret) < times):
try:
pos = cur.index(pivot)
except ValueError:
break
ret.append(cur[:pos])
cur = cur[pos+len(pivot):]
ret.append(cur)
return ret
def startswith(self, what):
return (what == str(self[:len(what)]))
2008-01-18 18:57:23 -07:00
2009-04-07 08:24:04 -06:00
def endswith(self, what):
return (what == str(self[-len(what):]))
2008-01-18 18:57:23 -07:00
if __name__ == '__main__':
gs = GapString()
gs.append('hi')
assert str(gs) == 'hi'
assert str(gs[:40]) == 'hi'
gs.append(3)
assert str(gs) == 'hi???'
assert str(gs[:40]) == 'hi???'
assert str(gs[:3]) == 'hi?'
assert str(gs[-4:]) == 'i???'
assert str(gs + gs) == 'hi???hi???'
assert str(gs ^ 1) == 'ih???'
gs = GapString()
gs.append('123456789A')
assert str(gs[:4]) == '1234'
assert len(gs[:4]) == 4
assert len(gs[6:]) == 4
2008-06-18 21:35:27 -06:00
assert str(gs[:0]) == ''