html.entities: faster html-escape by going through string once.

db4
John Benediktsson 2015-09-29 12:15:00 -07:00
parent 887d988a70
commit 08051d9ba8
2 changed files with 27 additions and 10 deletions

View File

@ -6,6 +6,8 @@ IN: html.entities
{ "<foo>" } [ "&lt;foo&gt;" html-unescape ] unit-test
{ "This &that" } [ "This &amp;that" html-unescape ] unit-test
{ "This &that" } [ "This &ampthat" html-unescape ] unit-test
{ "a&b<c>d" } [ "a&amp;b&lt;c&gt;d" html-unescape ] unit-test
{ "&amp;" } [ "&" html-escape ] unit-test
{ "&lt;foo&gt;" } [ "<foo>" html-escape ] unit-test
{ "a&amp;b&lt;c&gt;d" } [ "a&b<c>d" html-escape ] unit-test

View File

@ -1,20 +1,35 @@
! Copyright (C) 2014 John Benediktsson
! See http://factorcode.org/license.txt for BSD license
USING: assocs combinators.short-circuit kernel locals make math
math.order math.parser math.ranges regexp sequences splitting
strings ;
USING: assocs combinators.short-circuit kernel literals locals
make math math.order math.parser math.ranges regexp sequences
splitting strings ;
IN: html.entities
<PRIVATE
CONSTANT: html-escapes {
{ CHAR: & "&amp;" }
{ CHAR: < "&lt;" }
{ CHAR: > "&gt;" }
{ CHAR: \" "&quot;" }
{ CHAR: ' "&#39;" }
}
: next-escape ( seq -- i elt )
[ html-escapes key? ] find ;
: escape, ( seq i elt -- seq' )
[ [ head-slice , ] [ 1 + tail-slice ] 2bi ]
[ html-escapes at , ] bi* ;
PRIVATE>
: html-escape ( str -- newstr )
{
{ "&" "&amp;" }
{ "<" "&lt;" }
{ ">" "&gt;" }
{ "\"" "&quot;" }
{ "'" "&#39;" }
} [ replace ] assoc-each ;
[
[ dup next-escape dup ] [ escape, ] while 2drop ,
] { } make dup length 1 > [ concat ] [ first ] if ;
<PRIVATE