aboutsummaryrefslogtreecommitdiffstats
path: root/doc/lispref
diff options
context:
space:
mode:
authorChong Yidong2008-02-26 18:48:00 +0000
committerChong Yidong2008-02-26 18:48:00 +0000
commitb20e7c7dcc2fd4c676ce0afdb98db3e7e8e8fd5f (patch)
tree7f24898c7743365ece9c40d72f3b0c7f16bfb45c /doc/lispref
parentc6b0dfd519e7edd6e671a9026cd9022cd37a377e (diff)
downloademacs-b20e7c7dcc2fd4c676ce0afdb98db3e7e8e8fd5f.tar.gz
emacs-b20e7c7dcc2fd4c676ce0afdb98db3e7e8e8fd5f.zip
(Formatting Strings): Treat - and 0 as flag characters.
Diffstat (limited to 'doc/lispref')
-rw-r--r--doc/lispref/strings.texi99
1 files changed, 51 insertions, 48 deletions
diff --git a/doc/lispref/strings.texi b/doc/lispref/strings.texi
index 20884461b8c..37c19fc4f3e 100644
--- a/doc/lispref/strings.texi
+++ b/doc/lispref/strings.texi
@@ -823,58 +823,80 @@ operation} error.
823 823
824@cindex field width 824@cindex field width
825@cindex padding 825@cindex padding
826 A specification can have a @dfn{width}, which is a signed decimal 826 A specification can have a @dfn{width}, which is a decimal number
827number between the @samp{%} and the specification character. If the 827between the @samp{%} and the specification character. If the printed
828printed representation of the object contains fewer characters than 828representation of the object contains fewer characters than this
829this width, @code{format} extends it with padding. The padding goes 829width, @code{format} extends it with padding. The width specifier is
830on the left if the width is positive (or starts with zero) and on the 830ignored for the @samp{%%} specification. Any padding introduced by
831right if the width is negative. The padding character is normally a 831the width specifier normally consists of spaces inserted on the left:
832space, but it's @samp{0} if the width starts with a zero.
833
834 Some of these conventions are ignored for specification characters
835for which they do not make sense. That is, @samp{%s}, @samp{%S} and
836@samp{%c} accept a width starting with 0, but still pad with
837@emph{spaces} on the left. Also, @samp{%%} accepts a width, but
838ignores it. Here are some examples of padding:
839 832
840@example 833@example
841(format "%06d is padded on the left with zeros" 123) 834(format "%5d is padded on the left with spaces" 123)
842 @result{} "000123 is padded on the left with zeros" 835 @result{} " 123 is padded on the left with spaces"
843
844(format "%-6d is padded on the right" 123)
845 @result{} "123 is padded on the right"
846@end example 836@end example
847 837
848@noindent 838@noindent
849If the width is too small, @code{format} does not truncate the 839If the width is too small, @code{format} does not truncate the
850object's printed representation. Thus, you can use a width to specify 840object's printed representation. Thus, you can use a width to specify
851a minimum spacing between columns with no risk of losing information. 841a minimum spacing between columns with no risk of losing information.
842In the following three examples, @samp{%7s} specifies a minimum width
843of 7. In the first case, the string inserted in place of @samp{%7s}
844has only 3 letters, and needs 4 blank spaces as padding. In the
845second case, the string @code{"specification"} is 13 letters wide but
846is not truncated.
852 847
853 In the following three examples, @samp{%7s} specifies a minimum 848@example
854width of 7. In the first case, the string inserted in place of
855@samp{%7s} has only 3 letters, it needs 4 blank spaces as padding. In
856the second case, the string @code{"specification"} is 13 letters wide
857but is not truncated. In the third case, the padding is on the right.
858
859@smallexample
860@group 849@group
861(format "The word `%7s' actually has %d letters in it." 850(format "The word `%7s' actually has %d letters in it."
862 "foo" (length "foo")) 851 "foo" (length "foo"))
863 @result{} "The word ` foo' actually has 3 letters in it." 852 @result{} "The word ` foo' actually has 3 letters in it."
864@end group
865
866@group
867(format "The word `%7s' actually has %d letters in it." 853(format "The word `%7s' actually has %d letters in it."
868 "specification" (length "specification")) 854 "specification" (length "specification"))
869 @result{} "The word `specification' actually has 13 letters in it." 855 @result{} "The word `specification' actually has 13 letters in it."
870@end group 856@end group
857@end example
858
859@cindex flags in format specifications
860 Immediately after the @samp{%} and before the optional width
861specifier, you can also put certain @dfn{flag characters}.
862
863 The flag @samp{+} inserts a plus sign before a positive number, so
864that it always has a sign. A space character as flag inserts a space
865before a positive number. (Otherwise, positive numbers start with the
866first digit.) These flags are useful for ensuring that positive
867numbers and negative numbers use the same number of columns. They are
868ignored except for @samp{%d}, @samp{%e}, @samp{%f}, @samp{%g}, and if
869both flags are used, @samp{+} takes precedence.
870
871 The flag @samp{#} specifies an ``alternate form'' which depends on
872the format in use. For @samp{%o}, it ensures that the result begins
873with a @samp{0}. For @samp{%x} and @samp{%X}, it prefixes the result
874with @samp{0x} or @samp{0X}. For @samp{%e}, @samp{%f}, and @samp{%g},
875the @samp{#} flag means include a decimal point even if the precision
876is zero.
877
878 The flag @samp{-} causes the padding inserted by the width
879specifier, if any, to be inserted on the right rather than the left.
880The flag @samp{0} ensures that the padding consists of @samp{0}
881characters instead of spaces, inserted on the left. These flags are
882ignored for specification characters for which they do not make sense:
883@samp{%s}, @samp{%S} and @samp{%c} accept the @samp{0} flag, but still
884pad with @emph{spaces} on the left. If both @samp{-} and @samp{0} are
885present and valid, @samp{-} takes precedence.
871 886
887@example
872@group 888@group
889(format "%06d is padded on the left with zeros" 123)
890 @result{} "000123 is padded on the left with zeros"
891
892(format "%-6d is padded on the right" 123)
893 @result{} "123 is padded on the right"
894
873(format "The word `%-7s' actually has %d letters in it." 895(format "The word `%-7s' actually has %d letters in it."
874 "foo" (length "foo")) 896 "foo" (length "foo"))
875 @result{} "The word `foo ' actually has 3 letters in it." 897 @result{} "The word `foo ' actually has 3 letters in it."
876@end group 898@end group
877@end smallexample 899@end example
878 900
879@cindex precision in format specifications 901@cindex precision in format specifications
880 All the specification characters allow an optional @dfn{precision} 902 All the specification characters allow an optional @dfn{precision}
@@ -888,25 +910,6 @@ shows only the first three characters of the representation for
888@var{object}. Precision has no effect for other specification 910@var{object}. Precision has no effect for other specification
889characters. 911characters.
890 912
891@cindex flags in format specifications
892 Immediately after the @samp{%} and before the optional width and
893precision, you can put certain ``flag'' characters.
894
895 @samp{+} as a flag inserts a plus sign before a positive number, so
896that it always has a sign. A space character as flag inserts a space
897before a positive number. (Otherwise, positive numbers start with the
898first digit.) Either of these two flags ensures that positive numbers
899and negative numbers use the same number of columns. These flags are
900ignored except for @samp{%d}, @samp{%e}, @samp{%f}, @samp{%g}, and if
901both flags are used, the @samp{+} takes precedence.
902
903 The flag @samp{#} specifies an ``alternate form'' which depends on
904the format in use. For @samp{%o} it ensures that the result begins
905with a @samp{0}. For @samp{%x} and @samp{%X}, it prefixes the result
906with @samp{0x} or @samp{0X}. For @samp{%e}, @samp{%f}, and @samp{%g},
907the @samp{#} flag means include a decimal point even if the precision
908is zero.
909
910@node Case Conversion 913@node Case Conversion
911@comment node-name, next, previous, up 914@comment node-name, next, previous, up
912@section Case Conversion in Lisp 915@section Case Conversion in Lisp