Guest User

Untitled

a guest
Sep 3rd, 2019
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;; Code was a part of (Nietzsche io lowlevel)
  2. ;; use it as you see fit.
  3. ;; use it with the predicate char-whitespace?
  4. ;; (skip-while char-whitespace? port)
  5. ;; and (read-while char-whitespace? port)
  6. ;; to read your data
  7.  
  8.  
  9. (define (%read-until pred buf start len port)
  10.   (if (= start len)
  11.       #f
  12.       (let ([ch (peek-char port)])
  13.         (cond
  14.          [(eof-object? ch)
  15.           (if (zero? start)
  16.              ch
  17.              start)]
  18.          [(pred ch) start]
  19.          [else
  20.           (string-set! buf start ch)
  21.           (read-char port)
  22.           (%read-until pred buf (+ start 1) len port)]))))
  23.  
  24. ;; Buffered read-until.
  25. (define (read-until pred textual-port)
  26.   (let loop ([bufsize 20]
  27.              [buf (make-string 20)]
  28.              [start 0])
  29.     (let ([c (%read-until pred buf start bufsize textual-port)])
  30.       (cond
  31.        [(eof-object? c) c]
  32.        [(not c) ;; buffer too small
  33.         (let* ([newsize (* bufsize 2)]
  34.                [newbuf (make-string newsize)])
  35.           (string-copy! newbuf 0 buf 0 bufsize)
  36.           (loop newsize newbuf bufsize))]
  37.        [else
  38.         (substring buf 0 c)]))))
  39.  
  40. (define (skip-until pred port)
  41.   (let ([ch (peek-char port)])
  42.     (unless (or (eof-object? ch) (pred ch))
  43.       (read-char port)
  44.       (skip-until pred port))))
  45.  
  46. (define (read-while pred textual-port)
  47.   (read-until (negate pred) textual-port))
  48.  
  49. (define (skip-while pred textual-port)
  50.   (skip-until (negate pred) textual-port))
Advertisement
Add Comment
Please, Sign In to add comment