]> gitweb.factorcode.org Git - factor.git/commitdiff
sequence.extras: adding ?trim-head and ?trim-tail.
authorJohn Benediktsson <mrjbq7@gmail.com>
Wed, 3 Apr 2013 15:55:48 +0000 (08:55 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Wed, 3 Apr 2013 15:55:48 +0000 (08:55 -0700)
extra/sequences/extras/extras-tests.factor
extra/sequences/extras/extras.factor

index 199e56d090ec5f71594826a016816d3180fa2ccc..c8f794c3b063158a279587c8f0272d1db939921d 100644 (file)
@@ -77,6 +77,11 @@ IN: sequences.extras.tests
 { t } [ "ABC" dup [ blank? ] ?trim [ identity-hashcode ] same? ] unit-test
 { "ABC" } [ " ABC " [ blank? ] ?trim ] unit-test
 
+{ t } [ "ABC" dup [ blank? ] ?trim-head [ identity-hashcode ] same? ] unit-test
+{ t } [ "ABC" dup [ blank? ] ?trim-tail [ identity-hashcode ] same? ] unit-test
+{ "ABC " } [ " ABC " [ blank? ] ?trim-head ] unit-test
+{ " ABC" } [ " ABC " [ blank? ] ?trim-tail ] unit-test
+
 { "" } [ "" "" "" unsurround ] unit-test
 { "" } [ "  " " " " " unsurround ] unit-test
 { "foo.com" } [ "http://foo.com" "http://" "/" unsurround ] unit-test
index 31a0bbeebdf5fae67682b07e4baf709e402a265e..803c0e5bd9eba4e126c2a049b8bdb1eb535f5c67 100644 (file)
@@ -242,12 +242,24 @@ PRIVATE>
 : trim-as ( ... seq quot: ( ... elt -- ... ? ) exemplar -- ... newseq )
     [ trim-slice ] [ like ] bi* ; inline
 
-: ?trim ( ... seq quot: ( ... elt -- ... ? ) -- ... seq/newseq )
+: ?trim ( seq quot: ( elt -- ? ) -- seq/newseq )
     over empty? [ drop ] [
         over [ first-unsafe ] [ last-unsafe ] bi pick bi@ or
         [ trim ] [ drop ] if
     ] if ; inline
 
+: ?trim-head ( seq quot: ( elt -- ? ) -- seq/newseq )
+    over empty? [ drop ] [
+        over first-unsafe over call
+        [ trim-head ] [ drop ] if
+    ] if ; inline
+
+: ?trim-tail ( seq quot: ( elt -- ? ) -- seq/newseq )
+    over empty? [ drop ] [
+        over last-unsafe over call
+        [ trim-tail ] [ drop ] if
+    ] if ; inline
+
 : unsurround ( newseq seq2 seq3 -- seq1 )
    [ ?head drop ] [ ?tail drop ] bi* ;