File: eucjp.impure.lisp

package info (click to toggle)
sbcl 1%3A0.9.16.0-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 19,960 kB
  • ctags: 16,537
  • sloc: lisp: 231,164; ansic: 19,558; asm: 2,539; sh: 1,925; makefile: 308
file content (86 lines) | stat: -rw-r--r-- 3,523 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#-sb-unicode
(sb-ext:quit :unix-status 104)

(let ((p "eucjp-test.data")
      (eucjp "eucjp-test-eucjp.data")
      (utf8 "eucjp-test-utf8.data"))

  ;; generate test data
  (with-open-file (in "eucjp-test.lisp-expr" :direction :input)
    (with-open-file (out-eucjp eucjp :direction :output
                               :element-type '(unsigned-byte 8)
                               :if-exists :supersede)
      (with-open-file (out-utf8 utf8 :direction :output
                                :external-format :utf-8
                                :if-exists :supersede)
        (do ((euc (read in nil) (read in nil))
             (ucs (read in nil) (read in nil))
             (i 0 (1+ i)))
            ((or (null euc) (null ucs)))
          ;; write EUC-JP data as binary
          (let ((out out-eucjp))
            (when (>= euc #x10000)
              (write-byte (ldb (byte 8 16) euc) out))
            (when (>= euc #x100)
              (write-byte (ldb (byte 8 8) euc) out))
            (write-byte (ldb (byte 8 0) euc) out)
            (when (= (mod i 32) 31)
              (write-byte #x0a out)))
          ;; trust UTF-8 external format
          (let ((out out-utf8))
            (write-char (code-char ucs) out)
            (when (= (mod i 32) 31)
              (write-char (code-char #x0a) out)))))))

  ;; check if input works
  (with-open-file (in1 eucjp :direction :input
                       :external-format :euc-jp)
    (with-open-file (in2 utf8 :direction :input
                         :external-format :utf-8)
      (do ((c1 (read-char in1 nil) (read-char in1 nil))
           (c2 (read-char in2 nil) (read-char in2 nil)))
          ((and (null c1) (null c2)))
        (assert (eql c1 c2)))))

  ;; check if output works
  (with-open-file (in utf8 :direction :input
                      :external-format :utf-8)
    (with-open-file (out p :direction :output
                         :external-format :euc-jp
                         :if-exists :supersede)
      (do ((c (read-char in nil) (read-char in nil)))
          ((null c))
        (write-char c out))))
  (with-open-file (in1 eucjp :direction :input
                       :element-type '(unsigned-byte 8))
    (with-open-file (in2 p :direction :input
                         :element-type '(unsigned-byte 8))
      (do ((b1 (read-byte in1 nil) (read-byte in1 nil))
           (b2 (read-byte in2 nil) (read-byte in2 nil)))
          ((and (null b1) (null b2)))
        (assert (eql b1 b2)))))
  (delete-file p)
  (delete-file eucjp)
  (delete-file utf8))

;; check if string conversion works
(with-open-file (in "eucjp-test.lisp-expr" :direction :input)
  (do ((euc (read in nil) (read in nil))
       (ucs (read in nil) (read in nil))
       (i 0 (1+ i)))
      ((or (null euc) (null ucs)))
    (let ((o (coerce (cond ((>= euc #x10000)
                            (list (ldb (byte 8 16) euc)
                                  (ldb (byte 8 8) euc)
                                  (ldb (byte 8 0) euc)))
                           ((>= euc #x100)
                            (list (ldb (byte 8 8) euc)
                                  (ldb (byte 8 0) euc)))
                           (t (list euc)))
                     '(vector (unsigned-byte 8))))
          (s (string (code-char ucs))))
      (assert (equal (octets-to-string o :external-format :euc-jp) s))
      (assert (equal (coerce (string-to-octets s :external-format :euc-jp)
                             'list)
                     (coerce o 'list))))))
;;; success