Re: useful string utility

From: Erick Gallesio <eg_at_kaolin.unice.fr>
Date: Fri, 19 Sep 1997 22:23:16 +0200

Andrew Dorrell writes:
> Hi,
>
> Had cause to do some string processing recently and wondered whether
> others might find the following function useful. It is a scheme wrapper
> for the C stktok functionality:
>
> (substrings->list <string> . <delimiter-string)
>
> where the delimiter list defaults to the whitespace characters...

That's general purpose enough to be in the core release and I have already
integrated it in my source tree. However I have implemented it without strtok,
because strtok patches the original string by placing null characters into
it. Here is my current version of this function (note that I have also
changed its name to split-string, but I had to see if other implementations
agree on some other name).

PRIMITIVE STk_split_string(SCM string, SCM delimiters)
{
  SCM result = NIL;
  char *substring, *c_string, *c_delimiters, *s;
  
  if (!STRINGP(string)) STk_err("split-string: bad string", string);
  c_string = CHARS(string);

  if (delimiters == UNBOUND)
    c_delimiters = " \t\n";
  else {
    if (!STRINGP(delimiters))
      STk_err("split-string: bad delimiter string", delimiters);
    c_delimiters = CHARS(delimiters);
  }

  for (s = c_string; *s; s++) {
    if (strchr(c_delimiters, *s)) {
      if (s > c_string) {
        int len;
        SCM tmp;
        
        len = s - c_string;
        tmp = STk_makestrg(len, NULL);
        
        strncpy(CHARS(tmp), c_string, len);
        CHARS(tmp)[len+1] = '\0';
        result = Cons(tmp, result);
      }
      c_string = s + 1;
    }
  }
  if (s > c_string)
    result = Cons(STk_makestring(c_string), result);

  return STk_reverse(result);
}


> I had
> earlier tried to do the same thing in scheme - with much frustration:

Scheme is a wonderful language, but I agree with you that
character manipulation in it is very painful.



                -- Erick
Received on Fri Sep 19 1997 - 22:22:39 CEST

This archive was generated by hypermail 2.3.0 : Mon Jul 21 2014 - 19:38:59 CEST