escape-strings: Fix case where string ends in ] or ]=
Add escape-strings for nested strings.modern-harvey2
parent
86c086bafc
commit
5582ea1b02
|
@ -8,5 +8,18 @@ IN: escape-strings.tests
|
||||||
{ "[=[]]]=]" } [ "]]" escape-string ] unit-test
|
{ "[=[]]]=]" } [ "]]" escape-string ] unit-test
|
||||||
|
|
||||||
{ "[===[]]]==][=[=]=]]===]" } [ "]]]==][=[=]=]" escape-string ] unit-test
|
{ "[===[]]]==][=[=]=]]===]" } [ "]]]==][=[=]=]" escape-string ] unit-test
|
||||||
{ "[[[=[=]=]]]" } [ "[=[=]=]" escape-string ] unit-test
|
{ "[==[[=[=]=]]==]" } [ "[=[=]=]" escape-string ] unit-test
|
||||||
{ "[[[a[]]" } [ "[a[" escape-string ] unit-test
|
{ "[[[a[]]" } [ "[a[" escape-string ] unit-test
|
||||||
|
|
||||||
|
{ "[=[ab]]=]" } [ "ab]" escape-string ] unit-test
|
||||||
|
|
||||||
|
{ "[==[[=[abcd]]=]]==]" } [ { "abcd]" } escape-strings ] unit-test
|
||||||
|
{ "[==[[=[abcd]]]=]]==]" } [ { "abcd]]" } escape-strings ] unit-test
|
||||||
|
|
||||||
|
{ "[==[]]ab]=]==]" } [ "]]ab]=" escape-string ] unit-test
|
||||||
|
{ "[=[]]ab]==]=]" } [ "]]ab]==" escape-string ] unit-test
|
||||||
|
{ "[=[]]ab]===]=]" } [ "]]ab]===" escape-string ] unit-test
|
||||||
|
|
||||||
|
{ "[[]ab]=]]" } [ "]ab]=" escape-string ] unit-test
|
||||||
|
{ "[[]ab]==]]" } [ "]ab]==" escape-string ] unit-test
|
||||||
|
{ "[[]ab]===]]" } [ "]ab]===" escape-string ] unit-test
|
||||||
|
|
|
@ -1,22 +1,41 @@
|
||||||
! Copyright (C) 2017 John Benediktsson, Doug Coleman.
|
! Copyright (C) 2017 John Benediktsson, Doug Coleman.
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: combinators kernel math math.order sequences ;
|
USING: combinators kernel math math.order math.statistics
|
||||||
|
sequences sequences.extras sets ;
|
||||||
IN: escape-strings
|
IN: escape-strings
|
||||||
|
|
||||||
! TODO: Move the "]]" subseq? check into the each loop logic
|
: find-escapes ( str -- set )
|
||||||
: #escapes ( str -- n/f )
|
[ HS{ } clone 0 0 ] dip
|
||||||
[ f f f ] dip [
|
[
|
||||||
{
|
{
|
||||||
{ char: = [ [ dup [ 1 + ] when ] bi@ ] }
|
{ char: \] [ 1 + dup 2 = [ drop over adjoin 0 1 ] when ] }
|
||||||
{ char: \] [ [ [ 0 or ] 2dip [ max ] curry dip ] when* 0 ] }
|
{ char: = [ dup 1 = [ [ 1 + ] dip ] when ] }
|
||||||
[ 2drop f ]
|
[ 3drop 0 0 ]
|
||||||
} case
|
} case
|
||||||
] each 2drop ;
|
] each 0 > [ over adjoin ] [ drop ] if ;
|
||||||
|
|
||||||
|
: lowest-missing ( set -- min )
|
||||||
|
members dup [ = not ] find-index
|
||||||
|
[ nip ] [ drop length ] if ;
|
||||||
|
|
||||||
|
: escape-string* ( str n -- str' )
|
||||||
|
char: = <repetition>
|
||||||
|
[ "[" dup surround ] [ "]" dup surround ] bi surround ;
|
||||||
|
|
||||||
: escape-string ( str -- str' )
|
: escape-string ( str -- str' )
|
||||||
"]]" over subseq? [
|
dup find-escapes lowest-missing escape-string* ;
|
||||||
dup #escapes ?1+ char: = <repetition>
|
|
||||||
[ "[" dup surround ] [ "]" dup surround ] bi surround
|
: escape-strings ( strs -- str )
|
||||||
|
dup [ find-escapes ] map
|
||||||
|
[
|
||||||
|
[ lowest-missing ] map
|
||||||
|
[ escape-string* ] 2map concat
|
||||||
] [
|
] [
|
||||||
"[[" "]]" surround
|
[ ] [ union ] map-reduce
|
||||||
] if ;
|
] bi
|
||||||
|
dup cardinality 0 = [
|
||||||
|
drop 1
|
||||||
|
] [
|
||||||
|
members minmax nip 2 +
|
||||||
|
] if
|
||||||
|
escape-string* ;
|
||||||
|
|
Loading…
Reference in New Issue