Skip to content

Commit

Permalink
sequences.snipped: fix out-of-bounds issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjbq7 committed Dec 5, 2024
1 parent 85d94ba commit 09fc16a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
8 changes: 7 additions & 1 deletion extra/sequences/snipped/snipped-tests.factor
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,11 @@ USING: arrays sequences sequences.snipped tools.test ;

{ { 0 1 2 5 6 } } [ 3 5 7 <iota> <snipped> >array ] unit-test
{ { 0 1 2 } } [ 3 10 7 <iota> <snipped> >array ] unit-test
{ { 6 } } [ -1 5 7 <iota> <snipped> >array ] unit-test
{ { 5 6 } } [ -1 5 7 <iota> <snipped> >array ] unit-test
{ { } } [ -1 10 7 <iota> <snipped> >array ] unit-test

{ { 1 2 3 } } [ -1 { 1 2 3 } <removed> >array ] unit-test
{ { 2 3 } } [ 0 { 1 2 3 } <removed> >array ] unit-test
{ { 1 3 } } [ 1 { 1 2 3 } <removed> >array ] unit-test
{ { 1 2 } } [ 2 { 1 2 3 } <removed> >array ] unit-test
{ { 1 2 3 } } [ 3 { 1 2 3 } <removed> >array ] unit-test
4 changes: 3 additions & 1 deletion extra/sequences/snipped/snipped.factor
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ TUPLE: snipped < sequence-view
{ length integer read-only } ;

: <snipped> ( from to seq -- snipped )
[ length min ] keep -rot over - snipped boa ;
seq dup length :> n
from to [ 0 n clamp ] bi@
over - snipped boa ;

: <removed> ( i seq -- snipped )
[ dup 1 + ] dip <snipped> ;
Expand Down

0 comments on commit 09fc16a

Please sign in to comment.