home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / strspn.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  5KB  |  170 lines

  1. /***
  2. *strspn.c - find length of initial substring of chars from a control string
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       defines strspn() - finds the length of the initial substring of
  8. *       a string consisting entirely of characters from a control string.
  9. *
  10. *       defines strcspn()- finds the length of the initial substring of
  11. *       a string consisting entirely of characters not in a control string.
  12. *
  13. *       defines strpbrk()- finds the index of the first character in a string
  14. *       that is not in a control string
  15. *
  16. *******************************************************************************/
  17.  
  18. /* Determine which routine we're compiling for (default to STRSPN) */
  19.  
  20. #define _STRSPN         1
  21. #define _STRCSPN        2
  22. #define _STRPBRK        3
  23.  
  24. #if defined (SSTRCSPN)
  25. #define ROUTINE _STRCSPN
  26. #elif defined (SSTRPBRK)
  27. #define ROUTINE _STRPBRK
  28. #else  /* defined (SSTRPBRK) */
  29. #define ROUTINE _STRSPN
  30. #endif  /* defined (SSTRPBRK) */
  31.  
  32. #include <cruntime.h>
  33. #include <string.h>
  34.  
  35. /***
  36. *int strspn(string, control) - find init substring of control chars
  37. *
  38. *Purpose:
  39. *       Finds the index of the first character in string that does belong
  40. *       to the set of characters specified by control.  This is
  41. *       equivalent to the length of the initial substring of string that
  42. *       consists entirely of characters from control.  The '\0' character
  43. *       that terminates control is not considered in the matching process.
  44. *
  45. *Entry:
  46. *       char *string - string to search
  47. *       char *control - string containing characters not to search for
  48. *
  49. *Exit:
  50. *       returns index of first char in string not in control
  51. *
  52. *Exceptions:
  53. *
  54. *******************************************************************************/
  55.  
  56. /***
  57. *int strcspn(string, control) - search for init substring w/o control chars
  58. *
  59. *Purpose:
  60. *       returns the index of the first character in string that belongs
  61. *       to the set of characters specified by control.  This is equivalent
  62. *       to the length of the length of the initial substring of string
  63. *       composed entirely of characters not in control.  Null chars not
  64. *       considered.
  65. *
  66. *Entry:
  67. *       char *string - string to search
  68. *       char *control - set of characters not allowed in init substring
  69. *
  70. *Exit:
  71. *       returns the index of the first char in string
  72. *       that is in the set of characters specified by control.
  73. *
  74. *Exceptions:
  75. *
  76. *******************************************************************************/
  77.  
  78. /***
  79. *char *strpbrk(string, control) - scans string for a character from control
  80. *
  81. *Purpose:
  82. *       Finds the first occurence in string of any character from
  83. *       the control string.
  84. *
  85. *Entry:
  86. *       char *string - string to search in
  87. *       char *control - string containing characters to search for
  88. *
  89. *Exit:
  90. *       returns a pointer to the first character from control found
  91. *       in string.
  92. *       returns NULL if string and control have no characters in common.
  93. *
  94. *Exceptions:
  95. *
  96. *******************************************************************************/
  97.  
  98.  
  99.  
  100. /* Routine prototype */
  101. #if ROUTINE == _STRSPN
  102. size_t __cdecl strspn (
  103. #elif ROUTINE == _STRCSPN
  104. size_t __cdecl strcspn (
  105. #else  /* ROUTINE == _STRCSPN */
  106. char * __cdecl strpbrk (
  107. #endif  /* ROUTINE == _STRCSPN */
  108.         const char * string,
  109.         const char * control
  110.         )
  111. {
  112.         const unsigned char *str = string;
  113.         const unsigned char *ctrl = control;
  114.  
  115.         unsigned char map[32];
  116.         int count;
  117.  
  118.         /* Clear out bit map */
  119.         for (count=0; count<32; count++)
  120.                 map[count] = 0;
  121.  
  122.         /* Set bits in control map */
  123.         while (*ctrl)
  124.         {
  125.                 map[*ctrl >> 3] |= (1 << (*ctrl & 7));
  126.                 ctrl++;
  127.         }
  128.  
  129. #if ROUTINE == _STRSPN
  130.  
  131.         /* 1st char NOT in control map stops search */
  132.         if (*str)
  133.         {
  134.                 count=0;
  135.                 while (map[*str >> 3] & (1 << (*str & 7)))
  136.                 {
  137.                         count++;
  138.                         str++;
  139.                 }
  140.                 return(count);
  141.         }
  142.         return(0);
  143.  
  144. #elif ROUTINE == _STRCSPN
  145.  
  146.         /* 1st char in control map stops search */
  147.         count=0;
  148.         map[0] |= 1;    /* null chars not considered */
  149.         while (!(map[*str >> 3] & (1 << (*str & 7))))
  150.         {
  151.                 count++;
  152.                 str++;
  153.         }
  154.         return(count);
  155.  
  156. #else  /* ROUTINE == _STRCSPN */
  157.  
  158.         /* 1st char in control map stops search */
  159.         while (*str)
  160.         {
  161.                 if (map[*str >> 3] & (1 << (*str & 7)))
  162.                         return((char *)str);
  163.                 str++;
  164.         }
  165.         return(NULL);
  166.  
  167. #endif  /* ROUTINE == _STRCSPN */
  168.  
  169. }
  170.