ISBN-13 checksums worth checking out

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

ISBN-13 checksums worth checking out

Emacs - Help mailing list
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


Reply | Threaded
Open this post in threaded view
|

Re: ISBN-13 checksums worth checking out

Amin Bandali-4
Super happy to see you here again, Emanuel. :)

Would you consider adding license headers to
the (non-trivial) snippets you post here?  It
would be really helpful for those of us that
like to use them in our Emacs configs in
freedom!

You can see examples of Elisp license headers
in any of the lisp/*.el files in your Emacs
installation directory or in the Emacs git
repository, such as lisp/rot13.el:
https://git.sv.gnu.org/cgit/emacs.git/tree/lisp/rot13.el

Reply | Threaded
Open this post in threaded view
|

Re: ISBN-13 checksums worth checking out

Eric Abrahamsen-2
Amin Bandali <[hidden email]> writes:

> Super happy to see you here again, Emanuel. :)
>
> Would you consider adding license headers to
> the (non-trivial) snippets you post here?  It
> would be really helpful for those of us that
> like to use them in our Emacs configs in
> freedom!

And/or stick things in melpa/elpa! I would find this library useful.

Reply | Threaded
Open this post in threaded view
|

Re: ISBN-13 checksums worth checking out

Eric Abrahamsen-2
Eric Abrahamsen <[hidden email]> writes:

> Amin Bandali <[hidden email]> writes:
>
>> Super happy to see you here again, Emanuel. :)
>>
>> Would you consider adding license headers to
>> the (non-trivial) snippets you post here?  It
>> would be really helpful for those of us that
>> like to use them in our Emacs configs in
>> freedom!
>
> And/or stick things in melpa/elpa! I would find this library useful.

Actually, if you wanted to make an ISBN barcode generator in elisp, that
would be aces :)


Reply | Threaded
Open this post in threaded view
|

Re: ISBN-13 checksums worth checking out

Emacs - Help mailing list
In reply to this post by Eric Abrahamsen-2
Eric Abrahamsen wrote:

>> Super happy to see you here again, Emanuel.
>> :)
>>
>> Would you consider adding license headers to
>> the (non-trivial) snippets you post here?
>> It would be really helpful for those of us
>> that like to use them in our Emacs configs
>> in freedom!
>
> And/or stick things in melpa/elpa! I would
> find this library useful.

Thank you guys, well I have thought about it
because of obvious reasons and advantages to me
and maybe even other people, but never gotten
to it...

But maybe the stars are aligned for this
particular package because it is not intermixed
with configuration and it is such a nerdy
one-purpose thing that it is easy to understand
to anyone.

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