From bb4b2bc484ed1d827d707a5a50b234d6994af90c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Manuel=20Tom=C3=A1s?= Date: Mon, 24 Jul 2023 00:09:53 -0300 Subject: Add char, color and name transformers --- str.lisp | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) (limited to 'str.lisp') diff --git a/str.lisp b/str.lisp index aee3604..e00fb0f 100644 --- a/str.lisp +++ b/str.lisp @@ -1,8 +1,10 @@ (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)) @@ -10,17 +12,16 @@ (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)))) + (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)) @@ -28,3 +29,31 @@ (setf (char str i) item) (incf i)) str)) + +(defun underscore->hyphen (str) + (let ((result (make-string-output-stream))) + (dotimes (i (length str)) + (if (char= #\_ (char str i)) + (format result "-") + (format result "~a" (char str i)))) + (get-output-stream-string result))) + +(defun pascal->kebab (str) + (let ((result (make-string-output-stream))) + (dotimes (i (length str)) + (if (and (> i 0) (upper-case-p (char str i))) + (format result "-~a" (char str i)) + (format result "~a" (char str i)))) + (get-output-stream-string result))) + +(defun upcase->pascal (str) + (let ((result (make-string-output-stream))) + (dotimes (i (length str)) + (if (and (< 0 i) + (upper-case-p (char str i)) + (not (lower-case-p (char str (1- i)))) + (or (= i (1- (length str))) + (not (lower-case-p (char str (1+ i)))))) + (format result "~a" (char-downcase (char str i))) + (format result "~a" (char str i)))) + (get-output-stream-string result))) -- cgit v1.2.3