Anonymous user
CHANGESTR.REX: Difference between revisions
m
→{{header|REXX}}: added/changed comments and whitespace, changed indentations.
(added section headers.) |
m (→{{header|REXX}}: added/changed comments and whitespace, changed indentations.) |
||
Line 1:
==the CHANGESTR function==
This a RYO version of the REXX function '''changestr''' (
It is included in some of the more modern Classic REXXes, but older versions of
<br>Classic REXX don't have this function as a BIF (
Line 10:
==the CHANGESTR source==
<lang rexx>/*REXX program emulates the CHANGESTR BIF (built-in function) for older REXXes.*/
/*
/*
/*
/* ╔═══════════════════════════ CHANGESTR function ════════════════════════╗
╔═╩═══════════════════════════════════════════════════════════════════════╩═╗
║ The CHANGESTR function is used to replace some or all occurrences of
║ (old) string in a haystack with a new string. The changed string is ║
║ returned. If the haystack doesn't contain the old string, the ║
║ original haystack is returned. If the old string is a null string, ║
║ then the original string is prefixed with the new string. ║
║ ║
║ new string to be
║ original string (haystack)
║ old string to be
║ {O, H, and N can be null.} │ │ │ │ │
╚═╦═════════════════════════════╗ │ │ │ │ │ │ ╔═══════════════════════════╦═╝
╚═════════════════════════════╝ │ │ │ │ │ │ ╚═══════════════════════════╝
↓ ↓ ↓ ↓ ↓ ↓ */
changestr: parse arg o,h,n,t,b,p,
t=word(t
b=word(b
p=word(p
if arg() < 3 then signal syntax /*not enough arguments. */
if arg() > 6 then signal syntax /*too many arguments. */
if \datatype(t, 'W')
if \datatype(b, 'W')
if \datatype(p, 'W')
if t<0 then signal syntax /*4th arg not non-negative*/
if b<1 then signal syntax /*5th arg not positive. */
if p<1 then signal syntax /*6th " " " */
L=length(o) /*length of OLD string. */
if L==0 & t\=0 then return n || h /*changing a null char? */
if p\=1 then do /*if P ¬= 1, adjust F & H.*/
f=left(h, min(p-1, length(h))) /*keep first part intact. */
h=substr(h, p)
end /*now, proceed as usual. */
#=0 /*# of changed occurrences*/
do j=1 while # < t /*keep changing, T times. */
parse var h y (o) _ +(L)
if _=='' then return f || $ || y
$=$ || y /*append the residual txt.*/
if j<b then $=$ || o
else do
$=$ || n
#=#+1
end
end /*j*/ /*Note: most REXX ··· */
/* CHANGESTR BIFs only ···*/
return f || $ || h /* support three options. */</lang>
|