Skip to content

Commit

Permalink
Add VTMLBuffer.split
Browse files Browse the repository at this point in the history
Same semantics as str.split except the default sep is just `' '`.
  • Loading branch information
mayfield committed Apr 5, 2017
1 parent 457d533 commit e0d858a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
21 changes: 21 additions & 0 deletions shellish/rendering/vtml.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,27 @@ def startswith(self, other):
def endswith(self, other):
return self.text().endswith(other)

def split(self, sep=' ', maxsplit=None):
if not sep:
raise ValueError("empty separator")
splits = []
pos = 0
text = self.text()
sep_len = len(sep)
while maxsplit is None or len(splits) < maxsplit:
try:
end = text.index(sep, pos)
except ValueError:
break
splits.append((pos, end))
pos = end + sep_len
splits.append((pos, None))
return [self[start:end] for start, end in splits]
if splits:
return [self[start:end] for start, end in splits]
else:
return [self.copy()]


def vtmlrender(vtmarkup, plain=None, strict=False, vtmlparser=VTMLParser()):
""" Look for vt100 markup and render vt opcodes into a VTMLBuffer. """
Expand Down
31 changes: 31 additions & 0 deletions test/rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,37 @@ def test_in(self):
self.assertNotIn('ABCD', abc)
self.assertNotIn('abcd', abc)

def test_split_maxsplit(self):
ref = 'abc ABC xyz XYZ'
vs = R.vtmlrender(ref)
self.assertListEqual(vs.split(), ref.split(' '))
for i in range(6):
with self.subTest(i):
self.assertListEqual(vs.split(maxsplit=i), ref.split(' ', i))

def test_split_leading(self):
for ref in (' ', ' abc', ' ', ' abc'):
vs = R.vtmlrender(ref)
self.assertListEqual(vs.split(), ref.split(' '))

def test_split_trailing(self):
for ref in ('abc ', 'abc ', ' abc ', ' abc '):
vs = R.vtmlrender(ref)
self.assertListEqual(vs.split(), ref.split(' '))

def test_split_notfound(self):
ref = 'abc'
vs = R.vtmlrender(ref)
self.assertListEqual(vs.split('D'), ref.split('D'))
self.assertListEqual(vs.split('abcd'), ref.split('abcd'))
self.assertListEqual(vs.split('bcd'), ref.split('bcd'))

def test_split_multichar(self):
for ref in ('abcGAPABC', 'abcGAP', 'GAPabc', 'abGAPABGAP', 'aGAPbGAPc'):
vs = R.vtmlrender(ref)
self.assertListEqual(vs.split('GAP'), ref.split('GAP'))


class HTMLConversion(unittest.TestCase):

a_format = '<blue><u>%s</u></blue>'
Expand Down

0 comments on commit e0d858a

Please sign in to comment.