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

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.

> 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;

Lars Henriksen

Links