unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Paul Eggert <eggert@cs.ucla.edu>
To: Gustaf Waldemarson <gustaf.waldemarson@gmail.com>
Cc: 29609-done@debbugs.gnu.org
Subject: bug#29609: 27.0.50; Format number as hexadecimal with hash-flag
Date: Thu, 7 Dec 2017 18:14:13 -0800	[thread overview]
Message-ID: <a41d6252-6fea-b5d5-8b7a-c3266ab3dd34@cs.ucla.edu> (raw)
In-Reply-To: <CABehr5eccmeqqEgEQ8EcBBnqDahj4eUAx2fxcwE4dcLfv_e=ug@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 79 bytes --]

Thanks for reporting that. I installed the attached, which should fix the bug.

[-- Attachment #2: 0001-Fix-zero-padding-bug-with-format-08x-n.patch --]
[-- Type: text/x-patch, Size: 2700 bytes --]

From 47423f0603f2ecfb78352be5477fb02c44f1fd35 Mon Sep 17 00:00:00 2001
From: Paul Eggert <eggert@cs.ucla.edu>
Date: Thu, 7 Dec 2017 18:07:54 -0800
Subject: [PATCH] Fix zero-padding bug with (format "%#08x" n)

Problem reported by Gustaf Waldemarson (Bug#29609).
* src/editfns.c (styled_format):
Put zero padding after a leading "0x", not before.
* test/src/editfns-tests.el (format-sharp-0-x): New test.
---
 src/editfns.c             | 24 ++++++++++++++++--------
 test/src/editfns-tests.el |  6 ++++++
 2 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/src/editfns.c b/src/editfns.c
index e671ba0..ebf6518 100644
--- a/src/editfns.c
+++ b/src/editfns.c
@@ -4722,11 +4722,19 @@ styled_format (ptrdiff_t nargs, Lisp_Object *args, bool message)
 		  char src0 = src[0];
 		  int exponent_bytes = 0;
 		  bool signedp = src0 == '-' || src0 == '+' || src0 == ' ';
-		  unsigned char after_sign = src[signedp];
-		  if (zero_flag && 0 <= char_hexdigit (after_sign))
+		  int prefix_bytes = (signedp
+				      + ((src[signedp] == '0'
+					  && (src[signedp + 1] == 'x'
+					      || src[signedp + 1] == 'X'))
+					 ? 2 : 0));
+		  if (zero_flag)
 		    {
-		      leading_zeros += padding;
-		      padding = 0;
+		      unsigned char after_prefix = src[prefix_bytes];
+		      if (0 <= char_hexdigit (after_prefix))
+			{
+			  leading_zeros += padding;
+			  padding = 0;
+			}
 		    }
 
 		  if (excess_precision
@@ -4745,13 +4753,13 @@ styled_format (ptrdiff_t nargs, Lisp_Object *args, bool message)
 		      nchars += padding;
 		    }
 
-		  *p = src0;
-		  src += signedp;
-		  p += signedp;
+		  memcpy (p, src, prefix_bytes);
+		  p += prefix_bytes;
+		  src += prefix_bytes;
 		  memset (p, '0', leading_zeros);
 		  p += leading_zeros;
 		  int significand_bytes
-		    = sprintf_bytes - signedp - exponent_bytes;
+		    = sprintf_bytes - prefix_bytes - exponent_bytes;
 		  memcpy (p, src, significand_bytes);
                   p += significand_bytes;
 		  src += significand_bytes;
diff --git a/test/src/editfns-tests.el b/test/src/editfns-tests.el
index 70dc937..283a642 100644
--- a/test/src/editfns-tests.el
+++ b/test/src/editfns-tests.el
@@ -136,6 +136,12 @@ transpose-test-get-byte-positions
 (ert-deftest format-c-float ()
   (should-error (format "%c" 0.5)))
 
+;;; Test for Bug#29609.
+(ert-deftest format-sharp-0-x ()
+  (should (string-equal (format "%#08x" #x10) "0x000010"))
+  (should (string-equal (format "%#05X" #x10) "0X010"))
+  (should (string-equal (format "%#04x" 0) "0000")))
+
 ;;; Check format-time-string with various TZ settings.
 ;;; Use only POSIX-compatible TZ values, since the tests should work
 ;;; even if tzdb is not in use.
-- 
2.7.4


  reply	other threads:[~2017-12-08  2:14 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-12-07 21:02 bug#29609: 27.0.50; Format number as hexadecimal with hash-flag Gustaf Waldemarson
2017-12-08  2:14 ` Paul Eggert [this message]
2017-12-08 17:51   ` Glenn Morris
2017-12-08 21:31     ` Paul Eggert

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a41d6252-6fea-b5d5-8b7a-c3266ab3dd34@cs.ucla.edu \
    --to=eggert@cs.ucla.edu \
    --cc=29609-done@debbugs.gnu.org \
    --cc=gustaf.waldemarson@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).