Hi Bruno,

On 4/11/24 4:23 PM, Bruno Haible wrote:
> The bug is apparently that in argp-fmtstream.c, a character such as Ä or É
> augments fs->point_col by 2, when it should only augment it by 1.
> 
> In other words, it should use the Gnulib module 'mbswidth'.
> 
> Any volunteers?

I am very unfamiliar with these modules, so I would appreciate this
being double checked.

This patch fixes the two examples that you gave. I think it may be
missing the case where the buffer doesn't end in a partial line
though.

Here is the variable I add to adjust the columns:

       len = fs->p - buf;
+      visible_len = mbsnwidth (buf, len, 0);
       nl = memchr (buf, '\n', len);

After checking that nl != NULL, would I need to do:

      visible_len = mbsnwidth (buf, nl - buf, 0);

I have a feeling it is necessary, but I don't have a test case to make
sure I don't break things.

Collin
From 0f18bb3f38e4189a4f164d716fe411f7a3a868cf Mon Sep 17 00:00:00 2001
From: Collin Funk <collin.fu...@gmail.com>
Date: Fri, 12 Apr 2024 08:55:48 -0700
Subject: [PATCH] argp: Fix alignment of option doc strings.

Reported by Lasse Collin in
<https://lists.gnu.org/archive/html/bug-gnulib/2024-04/msg00179.html>
* lib/argp-fmtstream.c (__argp_fmtstream_update): Adjust the column of
output based on character width instead of number of bytes.
* modules/argp: Add mbswidth to module dependencies.
---
 ChangeLog            | 9 +++++++++
 lib/argp-fmtstream.c | 7 +++++--
 modules/argp         | 1 +
 3 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index b20f69b06b..c307284fcd 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2024-04-12  Collin Funk  <collin.fu...@gmail.com>
+
+	argp: Fix alignment of option doc strings.
+	Reported by Lasse Collin in
+	<https://lists.gnu.org/archive/html/bug-gnulib/2024-04/msg00179.html>
+	* lib/argp-fmtstream.c (__argp_fmtstream_update): Adjust the column of
+	output based on character width instead of number of bytes.
+	* modules/argp: Add mbswidth to module dependencies.
+
 2024-04-12  Simon Josefsson  <si...@josefsson.org>
 
 	gitlog-to-changelog: Make output reproducible.
diff --git a/lib/argp-fmtstream.c b/lib/argp-fmtstream.c
index 7f75879d69..b1042529b4 100644
--- a/lib/argp-fmtstream.c
+++ b/lib/argp-fmtstream.c
@@ -31,6 +31,7 @@
 
 #include "argp-fmtstream.h"
 #include "argp-namefrob.h"
+#include "mbswidth.h"
 
 #ifndef ARGP_FMTSTREAM_USE_LINEWRAP
 
@@ -122,6 +123,7 @@ __argp_fmtstream_update (argp_fmtstream_t fs)
 {
   char *buf, *nl;
   size_t len;
+  int visible_len;
 
   /* Scan the buffer for newlines.  */
   buf = fs->buf + fs->point_offs;
@@ -160,6 +162,7 @@ __argp_fmtstream_update (argp_fmtstream_t fs)
         }
 
       len = fs->p - buf;
+      visible_len = mbsnwidth (buf, len, 0);
       nl = memchr (buf, '\n', len);
 
       if (fs->point_col < 0)
@@ -169,12 +172,12 @@ __argp_fmtstream_update (argp_fmtstream_t fs)
         {
           /* The buffer ends in a partial line.  */
 
-          if (fs->point_col + len < fs->rmargin)
+          if (fs->point_col + visible_len < fs->rmargin)
             {
               /* The remaining buffer text is a partial line and fits
                  within the maximum line width.  Advance point for the
                  characters to be written and stop scanning.  */
-              fs->point_col += len;
+              fs->point_col += visible_len;
               break;
             }
           else
diff --git a/modules/argp b/modules/argp
index 88cc78c3cb..2605bccb89 100644
--- a/modules/argp
+++ b/modules/argp
@@ -37,6 +37,7 @@ stdio
 strerror
 memchr
 memmove
+mbswidth
 
 configure.ac:
 gl_ARGP
-- 
2.44.0

Reply via email to