From 97f8b6d2a990fe2e93704460fcdf08701616d7e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Manuel=20Tom=C3=A1s?= Date: Mon, 23 Jun 2025 02:23:36 -0300 Subject: Update utils --- str.lisp | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) (limited to 'str.lisp') 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)) -- cgit v1.2.3