-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
67 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,40 @@ | ||
;;; -*- Mode:Lisp; Syntax:ANSI-Common-Lisp; Coding:utf-8 -*- | ||
|
||
(in-package :cl-nixbase32) | ||
|
||
(defvar *alphabet* "0123456789abcdfghijklmnpqrsvwxyz") | ||
|
||
(defun byte-to-bits (byte) | ||
"Convert a byte to a list of its bits." | ||
(loop for i from 7 downto 0 | ||
collect (ldb (byte 1 i) byte))) | ||
|
||
(defun bits-to-int (bits) | ||
"Convert a list of bits to an integer." | ||
(reduce (lambda (acc bit) (+ (* 2 acc) bit)) bits)) | ||
|
||
(defun pad-in-front (list) | ||
(ecase (mod (length list) 5) | ||
(0 list) | ||
(1 (append '(0 0 0 0) list)))) | ||
|
||
(defun group-by-5 (list) | ||
(when list | ||
(cons (subseq list 0 5) | ||
(group-by-5 (nthcdr 5 list))))) | ||
|
||
(defun lookup-alphabet (list) | ||
(map 'string (lambda (x) (aref *alphabet* x)) list)) | ||
|
||
(defun bytes-to-nixbase32 (vector) | ||
;; Maybe I'll implement this more efficiently at some point. For | ||
;; now, this works. | ||
(arrow-macros:->> vector | ||
(map 'list #'identity) | ||
reverse | ||
(map 'list #'byte-to-bits) | ||
alexandria:flatten | ||
pad-in-front | ||
group-by-5 | ||
(mapcar #'bits-to-int) | ||
lookup-alphabet)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
|
||
(defpackage :cl-nixbase32 | ||
(:use :common-lisp :alexandria) | ||
(:export)) | ||
(:export #:bytes-to-nixbase32)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters