mapconcat now deals with empty sequences.

Pascal J. Bourguignon [2015-06-23 07:30]
mapconcat now deals with empty sequences.
Filename
common-lisp/cesarum/string.lisp
diff --git a/common-lisp/cesarum/string.lisp b/common-lisp/cesarum/string.lisp
index 48841dc..5c81737 100644
--- a/common-lisp/cesarum/string.lisp
+++ b/common-lisp/cesarum/string.lisp
@@ -149,7 +149,7 @@ CHARACTER-DESIGNATOR is the type of character or designators of


 (defun mapconcat (function sequence separator)
-    "
+  "

 FUNCTION:   This function is applied on each element of sequence and
             shall return a string designator.
@@ -171,17 +171,19 @@ RETURN:     A string containing the concatenation of the strings
                       sequence))
          (separator (string separator))
          (seplen (length separator))
-         (totlen (+ (reduce (function +) strings :key (function length) :initial-value 0)
-                    (* seplen (1- (length strings)))))
+         (totlen (if (zerop (length strings))
+                     0
+                     (+ (reduce (function +) strings :key (function length) :initial-value 0)
+                        (* seplen (1- (length strings))))))
          (result (make-string totlen)))
-    (loop
-      :for string :in strings
-      :with start = 0
-      :do (replace result string :start1 start)
-          (incf start (length string))
-          (unless (<= totlen start)
-            (replace result separator :start1 start)
-            (incf start seplen)))
+    (let ((start 0))
+      (map nil (lambda (string)
+                 (replace result string :start1 start)
+                 (incf start (length string))
+                 (unless (<= totlen start)
+                   (replace result separator :start1 start)
+                   (incf start seplen)))
+        strings))
     result))

 (defun concatenate-strings (list-of-string-designators)
ViewGit