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.

277 lines
6.1 KiB

  1. page ,132
  2. title strspn - search for init substring of chars from control str
  3. ;***
  4. ;strspn.asm - find length of initial substring of chars from a control string
  5. ;
  6. ; Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  7. ;
  8. ;Purpose:
  9. ; defines strspn() - finds the length of the initial substring of
  10. ; a string consisting entirely of characters from a control string.
  11. ;
  12. ; defines strcspn()- finds the length of the initial substring of
  13. ; a string consisting entirely of characters not in a control string.
  14. ;
  15. ; defines strpbrk()- finds the index of the first character in a string
  16. ; that is not in a control string
  17. ;
  18. ;Revision History:
  19. ; 10-28-83 RN initial version
  20. ; 06-30-87 SKS Faster version -- also reentrant
  21. ; 05-18-88 SJM Add model-independent (large model) ifdef
  22. ; 08-02-88 SJM Created 386-specific version.
  23. ; 08-23-88 JCR 386 cleanup
  24. ; 10-10-88 JCR Misc bug fixes
  25. ; 10-26-88 JCR General cleanup for 386-only code
  26. ; 03-26-90 GJF Changed to _stdcall. Also, fixed the copyright.
  27. ; 05-10-91 GJF Back to _cdecl, sigh...
  28. ; 09-25-91 JCR Build "strspn" if no other directives are given
  29. ; 01-17-92 GJF Fixed build of "strspn".
  30. ; 01-17-95 GJF Tuned.
  31. ; 06-12-01 PML inc->add 1, dec->sub 1 for Pentium 4 perf (vs7#267015)
  32. ;
  33. ;*******************************************************************************
  34. .xlist
  35. include cruntime.inc
  36. .list
  37. page
  38. ;***
  39. ;int strspn(string, control) - find init substring of control chars
  40. ;
  41. ;Purpose:
  42. ; Finds the index of the first character in string that does belong
  43. ; to the set of characters specified by control. This is
  44. ; equivalent to the length of the initial substring of string that
  45. ; consists entirely of characters from control. The '\0' character
  46. ; that terminates control is not considered in the matching process.
  47. ;
  48. ; Algorithm:
  49. ; int
  50. ; strspn (string, control)
  51. ; unsigned char *string, *control;
  52. ; {
  53. ; unsigned char map[32];
  54. ; int count;
  55. ;
  56. ; for (count = 0; count < 32; count++)
  57. ; map[count] = 0;
  58. ; while (*control)
  59. ; {
  60. ; map[*control >> 3] |= (1 << (*control & 7));
  61. ; control++;
  62. ; }
  63. ; if (*string)
  64. ; {
  65. ; while (map[*string >> 3] & (1 << (*string & 7)))
  66. ; {
  67. ; count++;
  68. ; string++;
  69. ; }
  70. ; return(count);
  71. ; }
  72. ; return(0);
  73. ; }
  74. ;
  75. ;Entry:
  76. ; char *string - string to search
  77. ; char *control - string containing characters not to search for
  78. ;
  79. ;Exit:
  80. ; returns index of first char in string not in control
  81. ;
  82. ;Uses:
  83. ;
  84. ;Exceptions:
  85. ;
  86. ;*******************************************************************************
  87. ;***
  88. ;int strcspn(string, control) - search for init substring w/o control chars
  89. ;
  90. ;Purpose:
  91. ; returns the index of the first character in string that belongs
  92. ; to the set of characters specified by control. This is equivalent
  93. ; to the length of the length of the initial substring of string
  94. ; composed entirely of characters not in control. Null chars not
  95. ; considered.
  96. ;
  97. ; Algorithm:
  98. ; int
  99. ; strcspn (string, control)
  100. ; unsigned char *string, *control;
  101. ; {
  102. ; unsigned char map[32];
  103. ; int count;
  104. ;
  105. ; for (count = 0; count < 32; count++)
  106. ; map[count] = 0;
  107. ; while (*control)
  108. ; {
  109. ; map[*control >> 3] |= (1 << (*control & 7));
  110. ; control++;
  111. ; }
  112. ; map[0] |= 1;
  113. ; while (!(map[*string >> 3] & (1 << (*string & 7))))
  114. ; {
  115. ; count++;
  116. ; string++;
  117. ; }
  118. ; return(count);
  119. ; }
  120. ;
  121. ;Entry:
  122. ; char *string - string to search
  123. ; char *control - set of characters not allowed in init substring
  124. ;
  125. ;Exit:
  126. ; returns the index of the first char in string
  127. ; that is in the set of characters specified by control.
  128. ;
  129. ;Uses:
  130. ;
  131. ;Exceptions:
  132. ;
  133. ;*******************************************************************************
  134. ;***
  135. ;char *strpbrk(string, control) - scans string for a character from control
  136. ;
  137. ;Purpose:
  138. ; Finds the first occurence in string of any character from
  139. ; the control string.
  140. ;
  141. ; Algorithm:
  142. ; char *
  143. ; strpbrk (string, control)
  144. ; unsigned char *string, *control;
  145. ; {
  146. ; unsigned char map[32];
  147. ; int count;
  148. ;
  149. ; for (count = 0; count < 32; count++)
  150. ; map[count] = 0;
  151. ; while (*control)
  152. ; {
  153. ; map[*control >> 3] |= (1 << (*control & 7));
  154. ; control++;
  155. ; }
  156. ; while (*string)
  157. ; {
  158. ; if (map[*string >> 3] & (1 << (*string & 7)))
  159. ; return(string);
  160. ; string++;
  161. ; }
  162. ; return(NULL);
  163. ; }
  164. ;
  165. ;Entry:
  166. ; char *string - string to search in
  167. ; char *control - string containing characters to search for
  168. ;
  169. ;Exit:
  170. ; returns a pointer to the first character from control found
  171. ; in string.
  172. ; returns NULL if string and control have no characters in common.
  173. ;
  174. ;Uses:
  175. ;
  176. ;Exceptions:
  177. ;
  178. ;*******************************************************************************
  179. ifdef SSTRCSPN
  180. _STRSPN_ equ <strcspn>
  181. elseifdef SSTRPBRK
  182. _STRSPN_ equ <strpbrk>
  183. else
  184. ; Default is to build strspn()
  185. SSTRSPN equ 1
  186. _STRSPN_ equ <strspn>
  187. endif
  188. % public _STRSPN_
  189. CODESEG
  190. _STRSPN_ proc \
  191. uses esi, \
  192. string:ptr byte, \
  193. control:ptr byte
  194. ; create and zero out char bit map
  195. xor eax,eax
  196. push eax ; 32
  197. push eax
  198. push eax
  199. push eax ; 128
  200. push eax
  201. push eax
  202. push eax
  203. push eax ; 256
  204. map equ [esp]
  205. ; Set control char bits in map
  206. mov edx,control ; si = control string
  207. align @WordSize
  208. lab listnext ; init char bit map
  209. mov al,[edx]
  210. or al,al
  211. jz short listdone
  212. add edx,1
  213. bts map,eax
  214. jmp short listnext
  215. lab listdone
  216. ; Loop through comparing source string with control bits
  217. mov esi,string ; si = string
  218. _ifnd SSTRPBRK <or ecx,-1> ; set ecx to -1
  219. align @WordSize
  220. lab dstnext
  221. _ifnd SSTRPBRK <add ecx,1>
  222. mov al,[esi]
  223. or al,al
  224. jz short dstdone
  225. add esi,1
  226. bt map, eax
  227. ifdef SSTRSPN
  228. jc short dstnext ; strspn: found char, continue
  229. elseifdef SSTRCSPN
  230. jnc short dstnext ; strcspn: did not find char, continue
  231. elseifdef SSTRPBRK
  232. jnc short dstnext ; strpbrk: did not find char, continue
  233. lea eax,[esi - 1] ; found char, return address of it
  234. endif
  235. ; Return code
  236. lab dstdone
  237. _ifnd SSTRPBRK <mov eax,ecx> ; strspn/strcspn: return index
  238. add esp,32
  239. ret ; _cdecl return
  240. _STRSPN_ endp
  241. end