Re: String/Buffer split & encoding

From: Henrik Nordstrom <henrik_at_henriknordstrom.net>
Date: Wed, 21 Jan 2009 19:44:09 +0100

ons 2009-01-21 klockan 09:37 -0700 skrev Alex Rousskov:

> You do not propose a class dedicated to I/O buffers. I think we would
> benefit from having such a class, but we can add it later, possibly as a
> part of vectorizing work. Do you want the low-level I/O code to use
> Buffer, for now?

That's effectively the "splitter" class I guess. Used whenever you have
a stream of data being procuded in one way or another needing raw memory
and having the result encapsulated into String pieces as it's produced
(or Buffer if that abstraction is kept).

Useful for not only I/O, but also many other uses.

Note: The name "splitter" is very wrong. It's the glue between "I need
linear memory to stuff things into, piece by piece" and String (or
Buffer). I guess a better name would be "allocation context" or similar.
But I'll stick to "splitter" for now to not confuse matters more.

A normal input I/O producer would be

   size_t avail = splitter.avail(); // maybe with a "min size" argument
   char *iobuf = splitter.address();
   ssize_t len = read(fd, iobuf, avail)
   String data = splitter.consume(len);

A sprintf based producer would in fact be quite similar.

   size_t min_size = 8;
   while(1) {
      size_t avail = splitter.avail(min_size);
      char *iobuf = splitter.address();
      va_list args_copy = va_copy(args)
      ssize_t len = vsnprintf(iobuf, avail, format, args_copy);
      if (len <= avail && len != -1)
         break;
      if (len > 0)
         min_size = len;
      else
         min_size *= 2;
   }
   String data = splitter.consume(len);

It can be argued that the above should be methods on the class. Makes
the code nicer, but many different producer methods may be needed and
extended while using a common context for the stream in question.
Additinally these thoughts is very old, from before we started the
venture into C++ land..

I guess you by now also see why StringV is needed for this to work out
well..

   StringV result;

   while (something) {
        ...
        String chunk = Sprintf(splitter, format, args);
        result += chunk;
   }

or

   StringV buf;

   while (need_more) {
         request more data and feed into buf
   }
   process buf.

(very schematic pseudo code, actual event driven code would be quite
different unless we plunge into the land of tame or switch completely to
another language with event support..)

Regards
Henrik
Received on Wed Jan 21 2009 - 18:44:21 MST

This archive was generated by hypermail 2.2.0 : Wed Jan 21 2009 - 12:00:26 MST