From 6fd9fab3266ade5039777509ae9c3374c99c53b8 Mon Sep 17 00:00:00 2001
From: David Declerck <david.declerck@ocamlpro.com>
Date: Fri, 22 Nov 2024 14:16:25 +0100
Subject: [PATCH] Fix bug 1008

---
 .github/workflows/macos.yml            |  2 +-
 libcob/ChangeLog                       |  6 ++
 libcob/move.c                          | 14 ++++-
 tests/testsuite.src/run_fundamental.at | 78 +++++++++++++++++++++++++-
 4 files changed, 95 insertions(+), 5 deletions(-)

diff --git a/.github/workflows/macos.yml b/.github/workflows/macos.yml
index 28ccbce6b..3e5e2b8e3 100644
--- a/.github/workflows/macos.yml
+++ b/.github/workflows/macos.yml
@@ -27,7 +27,7 @@ jobs:
 
       - name: Install packages
         run: |
-          brew install pkg-config automake libtool help2man texinfo bison berkeley-db@4 json-c
+          brew install automake libtool help2man texinfo bison berkeley-db@4 json-c
           opt="/opt/homebrew/opt"
           echo "$opt/pkg-config/bin" >> $GITHUB_PATH
           echo "LDFLAGS=-L$opt/berkeley-db@4/lib ${LDFLAGS}" >> $GITHUB_ENV
diff --git a/libcob/ChangeLog b/libcob/ChangeLog
index 92f1b255b..cb307a60c 100644
--- a/libcob/ChangeLog
+++ b/libcob/ChangeLog
@@ -1,4 +1,10 @@
 
+2024-11-22  Chuck Haatvedt <chuck.haatvedt+cobol@gmail.com>
+
+	* move.c (optimized_move_display_to_edited): fixed
+	  Bug #1008: regression in move to numeric edited
+	  items with insertion symbols B, 0 and /
+
 2024-10-22  Chuck Haatvedt <chuck.haatvedt+cobol@gmail.com>
 
 	* screenio.c (cob_screen_get_all): fixed Bug #999
diff --git a/libcob/move.c b/libcob/move.c
index 053bbf246..59f2a2685 100644
--- a/libcob/move.c
+++ b/libcob/move.c
@@ -1202,7 +1202,16 @@ optimized_move_display_to_edited (cob_field *f1, cob_field *f2)
 
 			case '0':
 			case '/':
-				*dst = c;
+				if (suppress_zero && prev_float_char) {
+					*dst = *prev_float_char;
+					*prev_float_char = pad;
+					prev_float_char = dst;
+					if (*dst != currency) {
+						sign_position = dst;
+					}
+				} else {
+					*dst = c;
+				}
 				dst++;
 				break;
 
@@ -1211,6 +1220,9 @@ optimized_move_display_to_edited (cob_field *f1, cob_field *f2)
 					*dst = *prev_float_char;
 					*prev_float_char = pad;
 					prev_float_char = dst;
+					if (*dst != currency) {
+						sign_position = dst;
+					}
 				} else if (have_check_protect) {
 					*dst = pad;
 				} else {
diff --git a/tests/testsuite.src/run_fundamental.at b/tests/testsuite.src/run_fundamental.at
index 7d450ef6a..4747d4060 100644
--- a/tests/testsuite.src/run_fundamental.at
+++ b/tests/testsuite.src/run_fundamental.at
@@ -1980,13 +1980,13 @@ AT_DATA([prog.cob], [
 
            MOVE 10                     TO identifier-1
            MOVE identifier-1           TO EX-13
-           IF EX-13 EQUAL "    $//00//10  "
+           IF EX-13 EQUAL "      $00//10  "
                MOVE "PASS"             TO MSG-1
-      D        DISPLAY "    $//00//10   IS WHAT I EXPECTED"
+      D        DISPLAY "      $00//10   IS WHAT I EXPECTED"
            ELSE
                ADD 1                   TO WS-COUNT
                MOVE "FAIL"             TO MSG-1
-               DISPLAY "EX-13.02 EXPECTING ==>'    $//00//10  '  "
+               DISPLAY "EX-13.02 EXPECTING ==>'      $00//10  '  "
                "WHAT I GOT WAS ==>'"
                EX-13 "'<=="
       D        DISPLAY "------------------" MSG-1
@@ -2510,6 +2510,78 @@ AT_CHECK([$COBCRUN_DIRECT ./prog], [0],
 AT_CLEANUP
 
 
+AT_SETUP([MOVE to item with +, -, B, 0, / and ,])
+AT_KEYWORDS([fundamental edited editing])
+
+AT_DATA([prog.cob], [
+       IDENTIFICATION DIVISION.
+       PROGRAM-ID. prog.
+
+       DATA DIVISION.
+       WORKING-STORAGE SECTION.
+       01 W-P4    PIC S9          VALUE +4.
+       01 W-M4    PIC S9          VALUE -4.
+       01 W-M     PIC -B-         VALUE SPACES.
+       01 W-P     PIC +B+         VALUE SPACES.
+
+       01 W-P123  PIC S9(3)       VALUE +123.
+       01 W-M123  PIC S9(3)       VALUE -123.
+       01 W-MM    PIC ---B--9     VALUE SPACES.
+       01 W-PP    PIC +++B++9     VALUE SPACES.
+
+       01 W-X     PIC -//00BB,,-  VALUE SPACES.
+
+       PROCEDURE DIVISION.
+           MOVE W-P4 TO W-M.
+           DISPLAY W-M.
+           MOVE W-M4 TO W-M.
+           DISPLAY W-M.
+           MOVE W-P4 TO W-P.
+           DISPLAY W-P.
+           MOVE W-M4 TO W-P.
+           DISPLAY W-P.
+
+           MOVE W-P123 TO W-MM.
+           DISPLAY W-MM.
+           MOVE W-M123 TO W-MM.
+           DISPLAY W-MM.
+           MOVE W-P123 TO W-PP.
+           DISPLAY W-PP.
+           MOVE W-M123 TO W-PP.
+           DISPLAY W-PP.
+
+           MOVE W-P4 TO W-X.
+           DISPLAY W-X.
+           MOVE W-M4 TO W-X.
+           DISPLAY W-X.
+
+           MOVE W-P123 TO W-X.
+           DISPLAY W-X.
+           MOVE W-M123 TO W-X.
+           DISPLAY W-X.
+
+           STOP RUN.
+])
+
+AT_CHECK([$COMPILE prog.cob], [0], [], [])
+AT_CHECK([$COBCRUN_DIRECT ./prog], [0],
+[  4
+ -4
+ +4
+ -4
+    123
+   -123
+   +123
+   -123
+         4
+        -4
+         3
+        -3
+])
+
+AT_CLEANUP
+
+
 AT_SETUP([MOVE to JUSTIFIED item])
 AT_KEYWORDS([fundamental])