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