Idiomatic way to write a range that tracks how much it consumes

Jon Degenhardt jond at noreply.com
Mon Apr 27 03:38:42 UTC 2020


I have a string that contains a sequence of elements, then a 
terminator character, followed by a different sequence of 
elements (of a different type).

I want to create an input range that traverses the initial 
sequence. This is easy enough. But after the initial sequence has 
been traversed, the caller will need to know where the next 
sequence starts. That is, the caller needs to know the index in 
the input string where the initial sequence ends and the next 
sequence begins.

The values returned by the range are a transformation of the 
input, so the values by themselves are insufficient for the 
caller to determined how much of the string has been consumed. 
And, the caller cannot simply search for the terminator character.

Tracking the number of bytes consumed is easy enough. I like to 
do in a way that is consistent with D's normal range paradigm.

Two candidate approaches:
a) Instead of having the range return the individual values, it 
could return a tuple containing the value and the number of bytes 
consumed.

b) Give the input range an extra member function which returns 
the number of bytes consumed. The caller could call this after 
'empty()' returns true to find the amount of data consumed.

Both will work, but I'm not especially satisfied with either. 
Approach (a) seems more consistent with the typical range 
paradigms, but also more of a hassle for callers.

Is there a better way to write this?

--Jon


More information about the Digitalmars-d-learn mailing list