Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
4.6 KiB

  1. /***
  2. *strspn.c - find length of initial substring of chars from a control string
  3. *
  4. * Copyright (c) 1985-2001, 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. *Revision History:
  17. * 06-01-89 JCR C version created.
  18. * 02-27-90 GJF Fixed calling type, #include <cruntime.h>, fixed
  19. * copyright.
  20. * 08-14-90 SBM Removed now redundant #include <stddef.h>
  21. * 10-02-90 GJF New-style function declarators.
  22. * 12-04-90 SRW Made it default to compiling for strspn
  23. * 05-21-93 GJF Used unsigned char pointers to access control and
  24. * source strings.
  25. * 09-03-93 GJF Replaced _CALLTYPE1 with __cdecl.
  26. *
  27. *******************************************************************************/
  28. /* Determine which routine we're compiling for (default to STRSPN) */
  29. #define _STRSPN 1
  30. #define _STRCSPN 2
  31. #define _STRPBRK 3
  32. #if defined(SSTRCSPN)
  33. #define ROUTINE _STRCSPN
  34. #elif defined(SSTRPBRK)
  35. #define ROUTINE _STRPBRK
  36. #else
  37. #define ROUTINE _STRSPN
  38. #endif
  39. #include <cruntime.h>
  40. #include <string.h>
  41. /***
  42. *int strspn(string, control) - find init substring of control chars
  43. *
  44. *Purpose:
  45. * Finds the index of the first character in string that does belong
  46. * to the set of characters specified by control. This is
  47. * equivalent to the length of the initial substring of string that
  48. * consists entirely of characters from control. The '\0' character
  49. * that terminates control is not considered in the matching process.
  50. *
  51. *Entry:
  52. * char *string - string to search
  53. * char *control - string containing characters not to search for
  54. *
  55. *Exit:
  56. * returns index of first char in string not in control
  57. *
  58. *Exceptions:
  59. *
  60. *******************************************************************************/
  61. /***
  62. *int strcspn(string, control) - search for init substring w/o control chars
  63. *
  64. *Purpose:
  65. * returns the index of the first character in string that belongs
  66. * to the set of characters specified by control. This is equivalent
  67. * to the length of the length of the initial substring of string
  68. * composed entirely of characters not in control. Null chars not
  69. * considered.
  70. *
  71. *Entry:
  72. * char *string - string to search
  73. * char *control - set of characters not allowed in init substring
  74. *
  75. *Exit:
  76. * returns the index of the first char in string
  77. * that is in the set of characters specified by control.
  78. *
  79. *Exceptions:
  80. *
  81. *******************************************************************************/
  82. /***
  83. *char *strpbrk(string, control) - scans string for a character from control
  84. *
  85. *Purpose:
  86. * Finds the first occurence in string of any character from
  87. * the control string.
  88. *
  89. *Entry:
  90. * char *string - string to search in
  91. * char *control - string containing characters to search for
  92. *
  93. *Exit:
  94. * returns a pointer to the first character from control found
  95. * in string.
  96. * returns NULL if string and control have no characters in common.
  97. *
  98. *Exceptions:
  99. *
  100. *******************************************************************************/
  101. /* Routine prototype */
  102. #if ROUTINE == _STRSPN /*IFSTRIP=IGN*/
  103. size_t __cdecl strspn (
  104. #elif ROUTINE == _STRCSPN /*IFSTRIP=IGN*/
  105. size_t __cdecl strcspn (
  106. #else /* ROUTINE == STRPBRK */
  107. char * __cdecl strpbrk (
  108. #endif
  109. const char * string,
  110. const char * control
  111. )
  112. {
  113. const unsigned char *str = string;
  114. const unsigned char *ctrl = control;
  115. unsigned char map[32];
  116. int count;
  117. /* Clear out bit map */
  118. for (count=0; count<32; count++)
  119. map[count] = 0;
  120. /* Set bits in control map */
  121. while (*ctrl)
  122. {
  123. map[*ctrl >> 3] |= (1 << (*ctrl & 7));
  124. ctrl++;
  125. }
  126. #if ROUTINE == _STRSPN /*IFSTRIP=IGN*/
  127. /* 1st char NOT in control map stops search */
  128. if (*str)
  129. {
  130. count=0;
  131. while (map[*str >> 3] & (1 << (*str & 7)))
  132. {
  133. count++;
  134. str++;
  135. }
  136. return(count);
  137. }
  138. return(0);
  139. #elif ROUTINE == _STRCSPN /*IFSTRIP=IGN*/
  140. /* 1st char in control map stops search */
  141. count=0;
  142. map[0] |= 1; /* null chars not considered */
  143. while (!(map[*str >> 3] & (1 << (*str & 7))))
  144. {
  145. count++;
  146. str++;
  147. }
  148. return(count);
  149. #else /* (ROUTINE == _STRPBRK) */
  150. /* 1st char in control map stops search */
  151. while (*str)
  152. {
  153. if (map[*str >> 3] & (1 << (*str & 7)))
  154. return((char *)str);
  155. str++;
  156. }
  157. return(NULL);
  158. #endif
  159. }