Debian Codegraph @

%3a130/130.body.scm

raw symbols SCIP
1 ;;; Copyright (C) William D Clinger (2016).
2 ;;;
3 ;;; Permission is hereby granted, free of charge, to any person
4 ;;; obtaining a copy of this software and associated documentation
5 ;;; files (the "Software"), to deal in the Software without
6 ;;; restriction, including without limitation the rights to use,
7 ;;; copy, modify, merge, publish, distribute, sublicense, and/or
8 ;;; sell copies of the Software, and to permit persons to whom the
9 ;;; Software is furnished to do so, subject to the following
10 ;;; conditions:
11 ;;;
12 ;;; The above copyright notice and this permission notice shall be
13 ;;; included in all copies or substantial portions of the Software.
14 ;;;
15 ;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 ;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17 ;;; OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 ;;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 ;;; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 ;;; WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 ;;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 ;;; OTHER DEALINGS IN THE SOFTWARE.
23
24 ;;; For convenient interoperation with SRFI 13,
25 ;;; cursors ought to be the same as indexes.
26
27 ;;; Some of the FIXME comments mark procedures that really ought
28 ;;; to do more checking for "is an error" situations.
29
30 ;; added for chez scheme support
31 (define string->vector
32 (lambda (str)
33 (let ([len (string-length str)])
34 (let ([v (make-vector len)])
35 (do ([i (fx- len 1) (fx- i 1)])
36 ((fx=? i -1))
37 (vector-set! v i (string-ref str i)))
38 v))))
39
40 (define (string-cursor? x)
41 (and (and (integer? x) (exact? x))
42 (>= x 0)))
43
44 (define (string-cursor-start s) 0)
45 (define (string-cursor-end s) (string-length s))
46 (define (string-cursor-next s curs) (+ curs 1)) ; FIXME
47 (define (string-cursor-prev s curs) (- curs 1)) ; FIXME
48 (define (string-cursor-forward s curs n) (+ curs n)) ; FIXME
49 (define (string-cursor-back s curs n) (- curs n)) ; FIXME
50 (define (string-cursor=? curs1 curs2) (= curs1 curs2))
51 (define (string-cursor<? curs1 curs2) (< curs1 curs2))
52 (define (string-cursor>? curs1 curs2) (> curs1 curs2))
53 (define (string-cursor<=? curs1 curs2) (<= curs1 curs2))
54 (define (string-cursor>=? curs1 curs2) (>= curs1 curs2))
55 (define (string-cursor-diff s start end) (- end start)) ; FIXME
56 (define (string-cursor->index s curs) curs)
57 (define (string-index->cursor s idx) idx)
58
59 (define string->list/cursors string->list)
60 (define string->vector/cursors string->vector)
61
62 (define string-ref/cursor string-ref)
63 (define substring/cursors substring)
64 (define string-copy/cursors string-copy)
65
66 ;;; The SRFI 13 procedures return #f sometimes, so they can't be the same
67 ;;; even if cursors are the same as indexes.
68 ;;; Furthermore string-index-right and string-skip-right return the
69 ;;; successor of the cursor for the character found.
70
71 (define string-index
72 (case-lambda
73 ((s pred)
74 (string-index s pred 0 (string-length s)))
75 ((s pred start)
76 (string-index s pred start (string-length s)))
77 ((s pred start end)
78 (or (srfi-13:string-index s pred start end)
79 end))))
80
81 (define string-index-right
82 (case-lambda
83 ((s pred)
84 (string-index-right s pred 0 (string-length s)))
85 ((s pred start)
86 (string-index-right s pred start (string-length s)))
87 ((s pred start end)
88 (let ((i (srfi-13:string-index-right s pred start end)))
89 (if i (+ i 1) start)))))
90
91 (define (string-skip s pred . rest)
92 (apply string-index s (lambda (x) (not (pred x))) rest))
93
94 (define (string-skip-right s pred . rest)
95 (apply string-index-right s (lambda (x) (not (pred x))) rest))
96
97 ;;; FIXME: inefficient
98
99 (define string-contains-right
100 (case-lambda
101 ((s1 s2)
102 (string-contains-right s1 s2 0 (string-length s1) 0 (string-length s2)))
103 ((s1 s2 start1)
104 (string-contains-right s1 s2
105 start1 (string-length s1) 0 (string-length s2)))
106 ((s1 s2 start1 end1)
107 (string-contains-right s1 s2 start1 end1 0 (string-length s2)))
108 ((s1 s2 start1 end1 start2)
109 (string-contains-right s1 s2 start1 end1 start2 (string-length s2)))
110 ((s1 s2 start1 end1 start2 end2)
111 (if (= start2 end2)
112 end1
113 (let loop ((i #f)
114 (j (string-contains s1 s2 start1 end1 start2 end2)))
115 (if (and j (< j end1))
116 (loop j (string-contains s1 s2 (+ j 1) end1 start2 end2))
117 i))))))
118
119 (define string-for-each-cursor
120 (case-lambda
121 ((proc s)
122 (string-for-each-cursor proc s 0 (string-length s)))
123 ((proc s start)
124 (string-for-each-cursor proc s start (string-length s)))
125 ((proc s start end)
126 (do ((i start (+ i 1)))
127 ((>= i end))
128 (proc i)))))
129
130 (define string-replicate
131 (case-lambda
132 ((s from to start end)
133 (string-replicate (substring s start end) from to))
134 ((s from to start)
135 (string-replicate (substring s start (string-length s)) from to))
136 ((s from to)
137 (let* ((n (- to from))
138 (len (string-length s)))
139 (cond ((= n 0)
140 "")
141 ((or (< n 0)
142 (= len 0))
143 (assertion-violation 'string-replicate
144 "unexpected arguments"
145 s from to))
146 (else
147 (let* ((from (mod from len)) ; make from non-negative
148 (to (+ from n)))
149 (do ((replicates '() (cons s replicates))
150 (replicates-length 0 (+ replicates-length len)))
151 ((>= replicates-length to)
152 (substring (apply string-append replicates)
153 from to))))))))))
154
155 (define string-split
156 (case-lambda
157 ((s delimiter grammar limit start end)
158 (string-split (substring s start end) delimiter grammar limit))
159 ((s delimiter grammar limit start)
160 (string-split (substring s start (string-length s))
161 delimiter grammar limit))
162 ((s delimiter)
163 (string-split s delimiter 'infix #f))
164 ((s delimiter grammar)
165 (string-split s delimiter grammar #f))
166 ((s delimiter grammar limit)
167 (define (complain)
168 (assertion-violation 'string-split
169 "unexpected arguments"
170 s delimiter grammar limit))
171 (let* ((limit (or limit (string-length s)))
172 (splits
173 (cond ((= 0 (string-length delimiter))
174 (string-split-into-characters s limit))
175 (else
176 (string-split-using-word s delimiter limit)))))
177 (case grammar
178 ((infix strict-infix)
179 (if (= 0 (string-length s))
180 (if (eq? grammar 'infix)
181 '()
182 (complain))
183 splits))
184 ((prefix)
185 (if (and (pair? splits)
186 (= 0 (string-length (car splits))))
187 (cdr splits)
188 splits))
189 ((suffix)
190 (if (and (pair? splits)
191 (= 0 (string-length (car (last-pair splits)))))
192 (reverse (cdr (reverse splits)))
193 splits))
194 (else
195 (complain)))))))
196
197 (define (string-split-into-characters s limit)
198 (let ((n (string-length s)))
199 (cond ((> n (+ limit 1))
200 (append (string-split-into-characters (substring s 0 limit) limit)
201 (substring s limit n)))
202 (else
203 (map string (string->list s))))))
204
205 ;;; FIXME: inefficient
206
207 (define (string-split-using-word s sep limit)
208 (cond ((= 0 limit)
209 (list s))
210 (else
211 (let ((i (string-contains s sep)))
212 (if i
213 (cons (substring s 0 i)
214 (string-split-using-word
215 (substring s (+ i (string-length sep)) (string-length s))
216 sep
217 (- limit 1)))
218 (list s))))))
219
220 (define (string-remove pred s . args)
221 (apply string-filter
222 (lambda (c) (not (pred c)))
223 s
224 args))
225