From: Emanuel Berg via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org>
To: help-gnu-emacs@gnu.org
Cc: emacs-devel@gnu.org
Subject: ISBN-13 checksums worth checking out
Date: Fri, 22 Nov 2019 07:26:29 +0100 [thread overview]
Message-ID: <867e3sv33e.fsf@zoho.eu> (raw)
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
next reply other threads:[~2019-11-22 6:26 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-11-22 6:26 Emanuel Berg via Users list for the GNU Emacs text editor [this message]
2019-11-22 13:29 ` ISBN-13 checksums worth checking out Amin Bandali
2019-11-22 17:05 ` Eric Abrahamsen
2019-11-22 18:37 ` Eric Abrahamsen
2019-11-23 0:14 ` Emanuel Berg via Users list for the GNU Emacs text editor
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=867e3sv33e.fsf@zoho.eu \
--to=help-gnu-emacs@gnu.org \
--cc=emacs-devel@gnu.org \
--cc=moasenwood@zoho.eu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.