Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #79394

Re: string processing - some problems whenever I have to parse a more complex string

From Ned Batchelder <ned@nedbatchelder.com>
Subject Re: string processing - some problems whenever I have to parse a more complex string
Date 2014-10-21 14:23 -0400
References <2637a169-46a9-4787-8267-ac0775449e7e@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.15041.1413915813.18130.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

string processing - some problems whenever I have to parse a more complex string CWr <christoph.wruck@gmail.com> - 2014-10-21 07:32 -0700
  Re: string processing - some problems whenever I have to parse a more complex string Ned Batchelder <ned@nedbatchelder.com> - 2014-10-21 14:23 -0400
  Re: string processing - some problems whenever I have to parse a more complex string Terry Reedy <tjreedy@udel.edu> - 2014-10-21 18:03 -0400

csiph-web