CHANGESTR.REX: Difference between revisions

From Rosetta Code
Content added Content deleted
m (updated some comments in this version of the BIF. -- ~~~~)
m (moved a category to TOF.)
 
(11 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[Category:REXX_library_routines]]

== the CHANGESTR function ==
This a RYO version of the REXX function &nbsp; '''changestr''' &nbsp; (<u>change</u> <u>str</u>ing).

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 &nbsp; BIF &nbsp; (<u>B</u>uilt-<u>I</u>n-<u>F</u>unction).



This version of the &nbsp; '''changestr''' &nbsp; BIF has more functionality than the standard BIF.
This version of the &nbsp; '''changestr''' &nbsp; BIF has more functionality than the standard BIF.


<lang rexx>/*REXX program emulates the CHANGESTR built-in function for older REXXes*/
/*──── This version has more functionality: limit the number of changes.*/
/*──── start of change occurrence#.*/
/*──── start of change position. */


__TOC__
/*╔══════════════════════════ CHANGESTR function ══════════════════════╗

╔═╩════════════════════════════════════════════════════════════════════╩═╗

║ The CHANGESTR function is used to replace some or all occurrences of an║

║ (old) string in a haystack with a new string. The changed string is ║
===the CHANGESTR (external) program source===
║ returned. If the haystack doesn't contain the old string, the ║
The following &nbsp; CHANGESTR &nbsp; program can be coded as is when the intention is to be an external routine (function).
║ original haystack is returned. If the old string is a null string, ║
<lang rexx>/*REXX program emulates the CHANGESTR BIF (built-in function) for older REXXes.*/
║ then the original string is prefixed with the new string. ║
/*──────────── This version has more functionality: limit the number of changes. */
║ ║
/*──────────── start of change occurrence#. */
║ new string to be used►──────────┐ ┌─────◄limit of # changes (times).║
/*──────────── start of change position. */
║ original string (haystack)►──────┐ │ │ [default: ≈ one billion]║

║ old string to be replaced►──┐ │ │ │ ┌────◄begin at this occurrence #║
/* ╔═══════════════════════════ CHANGESTR function ════════════════════════╗
║ {O, H, and N can be null.} │ │ │ │ │ ┌──◄start position (default=1)║
╔═╩═══════════════════════════════════════════════════════════════════════╩═╗
╚═╦════════════════════════════╗ │ │ │ │ │ │ ╔═════════════════════════╦═╝
║ The CHANGESTR function is used to replace some or all occurrences of an ║
╚════════════════════════════╝ │ │ │ │ │ │ ╚═════════════════════════╝
║ (old) string in a haystack with a new string. The changed string is ║
↓ ↓ ↓ ↓ ↓ ↓ */
changestr: parse arg o,h,n,t,b,p /* T, B, & P are optional.*/
returned. If the haystack doesn't contain the old string, the
║ original haystack is returned. If the old string is a null string, ║
$='' /*$: the returned string.*/
/*optional arguments ··· */
then the original string is prefixed with the new string.
t=word(t 999999999 , 1) /*maybe use the default? */
║ new string to be used ►──────┐ ┌──────◄ limit of # changes (times).║
b=word(b 1 , 1) /* " " " " */
║ original string (haystack) ►────┐ │ │ [default: ≈ one billion] ║
p=word(p 1 , 1) /* " " " " */
║ old string to be replaced ►──┐ │ │ │ ┌────◄ begin at this occurrence # ║
if arg() < 3 then signal syntax /*not enough arguments. */
║ {O, H, and N can be null.} │ │ │ │ │ ┌──◄ start position (default=1) ║
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*/
parse arg o,h,n,t,b,p, ,$ f /* ◄────────────────────┐*/
if b<1 then signal syntax /*5th arg not positive. */
/*T, B, P are optional ►─┘*/
if p<1 then signal syntax /*6th " " " */
t=word(t 999999999, 1) /*maybe use the default? */
L=length(o) /*length of OLD string. */
b=word(b 1 , 1) /* " " " " */
if L==0 & t\=0 then return n || h /*changing a null char? */
p=word(p 1 , 1) /* " " " " */
f='' /*first part of H if P>1. */
if arg() < 3 then signal syntax /*not enough arguments. */
if p\=1 then do /*if P ¬= 1, adjust F & H.*/
if arg() > 6 then signal syntax /*too many arguments. */
f=left(h, min(p-1, length(h))) /*keep first part intact. */
if \datatype(t, 'W') then signal syntax /*4th arg not an integer. */
h=substr(h,p) /*only use this part of H.*/
if \datatype(b, 'W') then signal syntax /*5th " " " " */
end /*now, proceed as usual. */
if \datatype(p, 'W') then signal syntax /*6th " " " " */
#=0 /*# of changed occurrences*/
if t<0 then signal syntax /*4th " " non-negative.*/
do j=1 while # < t /*keep changing, T times. */
if b<1 then signal syntax /*5th " " positive. */
parse var h y (o) _ +(L) h /*parse the haystack ··· */
if p<1 then signal syntax /*6th " " " */
if _=='' then return f || $ || y /*no more left, return. */
L=length(o) /*length of OLD string. */
$=$ || y /*append the residual txt.*/
if L==0 & t\=0 then return n || h /*changing a [null] char? */
if j<b then $=$ || o /*append OLD if too soon. */
/* [↓] check for position*/
else do /*met the occurrence test.*/
if p\=1 then do /*P¬=1? Then ajust F & H.*/
$=$ || n /*append the NEW string.*/
f=left(h, min(p-1, length(h))) /*keep first part intact. */
#=#+1 /*bump occurrence number.*/
h=substr(h, p) /*only use this part of H.*/
end
end /*now, proceed as usual. */
end /*j*/ /*Note: most REXX ··· */
#=0 /*# of changed occurrences*/
/* CHANGESTR BIFs only ···*/
do j=1 while # < t /*keep changing, T times. */
return f || $ || h /* support three options. */</lang>
parse var h y (o) _ +(L) h /*parse the haystack ··· */
if _=='' then return f || $ || y /*no more left, return. */
$=$ || y /*append the residual txt.*/
/* [↓] check if too soon.*/
if j<b then $=$ || o /*append OLD if too soon. */
else do /*met the occurrence test.*/
$=$ || n /*append the NEW string.*/
#=#+1 /*bump occurrence number. */
end /* [↑] append new string.*/
end /*j*/ /*Note: most REXX ··· */
/* CHANGESTR BIFs only ···*/
return f || $ || h /* support three options. */</lang>

=== the CHANGESTR (internal) procedure source ===
The following CHANGESTR program can be coded as is when the intention is to be an internal routine (function).

Only the '''changestr:''' statement is changed &nbsp; (by adding a &nbsp; '''procedure''' &nbsp; to the label).
<lang rexx>/*REXX program emulates the CHANGESTR BIF (built-in function) for older REXXes.*/
/*──────────── This version has more functionality: limit the number of changes. */
/*──────────── start of change occurrence#. */
/*──────────── start of change position. */

/* ╔═══════════════════════════ CHANGESTR function ════════════════════════╗
╔═╩═══════════════════════════════════════════════════════════════════════╩═╗
║ The CHANGESTR function is used to replace some or all occurrences of an ║
║ (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 used ►──────┐ ┌──────◄ limit of # changes (times).║
║ original string (haystack) ►────┐ │ │ [default: ≈ one billion] ║
║ old string to be replaced ►──┐ │ │ │ ┌────◄ begin at this occurrence # ║
║ {O, H, and N can be null.} │ │ │ │ │ ┌──◄ start position (default=1) ║
╚═╦═════════════════════════════╗ │ │ │ │ │ │ ╔═══════════════════════════╦═╝
╚═════════════════════════════╝ │ │ │ │ │ │ ╚═══════════════════════════╝
↓ ↓ ↓ ↓ ↓ ↓ */
changestr: procedure; parse arg o,h,n,t,b,p, ,$ f /* ◄────────────────────┐*/
/*T, B, P are optional ►─┘*/
t=word(t 999999999, 1) /*maybe use the default? */
b=word(b 1 , 1) /* " " " " */
p=word(p 1 , 1) /* " " " " */
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 /*6th " " " " */
if t<0 then signal syntax /*4th " " non-negative.*/
if b<1 then signal syntax /*5th " " 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? */
/* [↓] check for position*/
if p\=1 then do /*P¬=1? Then ajust 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 occurrences*/
do j=1 while # < t /*keep changing, T times. */
parse var h y (o) _ +(L) h /*parse the haystack ··· */
if _=='' then return f || $ || y /*no more left, return. */
$=$ || y /*append the residual txt.*/
/* [↓] check if too soon.*/
if j<b then $=$ || o /*append OLD if too soon. */
else do /*met the occurrence test.*/
$=$ || n /*append the NEW string.*/
#=#+1 /*bump occurrence number. */
end /* [↑] append new string.*/
end /*j*/ /*Note: most REXX ··· */
/* CHANGESTR BIFs only ···*/
return f || $ || h /* support three options. */</lang>

Latest revision as of 20:23, 15 October 2018


the CHANGESTR function

This a RYO version of the REXX function   changestr   (change string).

It is included in some of the more modern Classic REXXes, but older versions of
Classic REXX don't have this function as a   BIF   (Built-In-Function).


This version of the   changestr   BIF has more functionality than the standard BIF.



the CHANGESTR (external) program source

The following   CHANGESTR   program can be coded as is when the intention is to be an external routine (function). <lang rexx>/*REXX program emulates the CHANGESTR BIF (built-in function) for older REXXes.*/ /*──────────── This version has more functionality: limit the number of changes. */ /*──────────── start of change occurrence#. */ /*──────────── start of change position. */

 /* ╔═══════════════════════════ CHANGESTR function ════════════════════════╗
  ╔═╩═══════════════════════════════════════════════════════════════════════╩═╗
  ║ The  CHANGESTR  function is used to replace some or all occurrences of an ║
  ║ (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 used ►──────┐ ┌──────◄ limit of # changes (times).║
  ║   original string (haystack) ►────┐ │ │         [default:  ≈ one billion] ║
  ║    old string to be replaced ►──┐ │ │ │ ┌────◄ begin at this occurrence # ║
  ║ {O, H, and  N  can be null.}    │ │ │ │ │ ┌──◄ start position (default=1) ║
  ╚═╦═════════════════════════════╗ │ │ │ │ │ │ ╔═══════════════════════════╦═╝
    ╚═════════════════════════════╝ │ │ │ │ │ │ ╚═══════════════════════════╝
                                    ↓ ↓ ↓ ↓ ↓ ↓                                   */
                        parse arg   o,h,n,t,b,p,  ,$ f  /*  ◄────────────────────┐*/
                                                        /*T, B, P are optional ►─┘*/
          t=word(t 999999999, 1)                        /*maybe use the default?  */
          b=word(b 1        , 1)                        /*  "    "   "     "      */
          p=word(p 1        , 1)                        /*  "    "   "     "      */
          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   /*6th  "   "   "    "     */
          if t<0                   then signal syntax   /*4th  "   " non-negative.*/
          if b<1                   then signal syntax   /*5th  "   "   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? */
                                                        /* [↓]  check for position*/
          if p\=1  then do                              /*P¬=1?  Then ajust 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 occurrences*/
                   do j=1   while  # < t                /*keep changing, T times. */
                   parse var  h  y  (o)  _  +(L)  h     /*parse the haystack ···  */
                   if _==  then return f || $ || y    /*no more left,  return.  */
                   $=$ || y                             /*append the residual txt.*/
                                                        /* [↓]  check if too soon.*/
                   if j<b    then $=$ || o              /*append OLD if too soon. */
                             else do                    /*met the occurrence test.*/
                                  $=$ || n              /*append the  NEW  string.*/
                                  #=#+1                 /*bump occurrence number. */
                                  end                   /* [↑]  append new string.*/
                   end   /*j*/                          /*Note:  most REXX  ···   */
                                                        /* CHANGESTR BIFs only ···*/
          return  f || $ || h                           /* support three options. */</lang>

the CHANGESTR (internal) procedure source

The following CHANGESTR program can be coded as is when the intention is to be an internal routine (function).

Only the changestr: statement is changed   (by adding a   procedure   to the label). <lang rexx>/*REXX program emulates the CHANGESTR BIF (built-in function) for older REXXes.*/ /*──────────── This version has more functionality: limit the number of changes. */ /*──────────── start of change occurrence#. */ /*──────────── start of change position. */

 /* ╔═══════════════════════════ CHANGESTR function ════════════════════════╗
  ╔═╩═══════════════════════════════════════════════════════════════════════╩═╗
  ║ The  CHANGESTR  function is used to replace some or all occurrences of an ║
  ║ (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 used ►──────┐ ┌──────◄ limit of # changes (times).║
  ║   original string (haystack) ►────┐ │ │         [default:  ≈ one billion] ║
  ║    old string to be replaced ►──┐ │ │ │ ┌────◄ begin at this occurrence # ║
  ║ {O, H, and  N  can be null.}    │ │ │ │ │ ┌──◄ start position (default=1) ║
  ╚═╦═════════════════════════════╗ │ │ │ │ │ │ ╔═══════════════════════════╦═╝
    ╚═════════════════════════════╝ │ │ │ │ │ │ ╚═══════════════════════════╝
                                    ↓ ↓ ↓ ↓ ↓ ↓                                   */

changestr: procedure; parse arg o,h,n,t,b,p, ,$ f /* ◄────────────────────┐*/

                                                        /*T, B, P are optional ►─┘*/
          t=word(t 999999999, 1)                        /*maybe use the default?  */
          b=word(b 1        , 1)                        /*  "    "   "     "      */
          p=word(p 1        , 1)                        /*  "    "   "     "      */
          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   /*6th  "   "   "    "     */
          if t<0                   then signal syntax   /*4th  "   " non-negative.*/
          if b<1                   then signal syntax   /*5th  "   "   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? */
                                                        /* [↓]  check for position*/
          if p\=1  then do                              /*P¬=1?  Then ajust 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 occurrences*/
                   do j=1   while  # < t                /*keep changing, T times. */
                   parse var  h  y  (o)  _  +(L)  h     /*parse the haystack ···  */
                   if _==  then return f || $ || y    /*no more left,  return.  */
                   $=$ || y                             /*append the residual txt.*/
                                                        /* [↓]  check if too soon.*/
                   if j<b    then $=$ || o              /*append OLD if too soon. */
                             else do                    /*met the occurrence test.*/
                                  $=$ || n              /*append the  NEW  string.*/
                                  #=#+1                 /*bump occurrence number. */
                                  end                   /* [↑]  append new string.*/
                   end   /*j*/                          /*Note:  most REXX  ···   */
                                                        /* CHANGESTR BIFs only ···*/
          return  f || $ || h                           /* support three options. */</lang>