improve ->string

pull/2/head
Matthew Butterick 11 years ago
parent ad72822c04
commit a5e1ee9290

@ -24,24 +24,25 @@
;; general way of coercing to string ;; general way of coercing to string
(define+provide/contract (->string x) (define+provide/contract (->string x)
(any/c . -> . string?) (any/c . -> . string?)
(try (if (string? x)
(cond x ; fast exit for strings
[(string? x) x] (try
[(equal? '() x) ""] (cond
[(symbol? x) (symbol->string x)] [(equal? '() x) ""]
[(number? x) (number->string x)] [(symbol? x) (symbol->string x)]
[(url? x) (->string (->path x))] ; todo: a url is more than just a path-string ... it has character encoding issues [(number? x) (number->string x)]
[(path? x) (path->string x)] [(url? x) (->string (->path x))] ; todo: a url is more than just a path-string ... it has character encoding issues
[(char? x) (format "~a" x)] [(path? x) (path->string x)]
[else (error)]) ; put this last so other xexprish things don't get caught [(char? x) (format "~a" x)]
(except [exn:fail? (make-coercion-error-handler 'string x)]))) [else (error)]) ; put this last so other xexprish things don't get caught
(except [exn:fail? (make-coercion-error-handler 'string x)]))))
;; general way of coercing to html ;; general way of coercing to html
(define+provide/contract (->html x) (define+provide/contract (->html x)
(any/c . -> . string?) (any/c . -> . string?)
(try (xexpr->string x) (try (xexpr->string x)
(except [exn:fail? (make-coercion-error-handler 'html x)]))) (except [exn:fail? (make-coercion-error-handler 'html x)])))
;; general way of coercing to symbol ;; general way of coercing to symbol

Loading…
Cancel
Save