fix enum printer for small enumerations

This commit is contained in:
Robby Findler 2014-11-26 11:35:26 -06:00
parent c79a5573f9
commit 3e9f23a2c8
2 changed files with 37 additions and 29 deletions

View File

@ -205,38 +205,45 @@
[(#f) display] [(#f) display]
[else (lambda (p port) (print p port mode))])) [else (lambda (p port) (print p port mode))]))
(display "#<enum" port) (display "#<enum" port)
(parameterize ([enum-printing (+ (enum-printing) 1)]) (define the-size (enum-size enum))
(let loop ([i 0] [chars 0]) (define more-to-go?
;; chars is an approximation on how much (parameterize ([enum-printing (+ (enum-printing) 1)])
;; we've printed so far. (let loop ([i 0] [chars 0])
(when (<= chars 20) ;; chars is an approximation on how much
(define ele (from-nat enum i)) ;; we've printed so far.
(define sp (open-output-string))
(recur ele sp)
(define s (get-output-string sp))
(cond (cond
[(equal? s "") [(not (< i the-size)) #f]
;; if any enumeration values print as empty [(<= chars 20)
;; strings, then we just give up so as to avoid (define ele (from-nat enum i))
;; 'i' never incrementing and never terminating (define sp (open-output-string))
(display ">" port)] (recur ele sp)
[else (define s (get-output-string sp))
(if (zero? i)
(display ": " port)
(display " " port))
;; only print twice up to depth 2 in order to avoid bad
;; algorithmic behavior (so enumerations of enumerations
;; of enumerations might look less beautiful in drracket)
(cond (cond
[(<= (enum-printing) 2) [(equal? s "")
(display s port)] ;; if any enumeration values print as empty
;; strings, then we just give up so as to avoid
;; 'i' never incrementing and never terminating
#t]
[else [else
(recur ele port)]) (if (zero? i)
(display ": " port)
(loop (+ i 1) (display " " port))
(+ chars (string-length s) 1))]))))
(display "...>" port))]) ;; only print twice up to depth 2 in order to avoid bad
;; algorithmic behavior (so enumerations of enumerations
;; of enumerations might look less beautiful in drracket)
(cond
[(<= (enum-printing) 2)
(display s port)]
[else
(recur ele port)])
(loop (+ i 1)
(+ chars (string-length s) 1))])]
[else #t]))))
(if more-to-go?
(display "...>" port)
(display ">" port)))])
;; size : enum a -> Nat or +Inf ;; size : enum a -> Nat or +Inf
(define (size e) (define (size e)

View File

@ -468,6 +468,7 @@
(check-equal? (to-str nat/e #t) "#<enum: 0 1 2 3 4 5 6 7 8 9 10...>") (check-equal? (to-str nat/e #t) "#<enum: 0 1 2 3 4 5 6 7 8 9 10...>")
(check-equal? (to-str (cons/e nat/e nat/e) #t) "#<enum: '(0 . 0) '(0 . 1) '(1 . 0)...>") (check-equal? (to-str (cons/e nat/e nat/e) #t) "#<enum: '(0 . 0) '(0 . 1) '(1 . 0)...>")
(check-equal? (to-str (cons/e nat/e nat/e) #f) "#<enum: (0 . 0) (0 . 1) (1 . 0)...>") (check-equal? (to-str (cons/e nat/e nat/e) #f) "#<enum: (0 . 0) (0 . 1) (1 . 0)...>")
(check-equal? (to-str (const/e 0) #t) "#<enum: 0>")
;; just check that it doesn't crash when we get deep nesting ;; just check that it doesn't crash when we get deep nesting
;; (checks that we end up in the case that just uses the string ;; (checks that we end up in the case that just uses the string