diff --git a/racket/src/io/port/bytes-input.rkt b/racket/src/io/port/bytes-input.rkt index b9bfe82c44..3e76cb3519 100644 --- a/racket/src/io/port/bytes-input.rkt +++ b/racket/src/io/port/bytes-input.rkt @@ -49,8 +49,7 @@ ;; ---------------------------------------- (define/who (read-byte [orig-in (current-input-port)]) - (check who input-port? orig-in) - (let ([in (->core-input-port orig-in)]) + (let ([in (->core-input-port orig-in who)]) (do-read-byte/core-port who in))) (define (do-read-byte/core-port who in) diff --git a/racket/src/io/port/input-port.rkt b/racket/src/io/port/input-port.rkt index 5682bb9bd5..42991027f1 100644 --- a/racket/src/io/port/input-port.rkt +++ b/racket/src/io/port/input-port.rkt @@ -34,22 +34,18 @@ ;; This function should not be called in atomic mode, ;; since it can invoke an artitrary function -(define (->core-input-port v) +(define (->core-input-port v [who #f]) (cond - [(core-input-port? v) (if (impersonator? v) - ;; If there's an impersonator, it's only - ;; an evt impersonator - (unsafe-strip-impersonator v) - v)] - [(input-port? v) - (let ([p (input-port-ref v)]) - (cond - [(struct-accessor-procedure? p) - (->core-input-port (p v))] - [else - (->core-input-port p)]))] - [else - empty-input-port])) + [(core-input-port? v) v] + [(input-port-ref v #f) + => (lambda (p) + (cond + [(struct-accessor-procedure? p) + (->core-input-port (p v))] + [else + (->core-input-port p)]))] + [who (raise-argument-error who "input-port?" v)] + [else empty-input-port])) (struct core-input-port core-port ( diff --git a/racket/src/io/port/output-port.rkt b/racket/src/io/port/output-port.rkt index 49c55f4c74..73895aa57c 100644 --- a/racket/src/io/port/output-port.rkt +++ b/racket/src/io/port/output-port.rkt @@ -34,22 +34,18 @@ ;; This function should not be called in atomic mode, ;; since it can invoke an arbitrary function -(define (->core-output-port v) +(define (->core-output-port v [who #f]) (cond - [(core-output-port? v) (if (impersonator? v) - ;; If there's an impersonator, it's only - ;; an evt impersonator - (unsafe-strip-impersonator v) - v)] - [(output-port? v) - (let ([p (output-port-ref v)]) - (cond - [(struct-accessor-procedure? p) - (->core-output-port (p v))] - [else - (->core-output-port p)]))] - [else - empty-output-port])) + [(core-output-port? v) v] + [(output-port-ref v #f) + => (lambda (p) + (cond + [(struct-accessor-procedure? p) + (->core-output-port (p v))] + [else + (->core-output-port p)]))] + [who (raise-argument-error who "output-port?" v)] + [else empty-output-port])) (struct core-output-port core-port ( diff --git a/racket/src/io/port/special-input.rkt b/racket/src/io/port/special-input.rkt index ea1e707a02..ceddc49bf7 100644 --- a/racket/src/io/port/special-input.rkt +++ b/racket/src/io/port/special-input.rkt @@ -52,11 +52,11 @@ (define/who (read-char-or-special [in (current-input-port)] [special-wrap #f] [source-name #f]) - (check who input-port? in) - (check who #:or-false (procedure-arity-includes/c 1) special-wrap) - (extract-special-value (do-read-char who in #:special-ok? #t) - in source-name -1 - special-wrap)) + (let ([in (->core-input-port in who)]) + (check who #:or-false (procedure-arity-includes/c 1) special-wrap) + (extract-special-value (do-read-char/core-port who in #:special-ok? #t) + in source-name -1 + special-wrap))) (define/who (peek-char-or-special [in (current-input-port)] [skip-k 0] diff --git a/racket/src/io/port/string-input.rkt b/racket/src/io/port/string-input.rkt index b9d4015582..9a235bae9c 100644 --- a/racket/src/io/port/string-input.rkt +++ b/racket/src/io/port/string-input.rkt @@ -1,5 +1,6 @@ #lang racket/base -(require "../common/check.rkt" +(require racket/fixnum + "../common/check.rkt" "../host/thread.rkt" "parameter.rkt" "read-and-peek.rkt" @@ -20,7 +21,6 @@ peek-string peek-string! - do-read-char do-read-char/core-port do-peek-char) @@ -219,7 +219,7 @@ [(eof-object? b) b] [else (cond - [(b . < . 128) (integer->char b)] + [(b . fx< . 128) (integer->char b)] [else ;; UTF-8 decoding... May need to peek bytes to discover ;; whether the decoding will work (in which case this wasn't @@ -251,24 +251,23 @@ #:error-char #\uFFFD #:abort-mode 'state)) (cond - [(= got-chars 1) - (define actually-used-bytes (+ skip-k used-bytes)) - (unless (zero? actually-used-bytes) - (define finish-bstr (if (actually-used-bytes . <= . (bytes-length bstr)) - bstr - (make-bytes actually-used-bytes))) - (do-read-bytes! who in finish-bstr 0 actually-used-bytes)) + [(fx= got-chars 1) + (define actually-used-bytes (fx+ skip-k used-bytes)) + (cond + [(fx= actually-used-bytes 0) (void)] + [(fx= actually-used-bytes 1) (do-read-byte who read-byte in)] + [else + (define finish-bstr (if (actually-used-bytes . fx<= . (bytes-length bstr)) + bstr + (make-bytes actually-used-bytes))) + (do-read-bytes! who in finish-bstr 0 actually-used-bytes)]) (string-ref str 0)] [else - (loop (add1 skip-k) new-state)])]))])])])) + (loop (fx+ skip-k 1) new-state)])]))])])])) ;; ---------------------------------------- ;; If `special-ok?`, can return a special-value procedure -(define (do-read-char who in #:special-ok? [special-ok? #f]) - (let ([in (->core-input-port in)]) - (do-read-char/core-port who in #:special-ok? special-ok?))) - (define (do-read-char/core-port who in #:special-ok? [special-ok? #f]) (define read-byte (core-input-port-read-byte in)) (cond @@ -283,8 +282,8 @@ (read-char-via-read-byte who in read-byte #:special-ok? special-ok?)])) (define/who (read-char [in (current-input-port)]) - (check who input-port? in) - (do-read-char who in)) + (let ([in (->core-input-port in who)]) + (do-read-char/core-port who in))) (define/who (read-string amt [in (current-input-port)]) (check who exact-nonnegative-integer? amt) diff --git a/racket/src/io/string/utf-8-decode.rkt b/racket/src/io/string/utf-8-decode.rkt index 64c74fe51e..26c1bdf9d2 100644 --- a/racket/src/io/string/utf-8-decode.rkt +++ b/racket/src/io/string/utf-8-decode.rkt @@ -138,26 +138,26 @@ (encoding-failure)] [else (define next (fxand b #b00111111)) - (define next-accum (bitwise-ior (arithmetic-shift accum 6) next)) + (define next-accum (fxior (fxlshift accum 6) next)) (cond - [(= 1 remaining) + [(fx= 1 remaining) (cond - [(and (next-accum . > . 127) - (next-accum . <= . #x10FFFF) - (not (and (next-accum . >= . #xD800) - (next-accum . <= . #xDFFF)))) + [(and (next-accum . fx> . 127) + (next-accum . fx<= . #x10FFFF) + (not (and (next-accum . fx>= . #xD800) + (next-accum . fx<= . #xDFFF)))) (when out-str (string-set! out-str j (integer->char next-accum))) (continue)] [else ;; Not a valid character (encoding-failure)])] [(and (fx= 2 remaining) - (next-accum . <= . #b11111)) + (next-accum . fx<= . #b11111)) ;; A shorter byte sequence would work, so this is an ;; encoding mistae. (encoding-failure)] [(and (fx= 3 remaining) - (next-accum . <= . #b1111)) + (next-accum . fx<= . #b1111)) ;; A shorter byte sequence would work (encoding-failure)] [else