| 1 |
;;; Copyright (C) 2008 Abdulaziz Ghuloum, R. Kent Dybvig |
| 2 |
;;; Copyright (C) 2006,2007 Abdulaziz Ghuloum |
| 3 |
;;; |
| 4 |
;;; Permission is hereby granted, free of charge, to any person obtaining a |
| 5 |
;;; copy of this software and associated documentation files (the "Software"), |
| 6 |
;;; to deal in the Software without restriction, including without limitation |
| 7 |
;;; the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 |
;;; and/or sell copies of the Software, and to permit persons to whom the |
| 9 |
;;; Software is furnished to do so, subject to the following conditions: |
| 10 |
;;; |
| 11 |
;;; The above copyright notice and this permission notice shall be included in |
| 12 |
;;; all copies or substantial portions of the Software. |
| 13 |
;;; |
| 14 |
;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
;;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
;;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 17 |
;;; THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
;;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 |
;;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 |
;;; DEALINGS IN THE SOFTWARE. |
| 21 |
|
| 22 |
(import |
| 23 |
(ikarus) |
| 24 |
(unicode-data)) |
| 25 |
|
| 26 |
(include "extract-common.ss") |
| 27 |
|
| 28 |
(define ptr-bytes 4) |
| 29 |
(define code-point-limit #x110000) |
| 30 |
(define-table (make-table table-ref table-set! table-ref-code) |
| 31 |
(make-vector vector-ref vector-set!) |
| 32 |
code-point-limit #x40 #x40) |
| 33 |
|
| 34 |
(define (extract-range str) |
| 35 |
(define (find-char c s) |
| 36 |
(let f ([i 0] [n (string-length s)]) |
| 37 |
(cond |
| 38 |
[(= i n) #f] |
| 39 |
[(char=? (string-ref s i) c) i] |
| 40 |
[else (f (+ i 1) n)]))) |
| 41 |
(cond |
| 42 |
[(find-char #\. str) => |
| 43 |
(lambda (i) |
| 44 |
(cons |
| 45 |
(hex->num (substring str 0 i)) |
| 46 |
(hex->num (substring str (+ i 2) (string-length str)))))] |
| 47 |
[else (let ([n (hex->num str)]) (cons n n))])) |
| 48 |
|
| 49 |
; fixnum field laid out as follows: |
| 50 |
; bits 0-5: category number |
| 51 |
; bits 6-9: wordbreak property |
| 52 |
; bits 10-17: combining class |
| 53 |
; bits 18-29: case/type property bits |
| 54 |
|
| 55 |
(define-syntax define-bitfields |
| 56 |
(lambda (x) |
| 57 |
(define construct-name |
| 58 |
(lambda (template-identifier . args) |
| 59 |
(datum->syntax template-identifier |
| 60 |
(string->symbol |
| 61 |
(apply string-append |
| 62 |
(map (lambda (x) (format "~a" (syntax->datum x))) |
| 63 |
args)))))) |
| 64 |
(define extract |
| 65 |
(lambda (fld* bit def*) |
| 66 |
(define (enumerate ls) |
| 67 |
(let f ([ls ls] [i 0]) |
| 68 |
(cond |
| 69 |
[(null? ls) '()] |
| 70 |
[else (cons i (f (cdr ls) (+ i 1)))]))) |
| 71 |
(assert (< bit (fixnum-width))) |
| 72 |
(if (null? fld*) |
| 73 |
def* |
| 74 |
(syntax-case (car fld*) (flag enumeration integer) |
| 75 |
[(flag name) (identifier? #'name) |
| 76 |
(extract (cdr fld*) (+ bit 1) |
| 77 |
#`((define name #,(fxsll 1 bit)) #,@def*))] |
| 78 |
[(enumeration name id ...) |
| 79 |
(and (identifier? #'name) (for-all identifier? #'(id ...))) |
| 80 |
(let ([width (bitwise-length (length #'(id ...)))]) |
| 81 |
(with-syntax ([name-shift (construct-name #'name #'name "-shift")] |
| 82 |
[name-mask (construct-name #'name #'name "-mask")]) |
| 83 |
(extract (cdr fld*) (+ bit width) |
| 84 |
#`((define name-shift #,bit) |
| 85 |
(define name-mask #,(fx- (fxsll 1 width) 1)) |
| 86 |
#,@(map (lambda (id val) #`(define #,id #,val)) |
| 87 |
#'(id ...) |
| 88 |
(enumerate #'(id ...))) |
| 89 |
#,@def*))))] |
| 90 |
[(integer name width) (identifier? #'name) |
| 91 |
(let ([width (syntax->datum #'width)]) |
| 92 |
(with-syntax ([name-shift (construct-name #'name #'name "-shift")] |
| 93 |
[name-mask (construct-name #'name #'name "-mask")]) |
| 94 |
(extract (cdr fld*) (+ bit width) |
| 95 |
#`((define name-shift #,bit) |
| 96 |
(define name-mask #,(fx- (fxsll 1 width) 1)) |
| 97 |
#,@def*))))])))) |
| 98 |
(syntax-case x () |
| 99 |
[(_ fld ...) |
| 100 |
#`(begin #,@(extract #'(fld ...) 0 #'()))]))) |
| 101 |
|
| 102 |
(define-bitfields |
| 103 |
(flag cased-property) |
| 104 |
(flag case-ignorable-property) |
| 105 |
(flag constituent-property) |
| 106 |
(flag uppercase-property) |
| 107 |
(flag lowercase-property) |
| 108 |
(flag titlecase-property) |
| 109 |
(flag alphabetic-property) |
| 110 |
(flag numeric-property) |
| 111 |
(flag whitespace-property) |
| 112 |
(enumeration category Lu-cat Ll-cat Lt-cat Lm-cat Lo-cat |
| 113 |
Mn-cat Mc-cat Me-cat Nd-cat Nl-cat No-cat Pc-cat Pd-cat |
| 114 |
Ps-cat Pe-cat Pi-cat Pf-cat Po-cat Sm-cat Sc-cat Sk-cat |
| 115 |
So-cat Zs-cat Zl-cat Zp-cat Cc-cat Cf-cat Cs-cat Co-cat |
| 116 |
Cn-cat) |
| 117 |
; default wb-other-property must be zero, so must be listed first |
| 118 |
(enumeration wbproperty wb-other-property wb-aletter-property |
| 119 |
wb-numeric-property wb-katakana-property |
| 120 |
wb-extend-property wb-format-property wb-midnum-property |
| 121 |
wb-midletter-property wb-midnumlet-property |
| 122 |
wb-extendnumlet-property wb-cr-property wb-lf-property |
| 123 |
wb-newline-property) |
| 124 |
(integer combining-class 8)) |
| 125 |
|
| 126 |
;;; Uppercase = Lu + Other_Uppercase |
| 127 |
;;; Lowercase = Ll + Other_Lowercase |
| 128 |
;;; Titlecase = Lt |
| 129 |
;;; Alphabetic = Lu + Ll + Lt + Lm + Lo + Nl + Other_Alphabetic |
| 130 |
;;; Numeric = ??? |
| 131 |
;;; White_Space = |
| 132 |
|
| 133 |
;;; cased property: |
| 134 |
;;; D120: A character C is defined to be cased if and only if C has the |
| 135 |
;;; Lowercase or Uppercase property or has a General_Category value of |
| 136 |
;;; Titlecase_Letter |
| 137 |
;;; |
| 138 |
;;; case-ignorable property: |
| 139 |
;;; D121 A character C is defined to be case-ignorable if C has the |
| 140 |
;;; value MidLetter or the value MidNumLet for the Word_Break property |
| 141 |
;;; or its General_Category is one of Nonspacing_Mark (Mn), |
| 142 |
;;; Enclosing_Mark (Me), Format (Cf), Modifier_Letter (Lm), or |
| 143 |
;;; Modifier_Symbol (Sk). |
| 144 |
|
| 145 |
(define name->wbprop |
| 146 |
(lambda (name) |
| 147 |
(case (string->symbol name) |
| 148 |
[(ALetter) (fxsll wb-aletter-property wbproperty-shift)] |
| 149 |
[(Numeric) (fxsll wb-numeric-property wbproperty-shift)] |
| 150 |
[(Katakana) (fxsll wb-katakana-property wbproperty-shift)] |
| 151 |
[(MidLetter) (fxior (fxsll wb-midletter-property wbproperty-shift) case-ignorable-property)] |
| 152 |
[(Extend) (fxsll wb-extend-property wbproperty-shift)] |
| 153 |
[(Format) (fxsll wb-format-property wbproperty-shift)] |
| 154 |
[(MidNum) (fxsll wb-midnum-property wbproperty-shift)] |
| 155 |
[(MidNumLet) (fxior (fxsll wb-midnumlet-property wbproperty-shift) case-ignorable-property)] |
| 156 |
[(ExtendNumLet) (fxsll wb-extendnumlet-property wbproperty-shift)] |
| 157 |
[(CR) (fxsll wb-cr-property wbproperty-shift)] |
| 158 |
[(LF) (fxsll wb-lf-property wbproperty-shift)] |
| 159 |
[(Newline) (fxsll wb-newline-property wbproperty-shift)] |
| 160 |
[else (error 'name->wbprop "unexpected property ~a" name)]))) |
| 161 |
|
| 162 |
(define proplist-properties |
| 163 |
`(["Other_Uppercase" ,uppercase-property] |
| 164 |
["Other_Lowercase" ,lowercase-property] |
| 165 |
["Other_Alphabetic" ,alphabetic-property] |
| 166 |
["White_Space" ,whitespace-property])) |
| 167 |
|
| 168 |
(define categories |
| 169 |
;;; 30 categories |
| 170 |
`([Lu ,(+ (fxsll Lu-cat category-shift) constituent-property uppercase-property alphabetic-property) "Letter, Uppercase"] |
| 171 |
[Ll ,(+ (fxsll Ll-cat category-shift) constituent-property lowercase-property alphabetic-property) "Letter, Lowercase"] |
| 172 |
[Lt ,(+ (fxsll Lt-cat category-shift) constituent-property titlecase-property alphabetic-property cased-property) "Letter, Titlecase"] |
| 173 |
[Lm ,(+ (fxsll Lm-cat category-shift) constituent-property alphabetic-property case-ignorable-property) "Letter, Modifier"] |
| 174 |
[Lo ,(+ (fxsll Lo-cat category-shift) constituent-property alphabetic-property) "Letter, Other"] |
| 175 |
[Mn ,(+ (fxsll Mn-cat category-shift) constituent-property case-ignorable-property) "Mark, Nonspacing"] |
| 176 |
[Mc ,(+ (fxsll Mc-cat category-shift) ) "Mark, Spacing Combining"] |
| 177 |
[Me ,(+ (fxsll Me-cat category-shift) case-ignorable-property) "Mark, Enclosing"] |
| 178 |
[Nd ,(+ (fxsll Nd-cat category-shift) ) "Number, Decimal Digit"] |
| 179 |
[Nl ,(+ (fxsll Nl-cat category-shift) constituent-property alphabetic-property) "Number, Letter"] |
| 180 |
[No ,(+ (fxsll No-cat category-shift) constituent-property) "Number, Other"] |
| 181 |
[Pc ,(+ (fxsll Pc-cat category-shift) constituent-property) "Punctuation, Connector"] |
| 182 |
[Pd ,(+ (fxsll Pd-cat category-shift) constituent-property) "Punctuation, Dash"] |
| 183 |
[Ps ,(+ (fxsll Ps-cat category-shift) ) "Punctuation, Open"] |
| 184 |
[Pe ,(+ (fxsll Pe-cat category-shift) ) "Punctuation, Close"] |
| 185 |
[Pi ,(+ (fxsll Pi-cat category-shift) ) "Punctuation, Initial quote"] |
| 186 |
[Pf ,(+ (fxsll Pf-cat category-shift) ) "Punctuation, Final quote"] |
| 187 |
[Po ,(+ (fxsll Po-cat category-shift) constituent-property) "Punctuation, Other"] |
| 188 |
[Sm ,(+ (fxsll Sm-cat category-shift) constituent-property) "Symbol, Math"] |
| 189 |
[Sc ,(+ (fxsll Sc-cat category-shift) constituent-property) "Symbol, Currency"] |
| 190 |
[Sk ,(+ (fxsll Sk-cat category-shift) constituent-property case-ignorable-property) "Symbol, Modifier"] |
| 191 |
[So ,(+ (fxsll So-cat category-shift) constituent-property) "Symbol, Other"] |
| 192 |
[Zs ,(+ (fxsll Zs-cat category-shift) ) "Separator, Space"] |
| 193 |
[Zl ,(+ (fxsll Zl-cat category-shift) ) "Separator, Line"] |
| 194 |
[Zp ,(+ (fxsll Zp-cat category-shift) ) "Separator, Paragraph"] |
| 195 |
[Cc ,(+ (fxsll Cc-cat category-shift) ) "Other, Control"] |
| 196 |
[Cf ,(+ (fxsll Cf-cat category-shift) case-ignorable-property) "Other, Format"] |
| 197 |
[Cs ,(+ (fxsll Cs-cat category-shift) ) "Other, Surrogate"] |
| 198 |
[Co ,(+ (fxsll Co-cat category-shift) constituent-property) "Other, Private Use"] |
| 199 |
[Cn ,(+ (fxsll Cn-cat category-shift) ) "Other, Not Assigned"] |
| 200 |
)) |
| 201 |
|
| 202 |
(define (category/flags x) |
| 203 |
(cond |
| 204 |
[(assq x categories) => cadr] |
| 205 |
[else (error 'category/flags "invalid cat ~s" x)])) |
| 206 |
|
| 207 |
(define (make-cats-table ls) |
| 208 |
(let f ([i 1] [st (car ls)] [ls (cdr ls)] [ac '()]) |
| 209 |
(cond |
| 210 |
[(null? ls) (reverse (cons (cons i st) ac))] |
| 211 |
[(equal? (cdar ls) (cdr st)) (f (+ i 1) st (cdr ls) ac)] |
| 212 |
[else (f 1 (car ls) (cdr ls) (cons (cons i st) ac))]))) |
| 213 |
|
| 214 |
(define (string-suffix? s1 s2) |
| 215 |
(let ([n1 (string-length s1)] [n2 (string-length s2)]) |
| 216 |
(and (>= n1 n2) (string=? (substring s1 (- n1 n2) n1) s2)))) |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
; create table, placing all in category Cn until proven otherwise |
| 221 |
(let ([tbl (make-table (category/flags 'Cn))]) |
| 222 |
(define (setprop n prop) (table-set! tbl n prop)) |
| 223 |
(define (getprop n) (table-ref tbl n)) |
| 224 |
;;; interesting parts of each element in UnicodeData.txt are: |
| 225 |
;;; field0: the character index, numeric |
| 226 |
;;; field2: the category, symbolic |
| 227 |
;;; field3: the combining class (0-255) |
| 228 |
;;; field8: if set, then the char has the numeric property |
| 229 |
;;; field12: if set, then the char has upper-case mapping and is thus cased |
| 230 |
;;; field13: if set, then the char has lower-case mapping and is thus cased |
| 231 |
(let f ([ls (get-unicode-data "UNIDATA/UnicodeData.txt")]) |
| 232 |
(unless (null? ls) |
| 233 |
(let ([x (car ls)] [ls (cdr ls)]) |
| 234 |
(let ([n (hex->num (list-ref x 0))] |
| 235 |
[cclass (string->number (list-ref x 3))] |
| 236 |
[cat/flags (category/flags (string->symbol (list-ref x 2)))] |
| 237 |
[num (if (string=? (list-ref x 8) "") 0 numeric-property)] |
| 238 |
[cased (if (and (string=? (list-ref x 12) "") (string=? (list-ref x 13) "")) |
| 239 |
0 cased-property)]) |
| 240 |
(let ([props (fxior num cased |
| 241 |
(fxsll cclass combining-class-shift) |
| 242 |
cat/flags)]) |
| 243 |
(if (string-suffix? (list-ref x 1) "First>") |
| 244 |
(let ([y (car ls)] [ls (cdr ls)]) |
| 245 |
(unless (string-suffix? (list-ref y 1) "Last>") |
| 246 |
(error #f "expected entry marked Last following entry marked First for ~x" n)) |
| 247 |
(let ([m (hex->num (list-ref y 0))]) |
| 248 |
(do ([n n (fx+ n 1)]) |
| 249 |
((fx> n m)) |
| 250 |
(setprop n props))) |
| 251 |
(f ls)) |
| 252 |
(begin (setprop n props) (f ls)))))))) |
| 253 |
;;; interesting parts of each element in WordBreakProperty.txt are: |
| 254 |
;;; field0: the character index, numeric |
| 255 |
;;; field1: the word-break property |
| 256 |
(for-each |
| 257 |
(lambda (x) |
| 258 |
(let ([range (extract-range (list-ref x 0))]) |
| 259 |
(let f ([i (car range)] [j (cdr range)]) |
| 260 |
(unless (> i j) |
| 261 |
(let ([prop (getprop i)]) |
| 262 |
(unless (fx= (fxand (fxsra prop wbproperty-shift) wbproperty-mask) 0) |
| 263 |
(error #f "multiple word break properties found for ~x" i)) |
| 264 |
(setprop i (fxior prop (name->wbprop (list-ref x 1)))) |
| 265 |
(f (+ i 1) j)))))) |
| 266 |
(get-unicode-data "UNIDATA/WordBreakProperty.txt")) |
| 267 |
;;; interesting parts of each element in PropList.txt are: |
| 268 |
;;; field0: range of character indices |
| 269 |
;;; field1: property name |
| 270 |
(for-each |
| 271 |
(lambda (x) |
| 272 |
(let ([range (extract-range (list-ref x 0))] |
| 273 |
[name (list-ref x 1)]) |
| 274 |
(cond |
| 275 |
[(assoc name proplist-properties) => |
| 276 |
(lambda (a) |
| 277 |
(let ([n (cadr a)]) |
| 278 |
(let f ([i (car range)] [j (cdr range)]) |
| 279 |
(unless (> i j) |
| 280 |
(setprop i (fxlogor (getprop i) n)) |
| 281 |
(f (+ i 1) j)))))]))) |
| 282 |
(get-unicode-data "UNIDATA/PropList.txt")) |
| 283 |
(commonize* tbl) |
| 284 |
(with-output-to-file* "unicode-charinfo.ss" |
| 285 |
(lambda () |
| 286 |
(parameterize ([print-graph #t]) |
| 287 |
(pretty-print |
| 288 |
`(module ($char-constituent? $char-upper-case? $char-lower-case? $char-title-case? $char-alphabetic? |
| 289 |
$char-numeric? $char-whitespace? $char-cased? $char-case-ignorable? $char-category |
| 290 |
$wb-aletter? $wb-numeric? $wb-katakana? $wb-extend? $wb-format? $wb-midnum? $wb-midletter? |
| 291 |
$wb-midnumlet? $wb-extendnumlet? $char-combining-class $char-dump) |
| 292 |
(define category-mask ,category-mask) |
| 293 |
(define unicode-category-table ',tbl) |
| 294 |
(define unicode-category-names |
| 295 |
',(list->vector (map car categories))) |
| 296 |
(define table-ref ,table-ref-code) |
| 297 |
(define (getprop n) (table-ref unicode-category-table n)) |
| 298 |
(define $char-constituent? |
| 299 |
(lambda (c) |
| 300 |
(fxlogtest (getprop (char->integer c)) ,constituent-property))) |
| 301 |
(define $char-upper-case? |
| 302 |
(lambda (c) |
| 303 |
(fxlogtest (getprop (char->integer c)) ,uppercase-property))) |
| 304 |
(define $char-lower-case? |
| 305 |
(lambda (c) |
| 306 |
(fxlogtest (getprop (char->integer c)) ,lowercase-property))) |
| 307 |
(define $char-title-case? |
| 308 |
(lambda (c) |
| 309 |
(fxlogtest (getprop (char->integer c)) ,titlecase-property))) |
| 310 |
(define $char-alphabetic? |
| 311 |
(lambda (c) |
| 312 |
(fxlogtest (getprop (char->integer c)) ,alphabetic-property))) |
| 313 |
(define $char-numeric? |
| 314 |
(lambda (c) |
| 315 |
(fxlogtest (getprop (char->integer c)) ,numeric-property))) |
| 316 |
(define $char-whitespace? |
| 317 |
(lambda (c) |
| 318 |
(fxlogtest (getprop (char->integer c)) ,whitespace-property))) |
| 319 |
(define $char-cased? |
| 320 |
(lambda (c) |
| 321 |
(fxlogtest (getprop (char->integer c)) ,cased-property))) |
| 322 |
(define $char-case-ignorable? |
| 323 |
(lambda (c) |
| 324 |
(fxlogtest (getprop (char->integer c)) ,case-ignorable-property))) |
| 325 |
(define (wb prop) |
| 326 |
(lambda (c) |
| 327 |
(fx= (fxand |
| 328 |
(fxsra |
| 329 |
(getprop (char->integer c)) |
| 330 |
,wbproperty-shift) |
| 331 |
,wbproperty-mask) |
| 332 |
prop))) |
| 333 |
(define $wb-aletter? (wb ,wb-aletter-property)) |
| 334 |
(define $wb-numeric? (wb ,wb-numeric-property)) |
| 335 |
(define $wb-katakana? (wb ,wb-katakana-property)) |
| 336 |
(define $wb-extend? (wb ,wb-extend-property)) |
| 337 |
(define $wb-format? (wb ,wb-format-property)) |
| 338 |
(define $wb-midnum? (wb ,wb-midnum-property)) |
| 339 |
(define $wb-midletter? (wb ,wb-midletter-property)) |
| 340 |
(define $wb-midnumlet? (wb ,wb-midnumlet-property)) |
| 341 |
(define $wb-extendnumlet? (wb ,wb-extendnumlet-property)) |
| 342 |
(define $char-combining-class |
| 343 |
(lambda (c) |
| 344 |
(fxand (fxsra (getprop (char->integer c)) ,combining-class-shift) |
| 345 |
,combining-class-mask))) |
| 346 |
(define $char-category |
| 347 |
(lambda (c) |
| 348 |
(vector-ref unicode-category-names |
| 349 |
(fxand (fxsra (getprop (char->integer c)) ,category-shift) |
| 350 |
,category-mask)))) |
| 351 |
(define $char-dump |
| 352 |
(lambda (c) |
| 353 |
(define (list-true . args) (remq #f args)) |
| 354 |
(list-true |
| 355 |
(and ($char-constituent? c) 'constituent) |
| 356 |
(and ($char-upper-case? c) 'upper-case) |
| 357 |
(and ($char-lower-case? c) 'lower-case) |
| 358 |
(and ($char-title-case? c) 'title-case) |
| 359 |
(and ($char-alphabetic? c) 'alphabetic) |
| 360 |
(and ($char-numeric? c) 'whitespace) |
| 361 |
(and ($char-whitespace? c) 'whitespace) |
| 362 |
(and ($char-cased? c) 'cased) |
| 363 |
(and ($char-case-ignorable? c) 'case-ignorable) |
| 364 |
(and ($wb-aletter? c) 'aletter) |
| 365 |
(and ($wb-numeric? c) 'numeric) |
| 366 |
(and ($wb-katakana? c) 'katakana) |
| 367 |
(and ($wb-extend? c) 'extend) |
| 368 |
(and ($wb-format? c) 'format) |
| 369 |
(and ($wb-midnum? c) 'midnum) |
| 370 |
(and ($wb-midletter? c) 'midletter) |
| 371 |
(and ($wb-midnumlet? c) 'midnumlet) |
| 372 |
(and ($wb-extendnumlet? c) 'extendnumlet) |
| 373 |
`(combining-class ,($char-combining-class c)) |
| 374 |
($char-category c)))))))))) |
| 375 |
|
| 376 |
(printf "Happy Happy Joy Joy ~s\n" (sizeof cache)) |