summaryrefslogtreecommitdiff
path: root/str.lisp
blob: aee360476929704f37f2f078ee6cfa6be4e6b969 (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
25
26
27
28
29
30
(in-package #:str)

(declaim (optimize speed))

(declaim (ftype (function ((or pathname string)) (values simple-string &optional)) read-file))
(defun read-file (path)
  (with-open-file (file path)
    (let* ((size (file-length file))
           (buf (make-string size)))
      (read-sequence buf file)
      buf)))

(declaim (ftype (function (simple-string simple-string) (values (cons simple-string) &optional)) split))
(defun split (input delimiter)
  (labels ((split-rec (result start)
                      (let ((next (search delimiter input :start2 start)))
                        (if next
                          (split-rec (queue:add result (subseq input start next)) (min (the fixnum (+ (length delimiter) next))
                                                                                       (the fixnum (length input))))
                          (queue:add result (subseq input start next))))))
    (queue:to-list (split-rec (queue:new) 0))))

(declaim (ftype (function ((or null (cons character))) (values simple-string &optional)) from-list))
(defun from-list (lst)
  (let ((str (make-string (length lst)))
        (i 0))
    (dolist (item lst)
      (setf (char str i) item)
      (incf i))
    str))