Source

qtility / string.lisp

;;;; string.lisp
;;;; Copyright (c) 2011-2012 Robert Smith

;;;; String-related functions.

(in-package #:qtility)

;;; TODO: Benchmark.
(defun strcat-list (strings &key key)
  "Concatenate all of the STRINGS in a list, optionally applying the
function specified by KEY to each item."
  (with-output-to-string (stream)
    (if key
        (dolist (s strings)
          (princ (funcall key s) stream))
        (dolist (s strings)
          (princ s stream)))))

(defun strcat (&rest strings)
  "Concatenate all strings in ARGS. Useful for when you need to use
APPLY."
  (strcat-list strings))

;;; XXX: Can be made generic.
(defun string-join (joiner list-of-strings)
  "Concatenates the strings contained in LIST-OF-STRINGS, separated by
the string JOINER."
  (cond
    ((null list-of-strings) +empty-string+)
    ((null (cdr list-of-strings)) (first list-of-strings))
    (t (reduce (lambda (x y) (concatenate 'string x joiner y))
               list-of-strings))))