diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..99a6f24 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.*~ +*.*fasl +\#**\# diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..fdddb29 --- /dev/null +++ b/LICENSE @@ -0,0 +1,24 @@ +This is free and unencumbered software released into the public domain. + +Anyone is free to copy, modify, publish, use, compile, sell, or +distribute this software, either in source code form or as a compiled +binary, for any purpose, commercial or non-commercial, and by any +means. + +In jurisdictions that recognize copyright laws, the author or authors +of this software dedicate any and all copyright interest in the +software to the public domain. We make this dedication for the benefit +of the public at large and to the detriment of our heirs and +successors. We intend this dedication to be an overt act of +relinquishment in perpetuity of all present and future rights to this +software under copyright law. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +For more information, please refer to diff --git a/README.org b/README.md similarity index 95% rename from README.org rename to README.md index 7f07c08..4d9f7e4 100644 --- a/README.org +++ b/README.md @@ -1,4 +1,5 @@ -* SRFI 48: Intermediate Format Strings +# SRFI 48 for CL: Intermediate Format Strings + - Copyright (C) Kenneth A Dickey (2003). All Rights Reserved. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/package.lisp b/package.lisp index 0140855..43247a2 100644 --- a/package.lisp +++ b/package.lisp @@ -2,14 +2,18 @@ (cl:in-package :cl-user) -(defpackage :srfi-48 + +(defpackage "https://github.com/g000001/srfi-48" (:use) - (:export :format)) + (:export format)) + + +(defpackage "https://github.com/g000001/srfi-48#internals" + (:use cl fiveam) + (:shadow loop map member assoc) + (:shadowing-import-from "https://github.com/g000001/srfi-5" let) + (:shadowing-import-from "https://github.com/g000001/srfi-23" error) + (:shadowing-import-from "https://github.com/g000001/srfi-48" format)) -(defpackage :srfi-48-internal - (:use :cl :fiveam) - (:shadow :loop :map :member :assoc) - (:shadowing-import-from :srfi-5 :let) - (:shadowing-import-from :srfi-23 :error) - (:shadowing-import-from :srfi-48 :format)) +;;; *EOF* diff --git a/srfi-48.asd b/srfi-48.asd index b8c9b64..0982552 100644 --- a/srfi-48.asd +++ b/srfi-48.asd @@ -2,18 +2,42 @@ (cl:in-package :asdf) + (defsystem :srfi-48 + :version "20200307" + :description "SRFI 48 for CL: Intermediate Format Strings" + :long-description "SRFI 48 for CL: Intermediate Format Strings +https://srfi.schemers.org/srfi-48" + :author "Kenneth A Dickey" + :maintainer "CHIBA Masaomi" :serial t :depends-on (:srfi-1 :srfi-5 :srfi-23) :components ((:file "package") - (:file "utils") + ;;(:file "utils") (:file "srfi-48"))) + +(defmethod perform :after ((o load-op) (c (eql (find-system :srfi-48)))) + (let ((name "https://github.com/g000001/srfi-48") + (nickname :srfi-48)) + (if (and (find-package nickname) + (not (eq (find-package nickname) + (find-package name)))) + (warn "~A: A package with name ~A already exists." name nickname) + (rename-package name name `(,nickname))))) + + (defmethod perform ((o test-op) (c (eql (find-system :srfi-48)))) - (load-system :srfi-48) - (or (flet ((_ (pkg sym) - (intern (symbol-name sym) (find-package pkg)))) - (let ((result (funcall (_ :fiveam :run) (_ :srfi-48-internal :srfi-48)))) - (funcall (_ :fiveam :explain!) result) - (funcall (_ :fiveam :results-status) result))) - (error "test-op failed") )) + (let ((*package* + (find-package + "https://github.com/g000001/srfi-48#internals"))) + (eval + (read-from-string + " + (or (let ((result (run 'srfi-48))) + (explain! result) + (results-status result)) + (error \"test-op failed\") )")))) + + +;;; *EOF* diff --git a/srfi-48.lisp b/srfi-48.lisp index 03c725c..c246727 100644 --- a/srfi-48.lisp +++ b/srfi-48.lisp @@ -1,10 +1,9 @@ ;;;; srfi-48.lisp -(cl:in-package :srfi-48-internal) +(cl:in-package "https://github.com/g000001/srfi-48#internals") -(def-suite srfi-48) +(def-suite* srfi-48) -(in-suite srfi-48) ;; IMPLEMENTATION DEPENDENT options @@ -46,37 +45,38 @@ (defun compose-with-digits (digits pre-str frac-str exp-str) (let ((frac-len (length frac-str))) (cond - ((< frac-len digits) ;; grow frac part, pad with zeros - (string-append pre-str "." - frac-str (make-string (- digits frac-len) - :initial-element #\0) - exp-str)) - ((= frac-len digits) ;; frac-part is exactly the right size - (string-append pre-str "." - frac-str - exp-str)) - (:else ;; must round to shrink it - (let* ( (first-part (subseq frac-str 0 digits)) - (last-part (subseq frac-str digits frac-len)) - (temp-str - (write-to-string - (round (read-from-string - (string-append first-part "." last-part))))) - (dot-pos (string-index temp-str #\.)) - (carry? - (and (> dot-pos digits) - (> (round (read-from-string - (string-append "0." frac-str))) - 0))) - (new-frac - (subseq temp-str 0 digits))) - (string-append - (if carry? - (write-to-string (+ 1 (write-to-string pre-str))) - pre-str) - "." - new-frac - exp-str)))))) + ((< frac-len digits) ;; grow frac part, pad with zeros + (string-append pre-str "." + frac-str (make-string (- digits frac-len) + :initial-element #\0) + exp-str)) + ((= frac-len digits) ;; frac-part is exactly the right size + (string-append pre-str "." + frac-str + exp-str)) + (:else ;; must round to shrink it + (let* ( (first-part (subseq frac-str 0 digits)) + (last-part (subseq frac-str digits frac-len)) + (temp-str + (write-to-string + (round (read-from-string + (string-append first-part "." last-part))))) + (dot-pos (string-index temp-str #\.)) + (carry? + (and (> dot-pos digits) + (> (round (read-from-string + (string-append "0." frac-str))) + 0))) + (new-frac + (subseq temp-str 0 digits))) + (string-append + (if carry? + (write-to-string (+ 1 (read-from-string pre-str))) + pre-str) + "." + new-frac + exp-str)))))) + (defun format-fixed (number-or-string width digits) ; returns a string (cond @@ -86,7 +86,7 @@ (let ((real (realpart number-or-string)) (imag (imagpart number-or-string))) (cond - ((not (zero? imag)) + ((not (zerop imag)) (string-grow (string-append (format-fixed real 0 digits) (if (minusp imag) "" "+") @@ -130,8 +130,13 @@ (format "FORMAT: ~F requires a number or a string, got ~s" number-or-string))) )) -(setf (documentation 'format 'function) -"(format [] [...]) -- is T, nil or an output-port +(defun require-an-arg (args) + (when (null args) + (error "FORMAT: too few arguments" ))) + + +(defun format-doc () + "(format [] [...]) -- is T, nil or an output-port OPTION [MNEMONIC] DESCRIPTION -- Implementation Assumes ASCII Text Encoding ~H [Help] output this text ~A [Any] (display arg) for humans @@ -151,11 +156,10 @@ OPTION [MNEMONIC] DESCRIPTION -- Implementation Assumes ASCII Text Enc ~Y [Yuppify] the list arg is pretty-printed to the output ~? [Indirection] recursive format: next 2 args are format-string and list of arguments ~K [Indirection] same as ~? -" ) +") -(defun require-an-arg (args) - (when (null args) - (error "FORMAT: too few arguments" ))) +(setf (documentation 'format 'function) + (format-doc)) (defun format-help (format-strg arglist port) (let ((length-of-format-string (length format-strg))) @@ -172,9 +176,9 @@ OPTION [MNEMONIC] DESCRIPTION -- Implementation Assumes ASCII Text Enc (anychar-dispatch (+ pos 1) arglist nil)))))) (has-newline? (whatever last-was-newline) (or (eql whatever #\newline) - (and (string? whatever) + (and (stringp whatever) (let ( (len (length whatever)) ) - (if (zero? len) + (if (zerop len) last-was-newline (eql #\newline (char whatever (- len 1)))))))) @@ -282,7 +286,7 @@ OPTION [MNEMONIC] DESCRIPTION -- Implementation Assumes ASCII Text Enc (let ((width (read-from-string (coerce (reverse w-digits) 'string))) - (digits (if (zero? (length d-digits)) + (digits (if (zerop (length d-digits)) nil (read-from-string (coerce (reverse d-digits) @@ -308,14 +312,14 @@ OPTION [MNEMONIC] DESCRIPTION -- Implementation Assumes ASCII Text Enc ((< (length arglist) 2) (error (format "FORMAT: less arguments than specified for ~~?: ~s" arglist))) - ((not (string? (car arglist))) + ((not (stringp (car arglist))) (error (format "FORMAT: ~~? requires a string: ~s" (car arglist)))) (:else (format-help (car arglist) (cadr arglist) port) (anychar-dispatch (+ pos 1) (cddr arglist) nil)))) ((#\H) ; Help - (princ (documentation 'format 'function) port) + (princ (format-doc) port) (anychar-dispatch (+ pos 1) arglist T)) (otherwise (error (format "FORMAT: unknown tilde escape: ~s" @@ -365,15 +369,16 @@ OPTION [MNEMONIC] DESCRIPTION -- Implementation Assumes ASCII Text Enc (test format (signals (cl:error) (format)) - (is (string= (format "~A" 'foo) - "FOO")) + (is (string-equal (format "~A" 'foo) + "FOO")) (signals (cl:error) (format nil)) - (is (string= (format nil "~A" 'foo) - "FOO")) + (is (string-equal (format nil "~A" 'foo) + "FOO")) ;; H (is (string= (format "~H") - (documentation 'format 'function))) + (or (documentation 'format 'function) + (format-doc)))) ;; A (is (string= (format "~A" '(1 2 3 4)) "(1 2 3 4)")) @@ -431,3 +436,5 @@ OPTION [MNEMONIC] DESCRIPTION -- Implementation Assumes ASCII Text Enc ;; K (is (string= (format "~K" "~A~A~A" '(1 2 3)) "123"))) + +;;; *EOF* diff --git a/utils.lisp b/utils.lisp deleted file mode 100644 index fc95c2d..0000000 --- a/utils.lisp +++ /dev/null @@ -1,96 +0,0 @@ -(in-package :srfi-48-internal) - -;; from KMRCL -(defmacro defconstant* (sym value &optional doc) - "Ensure VALUE is evaluated only once." - `(defconstant ,sym (if (boundp ',sym) - (symbol-value ',sym) - ,value) - ,@(when doc (list doc)))) - -(defmacro defun-inline (name (&rest args) &body body) - `(progn - (declaim (inline ,name)) - (defun ,name (,@args) - ,@body))) - -(defun-inline map (function list &rest more-list) - (apply #'mapcar function list more-list)) - -(defun-inline mem (function item list) - (cl:member item list :test function)) - -(defun-inline null? (obj) (null obj)) - -(defun-inline eq? (x y) (eq x y)) - -(defun-inline pair? (obj) (consp obj)) - -(defun-inline zero? (x) (zerop x)) - -(defun-inline set-car! (list obj) - (rplaca list obj)) - -(defun-inline set-cdr! (cons x) - (rplacd cons x)) - -(defun-inline equal? (x y) - (equal x y)) - -(defun-inline memq (x list) - (cl:member x list :test #'eq)) - -(defmacro begin (&body body) - `(progn ,@body)) - -(defmacro receive ((&rest args) vals &body body) - `(multiple-value-bind (,@args) ,vals - ,@body)) - -(defun CHECK-ARG-to-DECLARE (expr) - (destructuring-bind (ignore var pred name) expr - (declare (ignore ignore name)) - `(declare ((satisfies ,pred) ,var)))) - -(defun restify (expr) - (etypecase expr - (symbol (list 'cl:&rest expr)) - (list (if (tailp () expr) - expr - (let ((last (last expr))) - (append (butlast expr) - (list (car last) - 'cl:&rest - (cdr last)))))))) - -(defmacro define (name&args &body body) - (etypecase name&args - (list (destructuring-bind (name &rest args) - name&args - (destructuring-bind (decl &rest body) body - (if (string= 'check-arg (car decl)) - `(eval-when (:compile-toplevel :load-toplevel :execute) - (defun ,name (,@(restify args)) - ,(CHECK-ARG-to-DECLARE decl) - ,@body) - (defconstant* ,name (function ,name))) - `(eval-when (:compile-toplevel :load-toplevel :execute) - (defun ,name (,@(restify args)) - ,decl - ,@body) - (defconstant* ,name (function ,name))))))) - (symbol `(setf (symbol-function ',name&args) (progn ,@body))))) - -(define any #'cl:some) -(define foldl #'srfi-1:fold) -(define pair-foldl #'srfi-1:pair-fold) -(define procedure? #'cl:functionp) -(define string? #'cl:stringp) - - - -;; a la lisp1 -(defconstant* equal? #'cl:equal) -(defconstant* eq? #'cl:eq) -(defconstant* eqv? #'cl:eql) -(defconstant* pair? #'cl:consp)