Skip to content

Commit d3429e2

Browse files
Implement an intrinsic function TRIM (#244)
1 parent 1271841 commit d3429e2

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2184,4 +2184,44 @@ public static AbstractCobolField funcStoredCharLength(AbstractCobolField srcfiel
21842184
currField.setInt(count);
21852185
return currField;
21862186
}
2187+
2188+
/** Equivalent to cob_intr_trim */
2189+
public static AbstractCobolField funcTrim(
2190+
int offset, int length, AbstractCobolField srcField, int direction) {
2191+
makeFieldEntry(srcField);
2192+
int i;
2193+
int srcFieldSize = srcField.getSize();
2194+
CobolDataStorage srcStorage = srcField.getDataStorage();
2195+
for (i = 0; i < srcFieldSize; ++i) {
2196+
if (srcStorage.getByte(i) != ' ') {
2197+
break;
2198+
}
2199+
}
2200+
if (i == srcFieldSize) {
2201+
currField.setSize(1);
2202+
currField.getDataStorage().setByte(0, (byte) ' ');
2203+
return currField;
2204+
}
2205+
int beginIndex = 0;
2206+
if (direction != 2) {
2207+
while (srcStorage.getByte(beginIndex) == ' ') {
2208+
++beginIndex;
2209+
}
2210+
}
2211+
int endIndex = srcFieldSize - 1;
2212+
if (direction != 1) {
2213+
while (srcStorage.getByte(endIndex) == ' ') {
2214+
--endIndex;
2215+
}
2216+
}
2217+
CobolDataStorage currStorage = currField.getDataStorage();
2218+
currField.setSize(endIndex - beginIndex + 1);
2219+
for (i = 0; i <= endIndex - beginIndex; ++i) {
2220+
currStorage.setByte(i, srcStorage.getByte(beginIndex + i));
2221+
}
2222+
if (offset > 0) {
2223+
calcRefMod(currField, offset, length);
2224+
}
2225+
return currField;
2226+
}
21872227
}

tests/run.src/functions.at

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1748,7 +1748,6 @@ AT_CHECK([java prog], [0],
17481748
AT_CLEANUP
17491749

17501750
AT_SETUP([FUNCTION TRIM])
1751-
AT_CHECK([${SKIP_TEST}])
17521751

17531752
AT_DATA([prog.cob], [
17541753
IDENTIFICATION DIVISION.
@@ -1773,7 +1772,6 @@ AT_CHECK([java prog], [0],
17731772
AT_CLEANUP
17741773

17751774
AT_SETUP([FUNCTION TRIM with reference modding])
1776-
AT_CHECK([${SKIP_TEST}])
17771775

17781776
AT_DATA([prog.cob], [
17791777
IDENTIFICATION DIVISION.

0 commit comments

Comments
 (0)