Permalink
Browse files

Style tweak: flatten out the conditionals.

  • Loading branch information...
Andy Chu
Andy Chu committed Jun 15, 2018
1 parent cbc99f0 commit 2503f258ef57ddfbc4b8da7cd96a20a5d42caae8
Showing with 22 additions and 20 deletions.
  1. +22 −20 core/word_compile.py
View
@@ -36,30 +36,32 @@ def Utf8Encode(code):
Based on https://stackoverflow.com/a/23502707
"""
num_cont_bytes = 0
if code <= 0x7F:
bytes_ = [code & 0x7F]
elif code > 0x10FFFF:
bytes_ = [0xEF, 0xBF, 0xBD] # unicode replacement character
return chr(code & 0x7F) # ASCII
elif code <= 0x7FF:
num_cont_bytes = 1
elif code <= 0xFFFF:
num_cont_bytes = 2
elif code <= 0x10FFFF:
num_cont_bytes = 3
else:
if code <= 0x7FF:
num_cont_bytes = 1
elif code <= 0xFFFF:
num_cont_bytes = 2
else:
num_cont_bytes = 3
bytes_ = []
for _ in xrange(num_cont_bytes):
bytes_.append(0x80 | (code & 0x3F))
code >>= 6
b = (0x1E << (6-num_cont_bytes)) | (code & (0x3F >> num_cont_bytes))
bytes_.append(b)
bytes_.reverse()
return '\xEF\xBF\xBD' # unicode replacement character
bytes_ = []
for _ in xrange(num_cont_bytes):
bytes_.append(0x80 | (code & 0x3F))
code >>= 6
b = (0x1E << (6-num_cont_bytes)) | (code & (0x3F >> num_cont_bytes))
bytes_.append(b)
bytes_.reverse()
# mod 256 because Python ints don't wrap around!
return "".join(chr(b & 0xFF) for b in bytes_)
return ''.join(chr(b & 0xFF) for b in bytes_)
# TODO: Strict mode syntax errors:

0 comments on commit 2503f25

Please sign in to comment.