1

以下get_file函数从磁盘读取文件作为 Scheme R6RS 字符串:

; Gets all characters from a port
(define (read_chars_from_port_rev port result)
  (let ((char (read-char port)))
    (if (eof-object? char)
      result
      (read_chars_from_port_rev port (cons char result)))))

; Gets the contents of a file as a string
; If it doesn't exist, returns empty
(define (get_file file)
  (if (file-exists? file)
    (let ((port (open-input-file file)))
      (let ((text (list->string (reverse (read_chars_from_port_rev port '())))))
        (begin
          (close-input-port port)
          text)))
    ""))

它的工作原理是打开文件,尾调用递归地逐个字符读取链表,直到我们找到 eof,关闭文件,然后反转链表(因为尾调用)并将其转换为字符串.

与 Node.js 相比,这个过程应该很慢readFile,因为它逐个字符地读取字符,并为文件中的每个字符分配一个包含一个单元格的链表。理想情况下,我们应该能够将文件作为字符串缓冲区读取,而无需动态内存分配。

有没有办法get_file使用 R6RS 中可用的原语进行优化?

4

1 回答 1

2

您可以使用get-string-all

> (let* ((fp (open-input-file "my-file.txt"))
         (buf (get-string-all fp)))
    (close-port fp)
    (display buf))
Four score and seven years ago
our fathers brought forth upon this continent,....

这可以通过使用更方便一些call-with-input-file

;;; Returns a string containing the contents of the file `fname`; closes the
;;; input port automatically (unless `get-string-all` does not return for
;;; some reason).
(define (get-file fname)
  (call-with-input-file fname get-string-all))
> (get-file "my-file.txt")
"Four score and seven years ago\nour fathers brought forth upon this continent,....\n"

guard当寻找的文件不存在时(如发布的代码中所示),您可以使用它来促进返回空字符串:

(define (guarded-get-file fname)
  (guard (con
          ((i/o-file-does-not-exist-error? con) ""))
    (call-with-input-file fname get-string-all)))
> (guarded-get-file "my-file.txt")
"Four score and seven years ago\nour fathers brought forth upon this continent,....\n"

> (guarded-get-file "oops.txt")
""
于 2021-05-03T23:01:18.603 回答