Re: more string functions (was Re: str_str()) 12:07:26 -0600"

From: d. hall (dhall@APK.NET)
Date: 01/22/98


// thus on Tue, 20 Jan 1998 12:07:26 -0600, Akuma/Chris virtually wrote:

> I finally got around to trying your version of the str_str() and I must
> say it worked fine, but it wasn't case insensitive :-P

> I fixed it by changing one line down there.
> the   'if (*h == *n)'  should be   'if (LOWER(*h) == LOWER(*n))'

Uhm, yeah... =)  Well I could always fall back on the crutch/excuse that I
was using vi instead of emacs at the time...

I did forget to check the start of the string for case-insensativity, nice
catch.  If the starts don't match, it would never start the internal loop
to test the start of the substring.

> Thanks d.

Here are a couple more semi-useful utility functions, I've cut them out of
my C library.  Since most of mud programming is string handling, hope
someone finds them useful.

/* derived from xstrcat from comp.sources.unix */

/* multiple argument strcat, requires NULL as last argument */
char *istrcat (char *d, const char *s, ...)
{
   register char *p;
   va_list va;

   for (p = d; *p; p++);        /* traverse dest with pointer */

   va_start (va, s);
   while (s != NULL) {
      while (*s != '\0')
         *p++ = *s++;
      s = va_arg (va, const char *);
   }
   va_end (va);

   *p = '\0';                   /* make sure to null terminate */
   return d;
}

/*
 * strcat that returns end of concatented string, useful for
 * linking several strings together, saves resources by not
 * traversing the start of the string over and over
 */
char *istrlink (char *d, const char *s)
{
   while (*d)                   /* traverse the first string */
      d++;
   while (*s)                   /* append second string */
      *d++ = *s++;
   *d = '\0';                   /* make sure to null terminate */
   return d;                    /* return current position */
}

/* reverse a string in place */
char *istrrev (char *s)
{
   char *l, *r;
   char c;

   for (r = s; *r; r++);        /* find the right side */
   for (l = s; l < r; l++) {    /* come from the left side */
      c = *l;                   /* swap via the c variable */
      *l = *--r;
      *r = c;
   }
   return s;
}

/* function will center a string within a specified width */
char *istrcen (char *s, size_t w)
{
   char *l, *r, *p;
   size_t len;

   for (l = s; isspace (*l); l++);      /* delete preceding whitespace */
   for (p = l; *p; p++);                /* goto end of string */
   for (r = (p - 1); isspace (*r); r--) /* delete trailing whitespace */
      *r = '\0';

   len = r - l;

   if (len >= w)
      memmove (s, l, len + 1);
   else {
      memmove (s + (w - len) / 2, l, len + 1);
      memset (s, ' ', (w - len) / 2);
   }
   return s;
}


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/15/00 PST