Translate uses of display with write-string.

Eli recommends that write-string should be faster in http://lists.racket-lang.org/dev/archive/2012-November/010764.html
This commit is contained in:
Danny Yoo 2012-11-08 09:40:48 -07:00
parent daca1c0d5b
commit 641e855786
2 changed files with 23 additions and 20 deletions

View File

@ -168,15 +168,19 @@
(define (escape x table)
(regexp-replace* table x replace-escaped))
(define (display/escape x table out)
(cond [(regexp-match table x)
(display (escape x table) out)]
;; write-string/excape: String Regexp Output-Port -> Void
;; Writes the string to the output port, with a fast-path
;; that tries to avoid using string escapes unless necessary.
(define (write-string/escape str table out)
(cond [(regexp-match table str)
(write-string (escape str table) out)]
[else
(display x out)]))
(write-string str out)]))
(provide escape
display/escape
write-string/escape
escape-table
escape-attribute-table
lowercase-symbol

View File

@ -133,44 +133,43 @@
(values (cadr x) (cddr x))
(values null (cdr x))))
; Write opening tag
(display "<" out)
(write-string "<" out)
(display name out)
; Write attributes
(for ([att (in-list attrs)])
(display " " out)
(write-string " " out)
(display (car att) out)
(display "=" out)
(display "\"" out)
(display/escape (cadr att) escape-attribute-table out)
(display "\"" out))
(write-string "=\"" out)
(write-string/escape (cadr att) escape-attribute-table out)
(write-string "\"" out))
; Write end of opening tag
(if (and (null? content)
(case short
[(always) #t]
[(never) #f]
[else (memq (lowercase-symbol name) short)]))
(display " />" out)
(write-string " />" out)
(begin
(display ">" out)
(write-string ">" out)
; Write body
(for ([xe (in-list content)])
(loop xe))
; Write closing tag
(display "</" out)
(write-string "</" out)
(display name out)
(display ">" out)))]
(write-string ">" out)))]
; PCData
[(string? x)
(display/escape x escape-table out)]
(write-string/escape x escape-table out)]
; Entities
[(symbol? x)
(display "&" out)
(write-string "&" out)
(display x out)
(display ";" out)]
(write-string ";" out)]
[(valid-char? x)
(display "&#" out)
(write-string "&#" out)
(display x out)
(display ";" out)]
(write-string ";" out)]
; Embedded XML
[(cdata? x)
(write-xml-cdata x 0 void out)]