bug-gnu-emacs
[Top][All Lists]
Advanced

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

bug#50752: 28.0.50; easy-menu-define lowers the menu-bar key


From: Eli Zaretskii
Subject: bug#50752: 28.0.50; easy-menu-define lowers the menu-bar key
Date: Fri, 15 Oct 2021 08:59:22 +0300

> Date: Wed, 13 Oct 2021 14:59:03 +0300
> From: Eli Zaretskii <eliz@gnu.org>
> Cc: shuguang79@qq.com, larsi@gnus.org, 50752@debbugs.gnu.org
> 
> > +     USE_SAFE_ALLOCA;
> > +     ptrdiff_t size = SCHARS (lc_key), n;
> > +     if (INT_MULTIPLY_WRAPV (size, MAX_MULTIBYTE_LENGTH, &n))
> > +       n = PTRDIFF_MAX;
> > +     unsigned char *dst = SAFE_ALLOCA (n);
> > +     unsigned char *o = dst;
> > +     ptrdiff_t j = 0, j_byte = 0, chars = 0;
> > +
> > +     while (j < SCHARS (lc_key))
> > +       {
> > +         int ch = fetch_string_char_advance (lc_key, &j, &j_byte);
> > +         if (ch == ' ')
> > +           *o = '-';
> > +         else
> > +           *o = ch;
> > +         chars++;
> 
> This will only work with plain-ASCII characters in lc_key (but then
> you don't need fetch_string_char_advance, you can access the bytes one
> by one).  You need to use CHAR_STRING instead.

Thinking more about this, you don't need all these complications with
fetch_string_char_advance and CHAR_STRING.  Since all you need is
replace ' ' with '-', you can walk the string data byte by byte,
because UTF-8 encoding makes sure no other byte of any multibyte
sequence will ever include a 7-bit byte equal to an ASCII single-byte
character.  So just checking the bytes for equality to ' ' is enough.
Thus, you could make a copy of the symbol's name, then walk that copy
byte by byte looking for space characters and replacing them.

Moreover, you could check up front, using 'strstr', whether the
symbol's name includes any space characters, and if not, short-circuit
the entire second attempt.

These two measures should make the code faster and easier to program
and understand.





reply via email to

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