Add index and split methods to GapString

This commit is contained in:
Neale Pickett 2009-04-06 15:07:18 -06:00
parent e4c37a3a24
commit 41a3eae8ab
1 changed files with 27 additions and 2 deletions

View File

@ -20,7 +20,7 @@ class GapString:
self.append(init) self.append(init)
def __len__(self): def __len__(self):
return self.length return int(self.length)
def __repr__(self): def __repr__(self):
return '<GapString of length %d>' % self.length return '<GapString of length %d>' % self.length
@ -31,7 +31,7 @@ class GapString:
self.contents.append(i) self.contents.append(i)
except TypeError: except TypeError:
self.length += i self.length += i
self.contents.append(long(i)) self.contents.append(i)
def __str__(self): def __str__(self):
ret = [] ret = []
@ -150,6 +150,31 @@ class GapString:
new.append(i) new.append(i)
return new 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 = []
n = 0
cur = self
while (not times) or (n < 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): def startswith(self, what):
return (what == str(self[:len(what)])) return (what == str(self[:len(what)]))