diff options
| author | Jonathan Yavner | 2004-01-24 17:39:47 +0000 |
|---|---|---|
| committer | Jonathan Yavner | 2004-01-24 17:39:47 +0000 |
| commit | 728345f8b15055d2d9b8273b87f674399f8c41e5 (patch) | |
| tree | 2b02fe222fe82efbffe82232a92a3b24cb89a728 | |
| parent | 2528f9c4a681cabd8038411807ddd2128d5e3dbb (diff) | |
| download | emacs-728345f8b15055d2d9b8273b87f674399f8c41e5.tar.gz emacs-728345f8b15055d2d9b8273b87f674399f8c41e5.zip | |
For `format', make source and documentation match.
| -rw-r--r-- | lispref/strings.texi | 52 | ||||
| -rw-r--r-- | src/editfns.c | 39 |
2 files changed, 55 insertions, 36 deletions
diff --git a/lispref/strings.texi b/lispref/strings.texi index 7cc182cc058..60a74313a85 100644 --- a/lispref/strings.texi +++ b/lispref/strings.texi | |||
| @@ -798,19 +798,18 @@ operation} error. | |||
| 798 | @cindex numeric prefix | 798 | @cindex numeric prefix |
| 799 | @cindex field width | 799 | @cindex field width |
| 800 | @cindex padding | 800 | @cindex padding |
| 801 | All the specification characters allow an optional numeric prefix | 801 | All the specification characters allow an optional ``width'', which |
| 802 | between the @samp{%} and the character. The optional numeric prefix | 802 | is a digit-string between the @samp{%} and the character. If the |
| 803 | defines the minimum width for the object. If the printed | 803 | printed representation of the object contains fewer characters than |
| 804 | representation of the object contains fewer characters than this, then | 804 | this width, then it is padded. The padding is on the left if the |
| 805 | it is padded. The padding is on the left if the prefix is positive | 805 | prefix is positive (or starts with zero) and on the right if the |
| 806 | (or starts with zero) and on the right if the prefix is negative. The | 806 | prefix is negative. The padding character is normally a space, but if |
| 807 | padding character is normally a space, but if the numeric prefix | 807 | the width starts with a zero, zeros are used for padding. Some of |
| 808 | starts with a zero, zeros are used for padding. Some of these | 808 | these conventions are ignored for specification characters for which |
| 809 | conventions are ignored for specification characters for which they do | 809 | they do not make sense. That is, %s, %S and %c accept a width |
| 810 | not make sense. That is, %s, %S and %c accept a numeric prefix | ||
| 811 | starting with 0, but still pad with @emph{spaces} on the left. Also, | 810 | starting with 0, but still pad with @emph{spaces} on the left. Also, |
| 812 | %% accepts a numeric prefix, but ignores it. Here are some examples | 811 | %% accepts a width, but ignores it. Here are some examples of |
| 813 | of padding: | 812 | padding: |
| 814 | 813 | ||
| 815 | @example | 814 | @example |
| 816 | (format "%06d is padded on the left with zeros" 123) | 815 | (format "%06d is padded on the left with zeros" 123) |
| @@ -820,10 +819,9 @@ of padding: | |||
| 820 | @result{} "123 is padded on the right" | 819 | @result{} "123 is padded on the right" |
| 821 | @end example | 820 | @end example |
| 822 | 821 | ||
| 823 | @code{format} never truncates an object's printed representation, no | 822 | If the width is too small, @code{format} does not truncate the |
| 824 | matter what width you specify. Thus, you can use a numeric prefix to | 823 | object's printed representation. Thus, you can use a width to specify |
| 825 | specify a minimum spacing between columns with no risk of losing | 824 | a minimum spacing between columns with no risk of losing information. |
| 826 | information. | ||
| 827 | 825 | ||
| 828 | In the following three examples, @samp{%7s} specifies a minimum width | 826 | In the following three examples, @samp{%7s} specifies a minimum width |
| 829 | of 7. In the first case, the string inserted in place of @samp{%7s} has | 827 | of 7. In the first case, the string inserted in place of @samp{%7s} has |
| @@ -851,6 +849,28 @@ not truncated. In the third case, the padding is on the right. | |||
| 851 | @end group | 849 | @end group |
| 852 | @end smallexample | 850 | @end smallexample |
| 853 | 851 | ||
| 852 | All the specification characters allow an optional ``precision'' | ||
| 853 | before the character (after the width, if present). The precision is | ||
| 854 | a decimal-point @samp{.} followed by a digit-string. For the | ||
| 855 | floating-point specifications (%e, %f, %g), the precision specifies | ||
| 856 | how many decimal places to show; if zero, the decimal-point itself is | ||
| 857 | also omitted. For %s and %S, the precision truncates the string to | ||
| 858 | the given width, so @code{"%.3s"} shows only the first three | ||
| 859 | characters of the representation for @var{object}. Precision is | ||
| 860 | ignored for other specification characters. | ||
| 861 | |||
| 862 | Immediately after the % and before the optional width and precision, | ||
| 863 | you can put certain ``flag'' characters. | ||
| 864 | |||
| 865 | A space @var{" "} inserts a space for positive numbers (otherwise | ||
| 866 | nothing is inserted for positive numbers). This flag is ignored | ||
| 867 | except for %d, %e, %f, %g. | ||
| 868 | |||
| 869 | The flag @var{"#"} indicates ``alternate form''. For %o it ensures | ||
| 870 | that the result begins with a 0. For %x and %X the result is prefixed | ||
| 871 | with ``0x'' or ``0X''. For %e, %f, and %g a decimal point is always | ||
| 872 | shown even if the precision is zero. | ||
| 873 | |||
| 854 | @node Case Conversion | 874 | @node Case Conversion |
| 855 | @comment node-name, next, previous, up | 875 | @comment node-name, next, previous, up |
| 856 | @section Case Conversion in Lisp | 876 | @section Case Conversion in Lisp |
diff --git a/src/editfns.c b/src/editfns.c index a636c35a464..d3039ca0273 100644 --- a/src/editfns.c +++ b/src/editfns.c | |||
| @@ -3193,6 +3193,10 @@ It may contain %-sequences meaning to substitute the next argument. | |||
| 3193 | The argument used for %d, %o, %x, %e, %f, %g or %c must be a number. | 3193 | The argument used for %d, %o, %x, %e, %f, %g or %c must be a number. |
| 3194 | Use %% to put a single % into the output. | 3194 | Use %% to put a single % into the output. |
| 3195 | 3195 | ||
| 3196 | The basic structure of a %-sequence is | ||
| 3197 | % <flags> <width> <precision> character | ||
| 3198 | where flags is [- #0]+, width is [0-9]+, and precision is .[0-9]+ | ||
| 3199 | |||
| 3196 | usage: (format STRING &rest OBJECTS) */) | 3200 | usage: (format STRING &rest OBJECTS) */) |
| 3197 | (nargs, args) | 3201 | (nargs, args) |
| 3198 | int nargs; | 3202 | int nargs; |
| @@ -3300,7 +3304,7 @@ usage: (format STRING &rest OBJECTS) */) | |||
| 3300 | 3304 | ||
| 3301 | where | 3305 | where |
| 3302 | 3306 | ||
| 3303 | flags ::= [#-* 0]+ | 3307 | flags ::= [- #0]+ |
| 3304 | field-width ::= [0-9]+ | 3308 | field-width ::= [0-9]+ |
| 3305 | precision ::= '.' [0-9]* | 3309 | precision ::= '.' [0-9]* |
| 3306 | 3310 | ||
| @@ -3312,14 +3316,7 @@ usage: (format STRING &rest OBJECTS) */) | |||
| 3312 | digits to print after the '.' for floats, or the max. | 3316 | digits to print after the '.' for floats, or the max. |
| 3313 | number of chars to print from a string. */ | 3317 | number of chars to print from a string. */ |
| 3314 | 3318 | ||
| 3315 | /* NOTE the handling of specifiers here differs in some ways | 3319 | while (index ("-0# ", *format)) |
| 3316 | from the libc model. There are bugs in this code that lead | ||
| 3317 | to incorrect formatting when flags recognized by C but | ||
| 3318 | neither parsed nor rejected here are used. Further | ||
| 3319 | revisions will be made soon. */ | ||
| 3320 | |||
| 3321 | /* incorrect list of flags to skip; will be fixed */ | ||
| 3322 | while (index ("-*# 0", *format)) | ||
| 3323 | ++format; | 3320 | ++format; |
| 3324 | 3321 | ||
| 3325 | if (*format >= '0' && *format <= '9') | 3322 | if (*format >= '0' && *format <= '9') |
| @@ -3403,7 +3400,7 @@ usage: (format STRING &rest OBJECTS) */) | |||
| 3403 | if (*format == 'c') | 3400 | if (*format == 'c') |
| 3404 | { | 3401 | { |
| 3405 | if (! SINGLE_BYTE_CHAR_P (XINT (args[n])) | 3402 | if (! SINGLE_BYTE_CHAR_P (XINT (args[n])) |
| 3406 | /* Note: No one can remeber why we have to treat | 3403 | /* Note: No one can remember why we have to treat |
| 3407 | the character 0 as a multibyte character here. | 3404 | the character 0 as a multibyte character here. |
| 3408 | But, until it causes a real problem, let's | 3405 | But, until it causes a real problem, let's |
| 3409 | don't change it. */ | 3406 | don't change it. */ |
| @@ -3494,17 +3491,19 @@ usage: (format STRING &rest OBJECTS) */) | |||
| 3494 | discarded[format - format_start] = 1; | 3491 | discarded[format - format_start] = 1; |
| 3495 | format++; | 3492 | format++; |
| 3496 | 3493 | ||
| 3497 | /* Process a numeric arg and skip it. */ | 3494 | while (index("-0# ", *format)) |
| 3498 | /* NOTE atoi is the wrong thing to use here; will be fixed */ | 3495 | { |
| 3496 | if (*format == '-') | ||
| 3497 | { | ||
| 3498 | negative = 1; | ||
| 3499 | } | ||
| 3500 | discarded[format - format_start] = 1; | ||
| 3501 | ++format; | ||
| 3502 | } | ||
| 3503 | |||
| 3499 | minlen = atoi (format); | 3504 | minlen = atoi (format); |
| 3500 | if (minlen < 0) | 3505 | |
| 3501 | minlen = - minlen, negative = 1; | 3506 | while ((*format >= '0' && *format <= '9') || *format == '.') |
| 3502 | |||
| 3503 | /* NOTE the parsing here is not consistent with the first | ||
| 3504 | pass, and neither attempt is what we want to do. Will be | ||
| 3505 | fixed. */ | ||
| 3506 | while ((*format >= '0' && *format <= '9') | ||
| 3507 | || *format == '-' || *format == ' ' || *format == '.') | ||
| 3508 | { | 3507 | { |
| 3509 | discarded[format - format_start] = 1; | 3508 | discarded[format - format_start] = 1; |
| 3510 | format++; | 3509 | format++; |