From a433de80e08656cf41afe4b6e3712ec71dfcadc0 Mon Sep 17 00:00:00 2001
From: Jim Jones <jim.jones@uni-muenster.de>
Date: Wed, 18 Nov 2026 08:18:51 +0100
Subject: [PATCH v1] Fix ISSN output truncation when the issue code is nonzero

ISSN output always used the 8-digit short form, which drops the
two-digit issue publication code of an ISSN-13.  Values that differ
only in that code were displayed identically while comparing unequal.
Use the short form only when the code is 00.

Author: Jim Jones <jim.jones@uni-muenster.de>
Reviewed-by: Jonah Shafran <jonahshafran@gmail.com>
---
 contrib/isn/expected/isn.out | 9 +++++----
 contrib/isn/isn.c            | 9 +++++++++
 contrib/isn/sql/isn.sql      | 3 ++-
 3 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/contrib/isn/expected/isn.out b/contrib/isn/expected/isn.out
index c533768d926..fa1759b55d1 100644
--- a/contrib/isn/expected/isn.out
+++ b/contrib/isn/expected/isn.out
@@ -105,10 +105,11 @@ SELECT '9790123456785'::ISMN13,
 (1 row)
 
 SELECT '9771234567003'::ISSN,
-       '12345679'::ISSN;
-   issn    |   issn    
------------+-----------
- 1234-5679 | 1234-5679
+       '12345679'::ISSN,
+       '9771234567898'::ISSN;
+   issn    |   issn    |       issn        
+-----------+-----------+-------------------
+ 1234-5679 | 1234-5679 | 977-1234-567-89-8
 (1 row)
 
 SELECT '9771234567003'::ISSN13,
diff --git a/contrib/isn/isn.c b/contrib/isn/isn.c
index a1d2f26158a..c60b05a3797 100644
--- a/contrib/isn/isn.c
+++ b/contrib/isn/isn.c
@@ -472,6 +472,15 @@ static inline void
 ean2ISSN(char *isn)
 {
 	unsigned	check;
+	char		digits[MAXEAN13LEN + 1];
+
+	/*
+	 * If the issue publication code is non-zero, we shouldn't
+	 * perform any truncation
+	 */
+	dehyphenate(digits, isn);
+	if (strncmp(digits + 10, "00", 2) != 0)
+		return;
 
 	/* the number should come in this format: 977-0000-000-00-0 */
 	/* Strip the first part, crop, and calculate the new check digit */
diff --git a/contrib/isn/sql/isn.sql b/contrib/isn/sql/isn.sql
index 043e5580b0d..270f5c38a22 100644
--- a/contrib/isn/sql/isn.sql
+++ b/contrib/isn/sql/isn.sql
@@ -48,7 +48,8 @@ SELECT '9790123456785'::ISMN13,
        'M-1234-5678-5'::ISMN13;
 
 SELECT '9771234567003'::ISSN,
-       '12345679'::ISSN;
+       '12345679'::ISSN,
+       '9771234567898'::ISSN;
 
 SELECT '9771234567003'::ISSN13,
        '12345679'::ISSN13,
-- 
2.56.1

