Re: Cursing code

From: Thomas Arp (t_arp@stofanet.dk)
Date: 05/15/03


From: "Templar Viper" <templarviper@hotmail.com>
> From: "Thomas Arp" <t_arp@stofanet.dk>
> > From: "Templar Viper" <templarviper@HOTMAIL.COM>
> > > Here is the function:
> > >
> > > int wordok(char *argument, bool call)
> > > {
<snip>
> > -   strlcpy(temp, argument, sizeof(temp));
> > /* this loop copies 'argument' into temp without *s */
> > +    for (; *p && p - argument < sizeof(temp);) {
> > +      if (*p == ' ' || *p == '\t')
> > +        continue;
> > +      *q++ = *p++;
> > +    }
> > +    *q = '\0';
> This is an infinite loop methinks. When called with a space, circle jumps
> to 100% cpu usage and doesn't repond anymore, and needs to be killed.
>
Yeah, of course. It was mailer code, too. the p++ should go in the
loop header instead of the body;
+    for (; *p && p - argument < sizeof(temp);p++) {
+      if (*p == ' ' || *p == '\t')
+        continue;
+      *q++ = *p;
+    }
+    *q = '\0';

> <Rest of function>
>
> > You didn't send a copy of search_replace(), so I'll not bother with
> > that one. This is implementation should run in O(M+(N*N*M)).
>
> Here is search_replace, I didn't write this myself, I found it somewhere
> on the developer site instead.

<snip of search_replace() calling str_strr 3 times>

void search_replace(char *txt, char *find, char *replace)
{
  int findlen, replacelen;
  char *p, *q;

  if (!txt || !*txt)
    return;

  if(NULL == (p = strcasestr(txt, find)))
    return;

  findlen = strlen(find);
  replacelen = strlen(replace);
  if ((strlen(txt) + replacelen - findlen + 1) > MAX_INPUT_LENGTH)
    return;
  memmove(q = p+replacelen, p+findlen, strlen(p+findlen)+1);
  memcpy(p, replace, replacelen);
  /*
   * search for more occurences
   * q is now pointing to the first char after the replace.
   */
  search_replace(q, find, replace);
}

This recursive function will replace all occurences of 'find' with
'replace'. And without using too many expensive strcat and strlen
ops.

Welcor

--
   +---------------------------------------------------------------+
   | FAQ: http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html |
   | Archives: http://post.queensu.ca/listserv/wwwarch/circle.html |
   | Newbie List:  http://groups.yahoo.com/group/circle-newbies/   |
   +---------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 06/26/03 PDT