X-Git-Url: http://dolda2000.com/gitweb/?p=lisp-utils.git;a=blobdiff_plain;f=charcode.lisp;h=a3cb5ad0949eb3c0eef81652585ec704f497cdb9;hp=6c08f42271088efc0184a287db93e98eebebeb8c;hb=HEAD;hpb=75545f661901820811acd2f93d70ca908cf3a32d diff --git a/charcode.lisp b/charcode.lisp index 6c08f42..a3cb5ad 100644 --- a/charcode.lisp +++ b/charcode.lisp @@ -2,15 +2,21 @@ ;;;; representations thereof (defpackage :charcode - (:use :cl #+sbcl :sb-gray #-sbcl :gray) + (:use :cl) (:export "MAKE-ENCODER" "MAKE-DECODER" "ENCODE-STRING" "DECODE-STRING" "SYSTEM-CHARSET" - "CODING-ERROR" + "NO-CODEC-ERROR" "CODING-ERROR" "MAKE-CODEC-CHARACTER-STREAM" "ASCII" "LATIN-1" "LATIN1" "UTF-8" "UTF8")) (in-package :charcode) ;;; General stuff +(define-condition no-codec-error (error) + ((codec-name :initarg :codec-name)) + (:report (lambda (c s) + (with-slots (codec-name) c + (format s "Could find no codec named ~A." codec-name))))) + (define-condition coding-error (error) ((input :initarg :input) (position :initarg :position) @@ -46,10 +52,12 @@ synonyms))) (defun make-encoder (name) - (the encoder-fun (values (funcall (get name 'make-encoder))))) + (the encoder-fun (values (funcall (or (get name 'make-encoder) + (error 'no-codec-error :codec-name name)))))) (defun make-decoder (name) - (the decoder-fun (values (funcall (get name 'make-decoder))))) + (the decoder-fun (values (funcall (or (get name 'make-decoder) + (error 'no-codec-error :codec-name name)))))) (defun system-charset () ;; XXX: Replace me with something perhaps more sensible. @@ -73,6 +81,10 @@ ;;; Gray stream implementation +;; Disabled for now. There doesn't seem to be any good way to get +;; these working generally over various implementations. + +#+unused ( (defclass codec-character-stream (fundamental-character-input-stream fundamental-character-output-stream) ((decoder :initarg :decoder) (encoder :initarg :encoder) @@ -181,15 +193,28 @@ (let ((outbuf (make-array (list (- end start)) :element-type '(unsigned-byte 8) :adjustable t :fill-pointer 0))) (funcall encoder seq outbuf) (write-sequence outbuf back)))) +) ;;; Implementation-specific functions -#+(or (and clisp unicode) sbcl) +#+(or (and clisp unicode) sbcl abcl) (defun unicode->char (unicode) (declare (type (unsigned-byte 24) unicode)) (code-char unicode)) -#+(or (and clisp unicode) sbcl) +#+(or (and clisp unicode) sbcl abcl) +(defun char->unicode (char) + (declare (type character char)) + (char-code char)) + +#+ecl +(defun unicode->char (unicode) + (declare (type (unsigned-byte 24) unicode)) + (when (>= unicode 256) + (error "ECL does not handle Unicode characters outside Latin-1.")) + (code-char unicode)) + +#+ecl (defun char->unicode (char) (declare (type character char)) (char-code char)) @@ -241,6 +266,8 @@ (define-encoder (ascii) #'encode-ascii) +(define-codec-synonyms ascii :ascii) + ;;; Latin-1 (defun decode-latin-1 (byteseq charseq &key (start 0) (end (length byteseq))) @@ -278,7 +305,7 @@ (define-encoder (latin-1) #'encode-latin-1) -(define-codec-synonyms latin-1 latin1 iso-8859-1) +(define-codec-synonyms latin-1 latin1 iso-8859-1 :latin-1 :latin1 :iso-8859-1) ;;; UTF-8 @@ -362,4 +389,4 @@ (setf mlen 0)))))))) #'decode))) -(define-codec-synonyms utf-8 utf8) +(define-codec-synonyms utf-8 utf8 :utf-8 :utf8)