From 9e7c8dfab25da9beb86fd4ed4115895eedb4c8ab Mon Sep 17 00:00:00 2001 From: Uko Kokņevičs Date: Sat, 18 Oct 2025 08:42:44 +0300 Subject: Replace bunch of our string utilities with str library --- src/strings.lisp | 55 ++++++++++--------------------------------------------- 1 file changed, 10 insertions(+), 45 deletions(-) (limited to 'src/strings.lisp') diff --git a/src/strings.lisp b/src/strings.lisp index b1b4f00..eae8ec0 100644 --- a/src/strings.lisp +++ b/src/strings.lisp @@ -1,33 +1,30 @@ ;; SPDX-License-Identifier: EUPL-1.2 ;; SPDX-FileCopyrightText: 2025 Uko Kokņevičs (defpackage :ukkoclot/strings + (:documentation "String-oriented utilities.") (:use :c2cl :iterate) (:import-from :cl-unicode :general-category) (:export - :ends-with :escape-xml :is-tg-whitespace - :is-tg-whitespace-str - :lisp->camel-case - :lisp->snake-case - :snake->lisp-case - :starts-with - :starts-with-ignore-case)) + :is-tg-whitespace-str)) (in-package :ukkoclot/strings) ;; These are very inefficient but I don't care until I profile -(defun ends-with (str suffix) - (and (> (length str) (length suffix)) - (string= str suffix :start1 (- (length str) (length suffix))))) - (defun escape-xml (str &optional out) + "Escape special XML characters in the STR. + +OUT is the output stream or `nil' for outputting to a string." (if out (escape-xml% str out) (with-output-to-string (out) (escape-xml% str out)))) (defun escape-xml% (str out) + "See `escape-xml'. + +OUT is always the stream." (loop for ch across str do (case ch (#\< (write-string "<" out)) @@ -37,6 +34,7 @@ (otherwise (write-char ch out))))) (defun is-tg-whitespace (ch) + "Checks if CH on its own would be considered whitespace by telegram." (let ((gc (general-category ch))) (or (string= gc "Zs") ; Separator, space (string= gc "Zl") ; Separator, line @@ -45,39 +43,6 @@ (= (char-code ch) #x2800)))) ; BRAILLE PATTERN BLANK (defun is-tg-whitespace-str (str) + "Checks if message containing just STR would be considered whitespace by telegram." (iter (for ch in-string str) (always (is-tg-whitespace ch)))) - -(defun lisp->camel-case (str) - (with-output-to-string (out) - (let ((should-caps nil)) - (iter (for ch in-string str) - (cond ((char= ch #\-) - (setf should-caps t)) - (should-caps - (write-char (char-upcase ch) out) - (setf should-caps nil)) - (t - (write-char (char-downcase ch) out))))))) - -(defun lisp->snake-case (str) - (with-output-to-string (out) - (loop for ch across str do - (case ch - (#\- (write-char #\_ out)) - (otherwise (write-char ch out)))))) - -(defun snake->lisp-case (str) - (with-output-to-string (out) - (loop for ch across str do - (case ch - (#\_ (write-char #\- out)) - (otherwise (write-char ch out)))))) - -(defun starts-with (str prefix) - (and (> (length str) (length prefix)) - (string= str prefix :end1 (length prefix)))) - -(defun starts-with-ignore-case (str prefix) - (and (> (length str) (length prefix)) - (string-equal str prefix :end1 (length prefix)))) -- cgit v1.2.3