From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Emanuel Berg via Users list for the GNU Emacs text editor Newsgroups: gmane.emacs.help,gmane.emacs.devel Subject: ISBN-13 checksums worth checking out Date: Fri, 22 Nov 2019 07:26:29 +0100 Message-ID: <867e3sv33e.fsf@zoho.eu> Reply-To: Emanuel Berg Mime-Version: 1.0 Content-Type: text/plain Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="231958"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (gnu/linux) Cc: emacs-devel@gnu.org To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Nov 22 07:27:00 2019 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1iY2PD-000y71-H2 for geh-help-gnu-emacs@m.gmane.org; Fri, 22 Nov 2019 07:26:59 +0100 Original-Received: from localhost ([::1]:47554 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iY2P6-0002Aq-Rn for geh-help-gnu-emacs@m.gmane.org; Fri, 22 Nov 2019 01:26:52 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:58886) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iY2Ov-0002Ag-PI for help-gnu-emacs@gnu.org; Fri, 22 Nov 2019 01:26:42 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iY2Ou-0007td-Ej for help-gnu-emacs@gnu.org; Fri, 22 Nov 2019 01:26:41 -0500 Original-Received: from 195-159-176-226.customer.powertech.no ([195.159.176.226]:51314 helo=blaine.gmane.org) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iY2Ou-0007qq-6G for help-gnu-emacs@gnu.org; Fri, 22 Nov 2019 01:26:40 -0500 Original-Received: from list by blaine.gmane.org with local (Exim 4.89) (envelope-from ) id 1iY2Or-000xqX-DZ for help-gnu-emacs@gnu.org; Fri, 22 Nov 2019 07:26:37 +0100 X-Injected-Via-Gmane: http://gmane.org/ Mail-Followup-To: help-gnu-emacs@gnu.org Original-Followup-To: gmane.emacs.help Mail-Copies-To: never Cancel-Lock: sha1:OdUtYa9U3q6m5ZEHOC0VKVQRbVk= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 195.159.176.226 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.org gmane.emacs.help:121875 gmane.emacs.devel:242605 Archived-At: 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