如何从流中读取以空格分隔的单词?
How do I read whitespace-delimited words from a stream?
在 Common Lisp 中,是否有一种简单的方法可以从输入流中读取单个以空格分隔的单词?本质上,我正在寻找与 C 的 scanf("%s", somevar);
等价的东西。
我确实想出了以下内容:
(defun read-word-from-stream (in)
(peek-char t in) ; skip initial whitespace
(do ((str (make-array 16 :fill-pointer 0 :element-type 'standard-char :adjustable t)
(progn (vector-push-extend (read-char in) str) str)))
((let ((c (peek-char nil in)))
(or (char= c #\Newline) (char= c #\Space))) str)))
...虽然可以满足我有限的需求,但对于如此简单的操作来说感觉有点笨拙。理想情况下,我会有一个方法为我做这件事,但是最干净和最短的正确方法是什么,使用任何可用的 Common Lisp 库(最好是与 flexi-streams 一起使用的库)?
使用 peek-char 检测空格
虽然我在 中发帖说没有标准的方法来执行此操作,部分原因是没有通用的空格概念。 (您的版本包括 Space 和换行符,但是 Tab、Vertical Tab、Carriage Return 等呢?)就是说,您对 peek-char 的使用提醒我 peek-char 采用可选的 peek-type 参数,指示是否应跳过空格。如果你使用 both 类型的 peeks,那么当他们不同意时,你一定是打了一个空白字符。这意味着您可以使用如下函数读取直到一个空白字符(其中空白字符的确切含义由实现确定):
(defun read-string (&optional (stream *standard-input*))
(loop
for c = (peek-char nil stream nil nil) ; include whitespace
while (and c (eql c (peek-char t stream nil nil))) ; skip whitespace
collect (read-char stream) into letters
finally (return (coerce letters 'string))))
CL-USER> (read-string)
this is some input
"this"
我在这里使用了 (coerce letters 'string) 来得到一个字符串,但是你也可以使用 with-output-to-string:
(defun read-string (&optional (stream *standard-input*))
(with-output-to-string (out)
(loop
for c = (peek-char nil stream nil nil)
while (and c (eql c (peek-char t stream nil nil)))
do (write-char (read-char stream) out))))
CL-USER> (read-string)
some more input
"some"
对空格使用词汇表条目
空格的词汇表条目说:
whitespace n. 1. one or more characters that are either the graphic
character #\Space or else non-graphic characters such as #\Newline
that only move the print position. 2. a. n. the syntax type of a
character that is a token separator. For details, see Section 2.1.4.7
(Whitespace Characters). b. adj. (of a character) having the
whitespace[2a] syntax type[2]. c. n. a whitespace[2b] character.
根据第一个定义,很容易定义一个粗略的近似值(这不检查打印位置;我不确定是否有可移植的方法来检查):
(defun whitespace-char-p (x)
(or (char= #\space x)
(not (graphic-char-p x))))
那就好办了:
(defun read-until (test &optional (stream *standard-input*))
(with-output-to-string (out)
(loop for c = (peek-char nil stream nil nil)
while (and c (not (funcall test c)))
do (write-char (read-char stream) out))))
CL-USER> (read-until 'whitespace-char-p)
this is some input
"this"
在 Common Lisp 中,是否有一种简单的方法可以从输入流中读取单个以空格分隔的单词?本质上,我正在寻找与 C 的 scanf("%s", somevar);
等价的东西。
我确实想出了以下内容:
(defun read-word-from-stream (in)
(peek-char t in) ; skip initial whitespace
(do ((str (make-array 16 :fill-pointer 0 :element-type 'standard-char :adjustable t)
(progn (vector-push-extend (read-char in) str) str)))
((let ((c (peek-char nil in)))
(or (char= c #\Newline) (char= c #\Space))) str)))
...虽然可以满足我有限的需求,但对于如此简单的操作来说感觉有点笨拙。理想情况下,我会有一个方法为我做这件事,但是最干净和最短的正确方法是什么,使用任何可用的 Common Lisp 库(最好是与 flexi-streams 一起使用的库)?
使用 peek-char 检测空格
虽然我在
(defun read-string (&optional (stream *standard-input*))
(loop
for c = (peek-char nil stream nil nil) ; include whitespace
while (and c (eql c (peek-char t stream nil nil))) ; skip whitespace
collect (read-char stream) into letters
finally (return (coerce letters 'string))))
CL-USER> (read-string)
this is some input
"this"
我在这里使用了 (coerce letters 'string) 来得到一个字符串,但是你也可以使用 with-output-to-string:
(defun read-string (&optional (stream *standard-input*))
(with-output-to-string (out)
(loop
for c = (peek-char nil stream nil nil)
while (and c (eql c (peek-char t stream nil nil)))
do (write-char (read-char stream) out))))
CL-USER> (read-string)
some more input
"some"
对空格使用词汇表条目
空格的词汇表条目说:
whitespace n. 1. one or more characters that are either the graphic character #\Space or else non-graphic characters such as #\Newline that only move the print position. 2. a. n. the syntax type of a character that is a token separator. For details, see Section 2.1.4.7 (Whitespace Characters). b. adj. (of a character) having the whitespace[2a] syntax type[2]. c. n. a whitespace[2b] character.
根据第一个定义,很容易定义一个粗略的近似值(这不检查打印位置;我不确定是否有可移植的方法来检查):
(defun whitespace-char-p (x)
(or (char= #\space x)
(not (graphic-char-p x))))
那就好办了:
(defun read-until (test &optional (stream *standard-input*))
(with-output-to-string (out)
(loop for c = (peek-char nil stream nil nil)
while (and c (not (funcall test c)))
do (write-char (read-char stream) out))))
CL-USER> (read-until 'whitespace-char-p)
this is some input
"this"