| 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 |
(library (ikarus.unicode) |
| 23 |
(export |
| 24 |
unicode-printable-char? |
| 25 |
char-upcase char-downcase char-titlecase char-foldcase |
| 26 |
char-whitespace? char-lower-case? char-upper-case? |
| 27 |
char-title-case? char-numeric? |
| 28 |
char-alphabetic? char-general-category char-ci<? char-ci<=? |
| 29 |
char-ci=? char-ci>? char-ci>=? string-upcase string-downcase |
| 30 |
string-foldcase string-titlecase string-ci<? string-ci<=? |
| 31 |
string-ci=? string-ci>? string-ci>=? string-normalize-nfd |
| 32 |
string-normalize-nfkd string-normalize-nfc string-normalize-nfkc ) |
| 33 |
(import |
| 34 |
(ikarus include) |
| 35 |
(except (ikarus) |
| 36 |
unicode-printable-char? |
| 37 |
char-upcase char-downcase char-titlecase char-foldcase |
| 38 |
char-whitespace? char-lower-case? char-upper-case? |
| 39 |
char-title-case? char-numeric? |
| 40 |
char-alphabetic? char-general-category char-ci<? char-ci<=? |
| 41 |
char-ci=? char-ci>? char-ci>=? string-upcase string-downcase |
| 42 |
string-foldcase string-titlecase string-ci<? string-ci<=? |
| 43 |
string-ci=? string-ci>? string-ci>=? string-normalize-nfd |
| 44 |
string-normalize-nfkd string-normalize-nfc string-normalize-nfkc )) |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
(module UNSAFE |
| 49 |
(fx< fx<= fx> fx>= fx= fx+ fx- |
| 50 |
fxior fxand fxsra fxsll fxzero? |
| 51 |
integer->char char->integer |
| 52 |
char<? char<=? char=? char>? char>=? |
| 53 |
string-ref string-set! string-length |
| 54 |
vector-ref vector-set! vector-length) |
| 55 |
(import |
| 56 |
(rename (ikarus system $strings) |
| 57 |
($string-length string-length) |
| 58 |
($string-ref string-ref) |
| 59 |
($string-set! string-set!)) |
| 60 |
(rename (ikarus system $vectors) |
| 61 |
($vector-length vector-length) |
| 62 |
($vector-ref vector-ref) |
| 63 |
($vector-set! vector-set!)) |
| 64 |
(rename (ikarus system $chars) |
| 65 |
($char->fixnum char->integer) |
| 66 |
($fixnum->char integer->char) |
| 67 |
($char< char<?) |
| 68 |
($char<= char<=?) |
| 69 |
($char= char=?) |
| 70 |
($char> char>?) |
| 71 |
($char>= char>=?)) |
| 72 |
(rename (ikarus system $fx) |
| 73 |
($fxzero? fxzero?) |
| 74 |
($fxsra fxsra) |
| 75 |
($fxsll fxsll) |
| 76 |
($fxlogor fxior) |
| 77 |
($fxlogand fxand) |
| 78 |
($fx+ fx+) |
| 79 |
($fx- fx-) |
| 80 |
($fx< fx<) |
| 81 |
($fx> fx>) |
| 82 |
($fx>= fx>=) |
| 83 |
($fx<= fx<=) |
| 84 |
($fx= fx=)))) |
| 85 |
|
| 86 |
(module |
| 87 |
(unicode-printable-char? |
| 88 |
char-upcase char-downcase char-titlecase char-foldcase |
| 89 |
char-whitespace? char-lower-case? char-upper-case? |
| 90 |
char-title-case? char-numeric? |
| 91 |
char-alphabetic? char-general-category char-ci<? char-ci<=? |
| 92 |
char-ci=? char-ci>? char-ci>=? string-upcase string-downcase |
| 93 |
string-foldcase string-titlecase string-ci<? string-ci<=? |
| 94 |
string-ci=? string-ci>? string-ci>=? string-normalize-nfd |
| 95 |
string-normalize-nfkd string-normalize-nfc string-normalize-nfkc) |
| 96 |
|
| 97 |
(import UNSAFE) |
| 98 |
(define (fxlogtest x y) |
| 99 |
(not (fxzero? (fxand x y)))) |
| 100 |
(define (char- x y) |
| 101 |
(fx- (char->integer x) (char->integer y))) |
| 102 |
|
| 103 |
(include "unicode/unicode-char-cases.ss") |
| 104 |
(include "unicode/unicode-charinfo.ss") |
| 105 |
|
| 106 |
(define-syntax define-char-op |
| 107 |
(syntax-rules () |
| 108 |
[(_ name unsafe-op) |
| 109 |
(define name |
| 110 |
(lambda (c) |
| 111 |
(if (char? c) |
| 112 |
(unsafe-op c) |
| 113 |
(assertion-violation 'name "not a char" c))))])) |
| 114 |
|
| 115 |
(define-char-op char-upcase $char-upcase) |
| 116 |
(define-char-op char-downcase $char-downcase) |
| 117 |
(define-char-op char-titlecase $char-titlecase) |
| 118 |
(define-char-op char-foldcase $char-foldcase) |
| 119 |
(define-char-op char-whitespace? $char-whitespace?) |
| 120 |
(define-char-op char-lower-case? $char-lower-case?) |
| 121 |
(define-char-op char-upper-case? $char-upper-case?) |
| 122 |
(define-char-op char-title-case? $char-title-case?) |
| 123 |
(define-char-op char-numeric? $char-numeric?) |
| 124 |
(define-char-op unicode-printable-char? $char-constituent?) |
| 125 |
(define-char-op char-alphabetic? $char-alphabetic?) |
| 126 |
(define-char-op char-general-category $char-category) |
| 127 |
|
| 128 |
(define (do-char-cmp a ls cmp who) |
| 129 |
(if (char? a) |
| 130 |
(let f ([a ($char-foldcase a)] [ls ls]) |
| 131 |
(cond |
| 132 |
[(null? ls) #t] |
| 133 |
[else |
| 134 |
(let ([b (car ls)]) |
| 135 |
(if (char? b) |
| 136 |
(let ([b ($char-foldcase b)]) |
| 137 |
(if (cmp a b) |
| 138 |
(f b (cdr ls)) |
| 139 |
(let f ([ls (cdr ls)]) |
| 140 |
(if (null? ls) |
| 141 |
#f |
| 142 |
(if (char? (car ls)) |
| 143 |
(f (cdr ls)) |
| 144 |
(assertion-violation who |
| 145 |
"not a char" (car ls))))))) |
| 146 |
(assertion-violation who "not a char" b)))])) |
| 147 |
(assertion-violation who "not a char" a))) |
| 148 |
|
| 149 |
(define-syntax define-char-cmp |
| 150 |
(syntax-rules () |
| 151 |
[(_ name cmp) |
| 152 |
(define name |
| 153 |
(case-lambda |
| 154 |
[(c1 c2) |
| 155 |
(if (char? c1) |
| 156 |
(if (char? c2) |
| 157 |
(cmp ($char-foldcase c1) ($char-foldcase c2)) |
| 158 |
(assertion-violation 'name "not a char" c2)) |
| 159 |
(assertion-violation 'name "not a char" c1))] |
| 160 |
[(c1 . rest) |
| 161 |
(do-char-cmp c1 rest (lambda (x y) (cmp x y)) 'name)]))])) |
| 162 |
|
| 163 |
(define-char-cmp char-ci<? char<?) |
| 164 |
(define-char-cmp char-ci<=? char<=?) |
| 165 |
(define-char-cmp char-ci=? char=?) |
| 166 |
(define-char-cmp char-ci>? char>?) |
| 167 |
(define-char-cmp char-ci>=? char>=?) |
| 168 |
|
| 169 |
(define (handle-special str ac) |
| 170 |
(define (chars ac n) |
| 171 |
(cond |
| 172 |
[(null? ac) n] |
| 173 |
[else |
| 174 |
(chars (cdr ac) |
| 175 |
(let f ([p (cdar ac)] [n n]) |
| 176 |
(cond |
| 177 |
[(pair? p) (f (cdr p) (fx+ n 1))] |
| 178 |
[else n])))])) |
| 179 |
(define (extend src ac src-len dst-len) |
| 180 |
(let f ([str str] [dst (make-string dst-len)] [i 0] [j 0] [ac (reverse ac)] [sigma* '()]) |
| 181 |
(cond |
| 182 |
[(null? ac) |
| 183 |
(string-copy! str i dst j (fx- src-len i)) |
| 184 |
(do-sigmas dst sigma*)] |
| 185 |
[else |
| 186 |
(let ([idx (caar ac)] [c* (cdar ac)] [ac (cdr ac)]) |
| 187 |
(let ([cnt (fx- idx i)]) |
| 188 |
(string-copy! str i dst j cnt) |
| 189 |
(let g ([str str] [dst dst] |
| 190 |
[i (fx+ i cnt)] [j (fx+ j cnt)] |
| 191 |
[ac ac] [c* c*]) |
| 192 |
(cond |
| 193 |
[(pair? c*) |
| 194 |
(string-set! dst j (car c*)) |
| 195 |
(g str dst i (fx+ j 1) ac (cdr c*))] |
| 196 |
[(char? c*) |
| 197 |
(string-set! dst j c*) |
| 198 |
(f str dst (fx+ i 1) (fx+ j 1) ac sigma*)] |
| 199 |
[else ; assume c* = sigma |
| 200 |
(f str dst (fx+ i 1) (fx+ j 1) ac (cons j sigma*))]))))]))) |
| 201 |
(define (do-sigmas str sigma*) |
| 202 |
(define nonfinal-sigma #\x3c3) |
| 203 |
(define final-sigma #\x3c2) |
| 204 |
(define (final? i) |
| 205 |
(define (scan i incr n) |
| 206 |
(and (not (fx= i n)) |
| 207 |
(or ($char-cased? (string-ref str i)) |
| 208 |
(and ($char-case-ignorable? (string-ref str i)) |
| 209 |
(scan (fx+ i incr) incr n))))) |
| 210 |
(and (scan (fx- i 1) -1 -1) (not (scan (fx+ i 1) +1 (string-length str))))) |
| 211 |
; scanning requires we have some character in place...guess nonfinal sigma |
| 212 |
(for-each (lambda (i) (string-set! str i nonfinal-sigma)) sigma*) |
| 213 |
(for-each (lambda (i) (when (final? i) (string-set! str i final-sigma))) sigma*) |
| 214 |
str) |
| 215 |
(let* ([src-len (string-length str)] |
| 216 |
[dst-len (chars ac src-len)]) |
| 217 |
(if (fx= dst-len src-len) |
| 218 |
(do-sigmas str (map car ac)) |
| 219 |
(extend str ac src-len dst-len)))) |
| 220 |
|
| 221 |
(define ($string-change-case str cvt-char) |
| 222 |
(let ([n (string-length str)]) |
| 223 |
(let f ([str str] [dst (make-string n)] [i 0] [n n] [ac '()]) |
| 224 |
(cond |
| 225 |
[(fx= i n) |
| 226 |
(if (null? ac) |
| 227 |
dst |
| 228 |
(handle-special dst ac))] |
| 229 |
[else |
| 230 |
(let ([c/ls (cvt-char (string-ref str i))]) |
| 231 |
(cond |
| 232 |
[(char? c/ls) |
| 233 |
(string-set! dst i c/ls) |
| 234 |
(f str dst (fx+ i 1) n ac)] |
| 235 |
[else |
| 236 |
(f str dst (fx+ i 1) n |
| 237 |
(cons (cons i c/ls) ac))]))])))) |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
(define-syntax define-string-op |
| 242 |
(syntax-rules () |
| 243 |
[(_ name unsafe-op) |
| 244 |
(define name |
| 245 |
(lambda (s) |
| 246 |
(if (string? s) |
| 247 |
(unsafe-op s) |
| 248 |
(assertion-violation 'name "not a string" s))))])) |
| 249 |
|
| 250 |
(define-string-op string-upcase |
| 251 |
(lambda (s) ($string-change-case s $str-upcase))) |
| 252 |
|
| 253 |
(define-string-op string-foldcase |
| 254 |
(lambda (s) ($string-change-case s $str-foldcase))) |
| 255 |
|
| 256 |
(define-string-op string-downcase |
| 257 |
(lambda (s) ($string-change-case s $str-downcase))) |
| 258 |
|
| 259 |
(define-string-op string-titlecase |
| 260 |
(lambda (str) |
| 261 |
(let* ((n (string-length str)) (dst (make-string n))) |
| 262 |
(define (trans s i c seen-cased? ac) |
| 263 |
(define (trans1 s i c/ls seen-cased? ac) |
| 264 |
(define (trans2 s i seen-cased? ac) |
| 265 |
(if (fx= i n) |
| 266 |
(handle-special dst ac) |
| 267 |
(s i seen-cased? ac))) |
| 268 |
(cond ((char? c/ls) |
| 269 |
(string-set! dst i c/ls) |
| 270 |
(trans2 s (fx+ i 1) seen-cased? ac)) |
| 271 |
(else |
| 272 |
(trans2 s (fx+ i 1) seen-cased? (cons (cons i c/ls) ac))))) |
| 273 |
(if seen-cased? |
| 274 |
(trans1 s i ($str-downcase c) #t ac) |
| 275 |
(if ($char-cased? c) |
| 276 |
(trans1 s i ($str-titlecase c) #t ac) |
| 277 |
(trans1 s i c #f ac)))) |
| 278 |
(define (s0 i ac) |
| 279 |
(let ((c (string-ref str i))) |
| 280 |
(cond |
| 281 |
(($wb-aletter? c) (trans sAletter i c #f ac)) |
| 282 |
(($wb-numeric? c) (trans sNumeric i c #f ac)) |
| 283 |
(($wb-katakana? c) (trans sKatakana i c #f ac)) |
| 284 |
(($wb-extendnumlet? c) (trans sExtendnumlet i c #f ac)) |
| 285 |
(else (string-set! dst i c) |
| 286 |
(let ((i (fx+ i 1))) |
| 287 |
(if (fx= i n) |
| 288 |
(handle-special dst ac) |
| 289 |
(s0 i ac))))))) |
| 290 |
(define (sAletter i seen-cased? ac) |
| 291 |
(let ((c (string-ref str i))) |
| 292 |
(cond |
| 293 |
((or ($wb-aletter? c) ($wb-extend? c) ($wb-format? c)) |
| 294 |
(trans sAletter i c seen-cased? ac)) |
| 295 |
((or ($wb-midletter? c) ($wb-midnumlet? c)) |
| 296 |
(trans sAletterMid i c seen-cased? ac)) |
| 297 |
(($wb-numeric? c) (trans sNumeric i c seen-cased? ac)) |
| 298 |
(($wb-extendnumlet? c) (trans sExtendnumlet i c seen-cased? ac)) |
| 299 |
(else (s0 i ac))))) |
| 300 |
(define (sAletterMid i seen-cased? ac) |
| 301 |
(let ((c (string-ref str i))) |
| 302 |
(cond |
| 303 |
((or ($wb-extend? c) ($wb-format? c)) |
| 304 |
(trans sAletterMid i c seen-cased? ac)) |
| 305 |
(($wb-aletter? c) (trans sAletter i c seen-cased? ac)) |
| 306 |
(else (s0 i ac))))) |
| 307 |
(define (sNumeric i seen-cased? ac) |
| 308 |
(let ((c (string-ref str i))) |
| 309 |
(cond |
| 310 |
((or ($wb-numeric? c) ($wb-extend? c) ($wb-format? c)) |
| 311 |
(trans sNumeric i c seen-cased? ac)) |
| 312 |
((or ($wb-midnum? c) ($wb-midnumlet? c)) |
| 313 |
(trans sNumericMid i c seen-cased? ac)) |
| 314 |
(($wb-aletter? c) |
| 315 |
(trans sAletter i c seen-cased? ac)) |
| 316 |
(($wb-extendnumlet? c) |
| 317 |
(trans sExtendnumlet i c seen-cased? ac)) |
| 318 |
(else (s0 i ac))))) |
| 319 |
(define (sNumericMid i seen-cased? ac) |
| 320 |
(let ((c (string-ref str i))) |
| 321 |
(cond |
| 322 |
((or ($wb-extend? c) ($wb-format? c)) |
| 323 |
(trans sNumericMid i c seen-cased? ac)) |
| 324 |
(($wb-numeric? c) (trans sNumeric i c seen-cased? ac)) |
| 325 |
(else (s0 i ac))))) |
| 326 |
(define (sKatakana i seen-cased? ac) |
| 327 |
(let ((c (string-ref str i))) |
| 328 |
(cond |
| 329 |
((or ($wb-katakana? c) ($wb-extend? c) ($wb-format? c)) |
| 330 |
(trans sKatakana i c seen-cased? ac)) |
| 331 |
(($wb-extendnumlet? c) (trans sExtendnumlet i c seen-cased? ac)) |
| 332 |
(else (s0 i ac))))) |
| 333 |
(define (sExtendnumlet i seen-cased? ac) |
| 334 |
(let ((c (string-ref str i))) |
| 335 |
(cond |
| 336 |
((or ($wb-extendnumlet? c) ($wb-extend? c) ($wb-format? c)) |
| 337 |
(trans sExtendnumlet i c seen-cased? ac)) |
| 338 |
(($wb-aletter? c) (trans sAletter i c seen-cased? ac)) |
| 339 |
(($wb-numeric? c) (trans sNumeric i c seen-cased? ac)) |
| 340 |
(($wb-katakana? c) (trans sKatakana i c seen-cased? ac)) |
| 341 |
(else (s0 i ac))))) |
| 342 |
(if (fx= n 0) dst (s0 0 '()))))) |
| 343 |
|
| 344 |
|
| 345 |
(define $string-ci=? ; two arguments, no string? checks |
| 346 |
(lambda (s1 s2) |
| 347 |
(let ([n1 (string-length s1)] [n2 (string-length s2)]) |
| 348 |
(if (fx= n1 0) |
| 349 |
(fx= n2 0) |
| 350 |
(and (not (fx= n2 0)) |
| 351 |
(let f ([i1 1] |
| 352 |
[i2 1] |
| 353 |
[c1* ($str-foldcase (string-ref s1 0))] |
| 354 |
[c2* ($str-foldcase (string-ref s2 0))]) |
| 355 |
(if (char? c1*) |
| 356 |
(if (char? c2*) |
| 357 |
(and (char=? c1* c2*) |
| 358 |
(if (fx= i1 n1) |
| 359 |
(fx= i2 n2) |
| 360 |
(and (not (fx= i2 n2)) |
| 361 |
(f (fx+ i1 1) (fx+ i2 1) |
| 362 |
($str-foldcase (string-ref s1 i1)) |
| 363 |
($str-foldcase (string-ref s2 i2)))))) |
| 364 |
(and (char=? c1* (car c2*)) |
| 365 |
(not (fx= i1 n1)) |
| 366 |
(f (fx+ i1 1) i2 |
| 367 |
($str-foldcase (string-ref s1 i1)) |
| 368 |
(cdr c2*)))) |
| 369 |
(if (char? c2*) |
| 370 |
(and (char=? (car c1*) c2*) |
| 371 |
(not (fx= i2 n2)) |
| 372 |
(f i1 (fx+ i2 1) (cdr c1*) |
| 373 |
($str-foldcase (string-ref s2 i2)))) |
| 374 |
(and (char=? (car c1*) (car c2*)) |
| 375 |
(f i1 i2 (cdr c1*) (cdr c2*))))))))))) |
| 376 |
|
| 377 |
(define $string-ci<? ; two arguments, no string? checks |
| 378 |
(lambda (s1 s2) |
| 379 |
(let ([n1 (string-length s1)] [n2 (string-length s2)]) |
| 380 |
(and (not (fx= n2 0)) |
| 381 |
(or (fx= n1 0) |
| 382 |
(let f ([i1 1] |
| 383 |
[i2 1] |
| 384 |
[c1* ($str-foldcase (string-ref s1 0))] |
| 385 |
[c2* ($str-foldcase (string-ref s2 0))]) |
| 386 |
(if (char? c1*) |
| 387 |
(if (char? c2*) |
| 388 |
(or (char<? c1* c2*) |
| 389 |
(and (char=? c1* c2*) |
| 390 |
(not (fx= i2 n2)) |
| 391 |
(or (fx= i1 n1) |
| 392 |
(f (fx+ i1 1) (fx+ i2 1) |
| 393 |
($str-foldcase (string-ref s1 i1)) |
| 394 |
($str-foldcase (string-ref s2 i2)))))) |
| 395 |
(or (char<? c1* (car c2*)) |
| 396 |
(and (char=? c1* (car c2*)) |
| 397 |
(or (fx= i1 n1) |
| 398 |
(f (fx+ i1 1) i2 |
| 399 |
($str-foldcase (string-ref s1 i1)) |
| 400 |
(cdr c2*)))))) |
| 401 |
(if (char? c2*) |
| 402 |
(or (char<? (car c1*) c2*) |
| 403 |
(and (char=? (car c1*) c2*) |
| 404 |
(not (fx= i2 n2)) |
| 405 |
(f i1 (fx+ i2 1) (cdr c1*) |
| 406 |
($str-foldcase (string-ref s2 i2))))) |
| 407 |
(or (char<? (car c1*) (car c2*)) |
| 408 |
(and (char=? (car c1*) (car c2*)) |
| 409 |
(f i1 i2 (cdr c1*) (cdr c2*)))))))))))) |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
(define (do-string-cmp a ls cmp who) |
| 414 |
(if (string? a) |
| 415 |
(let f ([a a] [ls ls]) |
| 416 |
(cond |
| 417 |
[(null? ls) #t] |
| 418 |
[else |
| 419 |
(let ([b (car ls)]) |
| 420 |
(if (string? b) |
| 421 |
(if (cmp a b) |
| 422 |
(f b (cdr ls)) |
| 423 |
(let f ([ls (cdr ls)]) |
| 424 |
(if (null? ls) |
| 425 |
#f |
| 426 |
(if (string? (car ls)) |
| 427 |
(f (cdr ls)) |
| 428 |
(assertion-violation who |
| 429 |
"not a string" (car ls))))))) |
| 430 |
(assertion-violation who "not a string" b))])) |
| 431 |
(assertion-violation who "not a string" a))) |
| 432 |
|
| 433 |
(define-syntax define-string-cmp |
| 434 |
(syntax-rules () |
| 435 |
[(_ name cmp) |
| 436 |
(define name |
| 437 |
(case-lambda |
| 438 |
[(s1 s2) |
| 439 |
(if (string? s1) |
| 440 |
(if (string? s2) |
| 441 |
(cmp s1 s2) |
| 442 |
(assertion-violation 'name "not a string" s2)) |
| 443 |
(assertion-violation 'name "not a string" s2))] |
| 444 |
[(s1 . rest) |
| 445 |
(do-string-cmp s1 rest cmp 'name)]))])) |
| 446 |
|
| 447 |
(define-string-cmp string-ci=? $string-ci=?) |
| 448 |
(define-string-cmp string-ci<? |
| 449 |
(lambda (s1 s2) ($string-ci<? s1 s2))) |
| 450 |
(define-string-cmp string-ci<=? |
| 451 |
(lambda (s1 s2) (not ($string-ci<? s2 s1)))) |
| 452 |
(define-string-cmp string-ci>=? |
| 453 |
(lambda (s1 s2) (not ($string-ci<? s1 s2)))) |
| 454 |
(define-string-cmp string-ci>? |
| 455 |
(lambda (s1 s2) ($string-ci<? s2 s1))) |
| 456 |
|
| 457 |
(module (hangul-sbase hangul-slimit $hangul-decomp |
| 458 |
hangul-lbase hangul-llimit |
| 459 |
hangul-vbase hangul-vlimit |
| 460 |
hangul-tbase hangul-tlimit |
| 461 |
hangul-vcount hangul-tcount) |
| 462 |
; adapted from UAX #15 |
| 463 |
(define SBase #xAC00) |
| 464 |
(define LBase #x1100) |
| 465 |
(define VBase #x1161) |
| 466 |
(define TBase #x11A7) |
| 467 |
(define LCount 19) |
| 468 |
(define VCount 21) |
| 469 |
(define TCount 28) |
| 470 |
(define NCount (* VCount TCount)) |
| 471 |
(define SCount (* LCount NCount)) |
| 472 |
(define hangul-sbase (integer->char SBase)) |
| 473 |
(define hangul-slimit (integer->char (+ SBase SCount -1))) |
| 474 |
(define hangul-lbase (integer->char LBase)) |
| 475 |
(define hangul-llimit (integer->char (+ LBase LCount -1))) |
| 476 |
(define hangul-vbase (integer->char VBase)) |
| 477 |
(define hangul-vlimit (integer->char (+ VBase VCount -1))) |
| 478 |
(define hangul-tbase (integer->char TBase)) |
| 479 |
(define hangul-tlimit (integer->char (+ TBase TCount -1))) |
| 480 |
(define hangul-vcount VCount) |
| 481 |
(define hangul-tcount TCount) |
| 482 |
(define ($hangul-decomp c) |
| 483 |
(let ([SIndex (char- c hangul-sbase)]) |
| 484 |
(let ([L (integer->char (fx+ LBase (fxdiv SIndex NCount)))] |
| 485 |
[V (integer->char (fx+ VBase (fxdiv (fxmod SIndex NCount) TCount)))] |
| 486 |
[adj (fxmod SIndex TCount)]) |
| 487 |
(if (fx= adj 0) |
| 488 |
(cons* L V) |
| 489 |
(cons* L V (integer->char (fx+ TBase adj)))))))) |
| 490 |
|
| 491 |
(define $decompose |
| 492 |
; might should optimize for sequences of ascii characters |
| 493 |
(lambda (s canonical?) |
| 494 |
(let ([n (string-length s)] [ac '()]) |
| 495 |
(define (canonical>? c1 c2) |
| 496 |
(fx> ($char-combining-class c1) ($char-combining-class c2))) |
| 497 |
(define (sort-and-flush comb*) |
| 498 |
(unless (null? comb*) |
| 499 |
(set! ac (append (list-sort canonical>? comb*) ac)))) |
| 500 |
(define ($char-decomp c) |
| 501 |
(if (and (char<=? hangul-sbase c) (char<=? c hangul-slimit)) |
| 502 |
($hangul-decomp c) |
| 503 |
(if canonical? |
| 504 |
($str-decomp-canon c) |
| 505 |
($str-decomp-compat c)))) |
| 506 |
(define (push-and-go c* c** i comb*) |
| 507 |
(if (char? c*) |
| 508 |
(go c* c** i comb*) |
| 509 |
(go (car c*) (cons (cdr c*) c**) i comb*))) |
| 510 |
(define (pop-and-go c** i comb*) |
| 511 |
(if (null? c**) |
| 512 |
(if (fx= i n) |
| 513 |
(sort-and-flush comb*) |
| 514 |
(go (string-ref s i) '() (fx+ i 1) comb*)) |
| 515 |
(push-and-go (car c**) (cdr c**) i comb*))) |
| 516 |
(define (go c c** i comb*) |
| 517 |
(let ([c* ($char-decomp c)]) |
| 518 |
(if (eq? c c*) ; should be eqv? |
| 519 |
(if (fxzero? ($char-combining-class c)) |
| 520 |
(begin |
| 521 |
(sort-and-flush comb*) |
| 522 |
(set! ac (cons c ac)) |
| 523 |
(pop-and-go c** i '())) |
| 524 |
(pop-and-go c** i (cons c comb*))) |
| 525 |
(push-and-go c* c** i comb*)))) |
| 526 |
(pop-and-go '() 0 '()) |
| 527 |
(list->string (reverse ac))))) |
| 528 |
|
| 529 |
(define $compose |
| 530 |
(let ([comp-table #f]) |
| 531 |
(define (lookup-composite c1 c2) |
| 532 |
(hashtable-ref comp-table (cons c1 c2) #f)) |
| 533 |
(define (init!) |
| 534 |
(set! comp-table |
| 535 |
(make-hashtable |
| 536 |
(lambda (x) |
| 537 |
(fxxor |
| 538 |
(fxsll (char->integer (car x)) 7) |
| 539 |
(char->integer (cdr x)))) |
| 540 |
(lambda (x y) |
| 541 |
(and (char=? (car x) (car y)) |
| 542 |
(char=? (cdr x) (cdr y)))))) |
| 543 |
(vector-for-each |
| 544 |
(lambda (c* c) (hashtable-set! comp-table c* c)) |
| 545 |
(car ($composition-pairs)) |
| 546 |
(cdr ($composition-pairs)))) |
| 547 |
(lambda (s) |
| 548 |
(unless comp-table (init!)) |
| 549 |
(let ([ac '()] [n (string-length s)]) |
| 550 |
(define (dump c acc) |
| 551 |
(set! ac (cons c ac)) |
| 552 |
(unless (null? acc) (set! ac (append acc ac)))) |
| 553 |
(define (s0 i) |
| 554 |
(unless (fx= i n) |
| 555 |
(let ([c (string-ref s i)]) |
| 556 |
(if (fxzero? ($char-combining-class c)) |
| 557 |
(s1 (fx+ i 1) c) |
| 558 |
(begin (set! ac (cons c ac)) (s0 (fx+ i 1))))))) |
| 559 |
(define (s1 i c) |
| 560 |
(if (fx= i n) |
| 561 |
(set! ac (cons c ac)) |
| 562 |
(let ([c1 (string-ref s i)]) |
| 563 |
(cond |
| 564 |
[(and (and (char<=? hangul-lbase c) |
| 565 |
(char<=? c hangul-llimit)) |
| 566 |
(and (char<=? hangul-vbase c1) |
| 567 |
(char<=? c1 hangul-vlimit))) |
| 568 |
(s1 (fx+ i 1) |
| 569 |
(let ([lindex (char- c hangul-lbase)] |
| 570 |
[vindex (char- c1 hangul-vbase)]) |
| 571 |
(integer->char |
| 572 |
(fx+ (char->integer hangul-sbase) |
| 573 |
(fx* (fx+ (fx* lindex hangul-vcount) vindex) |
| 574 |
hangul-tcount)))))] |
| 575 |
[(and (and (char<=? hangul-sbase c) |
| 576 |
(char<=? c hangul-slimit)) |
| 577 |
(and (char<=? hangul-tbase c1) |
| 578 |
(char<=? c1 hangul-tlimit)) |
| 579 |
(let ([sindex (char- c hangul-sbase)]) |
| 580 |
(fxzero? (fxmod sindex hangul-tcount)))) |
| 581 |
(let ([tindex (char- c1 hangul-tbase)]) |
| 582 |
(s1 (fx+ i 1) (integer->char (fx+ (char->integer c) tindex))))] |
| 583 |
[else (s2 i c -1 '())])))) |
| 584 |
(define (s2 i c class acc) |
| 585 |
(if (fx= i n) |
| 586 |
(dump c acc) |
| 587 |
(let ([c1 (string-ref s i)]) |
| 588 |
(let ([class1 ($char-combining-class c1)]) |
| 589 |
(cond |
| 590 |
[(and (fx< class class1) (lookup-composite c c1)) => |
| 591 |
(lambda (c) (s2 (fx+ i 1) c class acc))] |
| 592 |
[(fx= class1 0) |
| 593 |
(dump c acc) |
| 594 |
(s1 (fx+ i 1) c1)] |
| 595 |
[else (s2 (fx+ i 1) c class1 (cons c1 acc))]))))) |
| 596 |
(s0 0) |
| 597 |
(list->string (reverse ac)))))) |
| 598 |
|
| 599 |
(define-string-op string-normalize-nfd |
| 600 |
(lambda (s) ($decompose s #t))) |
| 601 |
|
| 602 |
(define-string-op string-normalize-nfkd |
| 603 |
(lambda (s) ($decompose s #f))) |
| 604 |
|
| 605 |
(define-string-op string-normalize-nfc |
| 606 |
(lambda (s) ($compose ($decompose s #t)))) |
| 607 |
|
| 608 |
(define-string-op string-normalize-nfkc |
| 609 |
(lambda (s) ($compose ($decompose s #f)))) |
| 610 |
|
| 611 |
)) |