Changeset 6633


Ignore:
Timestamp:
May 31, 2007, 5:06:19 PM (17 years ago)
Author:
Gary Byers
Message:

Try to provide some context - "surrounding characters" - for reader/stream
errors on character input streams.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ccl/level-1/l1-streams.lisp

    r6539 r6633  
    5858    :io
    5959    :output))
     60
     61;;; Try to return a string containing characters that're near the
     62;;; stream's current position, if that makes sense.  Return NIL
     63;;; if it doesn't make sense.
     64(defmethod stream-surrounding-characters ((s stream))
     65  nil)
    6066
    6167
     
    473479
    474480
    475 
    476 
     481(defun %ioblock-surrounding-characters (ioblock)
     482  (let* ((inbuf (ioblock-inbuf ioblock)))
     483    (when inbuf
     484      (let* ((encoding (or (ioblock-encoding ioblock)
     485                           (get-character-encoding nil)))
     486             (size (ash (character-encoding-code-unit-size encoding) -3))
     487             (buffer (io-buffer-buffer inbuf))
     488             (idx (io-buffer-idx inbuf))
     489             (count (io-buffer-count inbuf)))
     490        (unless (= count 0)
     491          (let* ((start (max (- idx (* 5 size)) 0))
     492                 (end (min (+ idx (* 5 size)) count))
     493                 (string (make-string (funcall (character-encoding-length-of-vector-encoding-function encoding) buffer start end))))
     494            (funcall (character-encoding-vector-decode-function encoding)
     495                     buffer
     496                     start
     497                     (- end start)
     498                     string)
     499            (if (position #\Replacement_Character string)
     500              (string-trim (string #\Replacement_Character) string)
     501              string)))))))
     502             
     503       
    477504
    478505
     
    37383765           (synonym-method interactive-stream-p)
    37393766           (synonym-method stream-direction)
    3740            (synonym-method stream-device direction))
     3767           (synonym-method stream-device direction)
     3768           (synonym-method stream-surrounding-characters))
    37413769
    37423770
     
    37973825  (two-way-input-method stream-read-list l c)
    37983826  (two-way-input-method stream-read-vector v start end)
     3827  (two-way-input-method stream-surrounding-characters)
    37993828  (two-way-output-method stream-write-char c)
    38003829  (two-way-output-method stream-write-byte b)
     
    44224451
    44234452
     4453(defmethod stream-surrounding-characters ((s string-input-stream))
     4454  (let* ((ioblock (basic-stream-ioblock s))
     4455         (start (string-input-stream-ioblock-start ioblock))
     4456         (idx (string-input-stream-ioblock-index ioblock))
     4457         (end (string-input-stream-ioblock-end ioblock))
     4458         (string (string-stream-ioblock-string ioblock)))
     4459    (subseq string (max (- idx 5) start) (min (+ idx 5) end))))
     4460   
     4461
    44244462(defmethod stream-position ((s string-input-stream) &optional newpos)
    44254463  (let* ((ioblock (basic-stream-ioblock s))
     
    47054743    (when (open-stream-p stream)
    47064744      (stream-force-output stream))))
     4745
     4746(defmethod stream-surrounding-characters ((stream buffered-character-input-stream-mixin))
     4747    (let* ((ioblock (stream-ioblock stream nil)))
     4748      (and ioblock (%ioblock-surrounding-characters ioblock))))
     4749
     4750(defmethod stream-surrounding-characters ((stream basic-character-input-stream))
     4751    (let* ((ioblock (stream-ioblock stream nil)))
     4752      (and ioblock (%ioblock-surrounding-characters ioblock))))
     4753
    47074754
    47084755#|
Note: See TracChangeset for help on using the changeset viewer.