Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

People are going to scream bloody murder about that. -- Seen on linux-kernel


devel / comp.lang.postscript / Re: sprintf

SubjectAuthor
* Re: sprintfluser droog
+* Re: sprintfluser droog
|`- Re: sprintfluser droog
`- Re: sprintfDavid Newall

1
Re: sprintf

<9af6f858-1cba-4ac2-ba11-3acc8d5915fan@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=199&group=comp.lang.postscript#199

  copy link   Newsgroups: comp.lang.postscript
X-Received: by 2002:a05:622a:1a85:b0:3de:bafb:82bf with SMTP id s5-20020a05622a1a8500b003debafb82bfmr2777507qtc.4.1679586890911;
Thu, 23 Mar 2023 08:54:50 -0700 (PDT)
X-Received: by 2002:a05:6808:1982:b0:386:a3ff:1f56 with SMTP id
bj2-20020a056808198200b00386a3ff1f56mr2750332oib.0.1679586890036; Thu, 23 Mar
2023 08:54:50 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.postscript
Date: Thu, 23 Mar 2023 08:54:49 -0700 (PDT)
In-Reply-To: <1556fe1d-3a65-2718-7ae1-3e923df0f118@davidnewall.com>
Injection-Info: google-groups.googlegroups.com; posting-host=24.107.184.18; posting-account=G1KGwgkAAAAyw4z0LxHH0fja6wAbo7Cz
NNTP-Posting-Host: 24.107.184.18
References: <f026e4a6-6939-4089-a740-7d944182c16an@googlegroups.com> <1556fe1d-3a65-2718-7ae1-3e923df0f118@davidnewall.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <9af6f858-1cba-4ac2-ba11-3acc8d5915fan@googlegroups.com>
Subject: Re: sprintf
From: luser.droog@gmail.com (luser droog)
Injection-Date: Thu, 23 Mar 2023 15:54:50 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 2895
 by: luser droog - Thu, 23 Mar 2023 15:54 UTC

On Wednesday, March 15, 2023 at 2:46:26 AM UTC-5, David Newall wrote:
> On 14/3/23 11:05, luser droog wrote:
> > The variable arguments are interpolated in reverse order (ie. the
> > normal top-of-stack down-ward way). I'm not sure how useful it
> > actually is. Should it take an array for the variable arguments and
> > take them left-to-right?
> I love this game! :-)
>
> Here's a slightly different implementation that uses search instead of
> testing each character.

Very cool. That ought to run faster and use less memory.

[snip]
> dup/stringtype eq { pop exit } if
> dup/nametype eq { pop (/) exch dup length string cvs exit } if
> dup/operatortype eq 1 index/integertype eq or 1 index/realtype eq or
> { pop 20 string cvs exit }
> if
> dup/booleantype eq { pop {(true)}{(false)} ifelse exit } if
> dup/marktype eq { pop (MARK) exit } if
> dup/nulltype eq { pop (-) exit } if
> dup/savetype eq { pop (-save-) exit } if
> dup/filetype eq { pop (-file-) exit } if
> dup/fonttype eq { pop (-font-) exit } if
> dup/gstatetype eq { pop (-gstate-) exit } if
> /sprintf_element cvx errordict/typecheck get exec

I'd be tempted to pull these out into a separate dictionary.

/_sprintf_dict <<
/stringtype{ pop exit }
/nametype{ pop (/) exch dup length string cvs exit }
...
/default{ pop (-unsupported-type-) exit }
>> def

{
...
//_sprintf_dict 1 index type 2 copy known not {pop/default} if get exec
...
}

Another bonus is that you could implement `==` quite easily if a `sprintf`
like this were available.

/== { (%) sprintf print } def

Re: sprintf

<0783f0cb-eab9-4d88-b508-6d3d95c89c81n@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=200&group=comp.lang.postscript#200

  copy link   Newsgroups: comp.lang.postscript
X-Received: by 2002:a05:6214:8e8:b0:537:7476:41fb with SMTP id dr8-20020a05621408e800b00537747641fbmr262639qvb.7.1679631360742;
Thu, 23 Mar 2023 21:16:00 -0700 (PDT)
X-Received: by 2002:a54:400a:0:b0:384:27f0:bd0a with SMTP id
x10-20020a54400a000000b0038427f0bd0amr252961oie.9.1679631360300; Thu, 23 Mar
2023 21:16:00 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!1.us.feeder.erje.net!feeder.erje.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.postscript
Date: Thu, 23 Mar 2023 21:16:00 -0700 (PDT)
In-Reply-To: <9af6f858-1cba-4ac2-ba11-3acc8d5915fan@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=24.107.184.18; posting-account=G1KGwgkAAAAyw4z0LxHH0fja6wAbo7Cz
NNTP-Posting-Host: 24.107.184.18
References: <f026e4a6-6939-4089-a740-7d944182c16an@googlegroups.com>
<1556fe1d-3a65-2718-7ae1-3e923df0f118@davidnewall.com> <9af6f858-1cba-4ac2-ba11-3acc8d5915fan@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <0783f0cb-eab9-4d88-b508-6d3d95c89c81n@googlegroups.com>
Subject: Re: sprintf
From: luser.droog@gmail.com (luser droog)
Injection-Date: Fri, 24 Mar 2023 04:16:00 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 3975
 by: luser droog - Fri, 24 Mar 2023 04:16 UTC

On Thursday, March 23, 2023 at 10:54:51 AM UTC-5, luser droog wrote:
> On Wednesday, March 15, 2023 at 2:46:26 AM UTC-5, David Newall wrote:
> > On 14/3/23 11:05, luser droog wrote:
> > > The variable arguments are interpolated in reverse order (ie. the
> > > normal top-of-stack down-ward way). I'm not sure how useful it
> > > actually is. Should it take an array for the variable arguments and
> > > take them left-to-right?
> > I love this game! :-)
> >
> > Here's a slightly different implementation that uses search instead of
> > testing each character.
> Very cool. That ought to run faster and use less memory.
>

A little further inspiration. I first wanted to bust it up into smaller re-usable
functions. This has the disadvantage of creating and discarding more
intermediate arrays, but oh well. Then I re-tooled it to use search instead
of going char by char, but now it uses more intermediate data.

/sprintf2 {
format
join
} def

/format { % ... obj (format%string) format [(f)(o)(r)(m)(a)(t)(obj*)(s)(t)(r)(i)(n)(g)]
[ exch
{
dup (%) eq { pop snag convert } if
} foreach
]
} def

/foreach { % array|string proc foreach - % array[1]|string[1] proc ?
2 dict begin {proc src}{exch def}forall
0 1 /src load length 1 sub ({ % i
//src exch 1 getinterval
//proc exec
}) cvx exec end for
} def

/snag {
counttomark 2 add -1 roll
} def

/join { % [(a){b)(c)] join (abc)
0 1 index {
length add
} forall % src dst-length
1 index 0 get type /stringtype eq {string}{array} ifelse
exch % dst src
0 exch {
3 copy putinterval
length add
} forall
pop
} def

/convert /convert2 cvx def

/convert1 {
256 string cvs
} def

/convert-dict <<
/stringtype { }
/arraytype { ([ ) exch { convert ( ) } forall (]) }
/default { 256 string cvs }
>> def
/convert2 {
//convert-dict 1 index type 2 copy known not { pop /default } if get exec
} def

/sprintf3 {
(%) split
[ exch
{ dup (%) eq { pop snag convert } if } forall
]
join
} def

/split { % (s.t.r.i.n.g) (.) split [(s)(.)(t)(.)(r)(.)(i)(.)(n)(.)(g)]
[ 3 1 roll
{
search not { exit } if % [ ,,, (t.r.i.n.g) (.) (s)
3 1 roll dup 3 1 roll % [ ... (s) (.) (t.r.i.n.g) (.)
} loop
]
} def

/a 25 def
/b /B def

b a (format % %) sprintf
pstack % => (format 25 B)
clear

b a (format % %) sprintf2
pstack % => (format 25 B)
clear

[1 2 3] b a (format % % %) sprintf3
pstack % => (format 25 B [ 1 2 3 ])

Re: sprintf

<7fc5b82d-41c3-4a9c-bf5b-51b4ac10e2b6n@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=201&group=comp.lang.postscript#201

  copy link   Newsgroups: comp.lang.postscript
X-Received: by 2002:a05:620a:28c6:b0:744:1072:e127 with SMTP id l6-20020a05620a28c600b007441072e127mr2596345qkp.0.1679883566675;
Sun, 26 Mar 2023 19:19:26 -0700 (PDT)
X-Received: by 2002:a9d:480b:0:b0:69f:15f4:aa8 with SMTP id
c11-20020a9d480b000000b0069f15f40aa8mr3100225otf.0.1679883566314; Sun, 26 Mar
2023 19:19:26 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.postscript
Date: Sun, 26 Mar 2023 19:19:26 -0700 (PDT)
In-Reply-To: <0783f0cb-eab9-4d88-b508-6d3d95c89c81n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=24.107.184.18; posting-account=G1KGwgkAAAAyw4z0LxHH0fja6wAbo7Cz
NNTP-Posting-Host: 24.107.184.18
References: <f026e4a6-6939-4089-a740-7d944182c16an@googlegroups.com>
<1556fe1d-3a65-2718-7ae1-3e923df0f118@davidnewall.com> <9af6f858-1cba-4ac2-ba11-3acc8d5915fan@googlegroups.com>
<0783f0cb-eab9-4d88-b508-6d3d95c89c81n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <7fc5b82d-41c3-4a9c-bf5b-51b4ac10e2b6n@googlegroups.com>
Subject: Re: sprintf
From: luser.droog@gmail.com (luser droog)
Injection-Date: Mon, 27 Mar 2023 02:19:26 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
 by: luser droog - Mon, 27 Mar 2023 02:19 UTC

On Thursday, March 23, 2023 at 11:16:01 PM UTC-5, luser droog wrote:
> On Thursday, March 23, 2023 at 10:54:51 AM UTC-5, luser droog wrote:
> > On Wednesday, March 15, 2023 at 2:46:26 AM UTC-5, David Newall wrote:
> > > On 14/3/23 11:05, luser droog wrote:
> > > > The variable arguments are interpolated in reverse order (ie. the
> > > > normal top-of-stack down-ward way). I'm not sure how useful it
> > > > actually is. Should it take an array for the variable arguments and
> > > > take them left-to-right?
> > > I love this game! :-)
> > >
> > > Here's a slightly different implementation that uses search instead of
> > > testing each character.
> > Very cool. That ought to run faster and use less memory.
> >
> A little further inspiration. I first wanted to bust it up into smaller re-usable
> functions. This has the disadvantage of creating and discarding more
> intermediate arrays, but oh well. Then I re-tooled it to use search instead
> of going char by char, but now it uses more intermediate data.
>

Got rid of the intermediate arrays at the cost of parsing and constructing
a new procedure body in the new, weird looping proc. This code uses /convert
as defined in my previous message. [I'm posting through GG until I fix my gnus
setup for posting, so I can't quote indented code without losing indentation
or doing extra work to add it back.]

/sprintf4 { % data (format%rem) sprintf4 (format<data>*rem)
[ exch
(%) { % data [ (format) (rem) (%) [
pop snag % [ (format) (rem) (%) data
mark exch % [ (format) (rem) (%) [ data
convert % [ (format) (rem) (%) (<data>*)
} on-matches
join-to-mark
} def

/on-matches { % string seek proc
1 dict begin /proc exch def
({
search not { exit } if
3 1 roll mark % pre post match [
//proc exec % pre post match [ proc*
counttomark % pre post match [ proc* n
dup 2 add -1 roll pop % pre post match proc* n
dup 2 add exch % pre post match proc* n+2 n
roll % pre proc* post match
}) cvx exec end loop
} def

/join-to-mark { % [ <obj1> .. <objN>
counttomark dup 1 add copy % [ <obj1> .. <objN> n <obj1> .. <objN> n
0 exch { % ... <obj1> .. <objn> 0
exch length add
} repeat % [ <obj1> .. <objN> n length
counttomark 1 sub index type /stringtype eq
{string}{array} ifelse % [ <obj1> .. <objN> n dest
exch 0 exch { % [ <obj1> .. <objN> dest pos
counttomark -1 roll % [ <obj2> .. <objN> dest pos <obj1>
3 copy putinterval length add % [ <obj2> .. <objN> dest' pos'
} repeat % [ dest length
pop exch pop
} def

Re: sprintf

<adb0ee58-a5bc-cc0f-1eae-4815a1186474@davidnewall.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=202&group=comp.lang.postscript#202

  copy link   Newsgroups: comp.lang.postscript
Message-ID: <adb0ee58-a5bc-cc0f-1eae-4815a1186474@davidnewall.com>
Date: Mon, 27 Mar 2023 19:05:09 +1100
MIME-Version: 1.0
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.7.1
Subject: Re: sprintf
Content-Language: en-US
Newsgroups: comp.lang.postscript
References: <f026e4a6-6939-4089-a740-7d944182c16an@googlegroups.com>
<1556fe1d-3a65-2718-7ae1-3e923df0f118@davidnewall.com>
<9af6f858-1cba-4ac2-ba11-3acc8d5915fan@googlegroups.com>
From: ghostscript@davidnewall.com (David Newall)
To: luser droog <luser.droog@gmail.com>
In-Reply-To: <9af6f858-1cba-4ac2-ba11-3acc8d5915fan@googlegroups.com>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: news.ausics.net
Organization: Ausics - https://www.ausics.net
Lines: 29
X-Complaints: abuse@ausics.net
Path: i2pn2.org!rocksolid2!news.neodome.net!csiph.com!news.bbs.nz!news.ausics.net!not-for-mail
 by: David Newall - Mon, 27 Mar 2023 08:05 UTC

On 24/3/23 02:54, luser droog wrote:
> I'd be tempted to pull these out into a separate dictionary.
>
> /_sprintf_dict <<
> /stringtype{ pop exit }
> /nametype{ pop (/) exch dup length string cvs exit }
> ...
> /default{ pop (-unsupported-type-) exit }
> >> def
> {
> ...
> //_sprintf_dict 1 index type 2 copy known not {pop/default} if get
exec
> ...
> }

Very smart.

Here's a tiny improvement to split:

/split { % (s.t.r.i.n.g) (.) split [(s)(.)(t)(.)(r)(.)(i)(.)(n)(.)(g)]
[ 3 1 roll
{
search {1 index 4 2 roll} {exit} ifelse
} loop
]
} def

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor