string processing - some problems whenever I have to parse a more complex string
christoph.wruck at gmail.com
Tue Oct 21 16:32:30 CEST 2014
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')
StringSlice('abcdef') at xxx
>>>s.chop(1) # chop the first item
>>>s # 'b' is the new first item
>>>s.chop(-1) # chop the last item
>>>while s != 'e':
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.
c = string
# 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...
More information about the Python-list