summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJuan Manuel Tomás <jtomas1815@gmail.com>2022-10-30 18:28:18 -0300
committerJuan Manuel Tomás <jtomas1815@gmail.com>2022-10-30 18:28:18 -0300
commitca2362bc53b548c1e0b2a725545984854fe62413 (patch)
treeac745bc46bbede30c1dc3addeebc5f6f3b1ae3f0
downloadutils-ca2362bc53b548c1e0b2a725545984854fe62413.tar.gz
utils-ca2362bc53b548c1e0b2a725545984854fe62413.zip
Initial Commit
-rw-r--r--package.lisp13
-rw-r--r--queue.lisp29
-rw-r--r--str.lisp21
-rw-r--r--utils.asd6
4 files changed, 69 insertions, 0 deletions
diff --git a/package.lisp b/package.lisp
new file mode 100644
index 0000000..de145ed
--- /dev/null
+++ b/package.lisp
@@ -0,0 +1,13 @@
+(defpackage #:queue
+ (:use #:cl)
+ (:export #:new
+ #:add
+ #:sub
+ #:peek
+ #:to-list
+ #:from-list))
+
+(defpackage #:str
+ (:use #:cl)
+ (:export #:split
+ #:read-file))
diff --git a/queue.lisp b/queue.lisp
new file mode 100644
index 0000000..cc91eb8
--- /dev/null
+++ b/queue.lisp
@@ -0,0 +1,29 @@
+(in-package #:queue)
+
+(declaim (optimize (speed 3) (safety 0)))
+
+(defun new ()
+ (cons nil nil))
+
+(defun add (q datum)
+ (let ((element (cons datum nil)))
+ (if (car q)
+ (progn
+ (setf (cddr q) element)
+ (setf (cdr q) (cddr q)))
+ (progn
+ (setf (car q) element)
+ (setf (cdr q) element)))
+ q))
+
+(defun sub (q)
+ (pop (car q)))
+
+(defun peek (q)
+ (caar q))
+
+(defun to-list (q)
+ (car q))
+
+(defun from-list (lst)
+ (cons lst (last lst)))
diff --git a/str.lisp b/str.lisp
new file mode 100644
index 0000000..e099e26
--- /dev/null
+++ b/str.lisp
@@ -0,0 +1,21 @@
+(in-package #:str)
+
+(declaim (optimize (speed 3) (safety 0)))
+
+(declaim (ftype (function ((or pathname string)) (values 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 string) &optional)) split))
+(defun split (input delimiter)
+ (defun split-rec (result start)
+ (let ((next (search delimiter input :start2 start)))
+ (if next
+ (split-rec (queue:add result (subseq input start next)) (min (+ (length delimiter) next)
+ (length input)))
+ (queue:add result (subseq input start next)))))
+ (queue:to-list (split-rec (queue:new) 0)))
diff --git a/utils.asd b/utils.asd
new file mode 100644
index 0000000..f9bcc50
--- /dev/null
+++ b/utils.asd
@@ -0,0 +1,6 @@
+(asdf:defsystem #:utils
+ :serial t
+ :components
+ ((:file "package")
+ (:file "queue")
+ (:file "str")))