diff options
| author | Chong Yidong | 2008-02-26 18:48:00 +0000 |
|---|---|---|
| committer | Chong Yidong | 2008-02-26 18:48:00 +0000 |
| commit | b20e7c7dcc2fd4c676ce0afdb98db3e7e8e8fd5f (patch) | |
| tree | 7f24898c7743365ece9c40d72f3b0c7f16bfb45c /doc/lispref | |
| parent | c6b0dfd519e7edd6e671a9026cd9022cd37a377e (diff) | |
| download | emacs-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.texi | 99 |
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 |
| 827 | number between the @samp{%} and the specification character. If the | 827 | between the @samp{%} and the specification character. If the printed |
| 828 | printed representation of the object contains fewer characters than | 828 | representation of the object contains fewer characters than this |
| 829 | this width, @code{format} extends it with padding. The padding goes | 829 | width, @code{format} extends it with padding. The width specifier is |
| 830 | on the left if the width is positive (or starts with zero) and on the | 830 | ignored for the @samp{%%} specification. Any padding introduced by |
| 831 | right if the width is negative. The padding character is normally a | 831 | the width specifier normally consists of spaces inserted on the left: |
| 832 | space, but it's @samp{0} if the width starts with a zero. | ||
| 833 | |||
| 834 | Some of these conventions are ignored for specification characters | ||
| 835 | for 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 | ||
| 838 | ignores 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 |
| 849 | If the width is too small, @code{format} does not truncate the | 839 | If the width is too small, @code{format} does not truncate the |
| 850 | object's printed representation. Thus, you can use a width to specify | 840 | object's printed representation. Thus, you can use a width to specify |
| 851 | a minimum spacing between columns with no risk of losing information. | 841 | a minimum spacing between columns with no risk of losing information. |
| 842 | In the following three examples, @samp{%7s} specifies a minimum width | ||
| 843 | of 7. In the first case, the string inserted in place of @samp{%7s} | ||
| 844 | has only 3 letters, and needs 4 blank spaces as padding. In the | ||
| 845 | second case, the string @code{"specification"} is 13 letters wide but | ||
| 846 | is not truncated. | ||
| 852 | 847 | ||
| 853 | In the following three examples, @samp{%7s} specifies a minimum | 848 | @example |
| 854 | width 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 | ||
| 856 | the second case, the string @code{"specification"} is 13 letters wide | ||
| 857 | but 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 | ||
| 861 | specifier, you can also put certain @dfn{flag characters}. | ||
| 862 | |||
| 863 | The flag @samp{+} inserts a plus sign before a positive number, so | ||
| 864 | that it always has a sign. A space character as flag inserts a space | ||
| 865 | before a positive number. (Otherwise, positive numbers start with the | ||
| 866 | first digit.) These flags are useful for ensuring that positive | ||
| 867 | numbers and negative numbers use the same number of columns. They are | ||
| 868 | ignored except for @samp{%d}, @samp{%e}, @samp{%f}, @samp{%g}, and if | ||
| 869 | both flags are used, @samp{+} takes precedence. | ||
| 870 | |||
| 871 | The flag @samp{#} specifies an ``alternate form'' which depends on | ||
| 872 | the format in use. For @samp{%o}, it ensures that the result begins | ||
| 873 | with a @samp{0}. For @samp{%x} and @samp{%X}, it prefixes the result | ||
| 874 | with @samp{0x} or @samp{0X}. For @samp{%e}, @samp{%f}, and @samp{%g}, | ||
| 875 | the @samp{#} flag means include a decimal point even if the precision | ||
| 876 | is zero. | ||
| 877 | |||
| 878 | The flag @samp{-} causes the padding inserted by the width | ||
| 879 | specifier, if any, to be inserted on the right rather than the left. | ||
| 880 | The flag @samp{0} ensures that the padding consists of @samp{0} | ||
| 881 | characters instead of spaces, inserted on the left. These flags are | ||
| 882 | ignored 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 | ||
| 884 | pad with @emph{spaces} on the left. If both @samp{-} and @samp{0} are | ||
| 885 | present 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 |
| 889 | characters. | 911 | characters. |
| 890 | 912 | ||
| 891 | @cindex flags in format specifications | ||
| 892 | Immediately after the @samp{%} and before the optional width and | ||
| 893 | precision, you can put certain ``flag'' characters. | ||
| 894 | |||
| 895 | @samp{+} as a flag inserts a plus sign before a positive number, so | ||
| 896 | that it always has a sign. A space character as flag inserts a space | ||
| 897 | before a positive number. (Otherwise, positive numbers start with the | ||
| 898 | first digit.) Either of these two flags ensures that positive numbers | ||
| 899 | and negative numbers use the same number of columns. These flags are | ||
| 900 | ignored except for @samp{%d}, @samp{%e}, @samp{%f}, @samp{%g}, and if | ||
| 901 | both flags are used, the @samp{+} takes precedence. | ||
| 902 | |||
| 903 | The flag @samp{#} specifies an ``alternate form'' which depends on | ||
| 904 | the format in use. For @samp{%o} it ensures that the result begins | ||
| 905 | with a @samp{0}. For @samp{%x} and @samp{%X}, it prefixes the result | ||
| 906 | with @samp{0x} or @samp{0X}. For @samp{%e}, @samp{%f}, and @samp{%g}, | ||
| 907 | the @samp{#} flag means include a decimal point even if the precision | ||
| 908 | is 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 |