Skip to content

Instantly share code, notes, and snippets.

@JadenGeller
Created January 24, 2018 08:57
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 JadenGeller/0917f4110a7017bf7354cd870d4eb282 to your computer and use it in GitHub Desktop.
Save JadenGeller/0917f4110a7017bf7354cd870d4eb282 to your computer and use it in GitHub Desktop.
A gross simple state machine
def join_quoted_strs(s):
r = ''
within_quotes = False
is_escaped = False
for c in s:
if within_quotes:
if is_escaped:
r += c
is_escaped = False
else:
if c == '\\':
is_escaped = True
elif c == '"':
within_quotes = False
else:
r += c
else:
if c == '"':
within_quotes = True
else:
assert c == " "
return r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment