Re: [misc] [PATCH] Update UTF-8 base code

On Wed, 06 Dec 2017 at 10:28:57, Lars Henriksen wrote:
> On Wed, Dec 06, 2017 at 09:26:29AM +0100, Lukas Fleischer wrote:
> > On Wed, 29 Nov 2017 at 22:19:10, Lars Henriksen wrote:
> > >  int utf8_strwidth(char *s)
> > >  {
> > > -       int width = 0;
> > > -
> > > -       for (; s && *s; s++) {
> > > -               if (!UTF8_ISCONT(*s))
> > > -                       width += utf8_width(s);
> > > -       }
> > > -
> > > +       int width;
> > > +       for (width = 0; *s; width += utf8_width(s++))
> > > +               ;
> > 
> > I find this to be much less readable than the original code. Doing the
> > increment of both "width" and "s" in a single statement like this is
> > quite tricky.
> 
> It's straight out of K&R.
> 

That does not make it readable... I can amend the patch myself and merge
into pu, though.

> > I am also unsure about removing the
> > safeguard (NULL check) but I can see there are arguments for it :)
> 
> At least it should be outside the loop and with a recognizable error value:
>         if (!s)
>                 return -1;
>         int width = 0;

I will leave it as you initially suggested. Might be better not to hide
bugs by safeguards...

Thanks again!

Regards,
Lukas

Links