1 ;;;; functions to implement FORMAT and FORMATTER
3 ;;;; This software is part of the SBCL system. See the README file for
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")
16 (defun format (destination control-string &rest format-arguments)
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
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
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
40 (etypecase destination
42 (with-output-to-string (stream)
43 (%format stream control-string format-arguments)))
45 (with-output-to-string (stream destination)
46 (%format stream control-string format-arguments)))
48 (%format *standard-output* control-string format-arguments)
51 (%format destination control-string format-arguments)
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)
58 (let* ((string (etypecase string-or-fun
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)
68 (defun interpret-directive-list (stream directives orig-args args)
70 (let ((directive (car directives)))
73 (write-string directive stream)
74 (interpret-directive-list stream (cdr directives) orig-args args))
76 (multiple-value-bind (new-directives new-args)
78 (svref *format-directive-interpreters*
79 (char-code (format-directive-character
81 (*default-format-error-offset*
82 (1- (format-directive-end directive))))
85 :complaint "unknown format directive"))
86 (multiple-value-bind (new-directives new-args)
87 (funcall function stream directive
88 (cdr directives) orig-args args)
89 (values new-directives new-args)))
90 (interpret-directive-list stream new-directives
91 orig-args new-args)))))
94 ;;;; FORMAT directive definition macros and runtime support
96 (eval-when (:compile-toplevel :execute)
98 ;;; This macro is used to extract the next argument from the current arg list.
99 ;;; This is the version used by format directive interpreters.
100 (sb!xc:defmacro next-arg (&optional offset)
104 :complaint "no more arguments"
106 `(:offset ,offset))))
107 (when *logical-block-popper*
108 (funcall *logical-block-popper*))
111 (sb!xc:defmacro def-complex-format-interpreter (char lambda-list &body body)
114 "~:@(~:C~)-FORMAT-DIRECTIVE-INTERPRETER"
117 (directives (if lambda-list (car (last lambda-list)) (gensym))))
119 (defun ,defun-name (stream ,directive ,directives orig-args args)
120 (declare (ignorable stream orig-args args))
122 `((let ,(mapcar (lambda (var)
124 (,(symbolicate "FORMAT-DIRECTIVE-" var)
126 (butlast lambda-list))
127 (values (progn ,@body) args)))
128 `((declare (ignore ,directive ,directives))
130 (%set-format-directive-interpreter ,char #',defun-name))))
132 (sb!xc:defmacro def-format-interpreter (char lambda-list &body body)
133 (let ((directives (gensym)))
134 `(def-complex-format-interpreter ,char (,@lambda-list ,directives)
138 (sb!xc:defmacro interpret-bind-defaults (specs params &body body)
139 (once-only ((params params))
140 (collect ((bindings))
142 (destructuring-bind (var default) spec
143 (bindings `(,var (let* ((param-and-offset (pop ,params))
144 (offset (car param-and-offset))
145 (param (cdr param-and-offset)))
147 (:arg (next-arg offset))
148 (:remaining (length args))
155 "too many parameters, expected no more than ~W"
156 :arguments (list ,(length specs))
157 :offset (caar ,params)))
162 ;;;; format interpreters and support functions for simple output
164 (defun format-write-field (stream string mincol colinc minpad padchar padleft)
166 (write-string string stream))
168 (write-char padchar stream))
169 ;; As of sbcl-0.6.12.34, we could end up here when someone tries to
170 ;; print e.g. (FORMAT T "~F" "NOTFLOAT"), in which case ANSI says
171 ;; we're supposed to soldier on bravely, and so we have to deal with
172 ;; the unsupplied-MINCOL-and-COLINC case without blowing up.
173 (when (and mincol colinc)
174 (do ((chars (+ (length string) minpad) (+ chars colinc)))
177 (write-char padchar stream))))
179 (write-string string stream)))
181 (defun format-princ (stream arg colonp atsignp mincol colinc minpad padchar)
182 (format-write-field stream
183 (if (or arg (not colonp))
184 (princ-to-string arg)
186 mincol colinc minpad padchar atsignp))
188 (def-format-interpreter #\A (colonp atsignp params)
190 (interpret-bind-defaults ((mincol 0) (colinc 1) (minpad 0)
193 (format-princ stream (next-arg) colonp atsignp
194 mincol colinc minpad padchar))
195 (princ (if colonp (or (next-arg) "()") (next-arg)) stream)))
197 (defun format-prin1 (stream arg colonp atsignp mincol colinc minpad padchar)
198 (format-write-field stream
199 (if (or arg (not colonp))
200 (prin1-to-string arg)
202 mincol colinc minpad padchar atsignp))
204 (def-format-interpreter #\S (colonp atsignp params)
206 (interpret-bind-defaults ((mincol 0) (colinc 1) (minpad 0)
209 (format-prin1 stream (next-arg) colonp atsignp
210 mincol colinc minpad padchar)))
212 (let ((arg (next-arg)))
215 (princ "()" stream))))
217 (prin1 (next-arg) stream))))
219 (def-format-interpreter #\C (colonp atsignp params)
220 (interpret-bind-defaults () params
222 (format-print-named-character (next-arg) stream)
224 (prin1 (next-arg) stream)
225 (write-char (next-arg) stream)))))
227 (defun format-print-named-character (char stream)
228 (let* ((name (char-name char)))
230 (write-string (string-capitalize name) stream))
231 ((<= 0 (char-code char) 31)
232 ;; Print control characters as "^"<char>. (This seems to be
233 ;; old pre-ANSI behavior, but ANSI just says that the "#^"
234 ;; sequence is undefined and not reserved for the user, so
235 ;; this behavior should be ANSI-compliant.)
236 (write-char #\^ stream)
237 (write-char (code-char (+ 64 (char-code char))) stream))
239 (write-char char stream)))))
241 (def-format-interpreter #\W (colonp atsignp params)
242 (interpret-bind-defaults () params
243 (let ((*print-pretty* (or colonp *print-pretty*))
244 (*print-level* (and atsignp *print-level*))
245 (*print-length* (and atsignp *print-length*)))
246 (output-object (next-arg) stream))))
248 ;;;; format interpreters and support functions for integer output
250 ;;; FORMAT-PRINT-NUMBER does most of the work for the numeric printing
251 ;;; directives. The parameters are interpreted as defined for ~D.
252 (defun format-print-integer (stream number print-commas-p print-sign-p
253 radix mincol padchar commachar commainterval)
254 (let ((*print-base* radix)
256 (if (integerp number)
257 (let* ((text (princ-to-string (abs number)))
258 (commaed (if print-commas-p
259 (format-add-commas text commachar commainterval)
261 (signed (cond ((minusp number)
262 (concatenate 'string "-" commaed))
264 (concatenate 'string "+" commaed))
266 ;; colinc = 1, minpad = 0, padleft = t
267 (format-write-field stream signed mincol 1 0 padchar t))
270 (defun format-add-commas (string commachar commainterval)
271 (let ((length (length string)))
272 (multiple-value-bind (commas extra) (truncate (1- length) commainterval)
273 (let ((new-string (make-string (+ length commas)))
274 (first-comma (1+ extra)))
275 (replace new-string string :end1 first-comma :end2 first-comma)
276 (do ((src first-comma (+ src commainterval))
277 (dst first-comma (+ dst commainterval 1)))
279 (setf (schar new-string dst) commachar)
280 (replace new-string string :start1 (1+ dst)
281 :start2 src :end2 (+ src commainterval)))
284 ;;; FIXME: This is only needed in this file, could be defined with
285 ;;; SB!XC:DEFMACRO inside EVAL-WHEN
286 (defmacro interpret-format-integer (base)
287 `(if (or colonp atsignp params)
288 (interpret-bind-defaults
289 ((mincol 0) (padchar #\space) (commachar #\,) (commainterval 3))
291 (format-print-integer stream (next-arg) colonp atsignp ,base mincol
292 padchar commachar commainterval))
293 (write (next-arg) :stream stream :base ,base :radix nil :escape nil)))
295 (def-format-interpreter #\D (colonp atsignp params)
296 (interpret-format-integer 10))
298 (def-format-interpreter #\B (colonp atsignp params)
299 (interpret-format-integer 2))
301 (def-format-interpreter #\O (colonp atsignp params)
302 (interpret-format-integer 8))
304 (def-format-interpreter #\X (colonp atsignp params)
305 (interpret-format-integer 16))
307 (def-format-interpreter #\R (colonp atsignp params)
309 (interpret-bind-defaults
310 ((base 10) (mincol 0) (padchar #\space) (commachar #\,)
313 (format-print-integer stream (next-arg) colonp atsignp base mincol
314 padchar commachar commainterval))
317 (format-print-old-roman stream (next-arg))
318 (format-print-roman stream (next-arg)))
320 (format-print-ordinal stream (next-arg))
321 (format-print-cardinal stream (next-arg))))))
323 (defparameter *cardinal-ones*
324 #(nil "one" "two" "three" "four" "five" "six" "seven" "eight" "nine"))
326 (defparameter *cardinal-tens*
327 #(nil nil "twenty" "thirty" "forty"
328 "fifty" "sixty" "seventy" "eighty" "ninety"))
330 (defparameter *cardinal-teens*
331 #("ten" "eleven" "twelve" "thirteen" "fourteen" ;;; RAD
332 "fifteen" "sixteen" "seventeen" "eighteen" "nineteen"))
334 (defparameter *cardinal-periods*
335 #("" " thousand" " million" " billion" " trillion" " quadrillion"
336 " quintillion" " sextillion" " septillion" " octillion" " nonillion"
337 " decillion" " undecillion" " duodecillion" " tredecillion"
338 " quattuordecillion" " quindecillion" " sexdecillion" " septendecillion"
339 " octodecillion" " novemdecillion" " vigintillion"))
341 (defparameter *ordinal-ones*
342 #(nil "first" "second" "third" "fourth"
343 "fifth" "sixth" "seventh" "eighth" "ninth"))
345 (defparameter *ordinal-tens*
346 #(nil "tenth" "twentieth" "thirtieth" "fortieth"
347 "fiftieth" "sixtieth" "seventieth" "eightieth" "ninetieth"))
349 (defun format-print-small-cardinal (stream n)
350 (multiple-value-bind (hundreds rem) (truncate n 100)
351 (when (plusp hundreds)
352 (write-string (svref *cardinal-ones* hundreds) stream)
353 (write-string " hundred" stream)
355 (write-char #\space stream)))
357 (multiple-value-bind (tens ones) (truncate rem 10)
359 (write-string (svref *cardinal-tens* tens) stream)
361 (write-char #\- stream)
362 (write-string (svref *cardinal-ones* ones) stream)))
364 (write-string (svref *cardinal-teens* ones) stream))
366 (write-string (svref *cardinal-ones* ones) stream)))))))
368 (defun format-print-cardinal (stream n)
370 (write-string "negative " stream)
371 (format-print-cardinal-aux stream (- n) 0 n))
373 (write-string "zero" stream))
375 (format-print-cardinal-aux stream n 0 n))))
377 (defun format-print-cardinal-aux (stream n period err)
378 (multiple-value-bind (beyond here) (truncate n 1000)
379 (unless (<= period 20)
380 (error "number too large to print in English: ~:D" err))
381 (unless (zerop beyond)
382 (format-print-cardinal-aux stream beyond (1+ period) err))
384 (unless (zerop beyond)
385 (write-char #\space stream))
386 (format-print-small-cardinal stream here)
387 (write-string (svref *cardinal-periods* period) stream))))
389 (defun format-print-ordinal (stream n)
391 (write-string "negative " stream))
392 (let ((number (abs n)))
393 (multiple-value-bind (top bot) (truncate number 100)
395 (format-print-cardinal stream (- number bot)))
396 (when (and (plusp top) (plusp bot))
397 (write-char #\space stream))
398 (multiple-value-bind (tens ones) (truncate bot 10)
399 (cond ((= bot 12) (write-string "twelfth" stream))
401 (write-string (svref *cardinal-teens* ones) stream);;;RAD
402 (write-string "th" stream))
403 ((and (zerop tens) (plusp ones))
404 (write-string (svref *ordinal-ones* ones) stream))
405 ((and (zerop ones)(plusp tens))
406 (write-string (svref *ordinal-tens* tens) stream))
408 (write-string (svref *cardinal-tens* tens) stream)
409 (write-char #\- stream)
410 (write-string (svref *ordinal-ones* ones) stream))
412 (write-string "th" stream))
414 (write-string "zeroth" stream)))))))
416 ;;; Print Roman numerals
418 (defun format-print-old-roman (stream n)
420 (error "Number too large to print in old Roman numerals: ~:D" n))
421 (do ((char-list '(#\D #\C #\L #\X #\V #\I) (cdr char-list))
422 (val-list '(500 100 50 10 5 1) (cdr val-list))
423 (cur-char #\M (car char-list))
424 (cur-val 1000 (car val-list))
425 (start n (do ((i start (progn
426 (write-char cur-char stream)
431 (defun format-print-roman (stream n)
433 (error "Number too large to print in Roman numerals: ~:D" n))
434 (do ((char-list '(#\D #\C #\L #\X #\V #\I) (cdr char-list))
435 (val-list '(500 100 50 10 5 1) (cdr val-list))
436 (sub-chars '(#\C #\X #\X #\I #\I) (cdr sub-chars))
437 (sub-val '(100 10 10 1 1 0) (cdr sub-val))
438 (cur-char #\M (car char-list))
439 (cur-val 1000 (car val-list))
440 (cur-sub-char #\C (car sub-chars))
441 (cur-sub-val 100 (car sub-val))
442 (start n (do ((i start (progn
443 (write-char cur-char stream)
446 (cond ((<= (- cur-val cur-sub-val) i)
447 (write-char cur-sub-char stream)
448 (write-char cur-char stream)
449 (- i (- cur-val cur-sub-val)))
455 (def-format-interpreter #\P (colonp atsignp params)
456 (interpret-bind-defaults () params
457 (let ((arg (if colonp
458 (if (eq orig-args args)
460 :complaint "no previous argument")
461 (do ((arg-ptr orig-args (cdr arg-ptr)))
462 ((eq (cdr arg-ptr) args)
466 (write-string (if (eql arg 1) "y" "ies") stream)
467 (unless (eql arg 1) (write-char #\s stream))))))
469 ;;;; format interpreters and support functions for floating point output
471 (defun decimal-string (n)
472 (write-to-string n :base 10 :radix nil :escape nil))
474 (def-format-interpreter #\F (colonp atsignp params)
478 "cannot specify the colon modifier with this directive"))
479 (interpret-bind-defaults ((w nil) (d nil) (k nil) (ovf nil) (pad #\space))
481 (format-fixed stream (next-arg) w d k ovf pad atsignp)))
483 (defun format-fixed (stream number w d k ovf pad atsign)
486 (format-fixed-aux stream number w d k ovf pad atsign)
487 (if (rationalp number)
488 (format-fixed-aux stream
489 (coerce number 'single-float)
490 w d k ovf pad atsign)
491 (format-write-field stream
492 (decimal-string number)
494 (format-princ stream number nil nil w 1 0 pad)))
496 ;;; We return true if we overflowed, so that ~G can output the overflow char
497 ;;; instead of spaces.
498 (defun format-fixed-aux (stream number w d k ovf pad atsign)
502 (or (float-infinity-p number)
503 (float-nan-p number))))
504 (prin1 number stream)
508 (when (and w (or atsign (minusp number))) (decf spaceleft))
509 (multiple-value-bind (str len lpoint tpoint)
510 (sb!impl::flonum-to-string (abs number) spaceleft d k)
511 ;;if caller specifically requested no fraction digits, suppress the
512 ;;optional trailing zero
513 (when (and d (zerop d)) (setq tpoint nil))
516 ;;optional leading zero
518 (if (or (> spaceleft 0) tpoint) ;force at least one digit
521 ;;optional trailing zero
526 (cond ((and w (< spaceleft 0) ovf)
527 ;;field width overflow
528 (dotimes (i w) (write-char ovf stream))
531 (when w (dotimes (i spaceleft) (write-char pad stream)))
533 (write-char #\- stream)
534 (if atsign (write-char #\+ stream)))
535 (when lpoint (write-char #\0 stream))
536 (write-string str stream)
537 (when tpoint (write-char #\0 stream))
540 (def-format-interpreter #\E (colonp atsignp params)
544 "cannot specify the colon modifier with this directive"))
545 (interpret-bind-defaults
546 ((w nil) (d nil) (e nil) (k 1) (ovf nil) (pad #\space) (mark nil))
548 (format-exponential stream (next-arg) w d e k ovf pad mark atsignp)))
550 (defun format-exponential (stream number w d e k ovf pad marker atsign)
553 (format-exp-aux stream number w d e k ovf pad marker atsign)
554 (if (rationalp number)
555 (format-exp-aux stream
556 (coerce number 'single-float)
557 w d e k ovf pad marker atsign)
558 (format-write-field stream
559 (decimal-string number)
561 (format-princ stream number nil nil w 1 0 pad)))
563 (defun format-exponent-marker (number)
564 (if (typep number *read-default-float-format*)
572 ;;; Here we prevent the scale factor from shifting all significance out of
573 ;;; a number to the right. We allow insignificant zeroes to be shifted in
574 ;;; to the left right, athough it is an error to specify k and d such that this
575 ;;; occurs. Perhaps we should detect both these condtions and flag them as
576 ;;; errors. As for now, we let the user get away with it, and merely guarantee
577 ;;; that at least one significant digit will appear.
579 ;;; toy@rtp.ericsson.se: The Hyperspec seems to say that the exponent
580 ;;; marker is always printed. Make it so. Also, the original version
581 ;;; causes errors when printing infinities or NaN's. The Hyperspec is
582 ;;; silent here, so let's just print out infinities and NaN's instead
583 ;;; of causing an error.
584 (defun format-exp-aux (stream number w d e k ovf pad marker atsign)
585 (if (and (floatp number)
586 (or (float-infinity-p number)
587 (float-nan-p number)))
588 (prin1 number stream)
589 (multiple-value-bind (num expt) (sb!impl::scale-exponent (abs number))
590 (let* ((expt (- expt k))
591 (estr (decimal-string (abs expt)))
592 (elen (if e (max (length estr) e) (length estr)))
593 (fdig (if d (if (plusp k) (1+ (- d k)) d) nil))
594 (fmin (if (minusp k) (- 1 k) nil))
597 (if (or atsign (minusp number))
600 (if (and w ovf e (> elen e)) ;exponent overflow
601 (dotimes (i w) (write-char ovf stream))
602 (multiple-value-bind (fstr flen lpoint)
603 (sb!impl::flonum-to-string num spaceleft fdig k fmin)
605 (decf spaceleft flen)
610 (cond ((and w (< spaceleft 0) ovf)
611 ;;significand overflow
612 (dotimes (i w) (write-char ovf stream)))
614 (dotimes (i spaceleft) (write-char pad stream)))
616 (write-char #\- stream)
617 (if atsign (write-char #\+ stream)))
618 (when lpoint (write-char #\0 stream))
619 (write-string fstr stream)
620 (write-char (if marker
622 (format-exponent-marker number))
624 (write-char (if (minusp expt) #\- #\+) stream)
626 ;;zero-fill before exponent if necessary
627 (dotimes (i (- e (length estr)))
628 (write-char #\0 stream)))
629 (write-string estr stream)))))))))
631 (def-format-interpreter #\G (colonp atsignp params)
635 "cannot specify the colon modifier with this directive"))
636 (interpret-bind-defaults
637 ((w nil) (d nil) (e nil) (k nil) (ovf nil) (pad #\space) (mark nil))
639 (format-general stream (next-arg) w d e k ovf pad mark atsignp)))
641 (defun format-general (stream number w d e k ovf pad marker atsign)
644 (format-general-aux stream number w d e k ovf pad marker atsign)
645 (if (rationalp number)
646 (format-general-aux stream
647 (coerce number 'single-float)
648 w d e k ovf pad marker atsign)
649 (format-write-field stream
650 (decimal-string number)
652 (format-princ stream number nil nil w 1 0 pad)))
654 ;;; toy@rtp.ericsson.se: Same change as for format-exp-aux.
655 (defun format-general-aux (stream number w d e k ovf pad marker atsign)
656 (if (and (floatp number)
657 (or (float-infinity-p number)
658 (float-nan-p number)))
659 (prin1 number stream)
660 (multiple-value-bind (ignore n) (sb!impl::scale-exponent (abs number))
661 (declare (ignore ignore))
662 ;; KLUDGE: Default d if omitted. The procedure is taken directly from
663 ;; the definition given in the manual, and is not very efficient, since
664 ;; we generate the digits twice. Future maintainers are encouraged to
665 ;; improve on this. -- rtoy?? 1998??
667 (multiple-value-bind (str len)
668 (sb!impl::flonum-to-string (abs number))
669 (declare (ignore str))
670 (let ((q (if (= len 1) 1 (1- len))))
671 (setq d (max q (min n 7))))))
672 (let* ((ee (if e (+ e 2) 4))
673 (ww (if w (- w ee) nil))
676 (let ((char (if (format-fixed-aux stream number ww dd nil
680 (dotimes (i ee) (write-char char stream))))
682 (format-exp-aux stream number w d e (or k 1)
683 ovf pad marker atsign)))))))
685 (def-format-interpreter #\$ (colonp atsignp params)
686 (interpret-bind-defaults ((d 2) (n 1) (w 0) (pad #\space)) params
687 (format-dollars stream (next-arg) d n w pad colonp atsignp)))
689 (defun format-dollars (stream number d n w pad colon atsign)
690 (if (rationalp number) (setq number (coerce number 'single-float)))
692 (let* ((signstr (if (minusp number) "-" (if atsign "+" "")))
693 (signlen (length signstr)))
694 (multiple-value-bind (str strlen ig2 ig3 pointplace)
695 (sb!impl::flonum-to-string number nil d nil)
696 (declare (ignore ig2 ig3))
697 (when colon (write-string signstr stream))
698 (dotimes (i (- w signlen (- n pointplace) strlen))
699 (write-char pad stream))
700 (unless colon (write-string signstr stream))
701 (dotimes (i (- n pointplace)) (write-char #\0 stream))
702 (write-string str stream)))
703 (format-write-field stream
704 (decimal-string number)
707 ;;;; format interpreters and support functions for line/page breaks etc.
709 (def-format-interpreter #\% (colonp atsignp params)
710 (when (or colonp atsignp)
713 "cannot specify either colon or atsign for this directive"))
714 (interpret-bind-defaults ((count 1)) params
718 (def-format-interpreter #\& (colonp atsignp params)
719 (when (or colonp atsignp)
722 "cannot specify either colon or atsign for this directive"))
723 (interpret-bind-defaults ((count 1)) params
725 (dotimes (i (1- count))
728 (def-format-interpreter #\| (colonp atsignp params)
729 (when (or colonp atsignp)
732 "cannot specify either colon or atsign for this directive"))
733 (interpret-bind-defaults ((count 1)) params
735 (write-char (code-char form-feed-char-code) stream))))
737 (def-format-interpreter #\~ (colonp atsignp params)
738 (when (or colonp atsignp)
741 "cannot specify either colon or atsign for this directive"))
742 (interpret-bind-defaults ((count 1)) params
744 (write-char #\~ stream))))
746 (def-complex-format-interpreter #\newline (colonp atsignp params directives)
747 (when (and colonp atsignp)
750 "cannot specify both colon and atsign for this directive"))
751 (interpret-bind-defaults () params
753 (write-char #\newline stream)))
754 (if (and (not colonp)
756 (simple-string-p (car directives)))
757 (cons (string-left-trim *format-whitespace-chars*
762 ;;;; format interpreters and support functions for tabs and simple pretty
765 (def-format-interpreter #\T (colonp atsignp params)
767 (interpret-bind-defaults ((n 1) (m 1)) params
768 (pprint-tab (if atsignp :section-relative :section) n m stream))
770 (interpret-bind-defaults ((colrel 1) (colinc 1)) params
771 (format-relative-tab stream colrel colinc))
772 (interpret-bind-defaults ((colnum 1) (colinc 1)) params
773 (format-absolute-tab stream colnum colinc)))))
775 (defun output-spaces (stream n)
776 (let ((spaces #.(make-string 100 :initial-element #\space)))
778 (when (< n (length spaces))
780 (write-string spaces stream)
781 (decf n (length spaces)))
782 (write-string spaces stream :end n)))
784 (defun format-relative-tab (stream colrel colinc)
785 (if (sb!pretty:pretty-stream-p stream)
786 (pprint-tab :line-relative colrel colinc stream)
787 (let* ((cur (sb!impl::charpos stream))
788 (spaces (if (and cur (plusp colinc))
789 (- (* (ceiling (+ cur colrel) colinc) colinc) cur)
791 (output-spaces stream spaces))))
793 (defun format-absolute-tab (stream colnum colinc)
794 (if (sb!pretty:pretty-stream-p stream)
795 (pprint-tab :line colnum colinc stream)
796 (let ((cur (sb!impl::charpos stream)))
798 (write-string " " stream))
800 (output-spaces stream (- colnum cur)))
802 (unless (zerop colinc)
803 (output-spaces stream
804 (- colinc (rem (- cur colnum) colinc)))))))))
806 (def-format-interpreter #\_ (colonp atsignp params)
807 (interpret-bind-defaults () params
808 (pprint-newline (if colonp
817 (def-format-interpreter #\I (colonp atsignp params)
820 :complaint "cannot specify the at-sign modifier"))
821 (interpret-bind-defaults ((n 0)) params
822 (pprint-indent (if colonp :current :block) n stream)))
824 ;;;; format interpreter for ~*
826 (def-format-interpreter #\* (colonp atsignp params)
830 :complaint "cannot specify both colon and at-sign")
831 (interpret-bind-defaults ((posn 0)) params
832 (if (<= 0 posn (length orig-args))
833 (setf args (nthcdr posn orig-args))
835 :complaint "Index ~W is out of bounds. (It should ~
836 have been between 0 and ~W.)"
837 :arguments (list posn (length orig-args))))))
839 (interpret-bind-defaults ((n 1)) params
840 (do ((cur-posn 0 (1+ cur-posn))
841 (arg-ptr orig-args (cdr arg-ptr)))
843 (let ((new-posn (- cur-posn n)))
844 (if (<= 0 new-posn (length orig-args))
845 (setf args (nthcdr new-posn orig-args))
848 "Index ~W is out of bounds. (It should
849 have been between 0 and ~W.)"
851 (list new-posn (length orig-args))))))))
852 (interpret-bind-defaults ((n 1)) params
856 ;;;; format interpreter for indirection
858 (def-format-interpreter #\? (colonp atsignp params string end)
861 :complaint "cannot specify the colon modifier"))
862 (interpret-bind-defaults () params
865 #'(lambda (condition)
868 "~A~%while processing indirect format string:"
869 :arguments (list condition)
871 :control-string string
874 (setf args (%format stream (next-arg) orig-args args))
875 (%format stream (next-arg) (next-arg))))))
877 ;;;; format interpreters for capitalization
879 (def-complex-format-interpreter #\( (colonp atsignp params directives)
880 (let ((close (find-directive directives #\) nil)))
883 :complaint "no corresponding close paren"))
884 (interpret-bind-defaults () params
885 (let* ((posn (position close directives))
886 (before (subseq directives 0 posn))
887 (after (nthcdr (1+ posn) directives))
888 (stream (make-case-frob-stream stream
896 (setf args (interpret-directive-list stream before orig-args args))
899 (def-complex-format-interpreter #\) ()
901 :complaint "no corresponding open paren"))
903 ;;;; format interpreters and support functions for conditionalization
905 (def-complex-format-interpreter #\[ (colonp atsignp params directives)
906 (multiple-value-bind (sublists last-semi-with-colon-p remaining)
907 (parse-conditional-directive directives)
913 "cannot specify both the colon and at-sign modifiers")
917 "can only specify one section")
918 (interpret-bind-defaults () params
919 (let ((prev-args args)
922 (interpret-directive-list stream
928 (if (= (length sublists) 2)
929 (interpret-bind-defaults () params
931 (interpret-directive-list stream (car sublists)
933 (interpret-directive-list stream (cadr sublists)
937 "must specify exactly two sections"))
938 (interpret-bind-defaults ((index (next-arg))) params
939 (let* ((default (and last-semi-with-colon-p
941 (last (1- (length sublists)))
943 (if (<= 0 index last)
944 (nth (- last index) sublists)
946 (interpret-directive-list stream sublist orig-args
950 (def-complex-format-interpreter #\; ()
953 "~~; not contained within either ~~[...~~] or ~~<...~~>"))
955 (def-complex-format-interpreter #\] ()
958 "no corresponding open bracket"))
960 ;;;; format interpreter for up-and-out
962 (defvar *outside-args*)
964 (def-format-interpreter #\^ (colonp atsignp params)
967 :complaint "cannot specify the at-sign modifier"))
968 (when (and colonp (not *up-up-and-out-allowed*))
970 :complaint "attempt to use ~~:^ outside a ~~:{...~~} construct"))
971 (when (case (length params)
973 (null *outside-args*)
975 (1 (interpret-bind-defaults ((count 0)) params
977 (2 (interpret-bind-defaults ((arg1 0) (arg2 0)) params
979 (t (interpret-bind-defaults ((arg1 0) (arg2 0) (arg3 0)) params
980 (<= arg1 arg2 arg3))))
981 (throw (if colonp 'up-up-and-out 'up-and-out)
984 ;;;; format interpreters for iteration
986 (def-complex-format-interpreter #\{
987 (colonp atsignp params string end directives)
988 (let ((close (find-directive directives #\} nil)))
992 "no corresponding close brace"))
993 (interpret-bind-defaults ((max-count nil)) params
994 (let* ((closed-with-colon (format-directive-colonp close))
995 (posn (position close directives))
996 (insides (if (zerop posn)
998 (subseq directives 0 posn)))
999 (*up-up-and-out-allowed* colonp))
1001 ((do-guts (orig-args args)
1005 #'(lambda (condition)
1006 (error 'format-error
1008 "~A~%while processing indirect format string:"
1009 :arguments (list condition)
1011 :control-string string
1012 :offset (1- end)))))
1013 (%format stream insides orig-args args))
1014 (interpret-directive-list stream insides
1016 (bind-args (orig-args args)
1018 (let* ((arg (next-arg))
1019 (*logical-block-popper* nil)
1020 (*outside-args* args))
1024 (do-guts orig-args args)))
1025 (do-loop (orig-args args)
1026 (catch (if colonp 'up-up-and-out 'up-and-out)
1028 (when (and (not closed-with-colon) (null args))
1030 (when (and max-count (minusp (decf max-count)))
1032 (setf args (bind-args orig-args args))
1033 (when (and closed-with-colon (null args))
1037 (setf args (do-loop orig-args args))
1038 (let ((arg (next-arg))
1039 (*logical-block-popper* nil))
1041 (nthcdr (1+ posn) directives))))))
1043 (def-complex-format-interpreter #\} ()
1044 (error 'format-error
1045 :complaint "no corresponding open brace"))
1047 ;;;; format interpreters and support functions for justification
1049 (def-complex-format-interpreter #\<
1050 (colonp atsignp params string end directives)
1051 (multiple-value-bind (segments first-semi close remaining)
1052 (parse-format-justification directives)
1054 (if (format-directive-colonp close)
1055 (multiple-value-bind (prefix per-line-p insides suffix)
1056 (parse-format-logical-block segments colonp first-semi
1057 close params string end)
1058 (interpret-format-logical-block stream orig-args args
1059 prefix per-line-p insides
1061 (interpret-format-justification stream orig-args args
1062 segments colonp atsignp
1063 first-semi params)))
1066 (defun interpret-format-justification
1067 (stream orig-args args segments colonp atsignp first-semi params)
1068 (interpret-bind-defaults
1069 ((mincol 0) (colinc 1) (minpad 0) (padchar #\space))
1071 (let ((newline-string nil)
1077 (when (and first-semi (format-directive-colonp first-semi))
1078 (interpret-bind-defaults
1080 (len (or (sb!impl::line-length stream) 72)))
1081 (format-directive-params first-semi)
1082 (setf newline-string
1083 (with-output-to-string (stream)
1085 (interpret-directive-list stream
1089 (setf extra-space extra)
1090 (setf line-len len)))
1091 (dolist (segment segments)
1092 (push (with-output-to-string (stream)
1094 (interpret-directive-list stream segment
1098 (format-justification stream newline-string extra-space line-len strings
1099 colonp atsignp mincol colinc minpad padchar)))
1102 (defun format-justification (stream newline-prefix extra-space line-len strings
1103 pad-left pad-right mincol colinc minpad padchar)
1104 (setf strings (reverse strings))
1105 (when (and (not pad-left) (not pad-right) (null (cdr strings)))
1107 (let* ((num-gaps (+ (1- (length strings))
1109 (if pad-right 1 0)))
1110 (chars (+ (* num-gaps minpad)
1112 for string in strings
1113 summing (length string))))
1114 (length (if (> chars mincol)
1115 (+ mincol (* (ceiling (- chars mincol) colinc) colinc))
1117 (padding (- length chars)))
1118 (when (and newline-prefix
1119 (> (+ (or (sb!impl::charpos stream) 0)
1122 (write-string newline-prefix stream))
1123 (flet ((do-padding ()
1124 (let ((pad-len (truncate padding num-gaps)))
1125 (decf padding pad-len)
1127 (dotimes (i pad-len) (write-char padchar stream)))))
1131 (write-string (car strings) stream)
1132 (dolist (string (cdr strings))
1134 (write-string string stream)))
1138 (defun interpret-format-logical-block
1139 (stream orig-args args prefix per-line-p insides suffix atsignp)
1140 (let ((arg (if atsignp args (next-arg))))
1142 (pprint-logical-block
1143 (stream arg :per-line-prefix prefix :suffix suffix)
1144 (let ((*logical-block-popper* #'(lambda () (pprint-pop))))
1146 (interpret-directive-list stream insides
1147 (if atsignp orig-args arg)
1149 (pprint-logical-block (stream arg :prefix prefix :suffix suffix)
1150 (let ((*logical-block-popper* #'(lambda () (pprint-pop))))
1152 (interpret-directive-list stream insides
1153 (if atsignp orig-args arg)
1155 (if atsignp nil args))
1157 ;;;; format interpreter and support functions for user-defined method
1159 (def-format-interpreter #\/ (string start end colonp atsignp params)
1160 (let ((symbol (extract-user-fun-name string start end)))
1162 (dolist (param-and-offset params)
1163 (let ((param (cdr param-and-offset)))
1165 (:arg (args (next-arg)))
1166 (:remaining (args (length args)))
1168 (apply (fdefinition symbol) stream (next-arg) colonp atsignp (args)))))