| 1 |
(cl:unless (cl:find-package :ISO-2022-JP-1) |
| 2 |
(make-package :ISO-2022-JP-1)) |
| 3 |
(in-package :ISO-2022-JP-1) |
| 4 |
|
| 5 |
(defun compose (bytes) |
| 6 |
(loop for i in bytes |
| 7 |
with cum = 0 |
| 8 |
do (setf cum (+ (ash cum 8) i)) |
| 9 |
finally (return cum))) |
| 10 |
|
| 11 |
(defun mapping-hash-table (sequence &optional (mask 0)) |
| 12 |
(loop with hash = (make-hash-table :size (floor (* 1.5 (length sequence))) |
| 13 |
:test 'eq) |
| 14 |
for i from 0 below (length sequence) by 2 |
| 15 |
for multibyte = (elt sequence i) |
| 16 |
for codepoint = (elt sequence (1+ i)) |
| 17 |
for unicode-char = (code-char codepoint) |
| 18 |
when (zerop (logand multibyte mask)) |
| 19 |
do (progn |
| 20 |
(setf (gethash multibyte hash) unicode-char) |
| 21 |
(setf (gethash unicode-char hash) multibyte) |
| 22 |
(when (> multibyte #xFF) |
| 23 |
(setf (gethash (ash multibyte -8) hash) t))) |
| 24 |
finally (return hash))) |
| 25 |
|
| 26 |
(defun multimap (escapes tables) |
| 27 |
(loop for seq in escapes |
| 28 |
for table in tables |
| 29 |
for table-cons = (member table tables :test 'eq) |
| 30 |
do (progn |
| 31 |
;; Change escape sequence into byte codes |
| 32 |
(setf seq (mapcar #'char-code seq)) |
| 33 |
;; Store it in the hash table |
| 34 |
(setf (gethash t table) seq) |
| 35 |
(loop for other-table in tables |
| 36 |
do (loop for i from 1 |
| 37 |
for precedings = (butlast seq i) |
| 38 |
while precedings |
| 39 |
do (setf (gethash (compose precedings) other-table) t) |
| 40 |
finally (setf (gethash (compose seq) other-table) table-cons))))) |
| 41 |
(nconc tables tables)) |
| 42 |
|
| 43 |
(defparameter ext::iso-2022-jp-1 |
| 44 |
(let* ((ascii-no-esc (mapping-hash-table (loop for i from 0 to 127 |
| 45 |
unless (= i (char-code #\esc)) |
| 46 |
nconc (list i i)))) |
| 47 |
(jis212 (mapping-hash-table (ext::load-encoding :jisx0212) #x8080)) |
| 48 |
(jis208 (mapping-hash-table (ext::load-encoding :jisx0208) #x8080)) |
| 49 |
(jis201 (mapping-hash-table (ext::load-encoding :jisx0201) #x80))) |
| 50 |
(multimap '((#\Esc #\( #\B) |
| 51 |
(#\Esc #\( #\J) |
| 52 |
(#\Esc #\$ #\@) |
| 53 |
(#\Esc #\$ #\B) |
| 54 |
(#\Esc #\$ #\( #\D)) |
| 55 |
(list ascii-no-esc jis201 jis208 jis208 jis212)))) |
| 56 |
|
| 57 |
(delete-package :ISO-2022-JP-1) |