Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'encoding': 0.05; 'subject:code': 0.07; 'utf-8': 0.07; 'string': 0.09; 'bytes.': 0.09; 'encode': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'url:unicode': 0.09; 'worked,': 0.09; 'python': 0.11; 'jan': 0.12; 'encodes': 0.16; 'encoding.': 0.16; 'encodings': 0.16; 'ought': 0.16; 'pairs': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'subject:UTF': 0.16; 'surrogate': 0.16; 'url:faq': 0.16; 'wrote:': 0.18; 'header:User-Agent:1': 0.23; 'byte': 0.24; 'bytes': 0.24; 'faq': 0.24; 'interpret': 0.24; '(or': 0.24; 'question': 0.24; 'this:': 0.26; 'defined': 0.27; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'am,': 0.29; 'code': 0.31; "skip:' 10": 0.31; "d'aprano": 0.31; 'steven': 0.31; 'says': 0.33; 'url:python': 0.33; 'convert': 0.35; 'point.': 0.35; 'but': 0.35; 'entry': 0.36; 'url:org': 0.36; 'should': 0.36; 'url:library': 0.38; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'previous': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'conversion': 0.61; 'url:3': 0.61; 'received:173': 0.61; 'back': 0.62; 'such': 0.63; 'direct': 0.67; 'believe': 0.68; 'points,': 0.84; 'received:fios.verizon.net': 0.84; '"how': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Encoding of surrogate code points to UTF-8 Date: Tue, 08 Oct 2013 18:17:01 -0400 References: <52540e03$0$29984$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 40 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1381270636 news.xs4all.nl 15926 [2001:888:2000:d::a6]:56314 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:56447 On 10/8/2013 5:47 PM, Terry Reedy wrote: > On 10/8/2013 9:52 AM, Steven D'Aprano wrote: >> But reading the previous entry in the FAQs: >> >> http://www.unicode.org/faq/utf_bom.html#utf8-4 >> >> I interpret this as meaning that I should be able to encode valid pairs >> of surrogates. > > It says you should be able to 'convert' them, and that the result for > utf-8 encoding must be a single 4-bytes code for the corresponding > supplementary codepoint. To expand on this: The FAQ question is "How do I convert a UTF-16 surrogate pair such as to UTF-8?" utf-16 and utf-8 are both byte (or double byte) encodings of codepoints. Direct conversion would be 'transcoding', not encoding. Python has a few bytes transcoders and one string transcoder (rot_13), listed at the end of http://docs.python.org/3/library/codecs.html#python-specific-encodings But in general, one must decode bytes to string and encode back to bytes. >> So if I find a code point that encodes to a surrogate pair >> in UTF-16: >> >> py> c = '\N{LINEAR B SYLLABLE B038 E}' >> py> surr_pair = c.encode('utf-16be') >> py> print(surr_pair) >> b'\xd8\x00\xdc\x01' >> >> and then use those same values as the code points, I ought to be able to >> encode to UTF-8, as if it were the same \N{LINEAR B SYLLABLE B038 E} code >> point. I believe the utf encodings are defined as 1 to 1. If the above worked, utf-8 would not be. -- Terry Jan Reedy