html.entities: faster html-escape by going through string once.
parent
887d988a70
commit
08051d9ba8
|
@ -6,6 +6,8 @@ IN: html.entities
|
||||||
{ "<foo>" } [ "<foo>" html-unescape ] unit-test
|
{ "<foo>" } [ "<foo>" html-unescape ] unit-test
|
||||||
{ "This &that" } [ "This &that" html-unescape ] unit-test
|
{ "This &that" } [ "This &that" html-unescape ] unit-test
|
||||||
{ "This &that" } [ "This &that" html-unescape ] unit-test
|
{ "This &that" } [ "This &that" html-unescape ] unit-test
|
||||||
|
{ "a&b<c>d" } [ "a&b<c>d" html-unescape ] unit-test
|
||||||
|
|
||||||
{ "&" } [ "&" html-escape ] unit-test
|
{ "&" } [ "&" html-escape ] unit-test
|
||||||
{ "<foo>" } [ "<foo>" html-escape ] unit-test
|
{ "<foo>" } [ "<foo>" html-escape ] unit-test
|
||||||
|
{ "a&b<c>d" } [ "a&b<c>d" html-escape ] unit-test
|
||||||
|
|
|
@ -1,20 +1,35 @@
|
||||||
! Copyright (C) 2014 John Benediktsson
|
! Copyright (C) 2014 John Benediktsson
|
||||||
! See http://factorcode.org/license.txt for BSD license
|
! See http://factorcode.org/license.txt for BSD license
|
||||||
|
|
||||||
USING: assocs combinators.short-circuit kernel locals make math
|
USING: assocs combinators.short-circuit kernel literals locals
|
||||||
math.order math.parser math.ranges regexp sequences splitting
|
make math math.order math.parser math.ranges regexp sequences
|
||||||
strings ;
|
splitting strings ;
|
||||||
|
|
||||||
IN: html.entities
|
IN: html.entities
|
||||||
|
|
||||||
|
<PRIVATE
|
||||||
|
|
||||||
|
CONSTANT: html-escapes {
|
||||||
|
{ CHAR: & "&" }
|
||||||
|
{ CHAR: < "<" }
|
||||||
|
{ CHAR: > ">" }
|
||||||
|
{ CHAR: \" """ }
|
||||||
|
{ CHAR: ' "'" }
|
||||||
|
}
|
||||||
|
|
||||||
|
: 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 )
|
: html-escape ( str -- newstr )
|
||||||
{
|
[
|
||||||
{ "&" "&" }
|
[ dup next-escape dup ] [ escape, ] while 2drop ,
|
||||||
{ "<" "<" }
|
] { } make dup length 1 > [ concat ] [ first ] if ;
|
||||||
{ ">" ">" }
|
|
||||||
{ "\"" """ }
|
|
||||||
{ "'" "'" }
|
|
||||||
} [ replace ] assoc-each ;
|
|
||||||
|
|
||||||
<PRIVATE
|
<PRIVATE
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue