]> gitweb.factorcode.org Git - factor.git/commitdiff
Remove stream-peek and stream-peek1, re-implement dns vocab to not need this abstraction
authorSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Sat, 9 Oct 2010 01:55:13 +0000 (18:55 -0700)
committerSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Sat, 9 Oct 2010 01:55:13 +0000 (18:55 -0700)
23 files changed:
basis/alien/data/data.factor
basis/delegate/protocols/protocols.factor
basis/io/buffers/buffers-docs.factor
basis/io/buffers/buffers.factor
basis/io/ports/ports-tests.factor
basis/io/ports/ports.factor
basis/io/streams/null/null.factor
basis/io/streams/string/string.factor
basis/io/streams/throwing/throwing.factor
core/io/encodings/encodings-tests.factor
core/io/encodings/encodings.factor
core/io/io-docs.factor
core/io/io.factor
core/io/streams/byte-array/byte-array-tests.factor
core/io/streams/byte-array/byte-array.factor
core/io/streams/c/c-tests.factor
core/io/streams/c/c.factor
core/io/streams/memory/memory.factor
core/io/streams/sequence/sequence.factor
core/sequences/sequences-tests.factor
core/sequences/sequences.factor
extra/dns/dns.factor
extra/images/gif/gif.factor

index f971d0ca7dc68418f65f3601326c22fbfb26ab0a..ab34bf5a4e7f645775db13874210032b6176e957 100644 (file)
@@ -62,13 +62,11 @@ M: pointer <c-direct-array>
 : malloc-string ( string encoding -- alien )
     string>alien malloc-byte-array ;
 
-M: memory-stream stream-peek
-    [ index>> ] [ alien>> ] bi <displaced-alien>
-    swap memory>byte-array ;
-
 M: memory-stream stream-read
-    [ stream-peek ]
-    [ [ + ] change-index drop ] 2bi ;
+    [
+        [ index>> ] [ alien>> ] bi <displaced-alien>
+        swap memory>byte-array
+    ] [ [ + ] change-index drop ] 2bi ;
 
 M: value-type c-type-rep drop int-rep ;
 
index 92f9387130a58c4fb0465ef31d3e8017b50e347d..40054bc4b0f721858c81efe706880855898d83d3 100644 (file)
@@ -13,8 +13,7 @@ at* assoc-size >alist set-at assoc-clone-like
 delete-at clear-assoc new-assoc assoc-like ;
 
 PROTOCOL: input-stream-protocol
-stream-peek1 stream-peek stream-read1 stream-read
-stream-read-partial stream-readln
+stream-read1 stream-read stream-read-partial stream-readln
 stream-read-until ;
 
 PROTOCOL: output-stream-protocol
index f7392126e3ceedfe5660d83318a1b849bda42d64..8a233337f0a23ff6bf6346eeed8a874b386ac118 100644 (file)
@@ -25,7 +25,7 @@ $nl
 }
 "Reading from the buffer:"
 { $subsections
-    buffer-peek1
+    buffer-peek
     buffer-pop
     buffer-read
 }
@@ -98,7 +98,7 @@ HELP: n>buffer
 { $description "Advances the fill pointer by " { $snippet "n" } " bytes." }
 { $warning "This word will leave the buffer in an invalid state if it does not have " { $snippet "n" } " bytes available." } ;
 
-HELP: buffer-peek1
+HELP: buffer-peek
 { $values { "buffer" buffer } { "byte" "a byte" } }
 { $description "Outputs the byte at the buffer position." } ;
 
index e2073b0a13b15aacfc516069969dc18719bab982..562abad082fb24911b73cdeeef7c102405694937 100644 (file)
@@ -32,11 +32,11 @@ M: buffer dispose* ptr>> free ;
     dup [ pos>> ] [ fill>> ] bi <
     [ 0 >>pos 0 >>fill ] unless drop ; inline
 
-: buffer-peek1 ( buffer -- byte )
+: buffer-peek ( buffer -- byte )
     [ ptr>> ] [ pos>> ] bi alien-unsigned-1 ; inline
 
 : buffer-pop ( buffer -- byte )
-    [ buffer-peek1 ] [ 1 swap buffer-consume ] bi ; inline
+    [ buffer-peek ] [ 1 swap buffer-consume ] bi ; inline
 
 : buffer-length ( buffer -- n )
     [ fill>> ] [ pos>> ] bi - ; inline
index 7380e195e7f9e0d4e6aa94a54216886e2dd6fe8a..d2fb5764ff6ac67ea42f3fe31fad7cdb504d3818 100644 (file)
@@ -23,8 +23,3 @@ IN: io.ports.tests
 ] unit-test
 
 [ ] [ "test.txt" temp-file delete-file ] unit-test
-
-[ t ]
-[
-    "resource:license.txt" binary [ 10 peek 10 peek ] with-file-reader =
-] unit-test
index 2b6f0918ff88dd9300a539b985190d81171c4515..6c2f75ec807811a6f4c6d3836802c34cc098003d 100644 (file)
@@ -41,10 +41,6 @@ HOOK: (wait-to-read) io-backend ( port -- )
         dup (wait-to-read) buffer>> buffer-empty?
     ] [ drop f ] if ; inline
 
-M: input-port stream-peek1
-    dup check-disposed dup wait-to-read
-    [ drop f ] [ buffer>> buffer-peek1 ] if ; inline
-
 M: input-port stream-read1
     dup check-disposed
     dup wait-to-read [ drop f ] [ buffer>> buffer-pop ] if ; inline
@@ -84,8 +80,6 @@ M: input-port stream-read
         ] [ 2nip ] if
     ] [ 2nip ] if ;
 
-M: input-port stream-peek [ stream-read ] with-input-rewind ;
-
 : read-until-step ( separators port -- string/f separator/f )
     dup wait-to-read [ 2drop f f ] [ buffer>> buffer-until ] if ;
 
index 040301013a5245d7d37ed1aa07672be8be51d31a..2b62ec938a4b5598a9e4bf22e5eb2e51c8813544 100644 (file)
@@ -14,8 +14,6 @@ M: null-reader stream-readln drop f ;
 M: null-reader stream-read1 drop f ;
 M: null-reader stream-read-until 2drop f f ;
 M: null-reader stream-read 2drop f ;
-M: null-reader stream-peek1 drop f ;
-M: null-reader stream-peek 2drop f ;
 
 M: null-writer stream-element-type drop +byte+ ;
 M: null-writer stream-write1 2drop ;
index ab18682d186993b7d50f73b0fd62098f0b082da5..be9016d1f27ffb87cb577feaa22366abe44c17fc 100644 (file)
@@ -13,8 +13,6 @@ M: string-reader stream-element-type drop +character+ ;
 M: string-reader stream-read-partial stream-read ;
 M: string-reader stream-read sequence-read ;
 M: string-reader stream-read1 sequence-read1 ;
-M: string-reader stream-peek sequence-peek ;
-M: string-reader stream-peek1 sequence-peek1 ;
 M: string-reader stream-read-until sequence-read-until ;
 M: string-reader stream-tell i>> ;
 M: string-reader stream-seek (stream-seek) ;
index d66df6b4ee7b2c0d206b19cd619caaef67fc089d..0b1f214d07de92c0fec8083e676c692e7cd74826 100644 (file)
@@ -20,18 +20,10 @@ M:: throws-on-eof-stream stream-read1 ( stream -- obj )
     stream stream>> stream-read1
     [ 1 stream \ read1 stream-exhausted ] unless* ;
 
-M:: throws-on-eof-stream stream-peek1 ( stream -- obj )
-    stream stream>> stream-peek1
-    [ 1 stream \ peek1 stream-exhausted ] unless* ;
-
 M:: throws-on-eof-stream stream-read ( n stream -- seq )
     n stream stream>> stream-read
     dup length n = [ n stream \ read stream-exhausted ] unless ;
 
-M:: throws-on-eof-stream stream-peek ( n stream -- seq )
-    n stream stream>> stream-peek
-    dup length n = [ n stream \ peek stream-exhausted ] unless ;
-
 M:: throws-on-eof-stream stream-read-partial ( n stream -- seq )
     n stream stream>> stream-read-partial
     [ n stream \ read-partial stream-exhausted ] unless* ;
index 0ba65a88028b0c16e745aec12325b615c580f2a5..cc32f30060ba9396940b08220b8c800ea93123bb 100644 (file)
@@ -73,13 +73,3 @@ unit-test
         output-stream get code>>
     ] with-byte-writer drop
 ] unit-test
-
-[ t ] [
-    "vocab:io/test/mac-os-eol.txt"
-    ascii [ 10 peek 10 peek = ] with-file-reader
-] unit-test
-
-[ t ] [
-    "vocab:io/test/mac-os-eol.txt"
-    ascii [ peek1 peek1 = ] with-file-reader
-] unit-test
index 8064a8a14be4b2189aa59c726a2b1b408fda324d..1880859db19d484d6eee6c8e4b006a56f0fd554b 100644 (file)
@@ -61,9 +61,6 @@ M: decoder stream-seek stream>> stream-seek ;
 M: decoder stream-read1
     dup >decoder< decode-char fix-read1 ;
 
-M: decoder stream-peek1 [ stream-read1 ] with-input-rewind ;
-M: decoder stream-peek [ stream-read ] with-input-rewind ;
-
 : fix-read ( stream string -- string )
     over cr>> [
         over cr-
index 408346d83c464a0b2372fa2f9f8683eabaa08744..aca460cc31e2f4d15c46c3b9fabdbff09579a9bb 100644 (file)
@@ -262,37 +262,6 @@ HELP: contents
 { $description "Reads all elements in the " { $link input-stream } " until the stream is exhausted. The type of the sequence depends on the stream's element type." }
 $io-error ;
 
-HELP: peek
-{ $values
-    { "n" integer }
-    { "seq/f" "a sequence or f" }
-}
-{ $description "Reads the next " { $snippet "n" } " elements from the stream and seeks the stream to before the read." } ;
-
-HELP: peek1
-{ $values
-        { "elt" "an element or f" }
-}
-{ $description "Reads the next object from a stream and seeks the stream to before the read." } ;
-
-HELP: stream-peek
-{ $values
-    { "n" integer } { "stream" "an input stream" }
-    { "seq/f" "a sequence or f" }
-}
-{ $contract "Peeks " { $snippet "n" } " elements from the stream. Outputs " { $link f } " on stream exhaustion." }
-{ $notes "Most code only works on one stream at a time and should instead use " { $link peek } "; see " { $link "stdio" } "." }
-$io-error ;
-
-HELP: stream-peek1
-{ $values
-    { "stream" "an input stream" }
-    { "elt/f" "an element or f" }
-}
-{ $contract "Peeks an element from the stream. Outputs " { $link f } " on stream exhaustion." }
-{ $notes "Most code only works on one stream at a time and should instead use " { $link peek1 } "; see " { $link "stdio" } "." }
-$io-error ;
-
 HELP: tell-input
 { $values
         { "n" integer }
@@ -305,18 +274,6 @@ HELP: tell-output
 }
 { $description "Returns the index of the stream stored in " { $link output-stream } "." } ;
 
-HELP: with-input-rewind
-{ $values
-    { "quot" quotation }    
-}
-{ $description "Records the current seek position of the stream and calls the quotation. The position is then reset after the call." } ;
-
-HELP: with-input-seek
-{ $values
-    { "n" integer } { "seek-type" "a seek singleton" } { "quot" quotation }    
-}
-{ $description "Seeks the stream to a location, calls " { $snippet "quot" } ", and resets the input stream to where it was before the quotation was called." } ;
-
 ARTICLE: "stream-protocol" "Stream protocol"
 "The stream protocol consists of a large number of generic words, many of which are optional."
 $nl
@@ -326,8 +283,6 @@ $nl
 { $subsections "stream-types" }
 "These words are required for binary and string input streams:"
 { $subsections
-    stream-peek1
-    stream-peek
     stream-read1
     stream-read
     stream-read-until
@@ -397,8 +352,6 @@ ARTICLE: "stdio" "Default input and output streams"
 $nl
 "Words reading from the default input stream:"
 { $subsections
-    peek1
-    peek
     read1
     read
     read-until
@@ -431,17 +384,6 @@ $nl
 }
 "Seeking on the default output stream:"
 { $subsections seek-output }
-"Seeking descriptors:"
-{ $subsections
-    seek-absolute
-    seek-relative
-    seek-end
-}
-"Seeking combinators:"
-{ $subsections
-    with-input-seek
-    with-input-rewind
-}
 "A pair of combinators for rebinding the " { $link output-stream } " variable:"
 { $subsections
     with-output-stream
index 3eaf7cefbf8abd128cec2d1a2ddc343340953350..ea37c13dd7cb00b9ad7ba4347a67ef0cd981ff00 100644 (file)
@@ -8,8 +8,6 @@ SYMBOLS: +byte+ +character+ ;
 
 GENERIC: stream-element-type ( stream -- type )
 
-GENERIC: stream-peek1 ( stream -- elt/f )
-GENERIC: stream-peek ( n stream -- seq/f )
 GENERIC: stream-read1 ( stream -- elt )
 GENERIC: stream-read ( n stream -- seq )
 GENERIC: stream-read-until ( seps stream -- seq sep/f )
@@ -35,8 +33,6 @@ SYMBOL: input-stream
 SYMBOL: output-stream
 SYMBOL: error-stream
 
-: peek1 ( -- elt ) input-stream get stream-peek1 ;
-: peek ( n -- seq/f ) input-stream get stream-peek ;
 : readln ( -- str/f ) input-stream get stream-readln ;
 : read1 ( -- elt ) input-stream get stream-read1 ;
 : read ( n -- seq ) input-stream get stream-read ;
@@ -74,14 +70,6 @@ SYMBOL: error-stream
     #! buffer before closing the FD.
     swapd [ with-output-stream ] curry with-input-stream ; inline
 
-: with-input-seek ( n seek-type quot -- )
-    tell-input [
-        [ seek-input ] dip call
-    ] dip seek-absolute seek-input ; inline
-
-: with-input-rewind ( quot -- )
-    [ 0 seek-absolute ] dip with-input-seek ; inline
-
 : print ( str -- ) output-stream get stream-print ;
 
 : bl ( -- ) " " write ;
index 2a7de28b8b26780f8843ce0b8df5653ac4a6975d..1c7826719cd9eed9a583b98a9dd1ada4c5d2dd93 100644 (file)
@@ -8,7 +8,6 @@ IN: io.streams.byte-array.tests
 [ B{ 1 2 3 } ] [ binary [ B{ 1 2 3 } write ] with-byte-writer ] unit-test
 [ B{ 1 2 3 4 5 6 } ] [ binary [ B{ 1 2 3 } write B{ 4 5 6 } write ] with-byte-writer ] unit-test
 [ B{ 1 2 3 } ] [ { 1 2 3 } binary [ 3 read ] with-byte-reader ] unit-test
-[ B{ 1 2 } ] [ B{ 1 2 3 4 } binary [ 2 peek ] with-byte-reader ] unit-test
 
 [ B{ BIN: 11110101 BIN: 10111111 BIN: 10000000 BIN: 10111111 BIN: 11101111 BIN: 10000000 BIN: 10111111 BIN: 11011111 BIN: 10000000 CHAR: x } ]
 [ { BIN: 101111111000000111111 BIN: 1111000000111111 BIN: 11111000000 CHAR: x } >string utf8 [ write ] with-byte-writer ] unit-test
index c8c8fc5341081624982f3b68edebb2f5b0dbee1b..6f9b05cf182cb2a51f4171ec2ef1a1c71f29f7ff 100644 (file)
@@ -18,8 +18,6 @@ TUPLE: byte-reader { underlying byte-array read-only } { i array-capacity } ;
 
 M: byte-reader stream-element-type drop +byte+ ;
 
-M: byte-reader stream-peek1 sequence-peek1 ;
-M: byte-reader stream-peek sequence-peek ;
 M: byte-reader stream-read-partial stream-read ;
 M: byte-reader stream-read sequence-read ;
 M: byte-reader stream-read1 sequence-read1 ;
index 6d5a828680cdcc006ec69ae50e81318741600d2a..63a56b4af116f880e1caf3dcd3c1fd605754f2cf 100644 (file)
@@ -34,18 +34,6 @@ IN: io.streams.c.tests
     int-array-cast
 ] unit-test
 
-[ t ] [
-    "test.txt" temp-file "rb" fopen <c-reader> [
-        3 4 * [ peek ] [ peek ] bi =
-    ] with-input-stream
-] unit-test
-
-[ t ] [
-    "test.txt" temp-file "rb" fopen <c-reader> [
-        peek1 peek1 =
-    ] with-input-stream
-] unit-test
-
 ! Writing strings to binary streams should fail
 [
     "test.txt" temp-file "wb" fopen <c-writer> [
index 4370827737793b2c65069cccacee54eeeb9f04db..9ebf7f701836ea99c4140f31306ad4db8c4d7ed1 100644 (file)
@@ -47,14 +47,10 @@ M: c-reader stream-element-type drop +byte+ ;
 
 M: c-reader stream-read dup check-disposed handle>> fread ;
 
-M: c-reader stream-peek [ stream-read ] with-input-rewind ;
-
 M: c-reader stream-read-partial stream-read ;
 
 M: c-reader stream-read1 dup check-disposed handle>> fgetc ;
 
-M: c-reader stream-peek1 [ stream-read1 ] with-input-rewind ;
-
 : read-until-loop ( stream delim -- ch )
     over stream-read1 dup [
         dup pick member-eq? [ 2nip ] [ , read-until-loop ] if
index 43de1f4311464a1874c247cbe5bbef993d0a3c0d..e7b4338388c49a1ab22ed3a634299697aa915080 100644 (file)
@@ -10,9 +10,6 @@ TUPLE: memory-stream alien index ;
 
 M: memory-stream stream-element-type drop +byte+ ;
 
-M: memory-stream stream-peek1
-    [ alien>> ] [ index>> ] bi alien-unsigned-1 ;
-
 M: memory-stream stream-read1
-    [ stream-peek1 ]
+    [ [ alien>> ] [ index>> ] bi alien-unsigned-1 ]
     [ [ 1 + ] change-index drop ] bi ;
index 838ac18079531bfc1dec48fb51537cc44477d33c..22882d6a24eaca585d5ca2ab0c5e47843a767924 100644 (file)
@@ -14,12 +14,6 @@ SLOT: i
 : next ( stream -- )
     [ 1 + ] change-i drop ; inline
 
-: sequence-peek1 ( seq -- elt/f )
-    [ i>> ] [ underlying>> ] bi ?nth ;
-
-: sequence-peek ( n seq -- elt/f )
-    [ nip i>> dup ] [ [ + ] [ underlying>> ] bi* ] 2bi ?subseq ;
-
 : sequence-read1 ( stream -- elt/f )
     [ >sequence-stream< ?nth ] [ next ] bi ; inline
 
index 6c1628516f8f25ce437c0a800e6c87221aedd441..175ab252e13af9c1d62658e1b304c0d200d3458b 100644 (file)
@@ -13,11 +13,6 @@ IN: sequences.tests
 [ V{ 4 5 } ] [ { 1 2 3 4 5 } 2 tail-slice* >vector ] unit-test
 [ V{ 3 4 } ] [ 2 4 1 10 dup iota <slice> subseq >vector ] unit-test
 [ V{ 3 4 } ] [ 0 2 2 4 1 10 dup iota <slice> <slice> subseq >vector ] unit-test
-
-[ "abc" ] [ 0 3 "abc" ?subseq ] unit-test
-[ "abc" ] [ 0 5 "abc" ?subseq ] unit-test
-[ "bc" ] [ 1 5 "abc" ?subseq ] unit-test
-[ "abc" ] [ -1 3 "abc" ?subseq ] unit-test
 [ 0 10 "hello" <slice> ] must-fail
 [ -10 3 "hello" <slice> ] must-fail
 [ 2 1 "hello" <slice> ] must-fail
index 6cc9ceb52357c6094b23c446a0ab81f0a43c76ee..55398ff02bedc45b6a02d5ab0b0d015295a8a954 100644 (file)
@@ -293,12 +293,6 @@ PRIVATE>
 : subseq ( from to seq -- subseq )
     [ check-slice subseq>copy (copy) ] keep like ;
 
-: ?subseq ( from to seq -- subseq )
-    [
-        [ 0 ] dip length [ clamp ] 2curry bi@
-        2dup > [ nip dup ] when
-    ] keep subseq f like ;
-
 : head ( seq n -- headseq ) (head) subseq ;
 
 : tail ( seq n -- tailseq ) (tail) subseq ;
index c16949a84d9efc49ecfc02c8d1ee1ca64b431ded..54bb03b008e48143b713e6561b96a9fe5e609d98 100644 (file)
@@ -9,6 +9,11 @@ math.parser namespaces nested-comments random sequences
 slots.syntax splitting system vectors vocabs.loader ;
 IN: dns
 
+: with-temporary-input-seek ( n seek-type quot -- )
+    tell-input [
+        [ seek-input ] dip call
+    ] dip seek-absolute seek-input ; inline
+
 ENUM: dns-type
 { A 1 } { NS 2 } { MD 3 } { MF 4 }
 { CNAME 5 } { SOA 6 } { MB 7 } { MG 8 }
@@ -143,7 +148,8 @@ CONSTANT: ipv4-arpa-suffix ".in-addr.arpa"
 CONSTANT: ipv6-arpa-suffix ".ip6.arpa"
 
 : ipv6>arpa ( string -- string )
-    ipv6>byte-array [ [ -4 shift 4 bits ] [ 4 bits ] bi 2array ] { } map-as
+    ipv6>byte-array
+    [ [ -4 shift 4 bits ] [ 4 bits ] bi 2array ] { } map-as
     B{ } concat-as reverse
     [ >hex ] { } map-as "." join ipv6-arpa-suffix append ;
 
@@ -161,19 +167,19 @@ CONSTANT: ipv6-arpa-suffix ".ip6.arpa"
         first2 swap [ hex> ] bi@ [ 4 shift ] [ ] bi* bitor
     ] B{ } map-as byte-array>ipv6 ;
 
-: parse-length-bytes ( -- sequence ) read1 read utf8 decode ;
+: parse-length-bytes ( byte -- sequence ) read utf8 decode ;
 
 : (parse-name) ( -- )
-    peek1 [
-        read1 drop
-    ] [
-        HEX: C0 mask? [
-            2 read be> HEX: 3fff bitand
-            seek-absolute [ parse-length-bytes , (parse-name) ] with-input-seek
+    read1 [
+        dup HEX: C0 mask? [
+            8 shift read1 bitor HEX: 3fff bitand
+            seek-absolute [
+                read1 parse-length-bytes , (parse-name)
+            ] with-temporary-input-seek
         ] [
             parse-length-bytes , (parse-name)
         ] if
-    ] if-zero ;
+    ] unless-zero ;
 
 : parse-name ( -- sequence )
     [ (parse-name) ] { } make "." join ;
index 55111113e5723ab3d942cd4258d3581ef8128782..b06210fc00a83d86c0dd7db5039c868ce4dac058 100644 (file)
@@ -3,7 +3,7 @@
 USING: accessors arrays combinators compression.lzw
 constructors destructors grouping images images.loader io
 io.binary io.buffers io.encodings.string io.encodings.utf8
-io.ports kernel make math math.bitwise namespaces sequences ;
+kernel make math math.bitwise namespaces sequences ;
 IN: images.gif
 
 SINGLETON: gif-image