bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [3.0] UTF-8 and ${#var} or ${var: -1}


From: Tim Waugh
Subject: Re: [3.0] UTF-8 and ${#var} or ${var: -1}
Date: Thu, 2 Sep 2004 14:43:15 +0100
User-agent: Mutt/1.4.1i

On Thu, Sep 02, 2004 at 02:19:57PM +0100, Stephane Chazelas wrote:

> On Thu, Sep 02, 2004 at 01:25:00PM +0100, Tim Waugh wrote:
> [...]
> > +#define MBSLEN(s,n) (((s) && (s)[0]) ? ((s)[1] ? ((s)[2] ?
> > mbstowcs(NULL,s,n) : 2) : 1) : 0)
> [...]
> 
> That doesn't work for strings less that 3 bytes long because of
> the optimisation above that can't be applied to multibyte:

Oops, I did catch that but obviously forgot to re-diff after making
the change. :-/

The optimization for length 1 strings still works I think.

Tim.
*/

--- bash-3.0/subst.c.mbslen     2004-09-02 12:55:32.035565602 +0100
+++ bash-3.0/subst.c    2004-09-02 12:58:27.477110497 +0100
@@ -4857,7 +4857,11 @@
          t = itos (number_of_args ());
          break;
        }
+#if defined (HANDLE_MULTIBYTE)
+      number = MBSLEN (t, STRLEN (t));
+#else
       number = STRLEN (t);
+#endif
       FREE (t);
     }
 #if defined (ARRAY_VARS)
@@ -4871,14 +4875,22 @@
       if (legal_number (name + 1, &arg_index))         /* ${#1} */
        {
          t = get_dollar_var_value (arg_index);
+#if defined (HANDLE_MULTIBYTE)
+         number = MBSLEN (t, STRLEN (t));
+#else
          number = STRLEN (t);
+#endif
          FREE (t);
        }
 #if defined (ARRAY_VARS)
       else if ((var = find_variable (name + 1)) && array_p (var))
        {
          t = array_reference (array_cell (var), 0);
+#if defined (HANDLE_MULTIBYTE)
+         number = MBSLEN (t, STRLEN (t));
+#else
          number = STRLEN (t);
+#endif
        }
 #endif
       else                             /* ${#PS1} */
@@ -4891,7 +4903,11 @@
          if (list)
            dispose_words (list);
 
+#if defined (HANDLE_MULTIBYTE)
+         number = MBSLEN (t, STRLEN (t));
+#else
          number = STRLEN (t);
+#endif
          FREE (t);
        }
     }
--- bash-3.0/general.h.mbslen   2004-09-02 12:53:23.832251022 +0100
+++ bash-3.0/general.h  2004-09-02 12:55:17.811194851 +0100
@@ -149,6 +149,7 @@
 
 /* More convenience definitions that possibly save system or libc calls. */
 #define STRLEN(s) (((s) && (s)[0]) ? ((s)[1] ? ((s)[2] ? strlen(s) : 2) : 1) : 
0)
+#define MBSLEN(s,n) (((s) && (s)[0]) ? ((s)[1] ? mbstowcs(NULL,s,n) : 1) : 0)
 #define FREE(s)  do { if (s) free (s); } while (0)
 #define MEMBER(c, s) (((c) && c == (s)[0] && !(s)[1]) || (member(c, s)))
 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]