Adding byte counting ports

This commit is contained in:
Jay McCarthy 2010-05-24 12:45:12 -06:00
parent 325ac1ae88
commit f44e3123b5
5 changed files with 51 additions and 0 deletions

View File

@ -1800,6 +1800,7 @@ path/s is either such a string or a list of them.
"collects/tests/units/test-runtime.rktl" drdr:command-line (racket "-f" *)
"collects/tests/units/test-unit-contracts.rktl" drdr:command-line (racket "-f" *)
"collects/tests/units/test-unit.rktl" drdr:command-line (racket "-f" *)
"collects/tests/unstable/byte-counting-port.rkt" responsible (jay)
"collects/tests/unstable/generics.rkt" responsible (jay)
"collects/tests/unstable/srcloc.rktl" responsible (cce) drdr:command-line (racket "-f" *)
"collects/tests/utils" responsible (unknown)
@ -1839,6 +1840,7 @@ path/s is either such a string or a list of them.
"collects/typed/rackunit/gui.rkt" drdr:command-line (gracket "-t" *)
"collects/typed-scheme" responsible (samth)
"collects/unstable" responsible (jay samth cce ryanc)
"collects/unstable/byte-counting-port.rkt" responsible (jay)
"collects/unstable/debug.rkt" responsible (samth)
"collects/unstable/gui/notify.rkt" drdr:command-line (gracket-text "-t" *)
"collects/unstable/gui/prefs.rkt" drdr:command-line (gracket-text "-t" *)
@ -1846,6 +1848,7 @@ path/s is either such a string or a list of them.
"collects/unstable/match.rkt" responsible (samth)
"collects/unstable/mutated-vars.rkt" responsible (samth)
"collects/unstable/poly-c.rkt" responsible (samth)
"collects/unstable/scribblings/byte-counting-port.scrbl" responsible (jay)
"collects/unstable/scribblings/debug.scrbl" responsible (samth)
"collects/unstable/scribblings/hash.scrbl" responsible (samth)
"collects/unstable/scribblings/match.scrbl" responsible (samth)

View File

@ -0,0 +1,15 @@
#lang racket
(require unstable/byte-counting-port
tests/eli-tester)
(define name (gensym))
(define cp (make-byte-counting-port name))
(test
(object-name cp) => name
(for/fold ([l 0])
([i (in-range 100)])
(define n (random 25))
(test
(file-position cp) => l
(write-bytes (make-bytes n) cp))
(+ l n)))

View File

@ -0,0 +1,17 @@
#lang racket
(define (make-byte-counting-port [name 'byte-counting-port])
(define location 0)
(define (write-out bs starting ending opt1 opt2)
(define how-many-written (- ending starting))
(set! location (+ location how-many-written))
how-many-written)
(define close void)
(define (get-location)
(values #f #f location))
(make-output-port name always-evt write-out close
#f #f #f
get-location))
(provide/contract
[make-byte-counting-port (any/c . -> . output-port?)])

View File

@ -0,0 +1,15 @@
#lang scribble/manual
@(require "utils.rkt" (for-label racket unstable/byte-counting-port))
@title{Byte Counting Ports}
@defmodule[unstable/byte-counting-port]
@unstable[@author+email["Jay McCarthy" "jay@racket-lang.org"]]
This library provides an output port constructor like @racket[open-output-nowhere], except it counts how many bytes have been written (available through @racket[file-position].)
@defproc[(make-byte-counting-port [name any/c 'byte-counting-port])
output-port?]{
Creates and returns an output port that discards all output sent to it (without blocking.) The @racket[name] argument is used as the port's name. The total number bytes written is available through @racket[file-position].}

View File

@ -95,6 +95,7 @@ Keep documentation and tests up to date.
@include-section["generics.scrbl"]
@include-section["markparam.scrbl"]
@include-section["debug.scrbl"]
@include-section["byte-counting-port.scrbl"]
@;{--------}