How to create X ANY LENGTH functions for string processing? Trivial (in other languages) "echo-reply" does something unexpected. Documentation, open resources and FAQ https://open-cobol.sourceforge.io/faq/index.html#id362 did not help.
IDENTIFICATION DIVISION.
PROGRAM-ID. stdtest.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
REPOSITORY.
FUNCTION ALL INTRINSIC
FUNCTION REPLY.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 tst BINARY-SHORT UNSIGNED.
01 expected PIC S9(6)V9(4) USAGE COMP.
01 argA PIC S9(6)V9(4) USAGE COMP.
01 argB PIC S9(6)V9(4) USAGE COMP.
PROCEDURE DIVISION.
*> must print whole string instead of first 8 chars
DISPLAY FUNCTION REPLY("The quick brown fox jumps over the lazy dog").
STOP RUN.
END PROGRAM stdtest.
IDENTIFICATION DIVISION.
FUNCTION-ID. REPLY.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
REPOSITORY.
FUNCTION ALL INTRINSIC.
DATA DIVISION.
LINKAGE SECTION.
01 argument PIC X ANY LENGTH.
01 result.
05 argument-pointer USAGE POINTER.
PROCEDURE DIVISION USING BY REFERENCE argument RETURNING result.
*> https://open-cobol.sourceforge.io/faq/index.html#id362
MOVE argument TO result.
END FUNCTION REPLY.
Your sample results in 8 characters as the returning group is (on your machine) 8 bytes long (because of the POINTER
below it). It compiles without errors as you only reference the group item, which is an implied alphanumeric(-group)-item (or national-group-item, if you add USAGE NATIONAL
)
The way to do this with (recent) "standard" COBOL is a variable-length RETURNING
item.
The options provided by the standard for functions are DYNAMIC LENGTH
(ideal version, but not supported with GnuCOBOL yet) or a variable-length group (OCCURS 0 TO ... DEPENDING ON
) that is big enough to hold the data.
The following sample should work (but results in an internal [codegen] error which should be fixed with upcoming 3.1rc1):
IDENTIFICATION DIVISION.
FUNCTION-ID. REPLY.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 arg-len USAGE BINARY-LONG.
LINKAGE SECTION.
01 argument PIC X ANY LENGTH.
01 result.
05 filler PIC X OCCURS 0 to 99999 DEPENDING ON arg-len.
PROCEDURE DIVISION USING BY REFERENCE argument RETURNING result.
MOVE FUNCTION LENGTH (argument) TO arg-len
MOVE argument TO result.
END FUNCTION REPLY.
Note: You find the bug report and a patch for a potential fix in the GnuCOBOL issue tracker as #641.