Speed up PSXHASH on complex numbers.
[sbcl.git] / src / code / target-format.lisp
blob0d29792f72b6474fc0ae2e5bfd610df67593dcfa
1 ;;;; functions to implement FORMAT and FORMATTER
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!FORMAT")
14 ;;;; FORMAT
16 (defun format (destination control-string &rest format-arguments)
17 #!+sb-doc
18 "Provides various facilities for formatting output.
19 CONTROL-STRING contains a string to be output, possibly with embedded
20 directives, which are flagged with the escape character \"~\". Directives
21 generally expand into additional text to be output, usually consuming one
22 or more of the FORMAT-ARGUMENTS in the process. A few useful directives
23 are:
24 ~A or ~nA Prints one argument as if by PRINC
25 ~S or ~nS Prints one argument as if by PRIN1
26 ~D or ~nD Prints one argument as a decimal integer
27 ~% Does a TERPRI
28 ~& Does a FRESH-LINE
29 where n is the width of the field in which the object is printed.
31 DESTINATION controls where the result will go. If DESTINATION is T, then
32 the output is sent to the standard output stream. If it is NIL, then the
33 output is returned in a string as the value of the call. Otherwise,
34 DESTINATION must be a stream to which the output will be sent.
36 Example: (FORMAT NIL \"The answer is ~D.\" 10) => \"The answer is 10.\"
38 FORMAT has many additional capabilities not described here. Consult the
39 manual for details."
40 (declare (explicit-check))
41 (etypecase destination
42 (null
43 (with-simple-output-to-string (stream)
44 (%format stream control-string format-arguments)))
45 (string
46 (with-simple-output-to-string (stream destination)
47 (%format stream control-string format-arguments)))
48 ((member t)
49 (%format *standard-output* control-string format-arguments)
50 nil)
51 (stream
52 (%format destination control-string format-arguments)
53 nil)))
55 (defun %format (stream string-or-fun orig-args &optional (args orig-args))
56 (if (functionp string-or-fun)
57 (apply string-or-fun stream args)
58 (catch 'up-and-out
59 (let* ((string (etypecase string-or-fun
60 (simple-string
61 string-or-fun)
62 (string
63 (coerce string-or-fun 'simple-string))))
64 (*default-format-error-control-string* string)
65 (*logical-block-popper* nil))
66 (interpret-directive-list stream (tokenize-control-string string)
67 orig-args args)))))
69 (defun interpret-directive-list (stream directives orig-args args)
70 (if directives
71 (let ((directive (car directives)))
72 (etypecase directive
73 (simple-string
74 (write-string directive stream)
75 (interpret-directive-list stream (cdr directives) orig-args args))
76 (format-directive
77 (multiple-value-bind (new-directives new-args)
78 (let* ((character (format-directive-character directive))
79 (function
80 (typecase character
81 (base-char
82 (svref *format-directive-interpreters* (char-code character)))))
83 (*default-format-error-offset*
84 (1- (format-directive-end directive))))
85 (unless function
86 (error 'format-error
87 :complaint "unknown format directive ~@[(character: ~A)~]"
88 :args (list (char-name character))))
89 (multiple-value-bind (new-directives new-args)
90 (funcall function stream directive
91 (cdr directives) orig-args args)
92 (values new-directives new-args)))
93 (interpret-directive-list stream new-directives
94 orig-args new-args)))))
95 args))
97 ;;;; FORMAT directive definition macros and runtime support
99 (eval-when (:compile-toplevel :execute)
101 ;;; This macro is used to extract the next argument from the current arg list.
102 ;;; This is the version used by format directive interpreters.
103 (sb!xc:defmacro next-arg (&optional offset)
104 `(progn
105 (when (null args)
106 (error 'format-error
107 :complaint "no more arguments"
108 ,@(when offset
109 `(:offset ,offset))))
110 (when *logical-block-popper*
111 (funcall *logical-block-popper*))
112 (pop args)))
114 (sb!xc:defmacro def-complex-format-interpreter (char lambda-list &body body)
115 (let ((defun-name
116 (intern (format nil
117 "~:@(~:C~)-FORMAT-DIRECTIVE-INTERPRETER"
118 char)))
119 (directive (sb!xc:gensym "DIRECTIVE"))
120 (directives (if lambda-list (car (last lambda-list)) (sb!xc:gensym "DIRECTIVES"))))
121 `(progn
122 (defun ,defun-name (stream ,directive ,directives orig-args args)
123 (declare (ignorable stream orig-args args))
124 ,@(if lambda-list
125 `((let ,(mapcar (lambda (var)
126 `(,var
127 (,(symbolicate "FORMAT-DIRECTIVE-" var)
128 ,directive)))
129 (butlast lambda-list))
130 (values (progn ,@body) args)))
131 `((declare (ignore ,directive ,directives))
132 ,@body)))
133 (%set-format-directive-interpreter ,char #',defun-name))))
135 (sb!xc:defmacro def-format-interpreter (char lambda-list &body body)
136 (let ((directives (sb!xc:gensym "DIRECTIVES")))
137 `(def-complex-format-interpreter ,char (,@lambda-list ,directives)
138 ,@body
139 ,directives)))
141 (sb!xc:defmacro interpret-bind-defaults (specs params &body body)
142 (once-only ((params params))
143 (collect ((bindings))
144 (dolist (spec specs)
145 (destructuring-bind (var default) spec
146 (bindings `(,var (let* ((param-and-offset (pop ,params))
147 (offset (car param-and-offset))
148 (param (cdr param-and-offset)))
149 (case param
150 (:arg (or (next-arg offset) ,default))
151 (:remaining (length args))
152 ((nil) ,default)
153 (t param)))))))
154 `(let* ,(bindings)
155 (when ,params
156 (error 'format-error
157 :complaint
158 "too many parameters, expected no more than ~W"
159 :args (list ,(length specs))
160 :offset (caar ,params)))
161 ,@body))))
163 ) ; EVAL-WHEN
165 ;;;; format interpreters and support functions for simple output
167 (defun format-write-field (stream string mincol colinc minpad padchar padleft)
168 (when (and colinc (<= colinc 0))
169 (error 'format-error
170 :complaint "The value of colinc is ~a, should be a positive integer"
171 :args (list colinc)))
172 (when (and mincol (< mincol 0))
173 (error 'format-error
174 :complaint "The value of mincol is ~a, should be a non-negative integer"
175 :args (list mincol)))
176 (unless padleft
177 (write-string string stream))
178 (dotimes (i minpad)
179 (write-char padchar stream))
180 ;; As of sbcl-0.6.12.34, we could end up here when someone tries to
181 ;; print e.g. (FORMAT T "~F" "NOTFLOAT"), in which case ANSI says
182 ;; we're supposed to soldier on bravely, and so we have to deal with
183 ;; the unsupplied-MINCOL-and-COLINC case without blowing up.
184 (when (and mincol colinc)
185 (do ((chars (+ (length string) (max minpad 0)) (+ chars colinc)))
186 ((>= chars mincol))
187 (dotimes (i colinc)
188 (write-char padchar stream))))
189 (when padleft
190 (write-string string stream)))
192 (defun format-princ (stream arg colonp atsignp mincol colinc minpad padchar)
193 (format-write-field stream
194 (if (or arg (not colonp))
195 (princ-to-string arg)
196 "()")
197 mincol colinc minpad padchar atsignp))
199 (def-format-interpreter #\A (colonp atsignp params)
200 (if params
201 (interpret-bind-defaults ((mincol 0) (colinc 1) (minpad 0)
202 (padchar #\space))
203 params
204 (format-princ stream (next-arg) colonp atsignp
205 mincol colinc minpad padchar))
206 (princ (if colonp (or (next-arg) "()") (next-arg)) stream)))
208 (defun format-prin1 (stream arg colonp atsignp mincol colinc minpad padchar)
209 (format-write-field stream
210 (if (or arg (not colonp))
211 (prin1-to-string arg)
212 "()")
213 mincol colinc minpad padchar atsignp))
215 (def-format-interpreter #\S (colonp atsignp params)
216 (cond (params
217 (interpret-bind-defaults ((mincol 0) (colinc 1) (minpad 0)
218 (padchar #\space))
219 params
220 (format-prin1 stream (next-arg) colonp atsignp
221 mincol colinc minpad padchar)))
222 (colonp
223 (let ((arg (next-arg)))
224 (if arg
225 (prin1 arg stream)
226 (princ "()" stream))))
228 (prin1 (next-arg) stream))))
230 (def-format-interpreter #\C (colonp atsignp params)
231 (interpret-bind-defaults () params
232 (let ((arg (next-arg)))
233 (unless (typep arg 'character)
234 (error 'format-error
235 :complaint "~s is not of type CHARACTER."
236 :args (list arg)))
237 (cond (colonp
238 (format-print-named-character arg stream))
239 (atsignp
240 (prin1 arg stream))
242 (write-char arg stream))))))
244 ;;; "printing" as defined in the ANSI CL glossary, which is normative.
245 (defun char-printing-p (char)
246 (and (not (eql char #\Space))
247 (graphic-char-p char)))
249 (defun format-print-named-character (char stream)
250 (cond ((not (char-printing-p char))
251 (write-string (string-capitalize (char-name char)) stream))
253 (write-char char stream))))
255 (def-format-interpreter #\W (colonp atsignp params)
256 (interpret-bind-defaults () params
257 (let ((*print-pretty* (or colonp *print-pretty*))
258 (*print-level* (unless atsignp *print-level*))
259 (*print-length* (unless atsignp *print-length*)))
260 (output-object (next-arg) stream))))
262 ;;;; format interpreters and support functions for integer output
264 ;;; FORMAT-PRINT-NUMBER does most of the work for the numeric printing
265 ;;; directives. The parameters are interpreted as defined for ~D.
266 (defun format-print-integer (stream number print-commas-p print-sign-p
267 radix mincol padchar commachar commainterval)
268 (let ((*print-base* radix)
269 (*print-radix* nil))
270 (if (integerp number)
271 (let* ((text (princ-to-string (abs number)))
272 (commaed (if print-commas-p
273 (format-add-commas text commachar commainterval)
274 text))
275 (signed (cond ((minusp number)
276 (concatenate 'string "-" commaed))
277 (print-sign-p
278 (concatenate 'string "+" commaed))
279 (t commaed))))
280 ;; colinc = 1, minpad = 0, padleft = t
281 (format-write-field stream signed mincol 1 0 padchar t))
282 (princ number stream))))
284 (defun format-add-commas (string commachar commainterval)
285 (let ((length (length string)))
286 (multiple-value-bind (commas extra) (truncate (1- length) commainterval)
287 (let ((new-string (make-string (+ length commas)))
288 (first-comma (1+ extra)))
289 (replace new-string string :end1 first-comma :end2 first-comma)
290 (do ((src first-comma (+ src commainterval))
291 (dst first-comma (+ dst commainterval 1)))
292 ((= src length))
293 (setf (schar new-string dst) commachar)
294 (replace new-string string :start1 (1+ dst)
295 :start2 src :end2 (+ src commainterval)))
296 new-string))))
298 (eval-when (:compile-toplevel :execute)
299 (sb!xc:defmacro interpret-format-integer (base)
300 `(if (or colonp atsignp params)
301 (interpret-bind-defaults
302 ((mincol 0) (padchar #\space) (commachar #\,) (commainterval 3))
303 params
304 (format-print-integer stream (next-arg) colonp atsignp ,base mincol
305 padchar commachar commainterval))
306 (let ((*print-base* ,base)
307 (*print-radix* nil))
308 (princ (next-arg) stream))))
309 ) ; EVAL-WHEN
311 (def-format-interpreter #\D (colonp atsignp params)
312 (interpret-format-integer 10))
314 (def-format-interpreter #\B (colonp atsignp params)
315 (interpret-format-integer 2))
317 (def-format-interpreter #\O (colonp atsignp params)
318 (interpret-format-integer 8))
320 (def-format-interpreter #\X (colonp atsignp params)
321 (interpret-format-integer 16))
323 (def-format-interpreter #\R (colonp atsignp params)
324 (interpret-bind-defaults
325 ((base nil) (mincol 0) (padchar #\space) (commachar #\,)
326 (commainterval 3))
327 params
328 (let ((arg (next-arg)))
329 (unless (or base
330 (integerp arg))
331 (error 'format-error
332 :complaint "~s is not of type INTEGER."
333 :args (list arg)))
334 (if base
335 (format-print-integer stream arg colonp atsignp base mincol
336 padchar commachar commainterval)
337 (if atsignp
338 (if colonp
339 (format-print-old-roman stream arg)
340 (format-print-roman stream arg))
341 (if colonp
342 (format-print-ordinal stream arg)
343 (format-print-cardinal stream arg)))))))
345 (defparameter *cardinal-ones*
346 #(nil "one" "two" "three" "four" "five" "six" "seven" "eight" "nine"))
348 (defparameter *cardinal-tens*
349 #(nil nil "twenty" "thirty" "forty"
350 "fifty" "sixty" "seventy" "eighty" "ninety"))
352 (defparameter *cardinal-teens*
353 #("ten" "eleven" "twelve" "thirteen" "fourteen" ;;; RAD
354 "fifteen" "sixteen" "seventeen" "eighteen" "nineteen"))
356 (defparameter *cardinal-periods*
357 #("" " thousand" " million" " billion" " trillion" " quadrillion"
358 " quintillion" " sextillion" " septillion" " octillion" " nonillion"
359 " decillion" " undecillion" " duodecillion" " tredecillion"
360 " quattuordecillion" " quindecillion" " sexdecillion" " septendecillion"
361 " octodecillion" " novemdecillion" " vigintillion"))
363 (defparameter *ordinal-ones*
364 #(nil "first" "second" "third" "fourth"
365 "fifth" "sixth" "seventh" "eighth" "ninth"))
367 (defparameter *ordinal-tens*
368 #(nil "tenth" "twentieth" "thirtieth" "fortieth"
369 "fiftieth" "sixtieth" "seventieth" "eightieth" "ninetieth"))
371 (defun format-print-small-cardinal (stream n)
372 (multiple-value-bind (hundreds rem) (truncate n 100)
373 (when (plusp hundreds)
374 (write-string (svref *cardinal-ones* hundreds) stream)
375 (write-string " hundred" stream)
376 (when (plusp rem)
377 (write-char #\space stream)))
378 (when (plusp rem)
379 (multiple-value-bind (tens ones) (truncate rem 10)
380 (cond ((< 1 tens)
381 (write-string (svref *cardinal-tens* tens) stream)
382 (when (plusp ones)
383 (write-char #\- stream)
384 (write-string (svref *cardinal-ones* ones) stream)))
385 ((= tens 1)
386 (write-string (svref *cardinal-teens* ones) stream))
387 ((plusp ones)
388 (write-string (svref *cardinal-ones* ones) stream)))))))
390 (defun format-print-cardinal (stream n)
391 (cond ((minusp n)
392 (write-string "negative " stream)
393 (format-print-cardinal-aux stream (- n) 0 n))
394 ((zerop n)
395 (write-string "zero" stream))
397 (format-print-cardinal-aux stream n 0 n))))
399 (defun format-print-cardinal-aux (stream n period err)
400 (multiple-value-bind (beyond here) (truncate n 1000)
401 (unless (<= period 21)
402 (error "number too large to print in English: ~:D" err))
403 (unless (zerop beyond)
404 (format-print-cardinal-aux stream beyond (1+ period) err))
405 (unless (zerop here)
406 (unless (zerop beyond)
407 (write-char #\space stream))
408 (format-print-small-cardinal stream here)
409 (write-string (svref *cardinal-periods* period) stream))))
411 (defun format-print-ordinal (stream n)
412 (when (minusp n)
413 (write-string "negative " stream))
414 (let ((number (abs n)))
415 (multiple-value-bind (top bot) (truncate number 100)
416 (unless (zerop top)
417 (format-print-cardinal stream (- number bot)))
418 (when (and (plusp top) (plusp bot))
419 (write-char #\space stream))
420 (multiple-value-bind (tens ones) (truncate bot 10)
421 (cond ((= bot 12) (write-string "twelfth" stream))
422 ((= tens 1)
423 (write-string (svref *cardinal-teens* ones) stream);;;RAD
424 (write-string "th" stream))
425 ((and (zerop tens) (plusp ones))
426 (write-string (svref *ordinal-ones* ones) stream))
427 ((and (zerop ones)(plusp tens))
428 (write-string (svref *ordinal-tens* tens) stream))
429 ((plusp bot)
430 (write-string (svref *cardinal-tens* tens) stream)
431 (write-char #\- stream)
432 (write-string (svref *ordinal-ones* ones) stream))
433 ((plusp number)
434 (write-string "th" stream))
436 (write-string "zeroth" stream)))))))
438 ;;; Print Roman numerals
440 (defun format-print-old-roman (stream n)
441 (unless (< 0 n 5000)
442 (error "Number too large to print in old Roman numerals: ~:D" n))
443 (do ((char-list '(#\D #\C #\L #\X #\V #\I) (cdr char-list))
444 (val-list '(500 100 50 10 5 1) (cdr val-list))
445 (cur-char #\M (car char-list))
446 (cur-val 1000 (car val-list))
447 (start n (do ((i start (progn
448 (write-char cur-char stream)
449 (- i cur-val))))
450 ((< i cur-val) i))))
451 ((zerop start))))
453 (defun format-print-roman (stream n)
454 (unless (< 0 n 4000)
455 (error "Number too large to print in Roman numerals: ~:D" n))
456 (do ((char-list '(#\D #\C #\L #\X #\V #\I) (cdr char-list))
457 (val-list '(500 100 50 10 5 1) (cdr val-list))
458 (sub-chars '(#\C #\X #\X #\I #\I) (cdr sub-chars))
459 (sub-val '(100 10 10 1 1 0) (cdr sub-val))
460 (cur-char #\M (car char-list))
461 (cur-val 1000 (car val-list))
462 (cur-sub-char #\C (car sub-chars))
463 (cur-sub-val 100 (car sub-val))
464 (start n (do ((i start (progn
465 (write-char cur-char stream)
466 (- i cur-val))))
467 ((< i cur-val)
468 (cond ((<= (- cur-val cur-sub-val) i)
469 (write-char cur-sub-char stream)
470 (write-char cur-char stream)
471 (- i (- cur-val cur-sub-val)))
472 (t i))))))
473 ((zerop start))))
475 ;;;; plural
477 (def-format-interpreter #\P (colonp atsignp params)
478 (interpret-bind-defaults () params
479 (let ((arg (if colonp
480 (if (eq orig-args args)
481 (error 'format-error
482 :complaint "no previous argument")
483 (do ((arg-ptr orig-args (cdr arg-ptr)))
484 ((eq (cdr arg-ptr) args)
485 (car arg-ptr))))
486 (next-arg))))
487 (if atsignp
488 (write-string (if (eql arg 1) "y" "ies") stream)
489 (unless (eql arg 1) (write-char #\s stream))))))
491 ;;;; format interpreters and support functions for floating point output
493 (defun decimal-string (n)
494 (write-to-string n :base 10 :radix nil :escape nil))
496 (def-format-interpreter #\F (colonp atsignp params)
497 (when colonp
498 (error 'format-error
499 :complaint
500 "cannot specify the colon modifier with this directive"))
501 (interpret-bind-defaults ((w nil) (d nil) (k nil) (ovf nil) (pad #\space))
502 params
503 (format-fixed stream (next-arg) w d k ovf pad atsignp)))
505 (defun format-fixed (stream number w d k ovf pad atsign)
506 (typecase number
507 (float
508 (format-fixed-aux stream number w d k ovf pad atsign))
509 (rational
510 (format-fixed-aux stream (coerce number 'single-float)
511 w d k ovf pad atsign))
512 (number
513 (format-write-field stream (decimal-string number) w 1 0 #\space t))
515 (let ((*print-base* 10))
516 (format-princ stream number nil nil w 1 0 pad)))))
518 ;;; We return true if we overflowed, so that ~G can output the overflow char
519 ;;; instead of spaces.
520 (defun format-fixed-aux (stream number w d k ovf pad atsign)
521 (declare (type float number))
522 (cond
523 ((or (float-infinity-p number)
524 (float-nan-p number))
525 (prin1 number stream)
526 nil)
528 (sb!impl::string-dispatch (single-float double-float)
529 number
530 (let ((spaceleft w))
531 (when (and w (or atsign (minusp (float-sign number))))
532 (decf spaceleft))
533 (multiple-value-bind (str len lpoint tpoint)
534 (sb!impl::flonum-to-string (abs number) spaceleft d k)
535 ;; if caller specifically requested no fraction digits, suppress the
536 ;; optional trailing zero
537 (when (and d (zerop d))
538 (setq tpoint nil))
539 (when w
540 (decf spaceleft len)
541 ;; optional leading zero
542 (when lpoint
543 (if (or (> spaceleft 0) tpoint) ;force at least one digit
544 (decf spaceleft)
545 (setq lpoint nil)))
546 ;; optional trailing zero
547 (when tpoint
548 (if (> spaceleft 0)
549 (decf spaceleft)
550 (setq tpoint nil))))
551 (cond ((and w (< spaceleft 0) ovf)
552 ;; field width overflow
553 (dotimes (i w)
554 (write-char ovf stream))
557 (when w
558 (dotimes (i spaceleft)
559 (write-char pad stream)))
560 (if (minusp (float-sign number))
561 (write-char #\- stream)
562 (when atsign
563 (write-char #\+ stream)))
564 (when lpoint
565 (write-char #\0 stream))
566 (write-string str stream)
567 (when tpoint
568 (write-char #\0 stream))
569 nil))))))))
571 (def-format-interpreter #\E (colonp atsignp params)
572 (when colonp
573 (error 'format-error
574 :complaint
575 "cannot specify the colon modifier with this directive"))
576 (interpret-bind-defaults
577 ((w nil) (d nil) (e nil) (k 1) (ovf nil) (pad #\space) (mark nil))
578 params
579 (format-exponential stream (next-arg) w d e k ovf pad mark atsignp)))
581 (defun format-exponential (stream number w d e k ovf pad marker atsign)
582 (if (numberp number)
583 (if (floatp number)
584 (format-exp-aux stream number w d e k ovf pad marker atsign)
585 (if (rationalp number)
586 (format-exp-aux stream
587 (coerce number 'single-float)
588 w d e k ovf pad marker atsign)
589 (format-write-field stream
590 (decimal-string number)
591 w 1 0 #\space t)))
592 (let ((*print-base* 10))
593 (format-princ stream number nil nil w 1 0 pad))))
595 (defun format-exponent-marker (number)
596 (if (case *read-default-float-format*
597 ((short-float single-float)
598 (typep number 'single-float))
599 ((double-float #!-long-float long-float)
600 (typep number 'double-float))
601 #!+long-float
602 (long-float
603 (typep number 'long-float)))
605 (typecase number
606 (single-float #\f)
607 (double-float #\d)
608 (short-float #\s)
609 (long-float #\l))))
611 ;;; Here we prevent the scale factor from shifting all significance out of
612 ;;; a number to the right. We allow insignificant zeroes to be shifted in
613 ;;; to the left right, athough it is an error to specify k and d such that this
614 ;;; occurs. Perhaps we should detect both these condtions and flag them as
615 ;;; errors. As for now, we let the user get away with it, and merely guarantee
616 ;;; that at least one significant digit will appear.
618 ;;; Raymond Toy writes: The Hyperspec seems to say that the exponent
619 ;;; marker is always printed. Make it so. Also, the original version
620 ;;; causes errors when printing infinities or NaN's. The Hyperspec is
621 ;;; silent here, so let's just print out infinities and NaN's instead
622 ;;; of causing an error.
623 (defun format-exp-aux (stream number w d e k ovf pad marker atsign)
624 (declare (type float number))
625 (if (or (float-infinity-p number)
626 (float-nan-p number))
627 (prin1 number stream)
628 (multiple-value-bind (num expt) (sb!impl::scale-exponent (abs number))
629 (let* ((k (if (= num 1.0) (1- k) k))
630 (expt (- expt k))
631 (estr (decimal-string (abs expt)))
632 (elen (if e (max (length estr) e) (length estr)))
633 spaceleft)
634 (when w
635 (setf spaceleft (- w 2 elen))
636 (when (or atsign (minusp (float-sign number)))
637 (decf spaceleft)))
638 (if (and w ovf e (> elen e)) ;exponent overflow
639 (dotimes (i w) (write-char ovf stream))
640 (let* ((fdig (if d (if (plusp k) (1+ (- d k)) d) nil))
641 (fmin (if (minusp k) 1 fdig)))
642 (multiple-value-bind (fstr flen lpoint tpoint)
643 (sb!impl::flonum-to-string num spaceleft fdig k fmin)
644 (when (and d (zerop d)) (setq tpoint nil))
645 (when w
646 (decf spaceleft flen)
647 (when lpoint
648 (if (or (> spaceleft 0) tpoint)
649 (decf spaceleft)
650 (setq lpoint nil)))
651 (when (and tpoint (<= spaceleft 0))
652 (setq tpoint nil)))
653 (cond ((and w (< spaceleft 0) ovf)
654 ;;significand overflow
655 (dotimes (i w) (write-char ovf stream)))
656 (t (when w
657 (dotimes (i spaceleft) (write-char pad stream)))
658 (if (minusp (float-sign number))
659 (write-char #\- stream)
660 (if atsign (write-char #\+ stream)))
661 (when lpoint (write-char #\0 stream))
662 (write-string fstr stream)
663 (write-char (if marker
664 marker
665 (format-exponent-marker number))
666 stream)
667 (write-char (if (minusp expt) #\- #\+) stream)
668 (when e
669 ;;zero-fill before exponent if necessary
670 (dotimes (i (- e (length estr)))
671 (write-char #\0 stream)))
672 (write-string estr stream))))))))))
674 (def-format-interpreter #\G (colonp atsignp params)
675 (when colonp
676 (error 'format-error
677 :complaint
678 "cannot specify the colon modifier with this directive"))
679 (interpret-bind-defaults
680 ((w nil) (d nil) (e nil) (k nil) (ovf nil) (pad #\space) (mark nil))
681 params
682 (format-general stream (next-arg) w d e k ovf pad mark atsignp)))
684 (defun format-general (stream number w d e k ovf pad marker atsign)
685 (if (numberp number)
686 (if (floatp number)
687 (format-general-aux stream number w d e k ovf pad marker atsign)
688 (if (rationalp number)
689 (format-general-aux stream
690 (coerce number 'single-float)
691 w d e k ovf pad marker atsign)
692 (format-write-field stream
693 (decimal-string number)
694 w 1 0 #\space t)))
695 (let ((*print-base* 10))
696 (format-princ stream number nil nil w 1 0 pad))))
698 ;;; Raymond Toy writes: same change as for format-exp-aux
699 (defun format-general-aux (stream number w d e k ovf pad marker atsign)
700 (declare (type float number))
701 (if (or (float-infinity-p number)
702 (float-nan-p number))
703 (prin1 number stream)
704 (multiple-value-bind (ignore n) (sb!impl::scale-exponent (abs number))
705 (declare (ignore ignore))
706 ;; KLUDGE: Default d if omitted. The procedure is taken directly from
707 ;; the definition given in the manual, and is not very efficient, since
708 ;; we generate the digits twice. Future maintainers are encouraged to
709 ;; improve on this. -- rtoy?? 1998??
710 (unless d
711 (multiple-value-bind (str len)
712 (sb!impl::flonum-to-string (abs number))
713 (declare (ignore str))
714 (let ((q (if (= len 1) 1 (1- len))))
715 (setq d (max q (min n 7))))))
716 (let* ((ee (if e (+ e 2) 4))
717 (ww (if w (- w ee) nil))
718 (dd (- d n)))
719 (cond ((<= 0 dd d)
720 (let ((char (if (format-fixed-aux stream number ww dd nil
721 ovf pad atsign)
723 #\space)))
724 (dotimes (i ee) (write-char char stream))))
726 (format-exp-aux stream number w d e (or k 1)
727 ovf pad marker atsign)))))))
729 (def-format-interpreter #\$ (colonp atsignp params)
730 (interpret-bind-defaults ((d 2) (n 1) (w 0) (pad #\space)) params
731 (format-dollars stream (next-arg) d n w pad colonp atsignp)))
733 (defun format-dollars (stream number d n w pad colon atsign)
734 (when (rationalp number)
735 ;; This coercion to SINGLE-FLOAT seems as though it gratuitously
736 ;; loses precision (why not LONG-FLOAT?) but it's the default
737 ;; behavior in the ANSI spec, so in some sense it's the right
738 ;; thing, and at least the user shouldn't be surprised.
739 (setq number (coerce number 'single-float)))
740 (if (floatp number)
741 (let* ((signstr (if (minusp (float-sign number))
743 (if atsign "+" "")))
744 (signlen (length signstr)))
745 (multiple-value-bind (str strlen ig2 ig3 pointplace)
746 (sb!impl::flonum-to-string number nil d nil)
747 (declare (ignore ig2 ig3 strlen))
748 (when colon
749 (write-string signstr stream))
750 (dotimes (i (- w signlen (max n pointplace) 1 d))
751 (write-char pad stream))
752 (unless colon
753 (write-string signstr stream))
754 (dotimes (i (- n pointplace))
755 (write-char #\0 stream))
756 (write-string str stream)))
757 (let ((*print-base* 10))
758 (format-write-field stream
759 (princ-to-string number)
760 w 1 0 #\space t))))
762 ;;;; FORMAT interpreters and support functions for line/page breaks etc.
764 (def-format-interpreter #\% (colonp atsignp params)
765 (when (or colonp atsignp)
766 (error 'format-error
767 :complaint
768 "cannot specify either colon or atsign for this directive"))
769 (interpret-bind-defaults ((count 1)) params
770 (dotimes (i count)
771 (terpri stream))))
773 (def-format-interpreter #\& (colonp atsignp params)
774 (when (or colonp atsignp)
775 (error 'format-error
776 :complaint
777 "cannot specify either colon or atsign for this directive"))
778 (interpret-bind-defaults ((count 1)) params
779 (when (plusp count)
780 (fresh-line stream)
781 (dotimes (i (1- count))
782 (terpri stream)))))
784 (def-format-interpreter #\| (colonp atsignp params)
785 (when (or colonp atsignp)
786 (error 'format-error
787 :complaint
788 "cannot specify either colon or atsign for this directive"))
789 (interpret-bind-defaults ((count 1)) params
790 (dotimes (i count)
791 (write-char (code-char form-feed-char-code) stream))))
793 (def-format-interpreter #\~ (colonp atsignp params)
794 (when (or colonp atsignp)
795 (error 'format-error
796 :complaint
797 "cannot specify either colon or atsign for this directive"))
798 (interpret-bind-defaults ((count 1)) params
799 (dotimes (i count)
800 (write-char #\~ stream))))
802 (def-complex-format-interpreter #\newline (colonp atsignp params directives)
803 (when (and colonp atsignp)
804 (error 'format-error
805 :complaint
806 "cannot specify both colon and atsign for this directive"))
807 (interpret-bind-defaults () params
808 (when atsignp
809 (write-char #\newline stream)))
810 (if (and (not colonp)
811 directives
812 (simple-string-p (car directives)))
813 (cons (string-left-trim *format-whitespace-chars*
814 (car directives))
815 (cdr directives))
816 directives))
818 ;;;; format interpreters and support functions for tabs and simple pretty
819 ;;;; printing
821 (def-format-interpreter #\T (colonp atsignp params)
822 (if colonp
823 (interpret-bind-defaults ((n 1) (m 1)) params
824 (pprint-tab (if atsignp :section-relative :section) n m stream))
825 (if atsignp
826 (interpret-bind-defaults ((colrel 1) (colinc 1)) params
827 (format-relative-tab stream colrel colinc))
828 (interpret-bind-defaults ((colnum 1) (colinc 1)) params
829 (format-absolute-tab stream colnum colinc)))))
831 (defun output-spaces (stream n)
832 (let ((spaces #.(make-string 100 :initial-element #\space)))
833 (loop
834 (when (< n (length spaces))
835 (return))
836 (write-string spaces stream)
837 (decf n (length spaces)))
838 (write-string spaces stream :end n)))
840 (defun format-relative-tab (stream colrel colinc)
841 (if (sb!pretty:pretty-stream-p stream)
842 (pprint-tab :line-relative colrel colinc stream)
843 (let* ((cur (sb!impl::charpos stream))
844 (spaces (if (and cur (plusp colinc))
845 (- (* (ceiling (+ cur colrel) colinc) colinc) cur)
846 colrel)))
847 (output-spaces stream spaces))))
849 (defun format-absolute-tab (stream colnum colinc)
850 (if (sb!pretty:pretty-stream-p stream)
851 (pprint-tab :line colnum colinc stream)
852 (let ((cur (sb!impl::charpos stream)))
853 (cond ((null cur)
854 (write-string " " stream))
855 ((< cur colnum)
856 (output-spaces stream (- colnum cur)))
858 (unless (zerop colinc)
859 (output-spaces stream
860 (- colinc (rem (- cur colnum) colinc)))))))))
862 (def-format-interpreter #\_ (colonp atsignp params)
863 (interpret-bind-defaults () params
864 (pprint-newline (if colonp
865 (if atsignp
866 :mandatory
867 :fill)
868 (if atsignp
869 :miser
870 :linear))
871 stream)))
873 (def-format-interpreter #\I (colonp atsignp params)
874 (when atsignp
875 (error 'format-error
876 :complaint "cannot specify the at-sign modifier"))
877 (interpret-bind-defaults ((n 0)) params
878 (pprint-indent (if colonp :current :block) n stream)))
880 ;;;; format interpreter for ~*
882 (def-format-interpreter #\* (colonp atsignp params)
883 (if atsignp
884 (if colonp
885 (error 'format-error
886 :complaint "cannot specify both colon and at-sign")
887 (interpret-bind-defaults ((posn 0)) params
888 (if (<= 0 posn (length orig-args))
889 (setf args (nthcdr posn orig-args))
890 (error 'format-error
891 :complaint "Index ~W is out of bounds. (It should ~
892 have been between 0 and ~W.)"
893 :args (list posn (length orig-args))))))
894 (if colonp
895 (interpret-bind-defaults ((n 1)) params
896 (do ((cur-posn 0 (1+ cur-posn))
897 (arg-ptr orig-args (cdr arg-ptr)))
898 ((eq arg-ptr args)
899 (let ((new-posn (- cur-posn n)))
900 (if (<= 0 new-posn (length orig-args))
901 (setf args (nthcdr new-posn orig-args))
902 (error 'format-error
903 :complaint
904 "Index ~W is out of bounds. (It should
905 have been between 0 and ~W.)"
906 :args
907 (list new-posn (length orig-args))))))))
908 (interpret-bind-defaults ((n 1)) params
909 (dotimes (i n)
910 (next-arg))))))
912 ;;;; format interpreter for indirection
914 (def-format-interpreter #\? (colonp atsignp params string end)
915 (when colonp
916 (error 'format-error
917 :complaint "cannot specify the colon modifier"))
918 (interpret-bind-defaults () params
919 (handler-bind
920 ((format-error
921 (lambda (condition)
922 (error 'format-error
923 :complaint
924 "~A~%while processing indirect format string:"
925 :args (list condition)
926 :print-banner nil
927 :control-string string
928 :offset (1- end)))))
929 (if atsignp
930 (setf args (%format stream (next-arg) orig-args args))
931 (%format stream (next-arg) (next-arg))))))
933 ;;;; format interpreters for capitalization
935 (def-complex-format-interpreter #\( (colonp atsignp params directives)
936 (let ((close (find-directive directives #\) nil)))
937 (unless close
938 (error 'format-error
939 :complaint "no corresponding close paren"))
940 (interpret-bind-defaults () params
941 (let* ((posn (position close directives))
942 (before (subseq directives 0 posn))
943 (after (nthcdr (1+ posn) directives))
944 (stream (make-case-frob-stream stream
945 (if colonp
946 (if atsignp
947 :upcase
948 :capitalize)
949 (if atsignp
950 :capitalize-first
951 :downcase)))))
952 (setf args (interpret-directive-list stream before orig-args args))
953 after))))
955 (def-complex-format-interpreter #\) ()
956 (error 'format-error
957 :complaint "no corresponding open paren"))
959 ;;;; format interpreters and support functions for conditionalization
961 (def-complex-format-interpreter #\[ (colonp atsignp params directives)
962 (multiple-value-bind (sublists last-semi-with-colon-p remaining)
963 (parse-conditional-directive directives)
964 (setf args
965 (if atsignp
966 (if colonp
967 (error 'format-error
968 :complaint
969 "cannot specify both the colon and at-sign modifiers")
970 (if (cdr sublists)
971 (error 'format-error
972 :complaint
973 "can only specify one section")
974 (interpret-bind-defaults () params
975 (let ((prev-args args)
976 (arg (next-arg)))
977 (if arg
978 (interpret-directive-list stream
979 (car sublists)
980 orig-args
981 prev-args)
982 args)))))
983 (if colonp
984 (if (= (length sublists) 2)
985 (interpret-bind-defaults () params
986 (if (next-arg)
987 (interpret-directive-list stream (car sublists)
988 orig-args args)
989 (interpret-directive-list stream (cadr sublists)
990 orig-args args)))
991 (error 'format-error
992 :complaint
993 "must specify exactly two sections"))
994 (interpret-bind-defaults ((index (next-arg))) params
995 (let* ((default (and last-semi-with-colon-p
996 (pop sublists)))
997 (last (1- (length sublists)))
998 (sublist
999 (if (<= 0 index last)
1000 (nth (- last index) sublists)
1001 default)))
1002 (interpret-directive-list stream sublist orig-args
1003 args))))))
1004 remaining))
1006 (def-complex-format-interpreter #\; ()
1007 (error 'format-error
1008 :complaint
1009 "~~; not contained within either ~~[...~~] or ~~<...~~>"))
1011 (def-complex-format-interpreter #\] ()
1012 (error 'format-error
1013 :complaint
1014 "no corresponding open bracket"))
1016 ;;;; format interpreter for up-and-out
1018 (defvar *outside-args*)
1020 (def-format-interpreter #\^ (colonp atsignp params)
1021 (when atsignp
1022 (error 'format-error
1023 :complaint "cannot specify the at-sign modifier"))
1024 (when (and colonp (not *up-up-and-out-allowed*))
1025 (error 'format-error
1026 :complaint "attempt to use ~~:^ outside a ~~:{...~~} construct"))
1027 (when (interpret-bind-defaults ((arg1 nil) (arg2 nil) (arg3 nil)) params
1028 (cond (arg3 (<= arg1 arg2 arg3))
1029 (arg2 (eql arg1 arg2))
1030 (arg1 (eql arg1 0))
1031 (t (if colonp
1032 (null *outside-args*)
1033 (null args)))))
1034 (throw (if colonp 'up-up-and-out 'up-and-out)
1035 args)))
1037 ;;;; format interpreters for iteration
1039 (def-complex-format-interpreter #\{
1040 (colonp atsignp params string end directives)
1041 (let ((close (find-directive directives #\} nil)))
1042 (unless close
1043 (error 'format-error
1044 :complaint
1045 "no corresponding close brace"))
1046 (interpret-bind-defaults ((max-count nil)) params
1047 (let* ((closed-with-colon (format-directive-colonp close))
1048 (posn (position close directives))
1049 (insides (if (zerop posn)
1050 (next-arg)
1051 (subseq directives 0 posn)))
1052 (*up-up-and-out-allowed* colonp))
1053 (labels
1054 ((do-guts (orig-args args)
1055 (if (zerop posn)
1056 (handler-bind
1057 ((format-error
1058 (lambda (condition)
1059 (error
1060 'format-error
1061 :complaint
1062 "~A~%while processing indirect format string:"
1063 :args (list condition)
1064 :print-banner nil
1065 :control-string string
1066 :offset (1- end)))))
1067 (%format stream insides orig-args args))
1068 (interpret-directive-list stream insides
1069 orig-args args)))
1070 (bind-args (orig-args args)
1071 (if colonp
1072 (let* ((arg (next-arg))
1073 (*logical-block-popper* nil)
1074 (*outside-args* args))
1075 (catch 'up-and-out
1076 (do-guts arg arg))
1077 args)
1078 (do-guts orig-args args)))
1079 (do-loop (orig-args args)
1080 (catch (if colonp 'up-up-and-out 'up-and-out)
1081 (loop
1082 (when (and (not closed-with-colon) (null args))
1083 (return))
1084 (when (and max-count (minusp (decf max-count)))
1085 (return))
1086 (setf args (bind-args orig-args args))
1087 (when (and closed-with-colon (null args))
1088 (return)))
1089 args)))
1090 (if atsignp
1091 (setf args (do-loop orig-args args))
1092 (let ((arg (next-arg))
1093 (*logical-block-popper* nil))
1094 (do-loop arg arg)))
1095 (nthcdr (1+ posn) directives))))))
1097 (def-complex-format-interpreter #\} ()
1098 (error 'format-error
1099 :complaint "no corresponding open brace"))
1101 ;;;; format interpreters and support functions for justification
1103 (def-complex-format-interpreter #\<
1104 (colonp atsignp params string end directives)
1105 (multiple-value-bind (segments first-semi close remaining)
1106 (parse-format-justification directives)
1107 (setf args
1108 (if (format-directive-colonp close) ; logical block vs. justification
1109 (multiple-value-bind (prefix per-line-p insides suffix)
1110 (parse-format-logical-block segments colonp first-semi
1111 close params string end)
1112 (interpret-format-logical-block stream orig-args args
1113 prefix per-line-p insides
1114 suffix atsignp))
1115 (let ((count (reduce #'+ (mapcar (lambda (x) (count-if #'illegal-inside-justification-p x)) segments))))
1116 (when (> count 0)
1117 ;; ANSI specifies that "an error is signalled" in this
1118 ;; situation.
1119 (error 'format-error
1120 :complaint "~D illegal directive~:P found inside justification block"
1121 :args (list count)
1122 :references (list '(:ansi-cl :section (22 3 5 2)))))
1123 ;; ANSI does not explicitly say that an error should
1124 ;; be signalled, but the @ modifier is not explicitly
1125 ;; allowed for ~> either.
1126 (when (format-directive-atsignp close)
1127 (error 'format-error
1128 :complaint "@ modifier not allowed in close ~
1129 directive of justification ~
1130 block (i.e. ~~<...~~@>."
1131 :offset (1- (format-directive-end close))
1132 :references (list '(:ansi-cl :section (22 3 6 2)))))
1133 (interpret-format-justification stream orig-args args
1134 segments colonp atsignp
1135 first-semi params))))
1136 remaining))
1138 (defun interpret-format-justification
1139 (stream orig-args args segments colonp atsignp first-semi params)
1140 (interpret-bind-defaults
1141 ((mincol 0) (colinc 1) (minpad 0) (padchar #\space))
1142 params
1143 (let ((newline-string nil)
1144 (strings nil)
1145 (extra-space 0)
1146 (line-len 0))
1147 (setf args
1148 (catch 'up-and-out
1149 (when (and first-semi (format-directive-colonp first-semi))
1150 (interpret-bind-defaults
1151 ((extra 0)
1152 (len (or (sb!impl::line-length stream) 72)))
1153 (format-directive-params first-semi)
1154 (setf newline-string
1155 (with-simple-output-to-string (stream)
1156 (setf args
1157 (interpret-directive-list stream
1158 (pop segments)
1159 orig-args
1160 args))))
1161 (setf extra-space extra)
1162 (setf line-len len)))
1163 (dolist (segment segments)
1164 (push (with-simple-output-to-string (stream)
1165 (setf args
1166 (interpret-directive-list stream segment
1167 orig-args args)))
1168 strings))
1169 args))
1170 (format-justification stream newline-string extra-space line-len strings
1171 colonp atsignp mincol colinc minpad padchar)))
1172 args)
1174 (defun format-justification (stream newline-prefix extra-space line-len strings
1175 pad-left pad-right mincol colinc minpad padchar)
1176 (setf strings (reverse strings))
1177 (let* ((num-gaps (+ (1- (length strings))
1178 (if pad-left 1 0)
1179 (if pad-right 1 0)))
1180 (chars (+ (* num-gaps minpad)
1181 (loop
1182 for string in strings
1183 summing (length string))))
1184 (length (if (> chars mincol)
1185 (+ mincol (* (ceiling (- chars mincol) colinc) colinc))
1186 mincol))
1187 (padding (+ (- length chars) (* num-gaps minpad))))
1188 (when (and newline-prefix
1189 (> (+ (or (sb!impl::charpos stream) 0)
1190 length extra-space)
1191 line-len))
1192 (write-string newline-prefix stream))
1193 (flet ((do-padding ()
1194 (let ((pad-len
1195 (if (zerop num-gaps) padding (truncate padding num-gaps))))
1196 (decf padding pad-len)
1197 (decf num-gaps)
1198 (dotimes (i pad-len) (write-char padchar stream)))))
1199 (when (or pad-left (and (not pad-right) (null (cdr strings))))
1200 (do-padding))
1201 (when strings
1202 (write-string (car strings) stream)
1203 (dolist (string (cdr strings))
1204 (do-padding)
1205 (write-string string stream)))
1206 (when pad-right
1207 (do-padding)))))
1209 (defun interpret-format-logical-block
1210 (stream orig-args args prefix per-line-p insides suffix atsignp)
1211 (let ((arg (if atsignp args (next-arg))))
1212 (if per-line-p
1213 (pprint-logical-block
1214 (stream arg :per-line-prefix prefix :suffix suffix)
1215 (let ((*logical-block-popper* (lambda () (pprint-pop))))
1216 (catch 'up-and-out
1217 (interpret-directive-list stream insides
1218 (if atsignp orig-args arg)
1219 arg))))
1220 (pprint-logical-block (stream arg :prefix prefix :suffix suffix)
1221 (let ((*logical-block-popper* (lambda () (pprint-pop))))
1222 (catch 'up-and-out
1223 (interpret-directive-list stream insides
1224 (if atsignp orig-args arg)
1225 arg))))))
1226 (if atsignp nil args))
1228 ;;;; format interpreter and support functions for user-defined method
1230 (def-format-interpreter #\/ (string start end colonp atsignp params)
1231 (let ((symbol (extract-user-fun-name string start end)))
1232 (collect ((args))
1233 (dolist (param-and-offset params)
1234 (let ((param (cdr param-and-offset)))
1235 (case param
1236 (:arg (args (next-arg)))
1237 (:remaining (args (length args)))
1238 (t (args param)))))
1239 (apply (fdefinition symbol) stream (next-arg) colonp atsignp (args)))))