factor/basis/escape-strings/escape-strings.factor

22 lines
696 B
Factor
Raw Normal View History

! 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 ] }
[ 2drop f ]
} case
] each 2drop ;
: escape-string ( str -- str' )
"]]" over subseq? [
dup #escapes ?1+ char: = <repetition>
[ "[" dup surround ] [ "]" dup surround ] bi surround
] [
"[[" "]]" surround
] if ;