Debian Codegraph @

scheme/unicode-data.ss

raw
1 ;;; Ikarus Scheme -- A compiler for R6RS Scheme.
2 ;;; Copyright (C) 2006,2007,2008 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 (unicode-data)
23 (export get-unicode-data)
24 (import (rnrs))
25
26 (define (find-semi/hash str i n)
27 (cond
28 [(or (fx=? i n) (memv (string-ref str i) '(#\; #\#))) i]
29 [else (find-semi/hash str (+ i 1) n)]))
30
31 (define (cleanup str)
32 (let ([lo
33 (let f ([i 0] [n (string-length str)])
34 (cond
35 [(= i n) n]
36 [(char=? #\space (string-ref str i))
37 (f (+ i 1) n)]
38 [else i]))]
39 [hi
40 (let f ([i (- (string-length str) 1)])
41 (cond
42 [(< i 0) i]
43 [(char=? #\space (string-ref str i))
44 (f (- i 1))]
45 [else (+ i 1)]))])
46 (if (> hi lo)
47 (substring str lo hi)
48 "")))
49
50 (define (split str)
51 (let f ([i 0] [n (string-length str)])
52 (cond
53 [(or (= i n) (memv (string-ref str i) '(#\#)))
54 '("")]
55 [else
56 (let ([j (find-semi/hash str i n)])
57 (cond
58 [(or (= j n) (memv (string-ref str i) '(#\#)))
59 (list (cleanup (substring str i j)))]
60 [else
61 (cons (cleanup (substring str i j))
62 (f (+ j 1) n))]))])))
63
64 (define (extract-uni-data)
65 (let f ([ls '()])
66 (let ([line (get-line (current-input-port))])
67 (cond
68 [(eof-object? line)
69 (reverse ls)]
70 [else
71 (let ([fields (split line)])
72 (if (or (null? fields) (equal? fields '("")))
73 (f ls)
74 (f (cons fields ls))))]))))
75
76 (define (get-unicode-data filename)
77 (with-input-from-file
78 filename
79 extract-uni-data)))