Difference between revisions of "Right String"

From MidrangeWiki
Jump to: navigation, search
(Service Procedure)
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
[[Category:Sample Code]]
 
[[Category:Service Procedures]]
 
[[Category:Service Procedures]]
 
== Summary ==
 
== Summary ==
 
The following are the RPG/LE fully free-form definitions and instructions needed for using the {{AN}} service procedure.  This service procedure simply allows the caller to extract the right-most '''''n''''' characters from a supplied string.
 
The following are the RPG/LE fully free-form definitions and instructions needed for using the {{AN}} service procedure.  This service procedure simply allows the caller to extract the right-most '''''n''''' characters from a supplied string.
  
== Justification ==
+
By [[User:DaveLClarkI|Dave Clark]]
Some service procedures (minus their error checking) are so small that one might question creating a service procedure at all.  They might feel that simply employing the stripped-down code directly in their program code is the best way to go (saving the necessity of calling a separate routine—with the additional overhead that such action would require).
 
{| class="wikitable"
 
|+Rule of Thumb
 
|-
 
|Sometimes, having a service procedure is simply a means of making code more self-documenting (i.e., easier for others to understand).
 
|}
 
To wit...  Which would you rather see in an RPG program—this?
 
<pre>
 
if %subst(myLongString:%len(myLongString)-4+1:4) = 'TEMP';
 
// do something
 
endif;
 
</pre>
 
...or, this?
 
<pre>
 
if GenUtl_RightString(myLongString:4) = 'TEMP';
 
// do something
 
endif;
 
</pre>
 
It is the opinion of many that the second form is more readable and is self-documenting.  Hence, this service procedure.
 
  
== Service Procedure ==
+
== Service Prototype ==
 +
Place the following in a separate copybook for inclusion in both the caller and the service program source members.
 
<pre>
 
<pre>
 
**free
 
**free
Line 36: Line 19:
 
   PadChar              char(1)  const    options(*nopass);
 
   PadChar              char(1)  const    options(*nopass);
 
end-pr;
 
end-pr;
 +
</pre>
 +
 +
== Service Procedure ==
 +
Place the following in a service program source member.
 +
<pre>
 +
**free
 +
ctl-opt NoMain AlwNull(*UsrCtl) Debug Option(*SrcStmt:*NoDebugIo)
 +
        DatFmt(*ISO) TimFmt(*ISO);
  
 
//==============================================================================
 
//==============================================================================
Line 73: Line 64:
 
end-proc;
 
end-proc;
 
</pre>
 
</pre>
 +
 +
== Example ==
 +
See the [[Suffix String]] service procedure.

Latest revision as of 17:43, 17 December 2018

Summary

The following are the RPG/LE fully free-form definitions and instructions needed for using the Right String service procedure. This service procedure simply allows the caller to extract the right-most n characters from a supplied string.

By Dave Clark

Service Prototype

Place the following in a separate copybook for inclusion in both the caller and the service program source members.

**free

//==============================================================================
// Return rightmost number of bytes -- optional padding on left.
//==============================================================================
dcl-pr GenUtl_RightString         varchar(256) rtnparm;
  OrigString            varchar(65535) const;
  ByteCount             packed(3:0) const;
  PadChar               char(1)   const     options(*nopass);
end-pr;

Service Procedure

Place the following in a service program source member.

**free
ctl-opt NoMain AlwNull(*UsrCtl) Debug Option(*SrcStmt:*NoDebugIo)
        DatFmt(*ISO) TimFmt(*ISO);

//==============================================================================
// Return rightmost number of bytes -- optional padding on left.
//==============================================================================
dcl-proc GenUtl_RightString       export;
  dcl-pi *n             varchar(256) rtnparm;
    OrigString          varchar(65535) const;
    ByteCount           packed(3:0) const;
    PadChar             char(1)   const     options(*nopass);
  end-pi;

  dcl-s StrLeng         like(ByteCount);
  dcl-s NewString       varchar(256) inz('');
  dcl-s PadString       char(256) inz(*blanks);

                                       // prevent errors
  StrLeng = ByteCount;                      // start with bytes requested
  if %parms < %parmnum(PadChar)             // if no padding and
  and StrLeng > %len(OrigString);           // request greater than string supplied
    StrLeng = %len(OrigString);             // take supplied string length
  endif;

  if StrLeng > *zero;                  // if something to return
    if StrLeng > %len(PadString);           // if more than what can return
      StrLeng = %len(PadString);            // set to max return length
    endif;
    if %parms < %parmnum(PadChar);          // if no padding
      NewString = %subst(OrigString: %len(OrigString) - StrLeng + 1);
    else;                                   // else, with left padding
      NewString = %subst( %xlate(' ': PadChar: PadString) + OrigString
                        : %len(PadString) + %len(OrigString) - StrLeng + 1 );
    endif;
  endif;

  return NewString;                    // return the suffix string
end-proc;

Example

See the Suffix String service procedure.