Pack or comPack

Started by newdep, August 01, 2004, 01:33:40 PM

Previous topic - Next topic

newdep

Hi Lutz,



I was wondering why the pack function has the behaviour of seperated spaces between the "str-format" and not a Perlish compact way ?



Building dynamic binary data currently needs extra spaces inside pack..



(pack "c c c s5" 1 2 3 "hello")



could be like (but less readable, like Perl..)



(pack "cccs5" 1 2 3 "hello")



Any idea if its an option to make it compact?



(Its not a real big deal actualy but in most pack circumstances more handier)



Regards,

Norman.
-- (define? (Cornflakes))

Lutz

#1
I did this for readability, may be I can allow both, if it is not code-expensive, I will look into it.



Lutz

Lutz

#2
At the moment I am using the normal newlisp tokenizer which breaks the string at spaces. I could do a special 'pack/unpack' format parser, but not for 8.1, which I try to get out soon. It is on the ToDo list for later.



Lutz

newdep

#3
Hi Lutz,



No problem..thanks for the reply..



Regards, Norman.
-- (define? (Cornflakes))

Sammo

#4
I'm sure somebody can wrap the following into a macro.(define (comPack str)
    (map (fn (x y) (replace x str y))
        '("c" "s" "ld" "lu" "u" "d" "f" "n" "LU" "LD")
        '(" c" " s" " LD" " LU" " u" " d" " f" " n" "lu" "ld"))
    (slice str 1))
(pack (comPack "cccs5") 1 2 3 "hello")

newdep

#5
Hello Sammo,



That comes close ;-)



But i have the problem , while packing static binary data, that ie. 's

has a length and 'n also... so it needs to be inserted too... its possible ;-)



Regards, Norman.
-- (define? (Cornflakes))

Sammo

#6
> (comPack "s123n456cccs12")

"s123 n456 c c c s12"

> (comPack "n1n12n123s1s12s123")

"n1 n12 n123 s1 s12 s123"

newdep

#7
;-)
-- (define? (Cornflakes))

newdep

#8
Thanks Sammo... ill use that function ;-)
-- (define? (Cornflakes))

Sammo

#9
Here's a macro that seems to work:(define-macro (compack str)

    (define (aux str)
        (map (fn (x y) (replace x str y))
            '("c" "s" "ld" "lu" "u" "d" "f" "n" "LU" "LD")
            '(" c" " s" " LD" " LU" " u" " d" " f" " n" "lu" "ld"))
        (slice str 1))

    (apply pack (cons (aux str) (rest (args)))) )
and is called like this:> (compack "cccs5" 1 2 3 "hello")
"010203hello"

Sammo

#10
In v8.1.0-rc2, Lutz now permits "cccs5" and "ccc s5" as well as "c c c s5". Excellent!