Pack or comPack

For the Compleat Fan
Locked
newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Pack or comPack

Post 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.
-- (define? (Cornflakes))

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

Hi Lutz,

No problem..thanks for the reply..

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

Sammo
Posts: 180
Joined: Sat Dec 06, 2003 6:11 pm
Location: Loveland, Colorado USA

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

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post 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.
-- (define? (Cornflakes))

Sammo
Posts: 180
Joined: Sat Dec 06, 2003 6:11 pm
Location: Loveland, Colorado USA

Post by Sammo »

> (comPack "s123n456cccs12")
"s123 n456 c c c s12"
> (comPack "n1n12n123s1s12s123")
"n1 n12 n123 s1 s12 s123"

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

;-)
-- (define? (Cornflakes))

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

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

Sammo
Posts: 180
Joined: Sat Dec 06, 2003 6:11 pm
Location: Loveland, Colorado USA

Post 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"

Sammo
Posts: 180
Joined: Sat Dec 06, 2003 6:11 pm
Location: Loveland, Colorado USA

Post by Sammo »

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

Locked