Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2185,6 +2185,116 @@ public static AbstractCobolField funcStoredCharLength(AbstractCobolField srcfiel
return currField;
}

public static AbstractCobolField funcSubstitute(
int offset, int length, int params, AbstractCobolField... fields) {
int i, j, k;
int numreps = params / 2;
AbstractCobolField[] f1 = new AbstractCobolField[numreps];
AbstractCobolField[] f2 = new AbstractCobolField[numreps];
CobolDataStorage src = fields[0].getDataStorage();
CobolDataStorage fData1;
int srcSize = fields[0].getSize();
int fSize1;
StringBuilder rtn = new StringBuilder();

for (i = 0; i < params - 1; i++) {
if ((i % 2) == 0) {
f1[i / 2] = fields[i + 1];
} else {
f2[i / 2] = fields[i + 1];
}
}

for (i = 0; i < srcSize; ) {
for (j = 0; j < numreps; j++) {
fData1 = f1[j].getDataStorage();
fSize1 = f1[j].getSize();
for (k = fSize1 - 1; k >= 0; k--) {
if (i + k >= srcSize || src.getByte(i + k) != fData1.getByte(k)) {
break;
}
}
if (k < 0) {
rtn.append(f2[j].getString());
i += fSize1;
break;
}
}
if (j == numreps) {
rtn.append((char) src.getByte(i));
i++;
}
}

CobolFieldAttribute attr =
new CobolFieldAttribute(CobolFieldAttribute.COB_TYPE_ALPHANUMERIC, 0, 0, 0, null);
AbstractCobolField field =
CobolFieldFactory.makeCobolField(rtn.length(), (CobolDataStorage) null, attr);
makeFieldEntry(field);
currField.setDataStorage(new CobolDataStorage(rtn.toString()));

if (offset > 0) {
calcRefMod(currField, offset, length);
}
return currField;
}

public static AbstractCobolField funcSubstituteCase(
int offset, int length, int params, AbstractCobolField... fields) {
int i, j, k;
int numreps = params / 2;
AbstractCobolField[] f1 = new AbstractCobolField[numreps];
AbstractCobolField[] f2 = new AbstractCobolField[numreps];
CobolDataStorage src = fields[0].getDataStorage();
CobolDataStorage fData1;
int srcSize = fields[0].getSize();
int fSize1;
StringBuilder rtn = new StringBuilder();

for (i = 0; i < params - 1; i++) {
if (i % 2 == 0) {
f1[i / 2] = fields[i + 1];
} else {
f2[i / 2] = fields[i + 1];
}
}

for (i = 0; i < srcSize; ) {
for (j = 0; j < numreps; j++) {
fData1 = f1[j].getDataStorage();
fSize1 = f1[j].getSize();
for (k = fSize1 - 1; k >= 0; k--) {
if (i + k >= srcSize
|| Character.toLowerCase((char) fData1.getByte(k))
!= Character.toLowerCase((char) src.getByte(i + k))) {
break;
}
}
if (k < 0) {
rtn.append(f2[j].getString());
i += fSize1;
break;
}
}
if (j == numreps) {
rtn.append((char) src.getByte(i));
i++;
}
}

CobolFieldAttribute attr =
new CobolFieldAttribute(CobolFieldAttribute.COB_TYPE_ALPHANUMERIC, 0, 0, 0, null);
AbstractCobolField field =
CobolFieldFactory.makeCobolField(rtn.length(), (CobolDataStorage) null, attr);
makeFieldEntry(field);

currField.setDataStorage(new CobolDataStorage(rtn.toString()));
if (offset > 0) {
calcRefMod(currField, offset, length);
}
return currField;
}

/** Equivalent to cob_intr_trim */
public static AbstractCobolField funcTrim(
int offset, int length, AbstractCobolField srcField, int direction) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,6 @@ public byte[] getByteArray(int index, int length) {
return result;
}

public byte[] getByteArray() {
int length = this.data.length;
byte[] result = new byte[length];
System.arraycopy(this.data, this.index, result, 0, length);
return result;
}

/**
* C言語のmemcpy
*
Expand Down
27 changes: 22 additions & 5 deletions tests/run.src/functions.at
Original file line number Diff line number Diff line change
Expand Up @@ -1625,7 +1625,6 @@ AT_CHECK([java prog], [0],
AT_CLEANUP

AT_SETUP([FUNCTION SUBSTITUTE])
AT_CHECK([${SKIP_TEST}])

AT_DATA([prog.cob], [
IDENTIFICATION DIVISION.
Expand All @@ -1637,18 +1636,27 @@ AT_DATA([prog.cob], [
MOVE "abc111444555defxxabc" TO Y.
DISPLAY FUNCTION SUBSTITUTE ( Y "abc" "zz" "55" "666" )
END-DISPLAY.
MOVE "abc111444555defxxabc" TO Y.
DISPLAY FUNCTION SUBSTITUTE
( Y "abc" "55" "55" "666" )
END-DISPLAY.
MOVE "abc111444555defxxabc" TO Y.
DISPLAY FUNCTION SUBSTITUTE
( Y "abc1" "666" "abc" "zz" )
END-DISPLAY.
STOP RUN.
])

AT_CHECK([${COMPILE} prog.cob])
AT_CHECK([java prog], [0],
[zz1114446665defxxzz
551114446665defxx55
66611444555defxxzz
])

AT_CLEANUP

AT_SETUP([FUNCTION SUBSTITUTE with reference modding])
AT_CHECK([${SKIP_TEST}])

AT_DATA([prog.cob], [
IDENTIFICATION DIVISION.
Expand All @@ -1672,7 +1680,6 @@ AT_CHECK([java prog], [0],
AT_CLEANUP

AT_SETUP([FUNCTION SUBSTITUTE-CASE])
AT_CHECK([${SKIP_TEST}])

AT_DATA([prog.cob], [
IDENTIFICATION DIVISION.
Expand All @@ -1684,18 +1691,28 @@ AT_DATA([prog.cob], [
MOVE "ABC111444555defxxabc" TO Y.
DISPLAY FUNCTION SUBSTITUTE-CASE (Y "abc" "zz" "55" "666")
END-DISPLAY.
MOVE "abc111444555defxxABC" TO Y.
DISPLAY FUNCTION SUBSTITUTE-CASE
( Y "abc" "55" "55" "666" )
END-DISPLAY.

MOVE "abc111444555defxxABC" TO Y.
DISPLAY FUNCTION SUBSTITUTE-CASE
( Y "abc1" "666" "abc" "zz" )
END-DISPLAY.
STOP RUN.
])

AT_CHECK([${COMPILE} prog.cob])
AT_CHECK([java prog], [0],
[zz1114446665defxxzz
551114446665defxx55
66611444555defxxzz
])

AT_CLEANUP

AT_SETUP([FUNCTION SUBSTITUTE-CASE with reference mod])
AT_CHECK([${SKIP_TEST}])

AT_DATA([prog.cob], [
IDENTIFICATION DIVISION.
Expand All @@ -1704,7 +1721,7 @@ AT_DATA([prog.cob], [
WORKING-STORAGE SECTION.
01 Y PIC X(20).
PROCEDURE DIVISION.
MOVE "abc111444555defxxabc" TO Y.
MOVE "abc111444555defxxABC" TO Y.
DISPLAY FUNCTION SUBSTITUTE-CASE
( Y "ABC" "zz" "55" "666" ) (2 : 9)
END-DISPLAY.
Expand Down
2 changes: 1 addition & 1 deletion texi/open-cobol.info
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
This is open-cobol.info, produced by makeinfo version 6.7 from
This is open-cobol.info, produced by makeinfo version 6.8 from
open-cobol.texi.

INFO-DIR-SECTION COBOL
Expand Down