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 (declare (explicit-check))
41 (etypecase destination
43 (with-simple-output-to-string (stream)
44 (%format stream control-string format-arguments
)))
46 (with-simple-output-to-string (stream destination
)
47 (%format stream control-string format-arguments
)))
49 (%format
*standard-output
* control-string format-arguments
)
52 (%format destination control-string format-arguments
)
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
)
59 (let* ((string (etypecase string-or-fun
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
)
69 (defun interpret-directive-list (stream directives orig-args args
)
71 (let ((directive (car directives
)))
74 (write-string directive stream
)
75 (interpret-directive-list stream
(cdr directives
) orig-args args
))
77 (multiple-value-bind (new-directives new-args
)
78 (let* ((character (format-directive-character directive
))
82 (svref *format-directive-interpreters
* (char-code character
)))))
83 (*default-format-error-offset
*
84 (1- (format-directive-end directive
))))
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
)))))
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
)
107 :complaint
"no more arguments"
109 `(:offset
,offset
))))
110 (when *logical-block-popper
*
111 (funcall *logical-block-popper
*))
114 (sb!xc
:defmacro def-complex-format-interpreter
(char lambda-list
&body body
)
117 "~:@(~:C~)-FORMAT-DIRECTIVE-INTERPRETER"
119 (directive (sb!xc
:gensym
"DIRECTIVE"))
120 (directives (if lambda-list
(car (last lambda-list
)) (sb!xc
:gensym
"DIRECTIVES"))))
122 (defun ,defun-name
(stream ,directive
,directives orig-args args
)
123 (declare (ignorable stream orig-args args
))
125 `((let ,(mapcar (lambda (var)
127 (,(symbolicate "FORMAT-DIRECTIVE-" var
)
129 (butlast lambda-list
))
130 (values (progn ,@body
) args
)))
131 `((declare (ignore ,directive
,directives
))
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
)
141 (sb!xc
:defmacro interpret-bind-defaults
(specs params
&body body
)
142 (once-only ((params params
))
143 (collect ((bindings))
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
)))
150 (:arg
(or (next-arg offset
) ,default
))
151 (:remaining
(length args
))
158 "too many parameters, expected no more than ~W"
159 :args
(list ,(length specs
))
160 :offset
(caar ,params
)))
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))
170 :complaint
"The value of colinc is ~a, should be a positive integer"
171 :args
(list colinc
)))
172 (when (and mincol
(< mincol
0))
174 :complaint
"The value of mincol is ~a, should be a non-negative integer"
175 :args
(list mincol
)))
177 (write-string string stream
))
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
)))
188 (write-char padchar stream
))))
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
)
197 mincol colinc minpad padchar atsignp
))
199 (def-format-interpreter #\A
(colonp atsignp params
)
201 (interpret-bind-defaults ((mincol 0) (colinc 1) (minpad 0)
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
)
213 mincol colinc minpad padchar atsignp
))
215 (def-format-interpreter #\S
(colonp atsignp params
)
217 (interpret-bind-defaults ((mincol 0) (colinc 1) (minpad 0)
220 (format-prin1 stream
(next-arg) colonp atsignp
221 mincol colinc minpad padchar
)))
223 (let ((arg (next-arg)))
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
)
235 :complaint
"~s is not of type CHARACTER."
238 (format-print-named-character 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
)
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
)
275 (signed (cond ((minusp number
)
276 (concatenate 'string
"-" commaed
))
278 (concatenate 'string
"+" 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)))
293 (setf (schar new-string dst
) commachar
)
294 (replace new-string string
:start1
(1+ dst
)
295 :start2 src
:end2
(+ src commainterval
)))
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))
304 (format-print-integer stream
(next-arg) colonp atsignp
,base mincol
305 padchar commachar commainterval
))
306 (let ((*print-base
* ,base
)
308 (princ (next-arg) stream
))))
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 #\
,)
328 (let ((arg (next-arg)))
332 :complaint
"~s is not of type INTEGER."
335 (format-print-integer stream arg colonp atsignp base mincol
336 padchar commachar commainterval
)
339 (format-print-old-roman stream arg
)
340 (format-print-roman stream arg
))
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
)
377 (write-char #\space stream
)))
379 (multiple-value-bind (tens ones
) (truncate rem
10)
381 (write-string (svref *cardinal-tens
* tens
) stream
)
383 (write-char #\- stream
)
384 (write-string (svref *cardinal-ones
* ones
) stream
)))
386 (write-string (svref *cardinal-teens
* ones
) stream
))
388 (write-string (svref *cardinal-ones
* ones
) stream
)))))))
390 (defun format-print-cardinal (stream n
)
392 (write-string "negative " stream
)
393 (format-print-cardinal-aux stream
(- n
) 0 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
))
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
)
413 (write-string "negative " stream
))
414 (let ((number (abs n
)))
415 (multiple-value-bind (top bot
) (truncate number
100)
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
))
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
))
430 (write-string (svref *cardinal-tens
* tens
) stream
)
431 (write-char #\- stream
)
432 (write-string (svref *ordinal-ones
* ones
) stream
))
434 (write-string "th" stream
))
436 (write-string "zeroth" stream
)))))))
438 ;;; Print Roman numerals
440 (defun format-print-old-roman (stream n
)
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
)
453 (defun format-print-roman (stream n
)
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
)
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
)))
477 (def-format-interpreter #\P
(colonp atsignp params
)
478 (interpret-bind-defaults () params
479 (let ((arg (if colonp
480 (if (eq orig-args args
)
482 :complaint
"no previous argument")
483 (do ((arg-ptr orig-args
(cdr arg-ptr
)))
484 ((eq (cdr arg-ptr
) args
)
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
)
500 "cannot specify the colon modifier with this directive"))
501 (interpret-bind-defaults ((w nil
) (d nil
) (k nil
) (ovf nil
) (pad #\space
))
503 (format-fixed stream
(next-arg) w d k ovf pad atsignp
)))
505 (defun format-fixed (stream number w d k ovf pad atsign
)
508 (format-fixed-aux stream number w d k ovf pad atsign
))
510 (format-fixed-aux stream
(coerce number
'single-float
)
511 w d k ovf pad atsign
))
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
))
523 ((or (float-infinity-p number
)
524 (float-nan-p number
))
525 (prin1 number stream
)
528 (sb!impl
::string-dispatch
(single-float double-float
)
531 (when (and w
(or atsign
(minusp (float-sign number
))))
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
))
541 ;; optional leading zero
543 (if (or (> spaceleft
0) tpoint
) ;force at least one digit
546 ;; optional trailing zero
551 (cond ((and w
(< spaceleft
0) ovf
)
552 ;; field width overflow
554 (write-char ovf stream
))
558 (dotimes (i spaceleft
)
559 (write-char pad stream
)))
560 (if (minusp (float-sign number
))
561 (write-char #\- stream
)
563 (write-char #\
+ stream
)))
565 (write-char #\
0 stream
))
566 (write-string str stream
)
568 (write-char #\
0 stream
))
571 (def-format-interpreter #\E
(colonp atsignp params
)
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
))
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
)
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
)
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
))
603 (typep number
'long-float
)))
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
))
631 (estr (decimal-string (abs expt
)))
632 (elen (if e
(max (length estr
) e
) (length estr
)))
635 (setf spaceleft
(- w
2 elen
))
636 (when (or atsign
(minusp (float-sign number
)))
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
))
646 (decf spaceleft flen
)
648 (if (or (> spaceleft
0) tpoint
)
651 (when (and tpoint
(<= spaceleft
0))
653 (cond ((and w
(< spaceleft
0) ovf
)
654 ;;significand overflow
655 (dotimes (i w
) (write-char ovf stream
)))
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
665 (format-exponent-marker number
))
667 (write-char (if (minusp expt
) #\-
#\
+) stream
)
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
)
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
))
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
)
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
)
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??
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
))
720 (let ((char (if (format-fixed-aux stream number ww dd nil
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
)))
741 (let* ((signstr (if (minusp (float-sign number
))
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
))
749 (write-string signstr stream
))
750 (dotimes (i (- w signlen
(max n pointplace
) 1 d
))
751 (write-char pad stream
))
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
)
762 ;;;; FORMAT interpreters and support functions for line/page breaks etc.
764 (def-format-interpreter #\%
(colonp atsignp params
)
765 (when (or colonp atsignp
)
768 "cannot specify either colon or atsign for this directive"))
769 (interpret-bind-defaults ((count 1)) params
773 (def-format-interpreter #\
& (colonp atsignp params
)
774 (when (or colonp atsignp
)
777 "cannot specify either colon or atsign for this directive"))
778 (interpret-bind-defaults ((count 1)) params
781 (dotimes (i (1- count
))
784 (def-format-interpreter #\|
(colonp atsignp params
)
785 (when (or colonp atsignp
)
788 "cannot specify either colon or atsign for this directive"))
789 (interpret-bind-defaults ((count 1)) params
791 (write-char (code-char form-feed-char-code
) stream
))))
793 (def-format-interpreter #\~
(colonp atsignp params
)
794 (when (or colonp atsignp
)
797 "cannot specify either colon or atsign for this directive"))
798 (interpret-bind-defaults ((count 1)) params
800 (write-char #\~ stream
))))
802 (def-complex-format-interpreter #\newline
(colonp atsignp params directives
)
803 (when (and colonp atsignp
)
806 "cannot specify both colon and atsign for this directive"))
807 (interpret-bind-defaults () params
809 (write-char #\newline stream
)))
810 (if (and (not colonp
)
812 (simple-string-p (car directives
)))
813 (cons (string-left-trim *format-whitespace-chars
*
818 ;;;; format interpreters and support functions for tabs and simple pretty
821 (def-format-interpreter #\T
(colonp atsignp params
)
823 (interpret-bind-defaults ((n 1) (m 1)) params
824 (pprint-tab (if atsignp
:section-relative
:section
) n m stream
))
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
)))
834 (when (< n
(length spaces
))
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
)
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
)))
854 (write-string " " stream
))
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
873 (def-format-interpreter #\I
(colonp atsignp params
)
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
)
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
))
891 :complaint
"Index ~W is out of bounds. (It should ~
892 have been between 0 and ~W.)"
893 :args
(list posn
(length orig-args
))))))
895 (interpret-bind-defaults ((n 1)) params
896 (do ((cur-posn 0 (1+ cur-posn
))
897 (arg-ptr orig-args
(cdr arg-ptr
)))
899 (let ((new-posn (- cur-posn n
)))
900 (if (<= 0 new-posn
(length orig-args
))
901 (setf args
(nthcdr new-posn orig-args
))
904 "Index ~W is out of bounds. (It should
905 have been between 0 and ~W.)"
907 (list new-posn
(length orig-args
))))))))
908 (interpret-bind-defaults ((n 1)) params
912 ;;;; format interpreter for indirection
914 (def-format-interpreter #\? (colonp atsignp params string end
)
917 :complaint
"cannot specify the colon modifier"))
918 (interpret-bind-defaults () params
924 "~A~%while processing indirect format string:"
925 :args
(list condition
)
927 :control-string string
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
)))
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
952 (setf args
(interpret-directive-list stream before orig-args args
))
955 (def-complex-format-interpreter #\
) ()
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
)
969 "cannot specify both the colon and at-sign modifiers")
973 "can only specify one section")
974 (interpret-bind-defaults () params
975 (let ((prev-args args
)
978 (interpret-directive-list stream
984 (if (= (length sublists
) 2)
985 (interpret-bind-defaults () params
987 (interpret-directive-list stream
(car sublists
)
989 (interpret-directive-list stream
(cadr sublists
)
993 "must specify exactly two sections"))
994 (interpret-bind-defaults ((index (next-arg))) params
995 (let* ((default (and last-semi-with-colon-p
997 (last (1- (length sublists
)))
999 (if (<= 0 index last
)
1000 (nth (- last index
) sublists
)
1002 (interpret-directive-list stream sublist orig-args
1006 (def-complex-format-interpreter #\
; ()
1007 (error 'format-error
1009 "~~; not contained within either ~~[...~~] or ~~<...~~>"))
1011 (def-complex-format-interpreter #\
] ()
1012 (error 'format-error
1014 "no corresponding open bracket"))
1016 ;;;; format interpreter for up-and-out
1018 (defvar *outside-args
*)
1020 (def-format-interpreter #\^
(colonp atsignp params
)
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
))
1032 (null *outside-args
*)
1034 (throw (if colonp
'up-up-and-out
'up-and-out
)
1037 ;;;; format interpreters for iteration
1039 (def-complex-format-interpreter #\
{
1040 (colonp atsignp params string end directives
)
1041 (let ((close (find-directive directives
#\
} nil
)))
1043 (error 'format-error
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
)
1051 (subseq directives
0 posn
)))
1052 (*up-up-and-out-allowed
* colonp
))
1054 ((do-guts (orig-args args
)
1062 "~A~%while processing indirect format string:"
1063 :args
(list condition
)
1065 :control-string string
1066 :offset
(1- end
)))))
1067 (%format stream insides orig-args args
))
1068 (interpret-directive-list stream insides
1070 (bind-args (orig-args args
)
1072 (let* ((arg (next-arg))
1073 (*logical-block-popper
* nil
)
1074 (*outside-args
* args
))
1078 (do-guts orig-args args
)))
1079 (do-loop (orig-args args
)
1080 (catch (if colonp
'up-up-and-out
'up-and-out
)
1082 (when (and (not closed-with-colon
) (null args
))
1084 (when (and max-count
(minusp (decf max-count
)))
1086 (setf args
(bind-args orig-args args
))
1087 (when (and closed-with-colon
(null args
))
1091 (setf args
(do-loop orig-args args
))
1092 (let ((arg (next-arg))
1093 (*logical-block-popper
* nil
))
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
)
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
1115 (let ((count (reduce #'+ (mapcar (lambda (x) (count-if #'illegal-inside-justification-p x
)) segments
))))
1117 ;; ANSI specifies that "an error is signalled" in this
1119 (error 'format-error
1120 :complaint
"~D illegal directive~:P found inside justification block"
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
))))
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
))
1143 (let ((newline-string nil
)
1149 (when (and first-semi
(format-directive-colonp first-semi
))
1150 (interpret-bind-defaults
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)
1157 (interpret-directive-list stream
1161 (setf extra-space extra
)
1162 (setf line-len len
)))
1163 (dolist (segment segments
)
1164 (push (with-simple-output-to-string (stream)
1166 (interpret-directive-list stream segment
1170 (format-justification stream newline-string extra-space line-len strings
1171 colonp atsignp mincol colinc minpad padchar
)))
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
))
1179 (if pad-right
1 0)))
1180 (chars (+ (* num-gaps minpad
)
1182 for string in strings
1183 summing
(length string
))))
1184 (length (if (> chars mincol
)
1185 (+ mincol
(* (ceiling (- chars mincol
) colinc
) colinc
))
1187 (padding (+ (- length chars
) (* num-gaps minpad
))))
1188 (when (and newline-prefix
1189 (> (+ (or (sb!impl
::charpos stream
) 0)
1192 (write-string newline-prefix stream
))
1193 (flet ((do-padding ()
1195 (if (zerop num-gaps
) padding
(truncate padding num-gaps
))))
1196 (decf padding pad-len
)
1198 (dotimes (i pad-len
) (write-char padchar stream
)))))
1199 (when (or pad-left
(and (not pad-right
) (null (cdr strings
))))
1202 (write-string (car strings
) stream
)
1203 (dolist (string (cdr strings
))
1205 (write-string string stream
)))
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))))
1213 (pprint-logical-block
1214 (stream arg
:per-line-prefix prefix
:suffix suffix
)
1215 (let ((*logical-block-popper
* (lambda () (pprint-pop))))
1217 (interpret-directive-list stream insides
1218 (if atsignp orig-args arg
)
1220 (pprint-logical-block (stream arg
:prefix prefix
:suffix suffix
)
1221 (let ((*logical-block-popper
* (lambda () (pprint-pop))))
1223 (interpret-directive-list stream insides
1224 (if atsignp orig-args 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
)))
1233 (dolist (param-and-offset params
)
1234 (let ((param (cdr param-and-offset
)))
1236 (:arg
(args (next-arg)))
1237 (:remaining
(args (length args
)))
1239 (apply (fdefinition symbol
) stream
(next-arg) colonp atsignp
(args)))))