Skip to content

Commit c1a83e5

Browse files
authored
FUNCTION SUBSTITUTE and FUNCTION SUBSTITUTE-CASE added (#246)
1 parent f7c7168 commit c1a83e5

File tree

4 files changed

+133
-13
lines changed

4 files changed

+133
-13
lines changed

libcobj/app/src/main/java/jp/osscons/opensourcecobol/libcobj/common/CobolIntrinsic.java

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2185,6 +2185,116 @@ public static AbstractCobolField funcStoredCharLength(AbstractCobolField srcfiel
21852185
return currField;
21862186
}
21872187

2188+
public static AbstractCobolField funcSubstitute(
2189+
int offset, int length, int params, AbstractCobolField... fields) {
2190+
int i, j, k;
2191+
int numreps = params / 2;
2192+
AbstractCobolField[] f1 = new AbstractCobolField[numreps];
2193+
AbstractCobolField[] f2 = new AbstractCobolField[numreps];
2194+
CobolDataStorage src = fields[0].getDataStorage();
2195+
CobolDataStorage fData1;
2196+
int srcSize = fields[0].getSize();
2197+
int fSize1;
2198+
StringBuilder rtn = new StringBuilder();
2199+
2200+
for (i = 0; i < params - 1; i++) {
2201+
if ((i % 2) == 0) {
2202+
f1[i / 2] = fields[i + 1];
2203+
} else {
2204+
f2[i / 2] = fields[i + 1];
2205+
}
2206+
}
2207+
2208+
for (i = 0; i < srcSize; ) {
2209+
for (j = 0; j < numreps; j++) {
2210+
fData1 = f1[j].getDataStorage();
2211+
fSize1 = f1[j].getSize();
2212+
for (k = fSize1 - 1; k >= 0; k--) {
2213+
if (i + k >= srcSize || src.getByte(i + k) != fData1.getByte(k)) {
2214+
break;
2215+
}
2216+
}
2217+
if (k < 0) {
2218+
rtn.append(f2[j].getString());
2219+
i += fSize1;
2220+
break;
2221+
}
2222+
}
2223+
if (j == numreps) {
2224+
rtn.append((char) src.getByte(i));
2225+
i++;
2226+
}
2227+
}
2228+
2229+
CobolFieldAttribute attr =
2230+
new CobolFieldAttribute(CobolFieldAttribute.COB_TYPE_ALPHANUMERIC, 0, 0, 0, null);
2231+
AbstractCobolField field =
2232+
CobolFieldFactory.makeCobolField(rtn.length(), (CobolDataStorage) null, attr);
2233+
makeFieldEntry(field);
2234+
currField.setDataStorage(new CobolDataStorage(rtn.toString()));
2235+
2236+
if (offset > 0) {
2237+
calcRefMod(currField, offset, length);
2238+
}
2239+
return currField;
2240+
}
2241+
2242+
public static AbstractCobolField funcSubstituteCase(
2243+
int offset, int length, int params, AbstractCobolField... fields) {
2244+
int i, j, k;
2245+
int numreps = params / 2;
2246+
AbstractCobolField[] f1 = new AbstractCobolField[numreps];
2247+
AbstractCobolField[] f2 = new AbstractCobolField[numreps];
2248+
CobolDataStorage src = fields[0].getDataStorage();
2249+
CobolDataStorage fData1;
2250+
int srcSize = fields[0].getSize();
2251+
int fSize1;
2252+
StringBuilder rtn = new StringBuilder();
2253+
2254+
for (i = 0; i < params - 1; i++) {
2255+
if (i % 2 == 0) {
2256+
f1[i / 2] = fields[i + 1];
2257+
} else {
2258+
f2[i / 2] = fields[i + 1];
2259+
}
2260+
}
2261+
2262+
for (i = 0; i < srcSize; ) {
2263+
for (j = 0; j < numreps; j++) {
2264+
fData1 = f1[j].getDataStorage();
2265+
fSize1 = f1[j].getSize();
2266+
for (k = fSize1 - 1; k >= 0; k--) {
2267+
if (i + k >= srcSize
2268+
|| Character.toLowerCase((char) fData1.getByte(k))
2269+
!= Character.toLowerCase((char) src.getByte(i + k))) {
2270+
break;
2271+
}
2272+
}
2273+
if (k < 0) {
2274+
rtn.append(f2[j].getString());
2275+
i += fSize1;
2276+
break;
2277+
}
2278+
}
2279+
if (j == numreps) {
2280+
rtn.append((char) src.getByte(i));
2281+
i++;
2282+
}
2283+
}
2284+
2285+
CobolFieldAttribute attr =
2286+
new CobolFieldAttribute(CobolFieldAttribute.COB_TYPE_ALPHANUMERIC, 0, 0, 0, null);
2287+
AbstractCobolField field =
2288+
CobolFieldFactory.makeCobolField(rtn.length(), (CobolDataStorage) null, attr);
2289+
makeFieldEntry(field);
2290+
2291+
currField.setDataStorage(new CobolDataStorage(rtn.toString()));
2292+
if (offset > 0) {
2293+
calcRefMod(currField, offset, length);
2294+
}
2295+
return currField;
2296+
}
2297+
21882298
/** Equivalent to cob_intr_trim */
21892299
public static AbstractCobolField funcTrim(
21902300
int offset, int length, AbstractCobolField srcField, int direction) {

libcobj/app/src/main/java/jp/osscons/opensourcecobol/libcobj/data/CobolDataStorage.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,6 @@ public byte[] getByteArray(int index, int length) {
158158
return result;
159159
}
160160

161-
public byte[] getByteArray() {
162-
int length = this.data.length;
163-
byte[] result = new byte[length];
164-
System.arraycopy(this.data, this.index, result, 0, length);
165-
return result;
166-
}
167-
168161
/**
169162
* C言語のmemcpy
170163
*

tests/run.src/functions.at

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1625,7 +1625,6 @@ AT_CHECK([java prog], [0],
16251625
AT_CLEANUP
16261626

16271627
AT_SETUP([FUNCTION SUBSTITUTE])
1628-
AT_CHECK([${SKIP_TEST}])
16291628

16301629
AT_DATA([prog.cob], [
16311630
IDENTIFICATION DIVISION.
@@ -1637,18 +1636,27 @@ AT_DATA([prog.cob], [
16371636
MOVE "abc111444555defxxabc" TO Y.
16381637
DISPLAY FUNCTION SUBSTITUTE ( Y "abc" "zz" "55" "666" )
16391638
END-DISPLAY.
1639+
MOVE "abc111444555defxxabc" TO Y.
1640+
DISPLAY FUNCTION SUBSTITUTE
1641+
( Y "abc" "55" "55" "666" )
1642+
END-DISPLAY.
1643+
MOVE "abc111444555defxxabc" TO Y.
1644+
DISPLAY FUNCTION SUBSTITUTE
1645+
( Y "abc1" "666" "abc" "zz" )
1646+
END-DISPLAY.
16401647
STOP RUN.
16411648
])
16421649

16431650
AT_CHECK([${COMPILE} prog.cob])
16441651
AT_CHECK([java prog], [0],
16451652
[zz1114446665defxxzz
1653+
551114446665defxx55
1654+
66611444555defxxzz
16461655
])
16471656

16481657
AT_CLEANUP
16491658

16501659
AT_SETUP([FUNCTION SUBSTITUTE with reference modding])
1651-
AT_CHECK([${SKIP_TEST}])
16521660

16531661
AT_DATA([prog.cob], [
16541662
IDENTIFICATION DIVISION.
@@ -1672,7 +1680,6 @@ AT_CHECK([java prog], [0],
16721680
AT_CLEANUP
16731681

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

16771684
AT_DATA([prog.cob], [
16781685
IDENTIFICATION DIVISION.
@@ -1684,18 +1691,28 @@ AT_DATA([prog.cob], [
16841691
MOVE "ABC111444555defxxabc" TO Y.
16851692
DISPLAY FUNCTION SUBSTITUTE-CASE (Y "abc" "zz" "55" "666")
16861693
END-DISPLAY.
1694+
MOVE "abc111444555defxxABC" TO Y.
1695+
DISPLAY FUNCTION SUBSTITUTE-CASE
1696+
( Y "abc" "55" "55" "666" )
1697+
END-DISPLAY.
1698+
1699+
MOVE "abc111444555defxxABC" TO Y.
1700+
DISPLAY FUNCTION SUBSTITUTE-CASE
1701+
( Y "abc1" "666" "abc" "zz" )
1702+
END-DISPLAY.
16871703
STOP RUN.
16881704
])
16891705

16901706
AT_CHECK([${COMPILE} prog.cob])
16911707
AT_CHECK([java prog], [0],
16921708
[zz1114446665defxxzz
1709+
551114446665defxx55
1710+
66611444555defxxzz
16931711
])
16941712

16951713
AT_CLEANUP
16961714

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

17001717
AT_DATA([prog.cob], [
17011718
IDENTIFICATION DIVISION.
@@ -1704,7 +1721,7 @@ AT_DATA([prog.cob], [
17041721
WORKING-STORAGE SECTION.
17051722
01 Y PIC X(20).
17061723
PROCEDURE DIVISION.
1707-
MOVE "abc111444555defxxabc" TO Y.
1724+
MOVE "abc111444555defxxABC" TO Y.
17081725
DISPLAY FUNCTION SUBSTITUTE-CASE
17091726
( Y "ABC" "zz" "55" "666" ) (2 : 9)
17101727
END-DISPLAY.

texi/open-cobol.info

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
This is open-cobol.info, produced by makeinfo version 6.7 from
1+
This is open-cobol.info, produced by makeinfo version 6.8 from
22
open-cobol.texi.
33

44
INFO-DIR-SECTION COBOL

0 commit comments

Comments
 (0)