Page 1 of 1

Pack or comPack

Posted: Sun Aug 01, 2004 8:33 pm
by 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.

Posted: Mon Aug 02, 2004 1:50 am
by Lutz
I did this for readability, may be I can allow both, if it is not code-expensive, I will look into it.

Lutz

Posted: Mon Aug 02, 2004 2:18 am
by Lutz
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

Posted: Mon Aug 02, 2004 11:45 am
by newdep
Hi Lutz,

No problem..thanks for the reply..

Regards, Norman.

Posted: Mon Aug 02, 2004 12:27 pm
by Sammo
I'm sure somebody can wrap the following into a macro.

Code: Select all

(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")

Posted: Mon Aug 02, 2004 5:13 pm
by newdep
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.

Posted: Mon Aug 02, 2004 5:34 pm
by Sammo
> (comPack "s123n456cccs12")
"s123 n456 c c c s12"
> (comPack "n1n12n123s1s12s123")
"n1 n12 n123 s1 s12 s123"

Posted: Mon Aug 02, 2004 5:55 pm
by newdep
;-)

Posted: Mon Aug 02, 2004 6:02 pm
by newdep
Thanks Sammo... ill use that function ;-)

Posted: Tue Aug 03, 2004 9:43 pm
by Sammo
Here's a macro that seems to work:

Code: Select all

(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:

Code: Select all

> (compack "cccs5" 1 2 3 "hello")
"\001\002\003hello"

Posted: Fri Aug 06, 2004 10:25 pm
by Sammo
In v8.1.0-rc2, Lutz now permits "cccs5" and "ccc s5" as well as "c c c s5". Excellent!