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

CWr christoph.wruck at gmail.com
Tue Oct 21 16:32:30 CEST 2014


Hello together,

currently I have to parse a string in an atomic way. Normally - in this case too - I have a counter variable to keep the current position inside the string. So far, I think this is the most flexible solution to do some lookaround's inside the string if necessary. Subroutines will be feed by the underlying data and the current position. A subroutine returns a tuple of the new position and the result. But I would like process subroutines with the same flexibillity (slicing/lookaround) but without returning the new position every again.

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

Subroutines could chop the number of processed items internally if no error occours.

Another possibillty will be to chop the current item manually. But I don't know how efficient this is in case of large strings.

>>>while string:
       c = string[0]
       # process it ...
       string = string[1:]

But this assumes that I have to return rest of the string too and not useful for
my problem covered abrove.

Has anyone some tips what's the best practice to process a complex string, handling prositions in/and subroutines.

Thanks for all answers...
Cheers
Chris



More information about the Python-list mailing list