Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;; Code was a part of (Nietzsche io lowlevel)
- ;; use it as you see fit.
- ;; use it with the predicate char-whitespace?
- ;; (skip-while char-whitespace? port)
- ;; and (read-while char-whitespace? port)
- ;; to read your data
- (define (%read-until pred buf start len port)
- (if (= start len)
- #f
- (let ([ch (peek-char port)])
- (cond
- [(eof-object? ch)
- (if (zero? start)
- ch
- start)]
- [(pred ch) start]
- [else
- (string-set! buf start ch)
- (read-char port)
- (%read-until pred buf (+ start 1) len port)]))))
- ;; Buffered read-until.
- (define (read-until pred textual-port)
- (let loop ([bufsize 20]
- [buf (make-string 20)]
- [start 0])
- (let ([c (%read-until pred buf start bufsize textual-port)])
- (cond
- [(eof-object? c) c]
- [(not c) ;; buffer too small
- (let* ([newsize (* bufsize 2)]
- [newbuf (make-string newsize)])
- (string-copy! newbuf 0 buf 0 bufsize)
- (loop newsize newbuf bufsize))]
- [else
- (substring buf 0 c)]))))
- (define (skip-until pred port)
- (let ([ch (peek-char port)])
- (unless (or (eof-object? ch) (pred ch))
- (read-char port)
- (skip-until pred port))))
- (define (read-while pred textual-port)
- (read-until (negate pred) textual-port))
- (define (skip-while pred textual-port)
- (skip-until (negate pred) textual-port))
Advertisement
Add Comment
Please, Sign In to add comment