Skip to content

Instantly share code, notes, and snippets.

@CallumHoward
Last active January 19, 2019 22:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CallumHoward/6d44d968e4c2a2ec7fbdc76ea947161c to your computer and use it in GitHub Desktop.
Save CallumHoward/6d44d968e4c2a2ec7fbdc76ea947161c to your computer and use it in GitHub Desktop.
Splitting escapes
# escape_splitter.py
# Callum Howard, 2019
escapes = {r'\a', r'\b', r'\f', r'\n', r'\r', r'\t', r'\v', r'\\', r"\'", r'\"', r'\?'}
def split_on_escape(input, prefix=''):
digraphs = [''.join(pair) for pair in zip(input, input[1:])]
for i, digraph in enumerate(digraphs):
if digraph in escapes:
return [prefix + input[:i]] + split_on_escape(input[i+2:], prefix=digraph)
return [prefix + input]
def split_on_escape1(input):
result = []
prefix = ''
skip = 0
digraphs = [''.join(pair) for pair in zip(input, input[1:])]
for i, digraph in enumerate(digraphs):
if skip > 0:
skip -= 1
continue
if digraph not in escapes:
prefix += input[i]
else:
result.append(prefix)
prefix = digraph
skip = 1
if skip == 0:
prefix += input[-1]
result.append(prefix)
return result
# tests
f = split_on_escape1
print(f(r'Hello\nworld'))
assert(f(r'Hello\nworld') == ['Hello', r'\nworld'])
print(f(r'\nHello world'))
assert(f(r'\nHello world') == ['', r'\nHello world'])
print(f(r'Hello world\n'))
assert(f(r'Hello world\n') == [r'Hello world', r'\n'])
print(f(r'Hell\o\nworld'))
assert(f(r'Hell\o\nworld') == [r'Hell\o', r'\nworld'])
print(f(r'Hello\\nworld'))
assert(f(r'Hello\\nworld') == ['Hello', r'\\nworld'])
print(f(r'Hello\nworld\nfoo\nbar\nbaz'))
assert(f(r'Hello\nworld\nfoo\nbar\nbaz') ==
['Hello', r'\nworld', r'\nfoo', r'\nbar', r'\nbaz'])
@CallumHoward
Copy link
Author

>>> a = 'hello'

>>> list( zip(a, a[1:]) )
[('h', 'e'), ('e', 'l'), ('l', 'l'), ('l', 'o')]

>>> (first + second for (first, second) in zip(a, a[1:]))
<generator object <genexpr> at 0x1030555e8>

>>> list(first + second for (first, second) in zip(a, a[1:]))
['he', 'el', 'll', 'lo']

>>> enumerate(first + second for (first, second) in zip(a, a[1:]))
<enumerate object at 0x103bffea0>

>>> list(enumerate(first + second for (first, second) in zip(a, a[1:])))
[(0, 'he'), (1, 'el'), (2, 'll'), (3, 'lo')]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment