Talk:Word wrap: Difference between revisions

→‎REXX Timings: please take Regina measurements or let's leave it at that.
(→‎REXX Timings: add PL/I timing)
(→‎REXX Timings: please take Regina measurements or let's leave it at that.)
Line 66:
 
:: Sorry, I forgot to mention that I adjusted all 3 versions so that they read the file (1 line) and create an output file (lineout instead of say). I could never display a million bytes on the screen in 10, 20 or what seconds :-) 200 paragraphs having 5000 characters each would be the same load. And I wanted to compare the algorithms' speed in some measurable way. --[[User:Walterpachl|Walterpachl]] ([[User talk:Walterpachl|talk]]) 18:02, 21 August 2013 (UTC)
 
::: ooRexx is the only (somewhat classic) Rexx I have! Can you please measure the timings with Regina? Here are the programs that should work on Regina as well as on ooRexx:
<lang>
/* REXX */
oid='long.txt'; 'erase' oid
s='A'
l=0
c='abcdefghijklmnopqrstuvwxyz'
c=c||translate(c)'1234567890.,-;:_!"§$%&/()=?`'||c
Say length(c)
do i=1 To 100
c.i=substr(c,i,1)
End
cnt.=0
Do Until l>=1000000
r=random(1,90)
s=s copies(c.r,r)
l=l+r+1
cnt.r=cnt.r+1
End
Say l
Call lineout oid,s
do r=1 To 90
Say right(r,3) right(cnt.r,5)
End
 
 
/*REXX pgm reads a file and displays it (with word wrap to the screen). */
Call time 'R'
parse arg iFID width /*get optional arguments from CL.*/
oid=fn(ifid)'0.'width
a='' /*nullify the text (so far). */
do j=0 while lines(iFID)\==0 /*read from the file until E-O-F.*/
a=a linein(iFID) /*append the file's text to a */
end /*j*/
d=''
do k=1 for words(a); x=word(a,k) /*parse until text (a) exhausted.*/
_=d x /*append it to the money and see.*/
if length(_)>width then do /*words exceeded the width? */
Call o d /*display what we got so far. */
_=x /*overflow for the next line. */
end
d=_ /*append this word to the output.*/
end /*k*/
if d\=='' then Call o d /*handle any residual words. */
/*stick a fork in it, we're done.*/
Say time('E')
Call lineout ifid
Exit
o: Return lineout(oid,arg(1))
 
 
/*REXX pgm reads a file and displays it (with word wrap to the screen). */
Call time 'R'
parse arg iFID width justify _ . /*get optional CL args.*/
if iFID='' |iFID==',' then iFID ='LAWS.TXT' /*default input file ID*/
if width==''|width==',' then width=linesize() /*Default? Use linesize*/
if width==0 then width=80 /*indeterminable width.*/
if right(width,1)=='%' then do /*handle % of width. */
width=translate(width,,'%') /*remove the %*/
width=linesize() * translate(width,,"%")%100
end
if justify==''|justify==',' then justify='Left' /*Default? Use LEFT */
just=left(justify,1) /*only use first char of JUSTIFY.*/
just=translate(just) /*be able to handle mixed case. */
if pos(just,'BCLR')==0 then call err "JUSTIFY (3rd arg) is illegal:" justify
if _\=='' then call err "too many arguments specified." _
if \datatype(width,'W') then call err "WIDTH (2nd arg) isn't an integer:" width
oid=fn(ifid)'1.'width
a='' /*nullify the text (so far). */
do j=0 while lines(iFID)\==0 /*read from the file until E-O-F.*/
a=a linein(iFID) /*append the file's text to a */
end /*j*/
 
if j==0 then call err 'file' iFID "not found."
if a='' then call err 'file' iFID "is empty."
d=''
do k=1 for words(a); x=word(a,k) /*parse until text (a) exhausted.*/
_=d x /*append it to the money and see.*/
if length(_)>width then call tell /*word(s) exceeded the width? */
d=_ /*the new words are OK so far. */
end /*k*/
 
call tell /*handle any residual words. */
Say 1 time('E')
Call lineout ifid
exit /*stick a fork in it, we're done.*/
/*----------------------------------ERR subroutine----------------------*/
err: say; say '***error!***'; say; say arg(1); say; say; exit 13
/*----------------------------------TELL subroutine---------------------*/
tell: if d=='' then return /*first word may be too long. */
w=max(width,length(d)) /*don't truncate very long words.*/
select
when just=='B' then d=justify(d,w) /*?----both----?*/
when just=='C' then d= center(d,w) /* ?centered? */
when just=='L' then d= strip(d) /*left ?--------*/
when just=='R' then d= right(d,w) /*------? right */
end /*select*/
Call o d /*show and tell, or write--?file?*/
_=x /*handle any word overflow. */
return /*go back and keep truckin'. */
 
 
o: Return lineout(oid,arg(1))
/* REXX ***************************************************************
* 20.08.2013 Walter Pachl "my way"
**********************************************************************/
Call time 'R'
Parse Arg iFid w
oid=fn(ifid)'2.'w
s=linein(ifid)
say length(s)
Call ow s
Say time('E')
Call lineout ifid
Exit
ow:
Parse Arg s
s=s' '
Do While length(s)>w
Do i=w+1 to 1 by -1
If substr(s,i,1)='' Then Leave
End
If i=0 Then
p=pos(' ',s)
Else
p=i
Call o left(s,p)
s=substr(s,p+1)
End
If s>'' Then
Call o s
Return
o:Return lineout(oid,arg(1))
</lang>
 
 
 
 
:: Translated version 2 to PL/I. Since PL/I has a limit of 32767 for character strings I had to cut the input into junks of 20000 bytes and add extra reads. Output is identical to REXX. --[[User:Walterpachl|Walterpachl]] ([[User talk:Walterpachl|talk]]) 19:38, 21 August 2013 (UTC)
2,295

edits