unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* ISBN-13 checksums worth checking out
@ 2019-11-22  6:26 Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 0 replies; only message in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2019-11-22  6:26 UTC (permalink / raw)
  To: help-gnu-emacs; +Cc: emacs-devel

Dear Emacs comrades, yes, my life has derailed
a bit, which explains my absence from the
programming scene.

But today, after a superhuman effort, I finally
did some of it again - and I couldn't have
picked a more urgent topic!

Alright, enough with the suspension, just like
Uri Geller can bend a spoon while still
connected to the Matrix (20 years: 1999-2019),
you guessed it, I added support for
ISBN-13 checksums!

  https://dataswamp.org/~incal/emacs-init/isbn-new.el

The new stuff is at lines 29-51. It seems to...
check out.

Right?

I yank it last (now) if you don't want to
follow the URL.

;;; -*- lexical-binding: t -*-

;; This file: http://user.it.uu.se/~embe8573/emacs-init/isbn-new.el
;;            https://dataswamp.org/~incal/emacs-init/isbn-new.el

;; Old ISBN stuff, partially still in use: (?)
;;   https://dataswamp.org/~incal/emacs-init/isbn.el

;; NOTE: This isn't a replacement of the old
;;       stuff URLd above, this is an all new
;;       little project to compute ISBN
;;       checksums with Elisp

;; Update: November 2019, support for ISBN-13!

;; here is how the ISBN checksums work:
;;   https://dataswamp.org/~incal/books/isbn.txt
;; (see an example execution/manual computation
;;  of ISBN-10 last in this file)

(require 'cl-lib)

(defun char-to-int (c)
  (- c ?0))
;; test:
;; (char-to-int ?0)
;; (char-to-int ?9)

(defun isbn-integer-list (isbn num-digits)
  (let*((isbn-list         (string-to-list isbn))
        (isbn-drop-dashes  (remove ?- isbn-list))
        (isbn-drop-surplus (cl-subseq isbn-drop-dashes 0 num-digits))
        (isbn-ints         (cl-map 'list
                                   (lambda (c) (char-to-int c))
                                   isbn-drop-surplus) ))
    isbn-ints) )

(defun checksum-isbn-13 (isbn)
  (let*((isbn-ints (isbn-integer-list isbn 12))
        (sum       0))
    (cl-loop for e in isbn-ints
             for i upfrom 0
             do (cl-incf sum (* e (or (and (zerop (mod i 2)) 1) 3))))
    (- 10 (mod sum 10)) ))

;; ISBN-13 tests:
;; (checksum-isbn-13 "978-91-87861-99-4") ; 4
;; (checksum-isbn-13 "978-91-87861-67-3") ; 3
;; (checksum-isbn-13 "978-91-87861-54-3") ; 3
;; (checksum-isbn-13 "978-91-7515-317-9") ; 9
;; (checksum-isbn-13 "978-91-7515-205-9") ; 3

(defun checksum-isbn-10 (isbn)
  (let*((isbn-ints (isbn-integer-list isbn 9))
        (sum          0) )
    (cl-loop for e in isbn-ints
             for i downfrom 10
             do (cl-incf sum (* e i)) )
    (let ((checksum (mod (- 11 (mod sum 11)) 11)))
      (if (= 10 checksum) "X" checksum) )))

;; ISBN-10 tests:
;; (checksum-isbn-10 "978-1-85669-6")
;;
;; 10 tests from [1]:
;;
;; (checksum-isbn-10 "91-7054-940-0") ; 0
;; (checksum-isbn-10 "0-201-53992-6") ; 6
;; (checksum-isbn-10 "91-85668-01-X") ; X
;; (checksum-isbn-10 "91-7089-710-7") ; 7
;; (checksum-isbn-10 "9177988515")    ; 5
;; (checksum-isbn-10 "0312168144")    ; 4
;; (checksum-isbn-10 "1-4012-0622-0") ; 0
;; (checksum-isbn-10 "91-510-6483-9") ; 9
;; (checksum-isbn-10 "91-88930-23-8") ; 8
;; (checksum-isbn-10 "1616558717")    ; 7
;;
;; (checksum-isbn-10 "4294967296")
;; invalid, i.e not an ISBN-10  - checksum is 3:
;;   (mod (- 11 (mod (+ (* 4 10)
;;                      (* 2  9)
;;                      (* 9  8)
;;                      (* 4  7)
;;                      (* 9  6)
;;                      (* 6  5)
;;                      (* 7  4)
;;                      (* 2  3)
;;                      (* 9  2))
;;                   11)) 11) ; 3
;;
;; [1] https://dataswamp.org/~incal/books/books.bib

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2019-11-22  6:26 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-11-22  6:26 ISBN-13 checksums worth checking out Emanuel Berg via Users list for the GNU Emacs text editor

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).