Andrei Alexandrescu , dans le message (digitalmars.D:144012), a écrit :
> On 9/6/11 7:18 AM, Christophe wrote:
>>
>>> size_t readFrom(const(char)[] data, size_t start); // same as
>>> readUntil delegate
>>
>> What happens if the buffer data get exhausted ? The function calling
>> readFrom has no way to know how many characters to put into data to
>> allow the read.
>> What is the point of start ?
>>
>> We could use a delegate to return new characters:
>>
>> void readFrom(const(char)[] delegate(size_t) stream,
>>                in char[] format = null);
> 
> This won't work for cases such as "parse digits until a non-digit is 
> found, but don't discard that non-digit".

It does, since the characters are only discarded at the next call to 
stream(n), according to the value n. See my answer to Steve.

I first considered a slightly more complicated API:

void readFrom(const(char())[] delegate() stream, void delegate(size_t) 
nread)
{
  auto buf = stream();
  // .. do things and count the number of readCharacters
  nread(n);
}

but:

void readFrom(const(char())[] delegate(size_t) stream)
{
  auto buf = stream(0);
  // .. do things and count the number of readCharacters
  stream(n);
}

Works about as good, and is IMO simpler.

> Reading is considerably more difficult than writing. I think it's fair 
> to leave it to more sophisticated APIs than one delegate.

Maybe.

-- 
Christophe Travert

Reply via email to