From 41a3eae8abe0bcf7951ec8c3241eac81c9e1b5e0 Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Mon, 6 Apr 2009 15:07:18 -0600 Subject: [PATCH] Add index and split methods to GapString --- gapstr.py | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/gapstr.py b/gapstr.py index e7303d8..b51b968 100755 --- a/gapstr.py +++ b/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 '' % 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)]))