Skip to content

Instantly share code, notes, and snippets.

@ethe
Created April 10, 2017 08:26
Show Gist options
  • Select an option

  • Save ethe/d16bf59c811dd5311f31492271c7387e to your computer and use it in GitHub Desktop.

Select an option

Save ethe/d16bf59c811dd5311f31492271c7387e to your computer and use it in GitHub Desktop.
def is_surrogate(string):
for index, char in enumerate(string):
if 0xD800 <= ord(char) <= 0xDBFF:
try:
next_char = string[index + 1]
except IndexError:
pass
if 0xDC00 <= ord(next_char) <= 0xDFFF:
return True
else:
raise ValueError(u"Illegal UTF-16 sequence: {}".format(string))
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment