From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Juri Linkov Newsgroups: gmane.emacs.bugs Subject: bug#44155: Print integers as characters Date: Sun, 25 Oct 2020 21:09:07 +0200 Organization: LINKOV.NET Message-ID: <87wnzenmwc.fsf@mail.linkov.net> References: <877drh7unm.fsf@igel.home> <87lffxuwgn.fsf@mail.linkov.net> <87d0172yev.fsf@mail.linkov.net> <83mu0afcex.fsf@gnu.org> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="31417"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (x86_64-pc-linux-gnu) Cc: 44155@debbugs.gnu.org, schwab@linux-m68k.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Oct 25 20:14:12 2020 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kWlT1-00084v-EC for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 25 Oct 2020 20:14:11 +0100 Original-Received: from localhost ([::1]:38320 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kWlT0-00031J-Fm for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 25 Oct 2020 15:14:10 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:58972) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kWlSs-00030t-AC for bug-gnu-emacs@gnu.org; Sun, 25 Oct 2020 15:14:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:54391) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kWlSs-00045c-0r for bug-gnu-emacs@gnu.org; Sun, 25 Oct 2020 15:14:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1kWlSr-0004Ck-SF for bug-gnu-emacs@gnu.org; Sun, 25 Oct 2020 15:14:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Juri Linkov Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 25 Oct 2020 19:14:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 44155 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 44155-submit@debbugs.gnu.org id=B44155.160365319116094 (code B ref 44155); Sun, 25 Oct 2020 19:14:01 +0000 Original-Received: (at 44155) by debbugs.gnu.org; 25 Oct 2020 19:13:11 +0000 Original-Received: from localhost ([127.0.0.1]:37704 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1kWlS3-0004BW-BL for submit@debbugs.gnu.org; Sun, 25 Oct 2020 15:13:11 -0400 Original-Received: from relay6-d.mail.gandi.net ([217.70.183.198]:53923) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1kWlRz-0004B3-Bw for 44155@debbugs.gnu.org; Sun, 25 Oct 2020 15:13:07 -0400 X-Originating-IP: 91.129.102.160 Original-Received: from mail.gandi.net (m91-129-102-160.cust.tele2.ee [91.129.102.160]) (Authenticated sender: juri@linkov.net) by relay6-d.mail.gandi.net (Postfix) with ESMTPSA id 3E79DC0002; Sun, 25 Oct 2020 19:12:59 +0000 (UTC) In-Reply-To: <83mu0afcex.fsf@gnu.org> (Eli Zaretskii's message of "Sun, 25 Oct 2020 19:22:46 +0200") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:191531 Archived-At: --=-=-= Content-Type: text/plain >> + if (EQ (Vinteger_output_format, Qt) && CHARACTERP (obj) && c < 4194176) > ^^^^^^^ > > Please use MAX_5_BYTE_CHAR here. Or, better yet, CHAR_BYTE8_P. Thanks, fixed. > And, btw, why not allow raw bytes here as well? is there some problem? Because of ambiguity, both these return the same value: (read (concat "?" (string 128))) => 128 (read (concat "?" (string 4194176))) => 128 >> + print_string (Fchar_to_string (obj), printcharfun); > > Why are you using print_string here instead of printchar? IOW, what > is the difference between printing a backslash and printing any other > character, that you can use printchar for the former, but not for the > latter? It was needed in earlier versions, but not now; fixed. >> + else if (INTEGERP (Vinteger_output_format) >> + && XFIXNUM (Vinteger_output_format) == 16 && c >= 0) > > If you really want to allow Vinteger_output_format to be a bignum, you > cannot use XFIXNUM with it, you need to use integer_to_intmax or > somesuch. Otherwise, you should use FIXNUMP instead of INTEGERP. Fixed. >> + DEFVAR_LISP ("integer-output-format", Vinteger_output_format, >> + doc: /* The format used to print integers. >> +When 't', print integers as characters. > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ > But only integers that are small enough, yes? Fixed the docstring as well. >> +When a number 16, print numbers in hex format. > > This immediately begs the question: why cannot the value be 8 or 2? Because octal and binary are not so widely used as hex. But variable makes room for further improvements to later support octal and binary too, and maybe string formats like in float-output-format. > P.S. This will eventually need a NEWS entry. And also updates in the Info manual will be in the final version of the patch. --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=integer-output-format-2.patch diff --git a/src/print.c b/src/print.c index 53aa353769..b04d5023f8 100644 --- a/src/print.c +++ b/src/print.c @@ -1908,8 +1908,30 @@ print_object (Lisp_Object obj, Lisp_Object printcharfun, bool escapeflag) { case_Lisp_Int: { - int len = sprintf (buf, "%"pI"d", XFIXNUM (obj)); - strout (buf, len, len, printcharfun); + int c = XFIXNUM (obj); + intmax_t i; + + if (EQ (Vinteger_output_format, Qt) && CHARACTERP (obj) && ! CHAR_BYTE8_P (c)) + { + printchar ('?', printcharfun); + if (escapeflag + && (c == ';' || c == '(' || c == ')' || c == '{' || c == '}' + || c == '[' || c == ']' || c == '\"' || c == '\'' || c == '\\')) + printchar ('\\', printcharfun); + printchar (c, printcharfun); + } + else if (INTEGERP (Vinteger_output_format) + && integer_to_intmax (Vinteger_output_format, &i) + && i == 16 && XFIXNUM (obj) >= 0) + { + int len = sprintf (buf, "#x%"pI"x", (EMACS_UINT) XFIXNUM (obj)); + strout (buf, len, len, printcharfun); + } + else + { + int len = sprintf (buf, "%"pI"d", XFIXNUM (obj)); + strout (buf, len, len, printcharfun); + } } break; @@ -2247,6 +2269,13 @@ syms_of_print (void) that represents the number without losing information. */); Vfloat_output_format = Qnil; + DEFVAR_LISP ("integer-output-format", Vinteger_output_format, + doc: /* The format used to print integers. +When 't', print characters from integers that represent characters. +When a number 16, print non-negative numbers in hex format. +Otherwise, print integers in decimal format. */); + Vinteger_output_format = Qnil; + DEFVAR_LISP ("print-length", Vprint_length, doc: /* Maximum length of list to print before abbreviating. A value of nil means no limit. See also `eval-expression-print-length'. */); --=-=-=--