Commit e0970ea65247b4e11ea32764992a58fd248f92c1

Thomas de Grivel 2019-01-16T17:48:01

base-positional

diff --git a/positional.lisp b/positional.lisp
index 46cd6d4..b6e85be 100644
--- a/positional.lisp
+++ b/positional.lisp
@@ -71,19 +71,24 @@
 #+nil (parse "DEADBEEF" 16)
 
 (defmethod base-positional ((base string) (number integer))
-  (let* ((base-n (length base))
-         (out-n (ceiling (log number base-n)))
-         (out (make-string out-n :initial-element (char base 0)))
-         (i (1- out-n)))
-    (loop
-       (when (= number 0)
-         (return out))
-       (when (< i 0)
-         (error "bad math"))
-       (multiple-value-bind (q r) (floor number base-n)
-         (setf number q
-               (char out i) (char base r)))
-       (decf i))))
+  (cond ((< number 0)
+         (concatenate 'string "-" (base-positional base (- number))))
+        ((= number 0)
+         (make-string 1 :initial-element (char base 0)))
+        (t
+         (let* ((base-n (length base))
+                (out-n (ceiling (log number base-n)))
+                (out (make-string out-n :initial-element (char base 0)))
+                (i (1- out-n)))
+           (loop
+              (when (= number 0)
+                (return out))
+              (when (< i 0)
+                (error "bad math"))
+              (multiple-value-bind (q r) (floor number base-n)
+                (setf number q
+                      (char out i) (char base r)))
+              (decf i))))))
 
 #+nil (base-positional (integer-base 16) 100)