Add new intersects? word and use it in a few places instead of intersect empty?

db4
Slava Pestov 2009-01-12 01:51:38 -06:00
parent 9a3908e7dc
commit e6aa33ac12
8 changed files with 44 additions and 19 deletions

View File

@ -45,8 +45,8 @@ IN: http
: check-header-string ( str -- str ) : check-header-string ( str -- str )
#! http://en.wikipedia.org/wiki/HTTP_Header_Injection #! http://en.wikipedia.org/wiki/HTTP_Header_Injection
dup "\r\n\"" intersect empty? dup "\r\n\"" intersects?
[ "Header injection attack" throw ] unless ; [ "Header injection attack" throw ] when ;
: write-header ( assoc -- ) : write-header ( assoc -- )
>alist sort-keys [ >alist sort-keys [
@ -97,8 +97,8 @@ TUPLE: cookie name value version comment path domain expires max-age http-only s
] { } make ; ] { } make ;
: check-cookie-string ( string -- string' ) : check-cookie-string ( string -- string' )
dup "=;'\"\r\n" intersect empty? dup "=;'\"\r\n" intersects?
[ "Bad cookie name or value" throw ] unless ; [ "Bad cookie name or value" throw ] when ;
: unparse-cookie-value ( key value -- ) : unparse-cookie-value ( key value -- )
{ {

View File

@ -57,7 +57,7 @@ IN: regexp.dfa
dup dup
[ nfa-table>> final-states>> keys ] [ nfa-table>> final-states>> keys ]
[ dfa-table>> transitions>> states ] bi [ dfa-table>> transitions>> states ] bi
[ intersect empty? not ] with filter [ intersects? ] with filter
swap dfa-table>> final-states>> swap dfa-table>> final-states>>
[ conjoin ] curry each ; [ conjoin ] curry each ;

View File

@ -68,8 +68,8 @@ ERROR: bad-email-address email ;
: validate-address ( string -- string' ) : validate-address ( string -- string' )
#! Make sure we send funky stuff to the server by accident. #! Make sure we send funky stuff to the server by accident.
dup "\r\n>" intersect empty? dup "\r\n>" intersects?
[ bad-email-address ] unless ; [ bad-email-address ] when ;
: mail-from ( fromaddr -- ) : mail-from ( fromaddr -- )
validate-address validate-address
@ -170,8 +170,8 @@ M: plain-auth send-auth
ERROR: invalid-header-string string ; ERROR: invalid-header-string string ;
: validate-header ( string -- string' ) : validate-header ( string -- string' )
dup "\r\n" intersect empty? dup "\r\n" intersects?
[ invalid-header-string ] unless ; [ invalid-header-string ] when ;
: write-header ( key value -- ) : write-header ( key value -- )
[ validate-header write ] [ validate-header write ]

View File

@ -69,8 +69,8 @@ IN: validators
: v-one-line ( str -- str ) : v-one-line ( str -- str )
v-required v-required
dup "\r\n" intersect empty? dup "\r\n" intersects?
[ "must be a single line" throw ] unless ; [ "must be a single line" throw ] when ;
: v-one-word ( str -- str ) : v-one-word ( str -- str )
v-required v-required

View File

@ -13,6 +13,8 @@ $nl
{ $subsection diff } { $subsection diff }
{ $subsection intersect } { $subsection intersect }
{ $subsection union } { $subsection union }
"Set-theoretic predicates:"
{ $subsection intersects? }
{ $subsection subset? } { $subsection subset? }
{ $subsection set= } { $subsection set= }
"A word used to implement the above:" "A word used to implement the above:"
@ -104,9 +106,15 @@ HELP: union
{ diff intersect union } related-words { diff intersect union } related-words
HELP: intersects?
{ $values { "seq1" sequence } { "seq2" sequence } { "?" "a boolean" } }
{ $description "Tests if " { $snippet "seq1" } " and " { $snippet "seq2" } " have any elements in common." }
{ $notes "If one of the sequences is empty, the result is always " { $link f } "." } ;
HELP: subset? HELP: subset?
{ $values { "seq1" sequence } { "seq2" sequence } { "?" "a boolean" } } { $values { "seq1" sequence } { "seq2" sequence } { "?" "a boolean" } }
{ $description "Tests if every element of " { $snippet "seq1" } " is contained in " { $snippet "seq2" } "." } ; { $description "Tests if every element of " { $snippet "seq1" } " is contained in " { $snippet "seq2" } "." }
{ $notes "If " { $snippet "seq1" } " is empty, the result is always " { $link t } "." } ;
HELP: set= HELP: set=
{ $values { "seq1" sequence } { "seq2" sequence } { "?" "a boolean" } } { $values { "seq1" sequence } { "seq2" sequence } { "?" "a boolean" } }

View File

@ -21,3 +21,11 @@ IN: sets.tests
[ V{ 1 2 3 } ] [ V{ 1 2 3 } ]
[ 3 V{ 1 3 2 } clone [ adjoin ] keep ] unit-test [ 3 V{ 1 3 2 } clone [ adjoin ] keep ] unit-test
[ t ] [ { 1 2 } { 1 3 } intersects? ] unit-test
[ f ] [ { 4 2 } { 1 3 } intersects? ] unit-test
[ f ] [ { } { 1 } intersects? ] unit-test
[ f ] [ { 1 } { } intersects? ] unit-test

View File

@ -1,4 +1,4 @@
! Copyright (C) 2008 Slava Pestov, Doug Coleman. ! Copyright (C) 2008, 2009 Slava Pestov, Doug Coleman.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: assocs hashtables kernel sequences vectors ; USING: assocs hashtables kernel sequences vectors ;
IN: sets IN: sets
@ -31,17 +31,26 @@ IN: sets
: all-unique? ( seq -- ? ) : all-unique? ( seq -- ? )
dup length <hashtable> [ (all-unique?) ] curry all? ; dup length <hashtable> [ (all-unique?) ] curry all? ;
<PRIVATE
: tester ( seq -- quot ) unique [ key? ] curry ; inline
PRIVATE>
: intersect ( seq1 seq2 -- newseq ) : intersect ( seq1 seq2 -- newseq )
unique [ key? ] curry filter ; tester filter ;
: intersects? ( seq1 seq2 -- newseq )
tester contains? ;
: diff ( seq1 seq2 -- newseq ) : diff ( seq1 seq2 -- newseq )
unique [ key? not ] curry filter ; tester [ not ] compose filter ;
: union ( seq1 seq2 -- newseq ) : union ( seq1 seq2 -- newseq )
append prune ; append prune ;
: subset? ( seq1 seq2 -- ? ) : subset? ( seq1 seq2 -- ? )
unique [ key? ] curry all? ; tester all? ;
: set= ( seq1 seq2 -- ? ) : set= ( seq1 seq2 -- ? )
[ unique ] bi@ = ; [ unique ] bi@ = ;

View File

@ -48,12 +48,12 @@ IN: splitting
: split ( seq separators -- pieces ) [ split, ] { } make ; : split ( seq separators -- pieces ) [ split, ] { } make ;
: string-lines ( str -- seq ) : string-lines ( str -- seq )
dup "\r\n" intersect empty? [ dup "\r\n" intersects? [
1array
] [
"\n" split [ "\n" split [
but-last-slice [ but-last-slice [
"\r" ?tail drop "\r" split "\r" ?tail drop "\r" split
] map ] map
] keep peek "\r" split suffix concat ] keep peek "\r" split suffix concat
] [
1array
] if ; ] if ;