From 38926c3ac650ca7503b7b95c9ce8276422c12e7b Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Fri, 29 Jul 2022 16:45:38 -0500 Subject: [PATCH] factor: refactor subseq-start --- basis/compression/gzip/gzip.factor | 2 +- basis/game/input/iokit/iokit.factor | 8 +++---- basis/mime/multipart/multipart.factor | 9 ++++---- basis/urls/urls.factor | 2 +- core/strings/strings-tests.factor | 9 ++++++++ extra/combinators/extras/extras-tests.factor | 23 ++++++++++++++++++++ extra/io/encodings/detect/detect.factor | 2 +- extra/project-euler/047/047.factor | 6 ++--- 8 files changed, 47 insertions(+), 14 deletions(-) diff --git a/basis/compression/gzip/gzip.factor b/basis/compression/gzip/gzip.factor index a5bf463271..cf8332397b 100644 --- a/basis/compression/gzip/gzip.factor +++ b/basis/compression/gzip/gzip.factor @@ -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 _ subseq-start + [ _ ] dip ind swap seq subsequence-starts ] map-find-last ; :: create-pair ( ind seq -- array ) diff --git a/basis/game/input/iokit/iokit.factor b/basis/game/input/iokit/iokit.factor index 272a88b066..3130e94519 100644 --- a/basis/game/input/iokit/iokit.factor +++ b/basis/game/input/iokit/iokit.factor @@ -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>> diff --git a/basis/mime/multipart/multipart.factor b/basis/mime/multipart/multipart.factor index c0192afa9e..9a6bc0044d 100644 --- a/basis/mime/multipart/multipart.factor +++ b/basis/mime/multipart/multipart.factor @@ -42,13 +42,14 @@ C: 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 diff --git a/basis/urls/urls.factor b/basis/urls/urls.factor index 6d1d232554..30f06abdc8 100644 --- a/basis/urls/urls.factor +++ b/basis/urls/urls.factor @@ -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 ; diff --git a/core/strings/strings-tests.factor b/core/strings/strings-tests.factor index ef071188e9..b6c232f9e4 100644 --- a/core/strings/strings-tests.factor +++ b/core/strings/strings-tests.factor @@ -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 diff --git a/extra/combinators/extras/extras-tests.factor b/extra/combinators/extras/extras-tests.factor index 54f0574493..506dedb315 100644 --- a/extra/combinators/extras/extras-tests.factor +++ b/extra/combinators/extras/extras-tests.factor @@ -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 diff --git a/extra/io/encodings/detect/detect.factor b/extra/io/encodings/detect/detect.factor index a111c2109f..6c31362da9 100644 --- a/extra/io/encodings/detect/detect.factor +++ b/extra/io/encodings/detect/detect.factor @@ -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 ; diff --git a/extra/project-euler/047/047.factor b/extra/project-euler/047/047.factor index f787c29f60..601682c3de 100644 --- a/extra/project-euler/047/047.factor +++ b/extra/project-euler/047/047.factor @@ -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 ] dip subseq-start ; +: consecutive-under ( limit m -- n/f ) + [ prime-tau-upto ] [ dup ] 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) -- 2.34.1