mirror of https://github.com/dirtbags/netarch.git
Add index and split methods to GapString
This commit is contained in:
parent
e4c37a3a24
commit
41a3eae8ab
29
gapstr.py
29
gapstr.py
|
@ -20,7 +20,7 @@ class GapString:
|
|||
self.append(init)
|
||||
|
||||
def __len__(self):
|
||||
return self.length
|
||||
return int(self.length)
|
||||
|
||||
def __repr__(self):
|
||||
return '<GapString of length %d>' % self.length
|
||||
|
@ -31,7 +31,7 @@ class GapString:
|
|||
self.contents.append(i)
|
||||
except TypeError:
|
||||
self.length += i
|
||||
self.contents.append(long(i))
|
||||
self.contents.append(i)
|
||||
|
||||
def __str__(self):
|
||||
ret = []
|
||||
|
@ -150,6 +150,31 @@ class GapString:
|
|||
new.append(i)
|
||||
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):
|
||||
return (what == str(self[:len(what)]))
|
||||
|
||||
|
|
Loading…
Reference in New Issue