Skip to content

Commit

Permalink
html.entities: faster html-escape by going through string once.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjbq7 committed Sep 29, 2015
1 parent 887d988 commit 08051d9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
2 changes: 2 additions & 0 deletions extra/html/entities/entities-tests.factor
Expand Up @@ -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
35 changes: 25 additions & 10 deletions extra/html/entities/entities.factor
@@ -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

Expand Down

0 comments on commit 08051d9

Please sign in to comment.