MEMCOLLAPSE

MEMCOLLAPSE fills a target buffer with the contents of a source buffer with all multiple occurrences of a specified character replaced by one, while the leading and trailing instances of that character are also trimmed. It returns a size_t value that indicates the number of bytes written to the target buffer.

Read syntax diagramSkip visual syntax diagramMEMCOLLAPSE( p, m, q, n, z,i)
p
Specifies the address of the target buffer.
m
Specifies the length in bytes of the target buffer. It must have a computational type and is converted to type size_t.
q
Specifies the address of the source buffer.
n
Specifies the length in bytes of the source buffer. It must have a computational type and is converted to type size_t. It must be non-negative.
z
An expression that must have the type CHARACTER(1) NONVARYING.
i
An optional expression that must be computational and will be converted to size_t as necessary. If not specified, the default value for i is 1. If i < 1, default value of 1 is used.
The returned value depends on the address of the target buffer or the size of the target buffer:
  • If the address of the target buffer is zero (null), the number of bytes that would be written is returned.
  • If the target buffer is not large enough, a value of -1 is returned.
  • If the target buffer is large enough, the number of bytes that are written to the buffer is returned.
  • The target buffer will include all the characters in the source buffer before the ith character (without any collapsing) and then all characters from the nth position onwards, squeezed and trimmed as appropriate.

Example

dcl s  char(20);
dcl t  char(20);
dcl cx fixed bin(31);
 
s  = '...abc....def...gh..';
cx = memcollapse(sysnull(), 0, addr(s), stg(s), '.');
       /* cx = 10         */
cx = memcollapse(addr(t), stg(t), addr(s), stg(s), '.');
       /* cx = 10         */
       /* t = 'abc.def.gh' */