On Sat, Jan 30, 2021 at 11:52:15AM +0100, Hiltjo Posthuma wrote:
> On Sat, Jan 30, 2021 at 12:22:04AM +0100, Christian Weisgerber wrote:
> > Hiltjo Posthuma:
> > 
> > > > @@ -75,19 +74,8 @@ cookie_load(void)
> > > >         if (fp == NULL)
> > > >                 err(1, "cannot open cookie file %s", cookiefile);
> > > >         date = time(NULL);
> > > > -       lbuf = NULL;
> > > > -       while ((line = fgetln(fp, &len)) != NULL) {
> > > > -               if (line[len - 1] == '\n') {
> > > > -                       line[len - 1] = '\0';
> > > > -                       --len;
> > > > -               } else {
> > > > -                       if ((lbuf = malloc(len + 1)) == NULL)
> > > > -                               err(1, NULL);
> > > > -                       memcpy(lbuf, line, len);
> > > > -                       lbuf[len] = '\0';
> > > > -                       line = lbuf;
> > > > -               }
> > > > -               line[strcspn(line, "\r")] = '\0';
> > > > +       while (getline(&line, &linesize, fp) != -1) {
> > > > +               line[strcspn(line, "\r\n")] = '\0';
> > > >  
> > > 
> > > getline returns the number of characters read including the delimeter. 
> > > This
> > > size could be used to '\0' terminate the string instead of a strcspn() 
> > > call.
> > 
> > A strcspn() call is already there.
> > 
> > -- 
> > Christian "naddy" Weisgerber                          [email protected]
> 
> Yes, my point is it scans the entire line again for delimeter, but it is not
> needed, because it is already known after getline() and returned.

But the strcspn will stop at the first \r or \n char while getline() only
checks for \n. So there is still a need to remove \r from the string.
I think the strcspn() is a very nice solution here and I doubt scanning
the line again will cause a performance issue.

-- 
:wq Claudio

Reply via email to