summaryrefslogtreecommitdiff
path: root/src/util.lisp
blob: 5cf28fad4aade1bc4a3f89a753c21c07b11fbbd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
(in-package :monkey)

(eval-when (:compile-toplevel :load-toplevel :execute)
  (defmacro eval-always (&body body)
    `(eval-when (:compile-toplevel :load-toplevel :execute)
       ,@body)))

(defun make-string-view (source start end)
  (make-array (- end start) :element-type 'character
              :displaced-to source
              :displaced-index-offset start))

(defun read-file-contents (file-name)
  (let* ((len 0)
         (contents
           (with-output-to-string (out)
             (with-open-file (in file-name :external-format :utf-8)
               (loop :with buffer := (make-array 8192 :element-type 'character)
                     :for n := (read-sequence buffer in)
                     :while (< 0 n)
                     :do (incf len n)
                         (write-sequence buffer out :start 0 :end n))))))
    (values contents len)))