From 51ec447991128807446535b8a19a877d6dd19227 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Wed, 20 Jun 2018 15:54:43 -0700 Subject: [PATCH] return #f not #\nul --- beautiful-racket-lib/br/indent.rkt | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/beautiful-racket-lib/br/indent.rkt b/beautiful-racket-lib/br/indent.rkt index f1d6de0..690af84 100644 --- a/beautiful-racket-lib/br/indent.rkt +++ b/beautiful-racket-lib/br/indent.rkt @@ -17,14 +17,15 @@ (define/contract (char text pos) ((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 (check-equal? (char t 0) #\f) (check-equal? (char t 2) #\o) (check-equal? (char t 3) #\newline) (check-equal? (char t 10) #\m) - (check-equal? (char t 11) #\nul)) + (check-equal? (char t 11) #f)) (define/contract (line text pos) ((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)) (and (valid-line? text line) - (for/list ([pos (in-range (line-start text line) (add1 (line-end text line)))]) - (char text pos)))) + (for*/list ([pos (in-range (line-start text line) (add1 (line-end text line)))] + [c (in-value (char text pos))] + #:when c) + c))) (module+ test (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 2) '(#\space #\space #\m #\nul)) + (check-equal? (line-chars t 2) '(#\space #\space #\m)) (check-equal? (line-chars t 3) #f)) @@ -155,9 +158,10 @@ (check-equal? (line-indent t 3) #f)) (define (count-char text c [start 0] [end (send text last-position)]) - (for/sum ([pos (in-range start (add1 end))] - #:when ((char text pos) . char=? . c)) - 1)) + (for*/sum ([pos (in-range start (add1 end))] + [d (in-value (char text pos))] + #:when (and d (d . char=? . c))) + 1)) (module+ test (check-equal? (count-char t #\f) 1) @@ -173,6 +177,7 @@ (define (space-char? x) (char=? x #\space)) + (define/contract (apply-indenter indenter t-or-str) (procedure? (or/c (is-a?/c text%) string?) . -> . string?) (define t (if (string? t-or-str) (str->text t-or-str) t-or-str))