CHANGESTR.REX: Difference between revisions

added functionality to this version of the CHANGESTR function, changed comments, indentation. -- ~~~~
m (another one :-))
(added functionality to this version of the CHANGESTR function, changed comments, indentation. -- ~~~~)
Line 1:
<lang rexx>/*REXX program emulates the CHANGESTR built-in function for older REXXes*/
<lang rexx>/*╔══════════════════════════════╗ CHANGESTR ╔═════════════════════════╗
/*──── This version has more functionality: limit the number of changes.*/
╔═╩══════════════════════════════╝ function ╚═════════════════════════╩═╗
b=word(b/*──── 1 , 1) /* " " " "start of change occurance. */
║ new string to be used──────────┐ ┌─────limit of # changes (times)║
$=''/*──── start of change position. /*$: the returned string.*/
║ original string (haystack)────────┐ │ │ [default: ≈ one billion]║
 
║ old string to be changed───┐ │ │ │ ┌───begin at this occurrence#.║
/*╔══════════════════════════ CHANGESTR function ══════════════════════╗
║ {O, H, and N can be null.} │ │ │ │ │ [default: 1st occurrence]║
╔═╩════════════════════════════════════════════════════════════════════╩═╗
╚═╦════════════════════════════╗ │ │ │ │ │ ╔═══════════════════════╦═╝
║ The CHANGESTR function is used to replace some or all occurances of an ║
╚════════════════════════════╝ ↓ ↓ ↓ ↓ ↓ ╚═══════════════════════╝*/
║ (old) string in a haystack with a new string. The changed string is ║
changestr: procedure; parse arg o,h,n,t,b /* T and B are optional.*/
║ returned. If the haystack doesn't contain the old string, the ║
$='' /*$: the returned string.*/
║ original haystack is returned. If the old string is a null string, ║
t=word(t 999999999 , 1) /*maybe use the default? */
║ then the original string is prefixed with the new string. ║
b=word(b 1 , 1) /* " " " " */
w=length(o) /*length of OLD string.*/
new string to be used──────────┐used►──────────┐ ┌─────limit┌─────◄limit of # changes (times).
if w==0 & t\=0 then return n || h /*changing a null char ? */
original string (haystack)────────┐►──────┐ │ │ [default: ≈ one billionbillian]║
#=0 /*# of changed occurrences*/
old string to be changed───┐replaced►──┐ │ │ │ ┌───begin┌────◄begin at this occurrenceoccurance #.║
do j=1 until # >= t /*keep changing, T times.*/
║ {O, H, and N can be null.} │ │ │ │ │ ┌──◄start position [(default: 1st occurrence]=1)
parse var h y (o) _ +(w) h /*parse the string ... */
╚═╦════════════════════════════╗ │ │ │ │ │ ╔═══════════════════════╦═╝╔═════════════════════════╦═╝
if _=='' then return $ || y /*no more left, return. */
╚════════════════════════════╝ ↓ ╚═══════════════════════╝*/╚═════════════════════════╝
if j<b then $=$ || y || o /*didn't meet begin at ? */
else do ↓ ↓ ↓ ↓ ↓ ↓ */
changestr: parse arg $=$ || y || o,h,n ,t,b,p /*build newT, STRB, from& S.P are optional.*/
$='' #=#+1 /*bump$: the occurrencereturned numberstring.*/
end /*optional arguments ··· */
t=word(t 999999999 , 1) end /*jmaybe use the default? */
b=word(b 1 , 1) /* " " " /*Most REXX" BIFs only ···*/
returnp=word(p $ ||1 h , 1) /* " " " " /* support three options.*/</lang>
if arg() < 3 then signal syntax /*not enough arguments. */
if arg() > 6 then signal syntax /*too many arguments. */
if \datatype(t,'W') then signal syntax /*4th arg not an integer. */
if \datatype(b,'W') then signal syntax /*5th " " " " */
if \datatype(p,'W') then signal syntax /*5th arg " " " */
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 wL==0 & t\=0 then return n || h /*changing a null char ? */
f='' /*first part of H if P>1. */
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) /*only use this part of H.*/
end /*now, proceed as usual. */
#=0 /*# of changed occurrencesoccurances.*/
do j=1 while # do< j=1t until # >= t /*keep changing, T times. */
parse var h y (o) _ +(wL) h /*parse the string ...haystack ··· */
if _=='' then return f || $ || y /*no more left, return. */
t $=word(t$ || y 999999999 , 1) /*maybe useappend the default?residual txt.*/
if j<b then $=$ || yo || o /*didn'tappend meetOLD beginif attoo ?soon. */
else do /*met the occurance test. */
$=$ || n /*append the NEW string.*/
#=#+1 /*bump occurance number.*/
end
end /*j*/
/*Note: Most REXX BIFs··· */
return f || $ || h /* only support 3 options.*/</lang>