English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Caml-list] Strings as arrays or lists...
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2003-03-04 (09:20)
From: Fabrice Le Fessant <fabrice@l...>
Subject: Re: [Caml-list] Strings as arrays or lists...

>  I recently wrote some code that made use of char lists, and explode
>  and implode came in handy.  I needed to marshal a recursive datatype
>  into a packet to be sent over a communication channel according to a
>  protocol that imposed a specific format, including a length byte at
>  the beginning and a checksum byte at the end.
>  I could have made one pass over the data to compute the packet length,
>  then a second pass marshaling it into a buffer.  But it was very
>  natural to just build up a list of bytes in a single traversal of the
>  datatype.  Then the length and checksum could easily be added to the
>  beginning and the end, and the result written out.

I had exactly the same code to write one year ago, and I simply built
the packet inside a buffer, with a 0 length-field, changed it to a
string, and filled the length-field just before sending, by changing
the chars in place in the string. It only allocates the buffer once,
and then the final string at the end, I cannot believe that using a
list of chars can be more efficient, but sometimes strange things

- Fabrice

To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners