2017-09-16 18:21:31 -04:00
|
|
|
! Copyright (C) 2017 John Benediktsson, Doug Coleman.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
|
|
|
USING: combinators kernel math math.order sequences ;
|
|
|
|
IN: escape-strings
|
|
|
|
|
|
|
|
! TODO: Move the "]]" subseq? check into the each loop logic
|
|
|
|
: #escapes ( str -- n/f )
|
|
|
|
[ f f f ] dip [
|
|
|
|
{
|
|
|
|
{ char: = [ [ dup [ 1 + ] when ] bi@ ] }
|
2017-09-24 14:12:49 -04:00
|
|
|
{ char: \] [ [ [ 0 or ] 2dip [ max ] curry dip ] when* 0 ] }
|
2017-09-16 18:21:31 -04:00
|
|
|
[ 2drop f ]
|
|
|
|
} case
|
|
|
|
] each 2drop ;
|
|
|
|
|
|
|
|
: escape-string ( str -- str' )
|
|
|
|
"]]" over subseq? [
|
|
|
|
dup #escapes ?1+ char: = <repetition>
|
|
|
|
[ "[" dup surround ] [ "]" dup surround ] bi surround
|
|
|
|
] [
|
|
|
|
"[[" "]]" surround
|
|
|
|
] if ;
|