Am Donnerstag, 30. Januar 2014 14:05:07 UTC+1 schrieb Michael Gardner: > > On Jan 30, 2014, at 01:36 , Steffen <steffen...@gmail.com <javascript:>> > wrote: > > > If you would like to use a specific codec other than :byte or :ubyte but > also restrict the number of bytes read this would only work if you expected > to have some kind of optional padding after your objects, like: > > > > (padding inner-codec 4096). > > Yes, that's exactly what I need. I didn't try 'padding because the docs > seemed to say that it works only when encoding. > My bad. I changed the readme. Padding will always read the given number of bytes before using the inner codec on those bytes. When writing it adds the needed amount of bytes to ensure that the expected number of bytes were written.
My only problem is that when decoding, I don't know how many objects to > expect before the padding (this is for parsing ID3v2 tags). Ideally I'd > like to say something like (padding (repeated frame-codec) byte-count), > with the padding taking over once the inner codec fails to parse the next > available bytes (but see the next point). > > That's exactly what padding is designed to do: Let's say you know there is a run of bytes with a known length (from a header field maybe) and you want to parse an unbounded number of objects within this area. You could use (padding (repeated inner-codec) 1024) Another example: Let's assume an inputstream with these bytes: [11 5 0 0 0 9 0 0 0 0x99 0x99 0x99] ;the padding length is determined by the byte header, the inner codec `repeated` can only read two integers (8 bytes) (header :byte #(padding (repeated :int-le) % 0x99) (constantly 11)) => [5 9] ; now the inputstream will be empty > > (defn enum [type m] > > (compile-codec type m > > (clojure.set/map-invert m))) > > So m would be a map of for example keywords to a native datatype like > int that would allow you to represent a fixed number of things with > distinct binary representations? Looks good to me. What do you think should > be the behaviour in case of an unspecified value (not in m)? > > I'd expect an exception to be thrown in case of an unspecified value. But > when decoding, it would be nice if the exception were (optionally?) > swallowed when occurring inside a 'padding construct, to allow something > like the above example. Though I don't know how many other binary formats > would require something like that; I imagine most aren't as dumb as ID3v2. > Currently codecs don't know about their context, that means, I can't behave differently depending on whether a codec is used within a padding or not, sorry. > Currently the index in the vector is the index of the bit. Yes, that > means LSB-first. > > Then the docs seem to be wrong (or at least confusing), since the example > code for 'bits says the first item corresponds to the "highest" bit. Thanks, I fixed the documentation. -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.