diff options
| author | Paul Eggert | 2018-07-26 00:34:10 -0700 |
|---|---|---|
| committer | Paul Eggert | 2018-07-26 00:39:17 -0700 |
| commit | 4a56ca5bbfabbb9c581828cd91648346e6b03844 (patch) | |
| tree | 90b804ea4ec22a8b7be181f0b505b57c40a85c27 /doc | |
| parent | 19f5f7b19b0dcdae87476a3fd51c41f840b2b80f (diff) | |
| download | emacs-4a56ca5bbfabbb9c581828cd91648346e6b03844.tar.gz emacs-4a56ca5bbfabbb9c581828cd91648346e6b03844.zip | |
%o and %x can now format signed integers
Optionally treat integers as signed numbers with %o
and %x format specifiers, instead of treating them as
a machine-dependent two’s complement representation.
This option is more machine-independent, allows formats
like "#x%x" to be useful for reading later, and is
better-insulated for future changes involving bignums.
Setting the new variable ‘binary-as-unsigned’ to nil
enables the new behavior (Bug#32252).
This is a simplified version of the change proposed in:
https://lists.gnu.org/r/emacs-devel/2018-07/msg00763.html
I simplified that proposal by omitting bitwidth modifiers, as
I could not find an any example uses in the Emacs source code
that needed them and doing them correctly would have been
quite a bit more work for apparently little benefit.
* doc/lispref/strings.texi (Formatting Strings):
Document that %x and %o format negative integers in a
platform-dependent way. Also, document how to format
numbers so that the same values can be read back in.
* etc/NEWS: Document the change.
* src/editfns.c (styled_format): Treat integers as signed
numbers even with %o and %x, if binary-as-unsigned is nil.
Support the + and space flags with %o and %x, since they’re
about signs.
(syms_of_editfns): New variable binary-as-unsigned.
* test/src/editfns-tests.el (read-large-integer):
Test that maximal integers can be read after printing
with all integer formats, if binary-as-unsigned is nil.
Diffstat (limited to 'doc')
| -rw-r--r-- | doc/lispref/strings.texi | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/doc/lispref/strings.texi b/doc/lispref/strings.texi index 2fff3c7c75c..3558f17301d 100644 --- a/doc/lispref/strings.texi +++ b/doc/lispref/strings.texi | |||
| @@ -922,7 +922,8 @@ Functions}). Thus, strings are enclosed in @samp{"} characters, and | |||
| 922 | @item %o | 922 | @item %o |
| 923 | @cindex integer to octal | 923 | @cindex integer to octal |
| 924 | Replace the specification with the base-eight representation of an | 924 | Replace the specification with the base-eight representation of an |
| 925 | unsigned integer. The object can also be a nonnegative floating-point | 925 | integer. Negative integers are formatted in a platform-dependent |
| 926 | way. The object can also be a nonnegative floating-point | ||
| 926 | number that is formatted as an integer, dropping any fraction, if the | 927 | number that is formatted as an integer, dropping any fraction, if the |
| 927 | integer does not exceed machine limits. | 928 | integer does not exceed machine limits. |
| 928 | 929 | ||
| @@ -935,7 +936,8 @@ formatted as an integer, dropping any fraction. | |||
| 935 | @itemx %X | 936 | @itemx %X |
| 936 | @cindex integer to hexadecimal | 937 | @cindex integer to hexadecimal |
| 937 | Replace the specification with the base-sixteen representation of an | 938 | Replace the specification with the base-sixteen representation of an |
| 938 | unsigned integer. @samp{%x} uses lower case and @samp{%X} uses upper | 939 | integer. Negative integers are formatted in a platform-dependent |
| 940 | way. @samp{%x} uses lower case and @samp{%X} uses upper | ||
| 939 | case. The object can also be a nonnegative floating-point number that | 941 | case. The object can also be a nonnegative floating-point number that |
| 940 | is formatted as an integer, dropping any fraction, if the integer does | 942 | is formatted as an integer, dropping any fraction, if the integer does |
| 941 | not exceed machine limits. | 943 | not exceed machine limits. |
| @@ -1108,6 +1110,17 @@ shows only the first three characters of the representation for | |||
| 1108 | precision is what the local library functions of the @code{printf} | 1110 | precision is what the local library functions of the @code{printf} |
| 1109 | family produce. | 1111 | family produce. |
| 1110 | 1112 | ||
| 1113 | @cindex formatting numbers for rereading later | ||
| 1114 | If you plan to use @code{read} later on the formatted string to | ||
| 1115 | retrieve a copy of the formatted value, use a specification that lets | ||
| 1116 | @code{read} reconstruct the value. To format numbers in this | ||
| 1117 | reversible way you can use @samp{%s} and @samp{%S}, to format just | ||
| 1118 | integers you can also use @samp{%d}, and to format just nonnegative | ||
| 1119 | integers you can also use @samp{#x%x} and @samp{#o%o}. Other formats | ||
| 1120 | may be problematic; for example, @samp{%d} and @samp{%g} can mishandle | ||
| 1121 | NaNs and can lose precision and type, and @samp{#x%x} and @samp{#o%o} | ||
| 1122 | can mishandle negative integers. @xref{Input Functions}. | ||
| 1123 | |||
| 1111 | @node Case Conversion | 1124 | @node Case Conversion |
| 1112 | @section Case Conversion in Lisp | 1125 | @section Case Conversion in Lisp |
| 1113 | @cindex upper case | 1126 | @cindex upper case |