Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'utf-8': 0.07; 'subject:How': 0.09; 'backwards': 0.09; 'counting': 0.09; 'subject:()': 0.09; 'subject:string': 0.09; 'subject:using': 0.09; 'aug': 0.13; 'encoding': 0.15; 'all).': 0.16; 'copying.': 0.16; 'ends,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'recognizing': 0.16; 'storing': 0.16; 'subject:unicode': 0.16; 'subject:variable': 0.16; 'string': 0.17; 'wrote:': 0.17; 'bytes': 0.17; 'char': 0.17; 'string,': 0.17; 'appropriate': 0.20; 'do.': 0.21; 'received:209.85.214.174': 0.21; 'constant': 0.22; 'paul': 0.24; 'header:In-Reply-To:1': 0.25; '(which': 0.26; '(as': 0.27; 'message-id:@mail.gmail.com': 0.27; 'rest': 0.28; "d'aprano": 0.29; 'end,': 0.29; 'relies': 0.29; 'steven': 0.29; 'writes:': 0.29; 'character': 0.29; 'points': 0.29; 'code': 0.31; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'pm,': 0.35; 'received:209.85': 0.35; 'next': 0.35; 'but': 0.36; 'characters': 0.36; 'should': 0.36; 'possible': 0.37; 'being': 0.37; 'maintaining': 0.37; 'received:209': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'received:209.85.214': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'end': 0.40; 'think': 0.40; 'your': 0.60; 'six': 0.65; 'taking': 0.65; '6:11': 0.84; 'subject:value': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=ZJKc9hMv4W2/jiwL4SboFaENyyUNUyA+5oZO7Wx5qs4=; b=L5DdjT7b+UzIyi7eBzw9Y0GE0fTNHWHse7OPoxXI4vBSTsVS9YQBIOalQ3BdcyvxgW Zg9vi7jLHdbXLGTNJYCJrXiI9SuAX9Q/Xc9yL0VU/Ag74nnlGkZg7a+AiDRrzJcNHiHb CEvmxlE9d0lnFPlrbPY4DIBvm60AGBu2gziMt34aEahkrpVNAgYQnJIRglYObNQZPvwn mjZsx7569TSP6QAc1Rj+7QM6fBF9fzHG3W+DOJGMvfV2mvYwAMShQhQ4a2xclr26BUyk Xw1+XDDjrO+szcfukMgRHhUhE5nkAEjXD7RdtPODPvmwnqhrzlzhz1uJhTodWjd1vB/h yHDw== MIME-Version: 1.0 In-Reply-To: <7x4nnzmhbn.fsf@ruckus.brouhaha.com> References: <308df2af-abe7-4043-b199-0a39f440e0ab@googlegroups.com> <502f8a2a$0$29978$c3e8da3$5496439d@news.astraweb.com> <7xehn4vyya.fsf@ruckus.brouhaha.com> <7xfw7j3a1x.fsf@ruckus.brouhaha.com> <7xtxvzehhb.fsf@ruckus.brouhaha.com> <50309d69$0$29978$c3e8da3$5496439d@news.astraweb.com> <7x4nnzmhbn.fsf@ruckus.brouhaha.com> Date: Sun, 19 Aug 2012 18:24:57 +1000 Subject: Re: How do I display unicode value stored in a string variable using ord() From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 26 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1345364700 news.xs4all.nl 6867 [2001:888:2000:d::a6]:36229 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:27362 On Sun, Aug 19, 2012 at 6:11 PM, Paul Rubin wrote: > Steven D'Aprano writes: >> result = text[end:] > > if end not near the end of the original string, then this is O(N) > even with fixed-width representation, because of the char copying. > > if it is near the end, by knowing where the string data area > ends, I think it should be possible to scan backwards from > the end, recognizing what bytes can be the beginning of code points and > counting off the appropriate number. This is O(1) if "near the end" > means "within a constant". Only if you know exactly where the end is (which requires storing and maintaining a character length - this may already be happening, I don't know). But that approach means you need to have code for both ways (forward search or reverse), and of course it relies on your encoding being reverse-scannable in this way (as UTF-8 is, but not all). And of course, taking the *entire* rest of the string isn't the only thing you do. What if you want to take the next six characters after that index? That would be constant time with a fixed-width storage format. ChrisA