[DGD]Need tips with LPC function Im writing.

Frank Schmidt franks at colargol.tihlde.hist.no
Mon Mar 22 09:41:59 CET 1999



On Sun, 21 Mar 1999 sampsa at netsonic.fi wrote:

> 
> 
> On Mon, 22 Mar 1999, MUD Account wrote:
> 
> > Hi - I need some help with a function Im writing for
> > the mud Im working on based upon Melville.
> > My background is mostly C, so Im at a bit of a loss
> > as to how to do this in LPC.  Well I could do it, but
> > it would be VERY messy and probably not very fast.
> > Im basically looking for some tips on how to write
> > a find_replace(string str, string find, string replace_with)
> > type of function.  ie. since you dont have strstr() and similar,
> > whats the quickest way to find a string inside of a string?
> 
>  implode(explode(find+str+find,find),replace_with) should do the trick
> 

If you want more consistent results using explode() and implode():
(exploded strings consisting of only separators, or having them in the
beginning and at the end doesn't give a consistent "backwards" compatible
result with implode()..)

NOTE: THIS CODE IS AS-IS, as it is found in the MudOSa4DGD library.

/* big_explode(str, separator)
   This function works like explode(), except that it also counts
   separators at the start and end of the string. Basically that means
   that the array returned is more practical to parse. And big_implode()
   will return the original string if needed be.
*/
static string *big_explode(string str, string sep) {
  int stlen, seplen;
  string *expl;
  expl = ::explode(str, sep);
  if ((stlen=strlen(str)) > (seplen=strlen(sep)) && seplen > 0) {
    if (str[..seplen-1] == sep)
      /* matching separator found at beginning */
      expl = ({ "" }) + expl;
    if (str[stlen-seplen..] == sep && ::sizeof(expl - ({ "" })))
      /* matching separator found at end */
      expl += ({ "" });
  }
  /* got correct array now, return */
  return expl;
}


/* big_implode(str, separator)
   fully implodes an array of strings, regaining the string from
   big_explode(), unlike the relation between explode() and implode()
   of certain strings.
*/
static string big_implode(string *arr, string sep) {
  if (!::sizeof(arr - ({ "" }))) {
    /* special array consisting only of separators, add one extra */
    arr += ({ "" });
  }
  return ::implode(arr, sep);
}

Regards,

Frank Schmidt


List config page:  http://list.imaginary.com/mailman/listinfo/dgd



More information about the DGD mailing list