return #f not #\nul

v6.3-exception
Matthew Butterick 7 years ago
parent 8a11393b03
commit 51ec447991

@ -17,14 +17,15 @@
(define/contract (char text pos) (define/contract (char text pos)
((is-a?/c text%) (or/c exact-nonnegative-integer? #f) . -> . (or/c char? #f)) ((is-a?/c text%) (or/c exact-nonnegative-integer? #f) . -> . (or/c char? #f))
(and pos (send text get-character pos))) (and pos (let ([c (send text get-character pos)])
(if (char=? #\nul c) #f c))))
(module+ test (module+ test
(check-equal? (char t 0) #\f) (check-equal? (char t 0) #\f)
(check-equal? (char t 2) #\o) (check-equal? (char t 2) #\o)
(check-equal? (char t 3) #\newline) (check-equal? (char t 3) #\newline)
(check-equal? (char t 10) #\m) (check-equal? (char t 10) #\m)
(check-equal? (char t 11) #\nul)) (check-equal? (char t 11) #f))
(define/contract (line text pos) (define/contract (line text pos)
((is-a?/c text%) (or/c exact-nonnegative-integer? #f) . -> . exact-nonnegative-integer?) ((is-a?/c text%) (or/c exact-nonnegative-integer? #f) . -> . exact-nonnegative-integer?)
@ -41,13 +42,15 @@
((is-a?/c text%) exact-nonnegative-integer? . -> . (or/c (listof char?) #f)) ((is-a?/c text%) exact-nonnegative-integer? . -> . (or/c (listof char?) #f))
(and (and
(valid-line? text line) (valid-line? text line)
(for/list ([pos (in-range (line-start text line) (add1 (line-end text line)))]) (for*/list ([pos (in-range (line-start text line) (add1 (line-end text line)))]
(char text pos)))) [c (in-value (char text pos))]
#:when c)
c)))
(module+ test (module+ test
(check-equal? (line-chars t 0) '(#\f #\o #\o #\newline)) (check-equal? (line-chars t 0) '(#\f #\o #\o #\newline))
(check-equal? (line-chars t 1) '(#\space #\a #\r #\newline)) (check-equal? (line-chars t 1) '(#\space #\a #\r #\newline))
(check-equal? (line-chars t 2) '(#\space #\space #\m #\nul)) (check-equal? (line-chars t 2) '(#\space #\space #\m))
(check-equal? (line-chars t 3) #f)) (check-equal? (line-chars t 3) #f))
@ -155,8 +158,9 @@
(check-equal? (line-indent t 3) #f)) (check-equal? (line-indent t 3) #f))
(define (count-char text c [start 0] [end (send text last-position)]) (define (count-char text c [start 0] [end (send text last-position)])
(for/sum ([pos (in-range start (add1 end))] (for*/sum ([pos (in-range start (add1 end))]
#:when ((char text pos) . char=? . c)) [d (in-value (char text pos))]
#:when (and d (d . char=? . c)))
1)) 1))
(module+ test (module+ test
@ -173,6 +177,7 @@
(define (space-char? x) (char=? x #\space)) (define (space-char? x) (char=? x #\space))
(define/contract (apply-indenter indenter t-or-str) (define/contract (apply-indenter indenter t-or-str)
(procedure? (or/c (is-a?/c text%) string?) . -> . string?) (procedure? (or/c (is-a?/c text%) string?) . -> . string?)
(define t (if (string? t-or-str) (str->text t-or-str) t-or-str)) (define t (if (string? t-or-str) (str->text t-or-str) t-or-str))

Loading…
Cancel
Save