home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume8 / hint / part01 / str.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-24  |  2.2 KB  |  78 lines

  1. /* str.c -- string functions missing from BSD
  2.    Copyright (C) 1989 David MacKenzie
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
  17.  
  18. #define NULL 0
  19.  
  20. char *index ();
  21.  
  22. /* Return the length of the span of characters at the start of `string'
  23.    that are members of `class'.  */
  24.  
  25. int
  26. strspn (string, class)
  27.      char *string;
  28.      char *class;
  29. {
  30.   int count;
  31.  
  32.   for (count = 0; string[count]; ++count)
  33.     if (!index (class, string[count]))
  34.       break;
  35.   return count;
  36. }
  37.  
  38. /* Return the length of the span of characters at the start of `string'
  39.    that are non-members of `class'.  */
  40.  
  41. int
  42. strcspn (string, class)
  43.      char *string;
  44.      char *class;
  45. {
  46.   int count;
  47.  
  48.   for (count = 0; string[count]; ++count)
  49.     if (index (class, string[count]))
  50.       break;
  51.   return count;
  52. }
  53.  
  54. /* Return the next token in `string', delimited by one or more members of
  55.    the set `separators'.  If `string' is NULL, use the same string as in the
  56.    last call.  */
  57.  
  58. char *
  59. strtok (string, separators)
  60.      char *string;
  61.      char *separators;
  62. {
  63.   static char *pos = NULL;    /* Current location in the string. */
  64.   int token_length;
  65.  
  66.   if (string)
  67.     pos = string;
  68.   pos += strspn (pos, separators);    /* Skip initial separators. */
  69.   token_length = strcspn (pos, separators);    /* Find token length. */
  70.   if (token_length == 0)
  71.     return NULL;        /* No more tokens; pos is on a 0. */
  72.   separators = pos;        /* Re-use separators to save start of token. */
  73.   pos += token_length;        /* Move onto the 0. */
  74.   if (*pos)            /* If not the last token, */
  75.     *pos++ = 0;            /* null terminate the token. */
  76.   return separators;
  77. }
  78.