Debian Codegraph @

src/lsp/unicode.lsp

raw
1 ;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: SYSTEM -*-
2 ;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab:
3
4 ;;;;
5 ;;;; Copyright (c) 2011, Juan Jose Garcia-Ripoll.
6 ;;;;
7 ;;;; See file 'LICENSE' for the copyright details.
8 ;;;;
9
10 ;;;; unicode.lsp -- encoding and decoding
11 ;;;;
12
13 (in-package "SYSTEM")
14
15 ;;;;
16 ;;;; ENCODING / DECODING ERRORS
17 ;;;;
18
19 (define-condition character-coding-error (error)
20 ((external-format :initarg :external-format :reader character-coding-error-external-format)))
21
22 (define-condition character-encoding-error (character-coding-error)
23 ((code :initarg :code :reader character-encoding-error-code)))
24
25 (define-condition character-decoding-error (character-coding-error)
26 ((octets :initarg :octets :reader character-decoding-error-octets)))
27
28 (define-condition stream-encoding-error (stream-error character-encoding-error)
29 ()
30 (:report
31 (lambda (c s)
32 (let ((stream (stream-error-stream c))
33 (code (character-encoding-error-code c)))
34 (format s "~@<encoding error on stream ~S (~S ~S): ~2I~_~
35 the character with code ~D cannot be encoded.~@:>"
36 stream ':external-format
37 (character-coding-error-external-format c)
38 code)))))
39
40 (define-condition stream-decoding-error (stream-error character-decoding-error)
41 ()
42 (:report
43 (lambda (c s)
44 (let ((stream (stream-error-stream c))
45 (octets (character-decoding-error-octets c)))
46 (format s "~@<decoding error on stream ~S (~S ~S): ~2I~_~
47 the octet sequence ~S cannot be decoded.~@:>"
48 stream ':external-format
49 (character-coding-error-external-format c)
50 octets)))))
51
52 (defun encoding-error (stream external-format code)
53 (restart-case (error 'stream-encoding-error
54 :stream stream
55 :external-format external-format
56 :code code)
57 (continue ()
58 :report "Ignore character"
59 nil)
60 (use-value (c)
61 :report "Store a different character code."
62 (if (characterp c) c (code-char c)))))
63
64 (defun decoding-error (stream external-format octets)
65 (restart-case (error 'stream-decoding-error
66 :stream stream
67 :external-format external-format
68 :octets octets)
69 (continue ()
70 :report "Read next character"
71 nil)
72 (use-value (c)
73 :report "Replace the bogus sequence with a character"
74 (if (characterp c) c (code-char c)))))