summaryrefslogtreecommitdiff
path: root/str.lisp
diff options
context:
space:
mode:
authorJuan Manuel Tomás <jtomas1815@gmail.com>2025-06-23 02:23:36 -0300
committerJuan Manuel Tomás <jtomas1815@gmail.com>2025-06-23 02:23:36 -0300
commit97f8b6d2a990fe2e93704460fcdf08701616d7e4 (patch)
treedec4638301655b0f6a7dea9ed8765a02f1eb94e2 /str.lisp
parentbb4b2bc484ed1d827d707a5a50b234d6994af90c (diff)
downloadutils-97f8b6d2a990fe2e93704460fcdf08701616d7e4.tar.gz
utils-97f8b6d2a990fe2e93704460fcdf08701616d7e4.zip
Update utilsHEADmain
Diffstat (limited to 'str.lisp')
-rw-r--r--str.lisp41
1 files changed, 28 insertions, 13 deletions
diff --git a/str.lisp b/str.lisp
index e00fb0f..3d88d11 100644
--- a/str.lisp
+++ b/str.lisp
@@ -1,10 +1,5 @@
(in-package #:str)
-(declaim (optimize speed))
-(declaim (ftype (function ((or pathname string)) (values simple-string &optional)) read-file))
-(declaim (ftype (function (simple-string simple-string) (values (cons simple-string) &optional)) split))
-(declaim (ftype (function ((or null (cons character))) (values simple-string &optional)) from-list))
-
(defun read-file (path)
(with-open-file (file path)
(let* ((size (file-length file))
@@ -13,21 +8,21 @@
buf)))
(defun split (input delimiter)
+ (declare (optimize speed)
+ (simple-string 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))
+ (split-rec (queue:push result (subseq input start next))
(min (the fixnum (+ (length delimiter) next))
(the fixnum (length input))))
- (queue:add result (subseq input start next))))))
+ (queue:push result (subseq input start next))))))
(queue:to-list (split-rec (queue:new) 0))))
-(defun from-list (lst)
- (let ((str (make-string (length lst)))
- (i 0))
- (dolist (item lst)
- (setf (char str i) item)
- (incf i))
+(defun remove-prefix (str prefix)
+ (if (and (> (length str) (length prefix))
+ (string= str prefix :end1 (length prefix)))
+ (subseq str (length prefix))
str))
(defun underscore->hyphen (str)
@@ -57,3 +52,23 @@
(format result "~a" (char-downcase (char str i)))
(format result "~a" (char str i))))
(get-output-stream-string result)))
+
+(defun line-and-column (str index)
+ (let ((line 1) (column 1))
+ (dotimes (i index)
+ (let ((c (char str i)))
+ (case c
+ (#\Newline
+ (incf line)
+ (setf column 1))
+ (t (incf column)))))
+ (cons line column)))
+
+(defun context-window (str index &key (side-length 20))
+ (let ((begin (max (- index side-length) 0))
+ (end (min (+ index side-length) (length str)))
+ (result '()))
+ (push (subseq str begin index) result)
+ (push (elt str index) result)
+ (push (subseq str (1+ index) end) result)
+ result))