As already reported several years ago argp counts bytes even when
actually what matters is the display length. This patch improves the
situation by counting only leading and standalone UTF-8 bytes. It
doesn't handle the double-width characters like Chinese sinograms

-- 
Regards
Vladimir 'φ-coder/phcoder' Serbinenko

=== modified file 'grub-core/gnulib/argp-fmtstream.c'
--- grub-core/gnulib/argp-fmtstream.c	2010-04-02 22:45:01 +0000
+++ grub-core/gnulib/argp-fmtstream.c	2012-02-12 15:45:43 +0000
@@ -116,6 +116,29 @@
 #endif
 #endif
 
+
+size_t
+__argp_get_display_len (char *beg, char *end)
+{
+  char *ptr;
+  size_t r = 0;
+  for (ptr = beg; ptr < end; ptr++)
+    if ((*(unsigned char *)ptr & 0x80) == 0
+	|| (*(unsigned char *)ptr & 0xc0) == 0xc0)
+      r++;
+  return r;
+}
+
+static inline char *
+add_length (char *ptr, size_t l)
+{
+  for(; l && *ptr; ptr++)
+    if ((*(unsigned char *)ptr & 0x80) == 0
+	|| (*(unsigned char *)ptr & 0xc0) == 0xc0)
+      l--;
+  return ptr;
+}
+
 /* Process FS's buffer so that line wrapping is done from POINT_OFFS to the
    end of its buffer.  This code is mostly from glibc stdio/linewrap.c.  */
 void
@@ -168,14 +191,15 @@
 
       if (!nl)
         {
+	  size_t display_len = __argp_get_display_len (buf, fs->p);
           /* The buffer ends in a partial line.  */
 
-          if (fs->point_col + len < fs->rmargin)
+          if (fs->point_col + display_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 += display_len;
               break;
             }
           else
@@ -183,14 +207,18 @@
                the end of the buffer.  */
             nl = fs->p;
         }
-      else if (fs->point_col + (nl - buf) < (ssize_t) fs->rmargin)
-        {
-          /* The buffer contains a full line that fits within the maximum
-             line width.  Reset point and scan the next line.  */
-          fs->point_col = 0;
-          buf = nl + 1;
-          continue;
-        }
+      else
+	{
+	  size_t display_len = __argp_get_display_len (buf, nl);
+	  if (display_len < (ssize_t) fs->rmargin)
+	    {
+	      /* The buffer contains a full line that fits within the maximum
+		 line width.  Reset point and scan the next line.  */
+	      fs->point_col = 0;
+	      buf = nl + 1;
+	      continue;
+	    }
+	}
 
       /* This line is too long.  */
       r = fs->rmargin - 1;
@@ -226,7 +254,7 @@
           char *p, *nextline;
           int i;
 
-          p = buf + (r + 1 - fs->point_col);
+	  p = add_length (buf, (r + 1 - fs->point_col));
           while (p >= buf && !isblank ((unsigned char) *p))
             --p;
           nextline = p + 1;     /* This will begin the next line.  */
@@ -244,7 +272,7 @@
             {
               /* A single word that is greater than the maximum line width.
                  Oh well.  Put it on an overlong line by itself.  */
-              p = buf + (r + 1 - fs->point_col);
+              p = add_length (buf, (r + 1 - fs->point_col));
               /* Find the end of the long word.  */
               if (p < nl)
                 do
@@ -278,7 +306,7 @@
               && fs->p > nextline)
             {
               /* The margin needs more blanks than we removed.  */
-              if (fs->end - fs->p > fs->wmargin + 1)
+              if (__argp_get_display_len (fs->p, fs->end) > fs->wmargin + 1)
                 /* Make some space for them.  */
                 {
                   size_t mv = fs->p - nextline;

=== modified file 'grub-core/gnulib/argp-fmtstream.h'
--- grub-core/gnulib/argp-fmtstream.h	2010-04-02 22:45:01 +0000
+++ grub-core/gnulib/argp-fmtstream.h	2012-02-12 15:44:50 +0000
@@ -335,6 +335,9 @@
   return __fs->point_col >= 0 ? __fs->point_col : 0;
 }
 
+size_t
+__argp_get_display_len (char *beg, char *end);
+
 #if !_LIBC
 #undef __argp_fmtstream_putc
 #undef __argp_fmtstream_puts

=== modified file 'grub-core/gnulib/argp-help.c'
--- grub-core/gnulib/argp-help.c	2010-04-02 22:45:01 +0000
+++ grub-core/gnulib/argp-help.c	2012-02-12 15:45:07 +0000
@@ -1448,7 +1448,7 @@
 
       /* Manually do line wrapping so that it (probably) won't get wrapped at
          any embedded spaces.  */
-      space (stream, 1 + nl - cp);
+      space (stream, 1 + __argp_get_display_len (cp, nl));
 
       __argp_fmtstream_write (stream, cp, nl - cp);
     }

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to