# HG changeset patch # User Matt Mackall # Date 2015-11-05 22:48:46 # Node ID cf47bdb2183caaaa5daa2acb0d7ab7e88c12b9f9 # Parent 853154f27525d46e62016095e36776a2f41908ea encoding: add getutf8char helper This allows us to find character boundaries in byte strings when trying to do custom encodings. diff --git a/mercurial/encoding.py b/mercurial/encoding.py --- a/mercurial/encoding.py +++ b/mercurial/encoding.py @@ -414,6 +414,25 @@ def jsonescape(s): return ''.join(_jsonmap[c] for c in toutf8b(s)) +_utf8len = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4] + +def getutf8char(s, pos): + '''get the next full utf-8 character in the given string, starting at pos + + Raises a UnicodeError if the given location does not start a valid + utf-8 character. + ''' + + # find how many bytes to attempt decoding from first nibble + l = _utf8len[ord(s[pos]) >> 4] + if not l: # ascii + return s[pos] + + c = s[pos:pos + l] + # validate with attempted decode + c.decode("utf-8") + return c + def toutf8b(s): '''convert a local, possibly-binary string into UTF-8b