Skip to content

Instantly share code, notes, and snippets.

@v2e4lisp
Created December 12, 2016 15:10
Show Gist options
  • Save v2e4lisp/9270449b29ebd788b0cb6921e7dcf6a9 to your computer and use it in GitHub Desktop.
Save v2e4lisp/9270449b29ebd788b0cb6921e7dcf6a9 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def utf8cut(ba, l):
if len(ba) <= l:
return ba
pos = l
while (pos > 0):
b = ba[pos]
if (b & 0x80 == 0) or (b & 0xc0 == 0xc0):
break
else:
pos -= 1
return ba[0:pos]
x = bytearray("1世2界1")
for i in range(len(x)):
print i, "= [", str(utf8cut(x, i)), "]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment