Debian Codegraph @

src/libchar.scm

raw
1 ;;;
2 ;;; libchar.scm - builtin character procedures
3 ;;;
4 ;;; Copyright (c) 2000-2024 Shiro Kawai <[email protected]>
5 ;;;
6 ;;; Redistribution and use in source and binary forms, with or without
7 ;;; modification, are permitted provided that the following conditions
8 ;;; are met:
9 ;;;
10 ;;; 1. Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
12 ;;;
13 ;;; 2. Redistributions in binary form must reproduce the above copyright
14 ;;; notice, this list of conditions and the following disclaimer in the
15 ;;; documentation and/or other materials provided with the distribution.
16 ;;;
17 ;;; 3. Neither the name of the authors nor the names of its contributors
18 ;;; may be used to endorse or promote products derived from this
19 ;;; software without specific prior written permission.
20 ;;;
21 ;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 ;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 ;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 ;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 ;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 ;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 ;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 ;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 ;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 ;;;
33
34 (select-module gauche.internal)
35
36 (inline-stub
37 (.include "gauche/priv/configP.h"
38 "gauche/vminsn.h"
39 "gauche/char_attr.h"
40 "gauche/priv/charP.h"
41 <ctype.h>)
42 )
43
44 ;;;
45 ;;; Characters
46 ;;;
47
48 (select-module scheme)
49 (define-cproc char? (obj) ::<boolean> :fast-flonum :constant
50 (inliner CHARP) SCM_CHARP)
51
52 (inline-stub
53 (define-cise-expr char-cmp
54 ;; Assumes local variables c1, c2, chars
55 [(_ op)
56 `(while 1
57 (cond [(not (SCM_PAIRP chars)) (return (,op c1 c2)) (break)]
58 [(,op c1 c2) (unless (SCM_CHARP (SCM_CAR chars))
59 (Scm_TypeError "char" "character" (SCM_CAR chars)))
60 (set! c1 c2)
61 (set! c2 (SCM_CHAR_VALUE (SCM_CAR chars)))
62 (set! chars (SCM_CDR chars))]
63 [else (return FALSE) (break)]))])
64 )
65
66 (define-cproc char=? (c1::<char> c2::<char> :rest chars)
67 ::<boolean> :constant (char-cmp ==))
68 (define-cproc char<? (c1::<char> c2::<char> :rest chars)
69 ::<boolean> :constant (char-cmp <))
70 (define-cproc char>? (c1::<char> c2::<char> :rest chars)
71 ::<boolean> :constant (char-cmp >))
72 (define-cproc char<=? (c1::<char> c2::<char> :rest chars)
73 ::<boolean> :constant (char-cmp <=))
74 (define-cproc char>=? (c1::<char> c2::<char> :rest chars)
75 ::<boolean> :constant (char-cmp >=))
76
77 (inline-stub
78 (define-cise-expr char-ci-cmp
79 ;; Assumes local variables c1, c2, chars
80 [(_ op)
81 `(begin
82 (set! c1 (Scm_CharFoldcase c1))
83 (set! c2 (Scm_CharFoldcase c2))
84 (while 1
85 (cond [(not (SCM_PAIRP chars)) (return (,op c1 c2)) (break)]
86 [(,op c1 c2) (unless (SCM_CHARP (SCM_CAR chars))
87 (Scm_TypeError "char" "character" (SCM_CAR chars)))
88 (set! c1 c2)
89 (set! c2 (Scm_CharFoldcase
90 (SCM_CHAR_VALUE (SCM_CAR chars))))
91 (set! chars (SCM_CDR chars))]
92 [else (return FALSE) (break)])))])
93 )
94
95 (define-cproc char-ci=? (c1::<char> c2::<char> :rest chars)
96 ::<boolean> :constant (char-ci-cmp ==))
97 (define-cproc char-ci<? (c1::<char> c2::<char> :rest chars)
98 ::<boolean> :constant (char-ci-cmp <))
99 (define-cproc char-ci>? (c1::<char> c2::<char> :rest chars)
100 ::<boolean> :constant (char-ci-cmp >))
101 (define-cproc char-ci<=? (c1::<char> c2::<char> :rest chars)
102 ::<boolean> :constant (char-ci-cmp <=))
103 (define-cproc char-ci>=? (c1::<char> c2::<char> :rest chars)
104 ::<boolean> :constant (char-ci-cmp >=))
105
106 (define-cproc char-alphabetic? (c::<char>) ::<boolean> Scm_CharAlphabeticP)
107 (define-cproc char-numeric? (c::<char>) ::<boolean> Scm_CharNumericP)
108 (define-cproc char-whitespace? (c::<char>) ::<boolean>
109 (return (or (and (SCM_CHAR_ASCII_P c) (isspace c))
110 (SCM_CHAR_EXTRA_WHITESPACE c))))
111 (define-cproc char-upper-case? (c::<char>) ::<boolean> Scm_CharUppercaseP)
112 (define-cproc char-lower-case? (c::<char>) ::<boolean> Scm_CharLowercaseP)
113
114 (define-cproc char->integer (c::<char>) ::<long> :constant
115 (return (cast (signed long) c)))
116 (define-cproc integer->char (c::<int>) ::<char> :constant
117 (return (cast ScmChar c)))
118
119 (define-cproc char-upcase (c::<char>) ::<char> Scm_CharUpcase)
120 (define-cproc char-downcase (c::<char>) ::<char> Scm_CharDowncase)
121
122 (select-module gauche)
123 (define-cproc digit->integer (ch::<char> :optional (radix::<fixnum> 10) (extended-range?::<boolean> #f))
124 :constant
125 (let* ([r::int])
126 (when (or (< radix SCM_RADIX_MIN) (> radix SCM_RADIX_MAX))
127 (Scm_Error "radix must be an integer between %d and %d, but got %d"
128 SCM_RADIX_MIN SCM_RADIX_MAX radix))
129 (when (and extended-range? (> radix 10))
130 (Scm_Error "for extended range, radix can't exceed 10" radix))
131 (set! r (Scm_DigitToInt ch radix extended-range?))
132 (return (?: (>= r 0) (SCM_MAKE_INT r) '#f))))
133
134 (define-cproc integer->digit (n::<fixnum> :optional (radix::<fixnum> 10) (basechar1::<char> #\0) (basechar2::<char> #\a))
135 :constant
136 (let* ([r::ScmChar])
137 (when (or (< radix SCM_RADIX_MIN) (> radix SCM_RADIX_MAX))
138 (Scm_Error "radix must be an integer between %d and %d, but got %d"
139 SCM_RADIX_MIN SCM_RADIX_MAX radix))
140 (set! r (Scm_IntToDigit n radix basechar1 basechar2))
141 (return (?: (== r SCM_CHAR_INVALID) '#f (SCM_MAKE_CHAR r)))))
142
143 (define-cproc ucs->char (n::<int>)
144 (let* ([ch::ScmChar (Scm_UcsToChar n)])
145 (return (?: (== ch SCM_CHAR_INVALID) '#f (SCM_MAKE_CHAR ch)))))
146
147 (define-cproc char->ucs (c::<char>)
148 (let* ([ucs::int (Scm_CharToUcs c)])
149 (return (?: (< ucs 0) '#f (Scm_MakeInteger ucs)))))
150
151 (define-cproc gauche-character-encoding () Scm_CharEncodingName)
152
153 (define-cproc supported-character-encodings ()
154 (return (Scm_CStringArrayToList (Scm_SupportedCharacterEncodings) -1 0)))
155
156 (define-cproc supported-character-encoding? (encoding::<const-cstring>)
157 ::<boolean> Scm_SupportedCharacterEncodingP)
158
159 (define-cproc char-title-case? (c::<char>)
160 ::<boolean> :constant Scm_CharTitlecaseP)
161 (define-cproc char-titlecase (c::<char>) ::<char> Scm_CharTitlecase)
162 (define-cproc char-foldcase (c::<char>) ::<char> Scm_CharFoldcase)
163
164 (define-cproc char-general-category (c::<char>) :constant
165 (case (Scm_CharGeneralCategory c)
166 [(SCM_CHAR_CATEGORY_Lu) (return 'Lu)]
167 [(SCM_CHAR_CATEGORY_Ll) (return 'Ll)]
168 [(SCM_CHAR_CATEGORY_Lt) (return 'Lt)]
169 [(SCM_CHAR_CATEGORY_Lm) (return 'Lm)]
170 [(SCM_CHAR_CATEGORY_Lo) (return 'Lo)]
171 [(SCM_CHAR_CATEGORY_Mn) (return 'Mn)]
172 [(SCM_CHAR_CATEGORY_Mc) (return 'Mc)]
173 [(SCM_CHAR_CATEGORY_Me) (return 'Me)]
174 [(SCM_CHAR_CATEGORY_Nd) (return 'Nd)]
175 [(SCM_CHAR_CATEGORY_Nl) (return 'Nl)]
176 [(SCM_CHAR_CATEGORY_No) (return 'No)]
177 [(SCM_CHAR_CATEGORY_Pc) (return 'Pc)]
178 [(SCM_CHAR_CATEGORY_Pd) (return 'Pd)]
179 [(SCM_CHAR_CATEGORY_Ps) (return 'Ps)]
180 [(SCM_CHAR_CATEGORY_Pe) (return 'Pe)]
181 [(SCM_CHAR_CATEGORY_Pi) (return 'Pi)]
182 [(SCM_CHAR_CATEGORY_Pf) (return 'Pf)]
183 [(SCM_CHAR_CATEGORY_Po) (return 'Po)]
184 [(SCM_CHAR_CATEGORY_Sm) (return 'Sm)]
185 [(SCM_CHAR_CATEGORY_Sc) (return 'Sc)]
186 [(SCM_CHAR_CATEGORY_Sk) (return 'Sk)]
187 [(SCM_CHAR_CATEGORY_So) (return 'So)]
188 [(SCM_CHAR_CATEGORY_Zs) (return 'Zs)]
189 [(SCM_CHAR_CATEGORY_Zl) (return 'Zl)]
190 [(SCM_CHAR_CATEGORY_Zp) (return 'Zp)]
191 [(SCM_CHAR_CATEGORY_Cc) (return 'Cc)]
192 [(SCM_CHAR_CATEGORY_Cf) (return 'Cf)]
193 [(SCM_CHAR_CATEGORY_Cs) (return 'Cs)]
194 [(SCM_CHAR_CATEGORY_Co) (return 'Co)]
195 [(SCM_CHAR_CATEGORY_Cn) (return 'Cn)]
196 [else (Scm_Panic "Scm_CharGeneralCategory returns unknown value \
197 for character %d" c)
198 (return SCM_UNDEFINED)])) ; dummy
199
200
201 ;;;
202 ;;; Character sets
203 ;;;
204
205 (select-module gauche)
206 (inline-stub
207 (define-constant *char-code-max* (c "Scm_MakeInteger(SCM_CHAR_MAX)"))
208
209 (define-cfn char_set_add (cs::ScmCharSet* chars) ::void :static
210 (dolist [ch chars]
211 (unless (SCM_CHARP ch) (Scm_Error "character required, but got %S" ch))
212 (Scm_CharSetAddRange cs (SCM_CHAR_VALUE ch) (SCM_CHAR_VALUE ch))))
213 )
214
215 (define-cproc char-set? (obj) ::<boolean> :constant SCM_CHARSETP)
216
217 (define-cproc char-set (:rest chars) ::<char-set>
218 (let* ([cs::ScmCharSet* (SCM_CHARSET (Scm_MakeEmptyCharSet))])
219 (char_set_add cs chars)
220 (return cs)))
221
222 (define-cproc char-set-copy (cs::<char-set>) Scm_CharSetCopy)
223
224 (define-cproc char-set-freeze (cs::<char-set>) Scm_CharSetFreeze)
225 (define-cproc char-set-freeze! (cs::<char-set>) Scm_CharSetFreezeX)
226
227 (define (char-set-size cs)
228 (rlet1 count 0
229 (for-each (^[range] (inc! count (- (cdr range) (car range) -1)))
230 ((with-module gauche.internal %char-set-ranges) cs))))
231
232 (define-cproc read-char-set
233 (port::<input-port> :key (error::<boolean> #t) (posix-bracket::<boolean> #t))
234 (return (Scm_CharSetRead port NULL error posix-bracket)))
235
236 (define-cproc char-set-contains? (cs::<char-set> ch::<char>)
237 ::<boolean> :constant Scm_CharSetContains)
238
239 (define-cproc char-set-complement! (cs::<char-set>) Scm_CharSetComplement)
240
241 (define (char-set-complement cs) (char-set-complement! (char-set-copy cs)))
242
243 (define-cproc char-set-immutable? (cs::<char-set>) ::<boolean>
244 (return (SCM_CHAR_SET_IMMUTABLE_P cs)))
245
246 (define (char-set-hash cs :optional (bound #x1fffffff))
247 (fold (^[range val] (modulo (hash (+ val (car range) (cdr range))) bound))
248 0 ((with-module gauche.internal %char-set-ranges) cs)))
249
250 (select-module gauche.internal)
251
252 (define-cproc %char-set-equal? (x::<char-set> y::<char-set>)
253 ::<boolean> Scm_CharSetEq)
254
255 (define-cproc %char-set<=? (x::<char-set> y::<char-set>)
256 ::<boolean> Scm_CharSetLE)
257
258 (define-cproc %char-set-add-chars! (cs::<char-set> chars::<list>) ::<char-set>
259 (char_set_add cs chars) (return cs))
260
261 (define-cproc %char-set-add-range! (cs::<char-set> from to)
262 (let* ([f::long -1] [t::long -1])
263 (cond [(SCM_INTP from) (set! f (SCM_INT_VALUE from))]
264 [(SCM_CHARP from) (set! f (SCM_CHAR_VALUE from))])
265 (when (< f 0) (SCM_TYPE_ERROR from "character or positive exact integer"))
266 (when (> f SCM_CHAR_MAX)
267 (Scm_Error "'from' argument out of range: %S" from))
268 (cond [(SCM_INTP to) (set! t (SCM_INT_VALUE to))]
269 [(SCM_CHARP to) (set! t (SCM_CHAR_VALUE to))])
270 (when (< t 0) (SCM_TYPE_ERROR to "character or positive exact integer"))
271 (when (> t SCM_CHAR_MAX)
272 (Scm_Error "'to' argument out of range: %S" to))
273 (return (Scm_CharSetAddRange cs (cast ScmChar f) (cast ScmChar t)))))
274
275 (define-cproc %char-set-add! (dst::<char-set> src::<char-set>) Scm_CharSetAdd)
276 (define-cproc %char-set-ranges (cs::<char-set>) Scm_CharSetRanges)
277 (define-cproc %char-set-predefined (num::<fixnum>) Scm_GetStandardCharSet)
278
279 (define-cproc %char-set-case-fold! (cs::<char-set>) Scm_CharSetCaseFold)
280
281 (define-cproc %char-set-dump (cs::<char-set>) ::<void>
282 (Scm_CharSetDump cs SCM_CUROUT))