Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3a.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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'string': 0.09; "'a'": 0.09; 'indexes': 0.09; 'override': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:string': 0.09; "'b'": 0.16; 'behavior:': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 's[0]': 0.16; 'skip:> 20': 0.16; 'sliced': 0.16; 'subject: \n ': 0.16; 'subject:problems': 0.16; 'wrote:': 0.18; 'implementing': 0.19; 'header:User-Agent:1': 0.23; 'certainly': 0.24; 'string,': 0.24; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'work.': 0.31; '>>>>': 0.31; 'subject:some': 0.31; 'this.': 0.32; 'worked': 0.33; 'could': 0.34; 'something': 0.35; 'operations': 0.35; 'there': 0.35; 'adjust': 0.36; 'c++': 0.36; 'false': 0.36; 'skip:> 10': 0.36; 'implement': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'new': 0.61; 'first': 0.61; 'subject:more': 0.64; 'subject:have': 0.80; "'bc'": 0.84; '10:32': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Ned Batchelder Subject: Re: string processing - some problems whenever I have to parse a more complex string Date: Tue, 21 Oct 2014 14:23:22 -0400 References: <2637a169-46a9-4787-8267-ac0775449e7e@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 18.189.23.172 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 In-Reply-To: <2637a169-46a9-4787-8267-ac0775449e7e@googlegroups.com> 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: 38 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1413915813 news.xs4all.nl 2905 [2001:888:2000:d::a6]:46194 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:79394 On 10/21/14 10:32 AM, CWr wrote: > Is there any implementation like C++ StringPiece class? Or something like the following behavior: > > >>>> >>>s = StringSlice('abcdef') >>>> >>>s > StringSlice('abcdef') at xxx >>>> >>>s[0] > 'a' >>>> >>>s.chop(1) # chop the first item >>>> >>>s[0] # 'b' is the new first item > 'b' >>>> >>>s[:2] > 'bc' >>>> >>>s.chop(-1) # chop the last item >>>> >>>s[-1] > 'e' >>>> >>>s[1:] > 'cde' >>>> >>>while s[0] != 'e': > s.chop(1) >>>> >>>s[0] > 'e' >>>> >>>s.startswith('e') > True >>>> >>>s.isdigit() > False You could certainly implement a StringSlice object that worked like this. Store the string, the start and end indexes. Override __getitem__ to adjust indexes and slice endpoints, implement chop() to change the start or end indexes. Implementing the string operations would get messy, likely you would materialize the sliced string to make them work. -- Ned Batchelder, http://nedbatchelder.com