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-simple-output-to-string (stream)
43 (%format stream control-string format-arguments
)))
45 (with-simple-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
)
77 (let* ((character (format-directive-character directive
))
81 (svref *format-directive-interpreters
* (char-code character
)))))
82 (*default-format-error-offset
*
83 (1- (format-directive-end directive
))))
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
)))))
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
)
106 :complaint
"no more arguments"
108 `(:offset
,offset
))))
109 (when *logical-block-popper
*
110 (funcall *logical-block-popper
*))
113 (sb!xc
:defmacro def-complex-format-interpreter
(char lambda-list
&body body
)
116 "~:@(~:C~)-FORMAT-DIRECTIVE-INTERPRETER"
118 (directive (sb!xc
:gensym
"DIRECTIVE"))
119 (directives (if lambda-list
(car (last lambda-list
)) (sb!xc
:gensym
"DIRECTIVES"))))
121 (defun ,defun-name
(stream ,directive
,directives orig-args args
)
122 (declare (ignorable stream orig-args args
))
124 `((let ,(mapcar (lambda (var)
126 (,(symbolicate "FORMAT-DIRECTIVE-" var
)
128 (butlast lambda-list
))
129 (values (progn ,@body
) args
)))
130 `((declare (ignore ,directive
,directives
))
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
)
140 (sb!xc
:defmacro interpret-bind-defaults
(specs params
&body body
)
141 (once-only ((params params
))
142 (collect ((bindings))
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
)))
149 (:arg
(or (next-arg offset
) ,default
))
150 (:remaining
(length args
))
157 "too many parameters, expected no more than ~W"
158 :args
(list ,(length specs
))
159 :offset
(caar ,params
)))
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))
169 :complaint
"The value of colinc is ~a, should be a positive integer"
170 :args
(list colinc
)))
171 (when (and mincol
(< mincol
0))
173 :complaint
"The value of mincol is ~a, should be a non-negative integer"
174 :args
(list mincol
)))
176 (write-string string stream
))
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
)))
187 (write-char padchar stream
))))
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
)
196 mincol colinc minpad padchar atsignp
))
198 (def-format-interpreter #\A
(colonp atsignp params
)
200 (interpret-bind-defaults ((mincol 0) (colinc 1) (minpad 0)
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
)
212 mincol colinc minpad padchar atsignp
))
214 (def-format-interpreter #\S
(colonp atsignp params
)
216 (interpret-bind-defaults ((mincol 0) (colinc 1) (minpad 0)
219 (format-prin1 stream
(next-arg) colonp atsignp
220 mincol colinc minpad padchar
)))
222 (let ((arg (next-arg)))
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
)
234 :complaint
"~s is not of type CHARACTER."
237 (format-print-named-character 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
)
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
)
274 (signed (cond ((minusp number
)
275 (concatenate 'string
"-" commaed
))
277 (concatenate 'string
"+" 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)))
292 (setf (schar new-string dst
) commachar
)
293 (replace new-string string
:start1
(1+ dst
)
294 :start2 src
:end2
(+ src commainterval
)))
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))
303 (format-print-integer stream
(next-arg) colonp atsignp
,base mincol
304 padchar commachar commainterval
))
305 (let ((*print-base
* ,base
)
307 (princ (next-arg) stream
))))
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 #\
,)
327 (let ((arg (next-arg)))
331 :complaint
"~s is not of type INTEGER."
334 (format-print-integer stream arg colonp atsignp base mincol
335 padchar commachar commainterval
)
338 (format-print-old-roman stream arg
)
339 (format-print-roman stream arg
))
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
)
376 (write-char #\space stream
)))
378 (multiple-value-bind (tens ones
) (truncate rem
10)
380 (write-string (svref *cardinal-tens
* tens
) stream
)
382 (write-char #\- stream
)
383 (write-string (svref *cardinal-ones
* ones
) stream
)))
385 (write-string (svref *cardinal-teens
* ones
) stream
))
387 (write-string (svref *cardinal-ones
* ones
) stream
)))))))
389 (defun format-print-cardinal (stream n
)
391 (write-string "negative " stream
)
392 (format-print-cardinal-aux stream
(- n
) 0 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
))
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
)
412 (write-string "negative " stream
))
413 (let ((number (abs n
)))
414 (multiple-value-bind (top bot
) (truncate number
100)
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
))
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
))
429 (write-string (svref *cardinal-tens
* tens
) stream
)
430 (write-char #\- stream
)
431 (write-string (svref *ordinal-ones
* ones
) stream
))
433 (write-string "th" stream
))
435 (write-string "zeroth" stream
)))))))
437 ;;; Print Roman numerals
439 (defun format-print-old-roman (stream n
)
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
)
452 (defun format-print-roman (stream n
)
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
)
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
)))
476 (def-format-interpreter #\P
(colonp atsignp params
)
477 (interpret-bind-defaults () params
478 (let ((arg (if colonp
479 (if (eq orig-args args
)
481 :complaint
"no previous argument")
482 (do ((arg-ptr orig-args
(cdr arg-ptr
)))
483 ((eq (cdr arg-ptr
) args
)
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
)
499 "cannot specify the colon modifier with this directive"))
500 (interpret-bind-defaults ((w nil
) (d nil
) (k nil
) (ovf nil
) (pad #\space
))
502 (format-fixed stream
(next-arg) w d k ovf pad atsignp
)))
504 (defun format-fixed (stream number w d k ovf pad atsign
)
507 (format-fixed-aux stream number w d k ovf pad atsign
))
509 (format-fixed-aux stream
(coerce number
'single-float
)
510 w d k ovf pad atsign
))
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
))
522 ((or (float-infinity-p number
)
523 (float-nan-p number
))
524 (prin1 number stream
)
527 (sb!impl
::string-dispatch
(single-float double-float
)
530 (when (and w
(or atsign
(minusp (float-sign number
))))
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
))
540 ;; optional leading zero
542 (if (or (> spaceleft
0) tpoint
) ;force at least one digit
545 ;; optional trailing zero
550 (cond ((and w
(< spaceleft
0) ovf
)
551 ;; field width overflow
553 (write-char ovf stream
))
557 (dotimes (i spaceleft
)
558 (write-char pad stream
)))
559 (if (minusp (float-sign number
))
560 (write-char #\- stream
)
562 (write-char #\
+ stream
)))
564 (write-char #\
0 stream
))
565 (write-string str stream
)
567 (write-char #\
0 stream
))
570 (def-format-interpreter #\E
(colonp atsignp params
)
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
))
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
)
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
)
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
*)
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
))
623 (estr (decimal-string (abs expt
)))
624 (elen (if e
(max (length estr
) e
) (length estr
)))
627 (setf spaceleft
(- w
2 elen
))
628 (when (or atsign
(minusp (float-sign number
)))
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
))
638 (decf spaceleft flen
)
640 (if (or (> spaceleft
0) tpoint
)
643 (when (and tpoint
(<= spaceleft
0))
645 (cond ((and w
(< spaceleft
0) ovf
)
646 ;;significand overflow
647 (dotimes (i w
) (write-char ovf stream
)))
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
657 (format-exponent-marker number
))
659 (write-char (if (minusp expt
) #\-
#\
+) stream
)
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
)
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
))
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
)
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
)
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??
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
))
712 (let ((char (if (format-fixed-aux stream number ww dd nil
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
)))
733 (let* ((signstr (if (minusp (float-sign number
))
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
))
741 (write-string signstr stream
))
742 (dotimes (i (- w signlen
(max n pointplace
) 1 d
))
743 (write-char pad stream
))
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
)
754 ;;;; FORMAT interpreters and support functions for line/page breaks etc.
756 (def-format-interpreter #\%
(colonp atsignp params
)
757 (when (or colonp atsignp
)
760 "cannot specify either colon or atsign for this directive"))
761 (interpret-bind-defaults ((count 1)) params
765 (def-format-interpreter #\
& (colonp atsignp params
)
766 (when (or colonp atsignp
)
769 "cannot specify either colon or atsign for this directive"))
770 (interpret-bind-defaults ((count 1)) params
773 (dotimes (i (1- count
))
776 (def-format-interpreter #\|
(colonp atsignp params
)
777 (when (or colonp atsignp
)
780 "cannot specify either colon or atsign for this directive"))
781 (interpret-bind-defaults ((count 1)) params
783 (write-char (code-char form-feed-char-code
) stream
))))
785 (def-format-interpreter #\~
(colonp atsignp params
)
786 (when (or colonp atsignp
)
789 "cannot specify either colon or atsign for this directive"))
790 (interpret-bind-defaults ((count 1)) params
792 (write-char #\~ stream
))))
794 (def-complex-format-interpreter #\newline
(colonp atsignp params directives
)
795 (when (and colonp atsignp
)
798 "cannot specify both colon and atsign for this directive"))
799 (interpret-bind-defaults () params
801 (write-char #\newline stream
)))
802 (if (and (not colonp
)
804 (simple-string-p (car directives
)))
805 (cons (string-left-trim *format-whitespace-chars
*
810 ;;;; format interpreters and support functions for tabs and simple pretty
813 (def-format-interpreter #\T
(colonp atsignp params
)
815 (interpret-bind-defaults ((n 1) (m 1)) params
816 (pprint-tab (if atsignp
:section-relative
:section
) n m stream
))
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
)))
826 (when (< n
(length spaces
))
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
)
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
)))
846 (write-string " " stream
))
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
865 (def-format-interpreter #\I
(colonp atsignp params
)
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
)
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
))
883 :complaint
"Index ~W is out of bounds. (It should ~
884 have been between 0 and ~W.)"
885 :args
(list posn
(length orig-args
))))))
887 (interpret-bind-defaults ((n 1)) params
888 (do ((cur-posn 0 (1+ cur-posn
))
889 (arg-ptr orig-args
(cdr arg-ptr
)))
891 (let ((new-posn (- cur-posn n
)))
892 (if (<= 0 new-posn
(length orig-args
))
893 (setf args
(nthcdr new-posn orig-args
))
896 "Index ~W is out of bounds. (It should
897 have been between 0 and ~W.)"
899 (list new-posn
(length orig-args
))))))))
900 (interpret-bind-defaults ((n 1)) params
904 ;;;; format interpreter for indirection
906 (def-format-interpreter #\? (colonp atsignp params string end
)
909 :complaint
"cannot specify the colon modifier"))
910 (interpret-bind-defaults () params
916 "~A~%while processing indirect format string:"
917 :args
(list condition
)
919 :control-string string
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
)))
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
944 (setf args
(interpret-directive-list stream before orig-args args
))
947 (def-complex-format-interpreter #\
) ()
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
)
961 "cannot specify both the colon and at-sign modifiers")
965 "can only specify one section")
966 (interpret-bind-defaults () params
967 (let ((prev-args args
)
970 (interpret-directive-list stream
976 (if (= (length sublists
) 2)
977 (interpret-bind-defaults () params
979 (interpret-directive-list stream
(car sublists
)
981 (interpret-directive-list stream
(cadr sublists
)
985 "must specify exactly two sections"))
986 (interpret-bind-defaults ((index (next-arg))) params
987 (let* ((default (and last-semi-with-colon-p
989 (last (1- (length sublists
)))
991 (if (<= 0 index last
)
992 (nth (- last index
) sublists
)
994 (interpret-directive-list stream sublist orig-args
998 (def-complex-format-interpreter #\
; ()
1001 "~~; not contained within either ~~[...~~] or ~~<...~~>"))
1003 (def-complex-format-interpreter #\
] ()
1004 (error 'format-error
1006 "no corresponding open bracket"))
1008 ;;;; format interpreter for up-and-out
1010 (defvar *outside-args
*)
1012 (def-format-interpreter #\^
(colonp atsignp params
)
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
))
1024 (null *outside-args
*)
1026 (throw (if colonp
'up-up-and-out
'up-and-out
)
1029 ;;;; format interpreters for iteration
1031 (def-complex-format-interpreter #\
{
1032 (colonp atsignp params string end directives
)
1033 (let ((close (find-directive directives
#\
} nil
)))
1035 (error 'format-error
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
)
1043 (subseq directives
0 posn
)))
1044 (*up-up-and-out-allowed
* colonp
))
1046 ((do-guts (orig-args args
)
1054 "~A~%while processing indirect format string:"
1055 :args
(list condition
)
1057 :control-string string
1058 :offset
(1- end
)))))
1059 (%format stream insides orig-args args
))
1060 (interpret-directive-list stream insides
1062 (bind-args (orig-args args
)
1064 (let* ((arg (next-arg))
1065 (*logical-block-popper
* nil
)
1066 (*outside-args
* args
))
1070 (do-guts orig-args args
)))
1071 (do-loop (orig-args args
)
1072 (catch (if colonp
'up-up-and-out
'up-and-out
)
1074 (when (and (not closed-with-colon
) (null args
))
1076 (when (and max-count
(minusp (decf max-count
)))
1078 (setf args
(bind-args orig-args args
))
1079 (when (and closed-with-colon
(null args
))
1083 (setf args
(do-loop orig-args args
))
1084 (let ((arg (next-arg))
1085 (*logical-block-popper
* nil
))
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
)
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
1107 (let ((count (reduce #'+ (mapcar (lambda (x) (count-if #'illegal-inside-justification-p x
)) segments
))))
1109 ;; ANSI specifies that "an error is signalled" in this
1111 (error 'format-error
1112 :complaint
"~D illegal directive~:P found inside justification block"
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
))))
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
))
1135 (let ((newline-string nil
)
1141 (when (and first-semi
(format-directive-colonp first-semi
))
1142 (interpret-bind-defaults
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)
1149 (interpret-directive-list stream
1153 (setf extra-space extra
)
1154 (setf line-len len
)))
1155 (dolist (segment segments
)
1156 (push (with-simple-output-to-string (stream)
1158 (interpret-directive-list stream segment
1162 (format-justification stream newline-string extra-space line-len strings
1163 colonp atsignp mincol colinc minpad padchar
)))
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
))
1171 (if pad-right
1 0)))
1172 (chars (+ (* num-gaps minpad
)
1174 for string in strings
1175 summing
(length string
))))
1176 (length (if (> chars mincol
)
1177 (+ mincol
(* (ceiling (- chars mincol
) colinc
) colinc
))
1179 (padding (+ (- length chars
) (* num-gaps minpad
))))
1180 (when (and newline-prefix
1181 (> (+ (or (sb!impl
::charpos stream
) 0)
1184 (write-string newline-prefix stream
))
1185 (flet ((do-padding ()
1187 (if (zerop num-gaps
) padding
(truncate padding num-gaps
))))
1188 (decf padding pad-len
)
1190 (dotimes (i pad-len
) (write-char padchar stream
)))))
1191 (when (or pad-left
(and (not pad-right
) (null (cdr strings
))))
1194 (write-string (car strings
) stream
)
1195 (dolist (string (cdr strings
))
1197 (write-string string stream
)))
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))))
1205 (pprint-logical-block
1206 (stream arg
:per-line-prefix prefix
:suffix suffix
)
1207 (let ((*logical-block-popper
* (lambda () (pprint-pop))))
1209 (interpret-directive-list stream insides
1210 (if atsignp orig-args arg
)
1212 (pprint-logical-block (stream arg
:prefix prefix
:suffix suffix
)
1213 (let ((*logical-block-popper
* (lambda () (pprint-pop))))
1215 (interpret-directive-list stream insides
1216 (if atsignp orig-args 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
)))
1225 (dolist (param-and-offset params
)
1226 (let ((param (cdr param-and-offset
)))
1228 (:arg
(args (next-arg)))
1229 (:remaining
(args (length args
)))
1231 (apply (fdefinition symbol
) stream
(next-arg) colonp atsignp
(args)))))