Debian Codegraph @

ext/charconv/gen-lattab.scm

raw
1 ;;;
2 ;;; Generate Unicode <-> Latin-N tables
3 ;;;
4
5 ;; The tables can be obtained from
6 ;; https://unicode.org/Public/MAPPINGS/ISO8859/
7
8 (use file.util)
9 (use gauche.dictionary)
10 (use gauche.collection)
11 (use gauche.cgen)
12 (use util.match)
13 (use srfi.42)
14
15 (define-class <latin-ces> ()
16 ((n :init-keyword :n) ; iso8859-N
17 (bimap :init-keyword :bimap) ; code <-> ucs
18 ))
19
20 ;; Returns bimap between iso8859-N (upper plane) <-> UCS
21 (define (parse-txt file)
22 (rlet1 b (make-bimap (make-hash-table 'eqv?) (make-hash-table 'eqv?))
23 (dolist [line (file->string-list file)]
24 (rxmatch-case line
25 [#/^0x([[:xdigit:]]+)\s+0x([[:xdigit:]]+)/ (_ scode sucs)
26 (let ([code (string->number scode 16)]
27 [ucs (string->number sucs 16)])
28 (when (>= code #xa0)
29 (bimap-put! b code ucs)))]))))
30
31 (define (load-ces-data dir n)
32 (make <latin-ces> :n n :bimap (parse-txt #"~|dir|/8859-~|n|.TXT")))
33
34 (define (latN->utf8 ces)
35 (define procname #"lat~(~ ces'n)_utf8")
36 (cgen-extern #"static ScmConvProc ~|procname|;")
37
38 (cgen-body ""
39 #"static ScmSize ~|procname|(ScmConvInfo *cinfo,"
40 #" const char *inptr, ScmSize inroom SCM_UNUSED,"
41 #" char *outptr, ScmSize outroom, ScmSize *outchars)"
42 #"{"
43 #" static u_char tab[] = {")
44 (do-ec (: code #xa0 #x100)
45 (let* ([ucs (hash-table-get (bimap-left (~ ces'bimap)) code 0)]
46 [utf8 (take* (ucs4->utf8 ucs) 3 #t 0)])
47 (cgen-body
48 (format " 0x~2,'0x,0x~2,'0x,0x~2,'0x, /* ~2,'0x */"
49 (car utf8) (cadr utf8) (caddr utf8)
50 code))))
51 (cgen-body #" };"
52 #" u_char c = *inptr;"
53 #" if (c < 0x80) {"
54 #" *outptr = c;"
55 #" *outchars = 1;"
56 #" } else if (c < 0xa0) {"
57 #" OUTCHK(2);"
58 #" outptr[0] = 0xc2;"
59 #" outptr[1] = c;"
60 #" *outchars = 2;"
61 #" } else {"
62 #" int index = (c - 0xa0) * 3;"
63 #" if (tab[index] == 0) {"
64 #" DO_SUBST;"
65 #" } else if (tab[index + 2] == 0) {"
66 #" OUTCHK(2);"
67 #" outptr[0] = tab[index];"
68 #" outptr[1] = tab[index+1];"
69 #" *outchars = 2;"
70 #" } else {"
71 #" OUTCHK(3);"
72 #" outptr[0] = tab[index];"
73 #" outptr[1] = tab[index+1];"
74 #" outptr[2] = tab[index+2];"
75 #" *outchars = 3;"
76 #" }"
77 #" }"
78 #" return 1;"
79 #"}"))
80
81 (define (utf8->latN ces)
82 (define procname #"utf8_lat~(~ ces'n)")
83 ;; grouped :: ((first-octet branch ...) ...)
84 ;; branch :: (second-octet . code) | (second-octet third-octet . code)
85 (define grouped
86 (sort (map (^g (cons (caar g) (sort (map cdr g) < car)))
87 (group-collection
88 (map (^p (match-let1 (ucs . code) p
89 (append (ucs4->utf8 ucs) code)))
90 (hash-table->alist (bimap-right (~ ces'bimap))))
91 :key car))
92 < car))
93
94 ;; If branch has more than 8 entries and all of them are 2-octet sequences,
95 ;; we use lookup table.
96 (define (gen-lookup pfx branches)
97 (receive (lo hi) (find-min&max (map car branches))
98 (cgen-decl ""
99 #"/* offset ~|lo|*/"
100 #"static u_char ~|procname|_~|pfx|[] = {")
101 (do-ec (: n lo (+ hi 1))
102 (:let e (assv n branches))
103 (cgen-decl (format " 0x~2,'0x, /* [~a ~2,'0x] */"
104 (if e (cdr e) 0) pfx n)))
105 (cgen-decl "};")
106 (cgen-body #" INCHK(2);"
107 #" u_char u1 = inptr[1];"
108 #" u_char out = 0;"
109 #" if (u1 >= ~|lo| && u1 <= ~|hi|) {"
110 #" out = utf8_lat~(~ ces'n)_~|pfx|[u1 - ~|lo|];"
111 #" }"
112 #" if (out == 0) DO_SUBST;"
113 #" else outptr[0] = out;")))
114
115 ;; Else we do linear search
116 (define (gen-linear pfx branches)
117 (cgen-body " u_char u1 = inptr[1];")
118 (dolist [br branches]
119 (if (not (pair? (cdr br)))
120 (cgen-body (format " if (u1 == 0x~2,'0x) {" (car br))
121 (format " *outptr = 0x~2,'0x;" (cdr br)))
122 (cgen-body (format " if (u1 == 0x~2,'0x &&" (car br))
123 (format " (u_char)inptr[2] == 0x~2,'0x) {" (cadr br))
124 (format " *outptr = 0x~2,'0x;" (cddr br))))
125 (cgen-body " } else"))
126 (cgen-body " { DO_SUBST; }"))
127
128 (cgen-extern #"static ScmConvProc ~|procname|;")
129 (cgen-body #"static ScmSize ~|procname|(ScmConvInfo *cinfo,"
130 #" const char *inptr, ScmSize inroom,"
131 #" char *outptr, ScmSize outroom, ScmSize *outchars)"
132 #"{"
133 #" u_char u0 = inptr[0];"
134 #" ScmSize nread;"
135 #" if (u0 < 0x80) {"
136 #" outptr[0] = u0;"
137 #" *outchars = 1;"
138 #" return 1;"
139 #" }"
140 #" else if (u0 < 0xc0) { return ILLEGAL_SEQUENCE; }"
141 #" else if (u0 < 0xe0) { nread = 2; }"
142 #" else if (u0 < 0xf0) { nread = 3; }"
143 #" else if (u0 < 0xf8) { nread = 4; }"
144 #" else if (u0 < 0xfc) { nread = 5; }"
145 #" else if (u0 < 0xfe) { nread = 6; }"
146 #" else { return ILLEGAL_SEQUENCE; }"
147 #" INCHK(nread);")
148
149 (cgen-body " if (u0 == 0xc2 && (u_char)inptr[1] >= 0x80 && (u_char)inptr[1] < 0xa0) {")
150 (cgen-body " outptr[0] = inptr[1]; /* 0x80 - 0x9f */")
151 (cgen-body " } else")
152 (dolist [group grouped]
153 (define pfx (format "~2,'0x" (car group)))
154 (cgen-body #" if (u0 == 0x~|pfx|) {")
155 (if (or (length<? (cdr group) 8)
156 (any (^e (pair? (cdr e))) (cdr group)))
157 (gen-linear pfx (cdr group))
158 (gen-lookup pfx (cdr group)))
159 (cgen-body " } else"))
160 (cgen-body " { DO_SUBST; }")
161 (cgen-body " *outchars = 1;")
162 (cgen-body " return nread;")
163 (cgen-body "}"))
164
165 ;; This is in gauche.unicode, but we can't depend on it yet during
166 ;; building charconv.
167 ;; For Latin-N set, we only need to take care up to 3-octet ranges.
168 (define (ucs4->utf8 code)
169 (cond [(< code #x80) `(,code)]
170 [(< code #x800) `(,(logior (ash code -6) #xc0)
171 ,(logior (logand code #x3f) #x80))]
172 [else `(,(logior (ash code -12) #xe0)
173 ,(logior (logand (ash code -6) #x3f) #x80)
174 ,(logior (logand code #x3f) #x80))]))
175
176 (define (main args)
177 (match (cdr args)
178 [(datadir)
179 (parameterize ([cgen-current-unit (make <cgen-unit>
180 :name "latin_tab"
181 :preamble "/* Generated by gen-lattab.scm */"
182 :init-prologue ""
183 :init-epilogue "")])
184 (dolist [n '(2 3 4 5 6 7 8 9 10 11 13 14 15 16)]
185 (let1 ces (load-ces-data datadir n)
186 (latN->utf8 ces)
187 (utf8->latN ces)))
188 (cgen-emit-c (cgen-current-unit))
189 (cgen-emit-h (cgen-current-unit)))]
190 )
191 0)