useful string utility

From: Andrew Dorrell <andrewd_at_eng.uts.edu.au>
Date: Fri, 19 Sep 1997 10:10:06 +1000 (EST)

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... I had
earlier tried to do the same thing in scheme - with much frustration:

-----------------------------------------------------------------------

#include <string.h>
#include <stk.h>

PRIMITIVE
substrings_to_list(SCM string, SCM delimiters)
{
  SCM result;
  char *substring, *c_string, *c_delimiters;
  char c_default_delimiters[3];

  sprintf(c_default_delimiters, " \t");

  if(!STRINGP(string))
    STk_err("substrings->list: bad string", string);
  c_string = CHARS(string);

  if(!(delimiters == UNBOUND || STRINGP(delimiters)))
    STk_err("substrings->list: bad delimiter string", delimiters);
  c_delimiters = (delimiters == UNBOUND ? c_default_delimiters :
CHARS(delimiter
s));
  
  substring = strtok(c_string, c_delimiters);
  result = (substring==NULL ? NIL : LIST1(STk_makestring(substring)));

  while((substring=strtok(NULL, c_delimiters)) != NULL)
    result = STk_cons(STk_makestring(substring), result);
  
  return(STk_reverse(result));
}

void
STk_init_substrings(void)
{
    STk_add_new_primitive("substrings->list",
                          tc_subr_1_or_2,
                          (PRIMITIVE(*)())substrings_to_list);
}

-- 
------------------------------------------------------------------------------
Mr Andrew Dorrell
School of Electrical Engineering                 *
University of Technology, Sydney                     *
PO Box 123                                     *
Broadway NSW 2007                                   .  
AUSTRALIA
                                                    *       /---\  Whoo?
Phone:   61 2 330 2395                                      (o o) /
Fax:     61 2 330 2435                                      ( : )  
email:   andrewd_at_ee.uts.edu.au                               ^ ^     
    OR   dorrell_at_ihf.uts.edu.au
------------------------------------------------------------------------------
Received on Fri Sep 19 1997 - 02:07:45 CEST

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