;; 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 :escape-xml :is-tg-whitespace :is-tg-whitespace-str)) (in-package :ukkoclot/strings) ;; These are very inefficient but I don't care until I profile (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)) (#\> (write-string ">" out)) (#\& (write-string "&" out)) (#\" (write-string """ out)) (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 (string= gc "Zp") ; Separator, paragraph (string= gc "Cc") ; Other, control (= (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))))