]> gitweb.factorcode.org Git - factor.git/commitdiff
factor: refactor subseq-start
authorDoug Coleman <doug.coleman@gmail.com>
Fri, 29 Jul 2022 21:45:38 +0000 (16:45 -0500)
committerDoug Coleman <doug.coleman@gmail.com>
Sun, 31 Jul 2022 18:25:41 +0000 (13:25 -0500)
basis/compression/gzip/gzip.factor
basis/game/input/iokit/iokit.factor
basis/mime/multipart/multipart.factor
basis/urls/urls.factor
core/strings/strings-tests.factor
extra/combinators/extras/extras-tests.factor
extra/io/encodings/detect/detect.factor
extra/project-euler/047/047.factor

index a5bf463271309635c0d9287beda65104f176857c..cf8332397b1ded56873ddfc5d995ec9109c882e5 100644 (file)
@@ -16,7 +16,7 @@ SYMBOL: lit-vec
 :: longest-prefix ( ind seq -- start end )
     ind dup ind + seq length min [a..b]
     seq ind head-slice '[
-    ind swap seq <slice> _ subseq-start
+        [ _ ] dip ind swap seq <slice> subsequence-starts
     ] map-find-last ;
 
 :: create-pair ( ind seq -- array )
index 272a88b0660366782ca37839632854d6301d4abd..3130e9451961ff6f8befbb933fafd2912a4b9dc4 100644 (file)
@@ -337,13 +337,13 @@ M: iokit-game-input-backend (close-game-input)
 M: iokit-game-input-backend get-controllers
     +controller-states+ get-global keys [ controller boa ] map ;
 
-: ?join ( pre post sep -- string )
-    2over subseq-start [ swap 2nip ] [ [ 2array ] dip join ] if ;
+: ?glue ( seq subseq sep -- string )
+    2over subsequence-starts [ drop nip ] [ glue ] if ;
 
 M: iokit-game-input-backend product-string
     handle>>
-    [ kIOHIDManufacturerKey device-property ]
-    [ kIOHIDProductKey      device-property ] bi " " ?join ;
+    [ kIOHIDProductKey      device-property ]
+    [ kIOHIDManufacturerKey device-property ] bi " " ?glue ;
 
 M: iokit-game-input-backend product-id
     handle>>
index c0192afa9ea09000515f47b3b8ed1387a9429f9f..9a6bc0044da690df12390eca8a1d5c9fe999c7ef 100644 (file)
@@ -42,13 +42,14 @@ C: <mime-variable> mime-variable
 
 ERROR: mime-decoding-ran-out-of-bytes ;
 : dump-until-separator ( multipart -- multipart )
-    [ ] [ current-separator>> ] [ bytes>> ] tri
-    dup [ mime-decoding-ran-out-of-bytes ] unless
-    2dup subseq-start [
-        cut-slice
+    [ ] [ bytes>> ] [ current-separator>> ] tri
+    over [ mime-decoding-ran-out-of-bytes ] unless
+    2dup subsequence-starts [
+        swapd cut-slice
         [ mime-write ]
         [ swap length tail-slice >>bytes ] bi*
     ] [
+        swap
         tuck [ length ] bi@ - 1 - cut-slice
         [ mime-write ]
         [ >>bytes ] bi* fill-bytes
index 6d1d23255423670651e62c23f8ad9a74661ad250..30f06abdc8304b0a5595c82c6ad7d867883fc933 100644 (file)
@@ -164,7 +164,7 @@ PRIVATE>
         { [ dup "/" head? ] [ nip ] }
         { [ dup empty? ] [ drop ] }
         { [ over "/" tail? ] [ append ] }
-        { [ "/" pick subseq-start not ] [ nip ] }
+        { [ over "/" subsequence-starts not ] [ nip ] }
         [ [ "/" split1-last drop "/" ] dip 3append ]
     } cond remove-dot-segments ;
 
index ef071188e9583408e119af58c8e99b1b19ca9340..b6c232f9e4dd944351202ff3a83b25abe7fadf35 100644 (file)
@@ -21,6 +21,15 @@ vectors ;
 { f } [ "amigo" "hola" subseq-start ] unit-test
 { f } [ "holaa" "hola" subseq-start ] unit-test
 
+{ 3 } [ "hola" "a" subsequence-starts ] unit-test
+{ f } [ "hola" "x" subsequence-starts ] unit-test
+{ 0 } [ "a" "" subsequence-starts ] unit-test
+{ 0 } [ "" "" subsequence-starts ] unit-test
+{ 0 } [ "hola" "hola" subsequence-starts ] unit-test
+{ 1 } [ "hola" "ol" subsequence-starts ] unit-test
+{ f } [ "hola" "amigo" subsequence-starts ] unit-test
+{ f } [ "hola" "holaa" subsequence-starts ] unit-test
+
 { "Beginning" } [ "Beginning and end" 9 head ] unit-test
 
 { f } [ CHAR: I "team" member? ] unit-test
index 54f057449378827e30730e7dec3a2aa39ddfc91e..506dedb3155def7e26116a9485cb98069951dd43 100644 (file)
@@ -63,6 +63,29 @@ IN: combinators.extras.tests
     } cond*
 ] unit-test
 
+{ "hi " "there" } [
+    "hi there" {
+        { [ dup "there" subsequence-starts ] [ cut ] }
+        [ f ]
+    } cond*
+] unit-test
+
+{ "hi " "there" } [
+    "hi there" {
+        { [ dup "foo" subsequence-starts ] [ head f ] }
+        { [ dup "there" subsequence-starts ] [ cut ] }
+        [ f ]
+    } cond*
+] unit-test
+
+{ "hi there" f } [
+    "hi there" {
+        { [ dup "foo" subsequence-starts ] [ head f ] }
+        { [ dup "bar" subsequence-starts ] [ cut ] }
+        [ f ]
+    } cond*
+] unit-test
+
 { f } [ f { } chain ] unit-test
 { 3 } [ H{ { 1 H{ { 2 3 } } } } { [ 1 of ] [ 2 of ] } chain ] unit-test
 { f } [ H{ { 1 H{ { 3 4 } } } } { [ 1 of ] [ 2 of ] } chain ] unit-test
index a111c2109fa6293be31206beb86e712da92ebe9e..6c31362da97a3a3c90450fd7bebce3955fcf0e4f 100644 (file)
@@ -17,7 +17,7 @@ default-encoding [ latin1 ] initialize
 
 : prolog-encoding ( string -- iana-encoding )
     '[
-        _ "encoding=" over subseq-start
+        _ dup "encoding=" subsequence-starts
         10 + swap [ [ 1 - ] dip nth ] [ index-from ] [ swapd subseq ] 2tri
     ] [ drop "UTF-8" ] recover ;
 
index f787c29f6085af0f2d30d50441c5093445619e61..601682c3de636c6b1c2f6646ba628e9aec7f7210 100644 (file)
@@ -79,13 +79,13 @@ SYMBOL: sieve
         dup is-prime? [ increment-counts ] [ drop ] if
     ] each sieve get ;
 
-: consecutive-under ( m limit -- n/f )
-    prime-tau-upto [ dup <repetition> ] dip subseq-start ;
+: consecutive-under ( limit m -- n/f )
+   [ prime-tau-upto ] [ dup <repetition> ] bi* subsequence-starts ;
 
 PRIVATE>
 
 : euler047a ( -- answer )
-    4 200000 consecutive-under ;
+    200000 4 consecutive-under ;
 
 ! [ euler047a ] 100 ave-time
 ! 331 ms ave run time - 19.14 SD (100 trials)