home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff218.lzh / EdLib / barrett.doc < prev    next >
Text File  |  1989-06-04  |  4KB  |  109 lines

  1. ****************************************************************************
  2. * STRING FUNCTIONS by Daniel J. Barrett.                                   *
  3. *               barrett@cs.jhu.edu, ins_adjb@jhunix.UUCP             *
  4. *                                                                          *
  5. * THESE ROUTINES ARE IN THE PUBLIC DOMAIN.                                 *
  6. ****************************************************************************
  7.  
  8.     Manx `C' provides most of the standard "UNIX" string functions, such
  9. as strcat(), strcmp(), and so on.
  10.  
  11.     Four of the functions that Manx does NOT provide are strspn(), 
  12. strcspn(), strpbrk(), and strtok().  Here are my versions of these 4 missing 
  13. functions, written from scratch.  I wrote several versions, and these were 
  14. the fastest.  (Undoubtedly, assembler would be faster, but I don't know
  15. assembler.  Feel free to "one-up" me.  :-))
  16.  
  17.     Note that some of these functions call built-in Manx functions like
  18. index() and strchr().  These versions are FASTER than when I did everything 
  19. "by hand".
  20.  
  21.     I don't own Lattice `C', but I suppose these routines will work
  22. if you have the functions index() and strchr()... they would be easy to
  23. write, anyway.
  24.  
  25. Here are brief descriptions of the 4 functions.
  26.  
  27. int strspn(string, character_set)
  28. char *string, *character_set;
  29.  
  30.     Starting from the beginning of string "string", count how
  31.     many of its characters are found in "character_set".  When
  32.     you hit the first character NOT in "character_set", RETURN
  33.     the number of characters found so far.  If either argument
  34.     is NULL, strspn() returns 0.
  35.  
  36.     strspn("abcdefg", "abd")    returns 2.
  37.     strspn("abcdefg", "xyyz")    returns 0.
  38.     strspn("abcdefg", "dbc")    returns 0.
  39.     strspn("abcdefg", "dxbgac")    returns 4.
  40.  
  41. int strcspn(string, character_set)
  42. char *string, *character_set;
  43.  
  44.     This function is exactly the opposite of strspn().  Return the
  45.     number of characters, starting from the beginning of the string,
  46.     that are NOT found in "character_set".  Keep counting until you
  47.     hit the first character in "string" that IS found in "character_set";
  48.     then RETURN.  If either argument is NULL, strcspn() returns 0.
  49.  
  50.     strcspn("abcdefg", "abd")    returns 0.
  51.     strcspn("abcdefg", "xyyz")    returns 7.
  52.     strspn("abcdefg", "dbc")    returns 1.
  53.     strspn("abcdefg", "dxbgac")    returns 0.
  54.  
  55. char *strpbrk(string, character_set)
  56. char *string, *character_set;
  57.  
  58.     Return a pointer to the first character in "string" that
  59.     appears in "character_set".  If either argument is NULL,
  60.     strpbrk() returns NULL.
  61.  
  62.     strcspn("abcdefg", "abd")    returns "abcdefg".
  63.     strcspn("abcdefg", "xyyz")    returns NULL.
  64.     strspn("abcdefg", "dbc")    returns "bcdefg".
  65.     strspn("abcdefg", "dxbgac")    returns "abcdefg".
  66.  
  67. char *strtok(string, character_set)
  68. char *string, *character_set;
  69.  
  70.     This is a VERY USEFUL function.  The UNIX manual explains it best:
  71.  
  72.     `The strtok subroutine considers the string "string" to consist of
  73.     a sequence of zero or more text tokens separated by spans of
  74.     one or more characters from the separator string "character_set".  
  75.     The first call (with pointer "string" specified) returns a pointer
  76.     to the first character of the first token, and will have written
  77.     a null character into "string" immediately following the
  78.     returned token.  The function keeps track of its position in
  79.     the string between separate calls, so that subsequent calls
  80.     (which must be made with the first argument a NULL pointer)
  81.     will work through the string "string" immediately following that
  82.     token.  In this way, subsequent calls will work through the
  83.     string "string" until no tokens remain.  The separator string 
  84.     "character_set" may be different from call to call.  When no token
  85.     remains in "string", a NULL pointer is returned.'
  86.  
  87. Here is an example program demonstrating strtok().
  88.  
  89. /******************************************************************/
  90. #include <stdio.h>
  91.  
  92. extern char *strtok();
  93. char tokesep[] = " \n\t\rx";
  94.     
  95. main()
  96. {
  97.     char buf[BUFSIZ], *tokep;
  98.  
  99.     while (fgets(buf, sizeof(buf), stdin)) {
  100.         tokep = strtok(buf, tokesep);
  101.         do {
  102.             printf("Token is %s\n", tokep);
  103.             tokep = strtok((char *)NULL, tokesep);
  104.         }while (tokep);
  105.     }
  106. }
  107.  
  108. /******************************************************************/
  109.