Source code of Windows XP (NT5)
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.

276 lines
5.7 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. ;
  32. ;*******************************************************************************
  33. .xlist
  34. include cruntime.inc
  35. .list
  36. page
  37. ;***
  38. ;int strspn(string, control) - find init substring of control chars
  39. ;
  40. ;Purpose:
  41. ; Finds the index of the first character in string that does belong
  42. ; to the set of characters specified by control. This is
  43. ; equivalent to the length of the initial substring of string that
  44. ; consists entirely of characters from control. The '\0' character
  45. ; that terminates control is not considered in the matching process.
  46. ;
  47. ; Algorithm:
  48. ; int
  49. ; strspn (string, control)
  50. ; unsigned char *string, *control;
  51. ; {
  52. ; unsigned char map[32];
  53. ; int count;
  54. ;
  55. ; for (count = 0; count < 32; count++)
  56. ; map[count] = 0;
  57. ; while (*control)
  58. ; {
  59. ; map[*control >> 3] |= (1 << (*control & 7));
  60. ; control++;
  61. ; }
  62. ; if (*string)
  63. ; {
  64. ; while (map[*string >> 3] & (1 << (*string & 7)))
  65. ; {
  66. ; count++;
  67. ; string++;
  68. ; }
  69. ; return(count);
  70. ; }
  71. ; return(0);
  72. ; }
  73. ;
  74. ;Entry:
  75. ; char *string - string to search
  76. ; char *control - string containing characters not to search for
  77. ;
  78. ;Exit:
  79. ; returns index of first char in string not in control
  80. ;
  81. ;Uses:
  82. ;
  83. ;Exceptions:
  84. ;
  85. ;*******************************************************************************
  86. ;***
  87. ;int strcspn(string, control) - search for init substring w/o control chars
  88. ;
  89. ;Purpose:
  90. ; returns the index of the first character in string that belongs
  91. ; to the set of characters specified by control. This is equivalent
  92. ; to the length of the length of the initial substring of string
  93. ; composed entirely of characters not in control. Null chars not
  94. ; considered.
  95. ;
  96. ; Algorithm:
  97. ; int
  98. ; strcspn (string, control)
  99. ; unsigned char *string, *control;
  100. ; {
  101. ; unsigned char map[32];
  102. ; int count;
  103. ;
  104. ; for (count = 0; count < 32; count++)
  105. ; map[count] = 0;
  106. ; while (*control)
  107. ; {
  108. ; map[*control >> 3] |= (1 << (*control & 7));
  109. ; control++;
  110. ; }
  111. ; map[0] |= 1;
  112. ; while (!(map[*string >> 3] & (1 << (*string & 7))))
  113. ; {
  114. ; count++;
  115. ; string++;
  116. ; }
  117. ; return(count);
  118. ; }
  119. ;
  120. ;Entry:
  121. ; char *string - string to search
  122. ; char *control - set of characters not allowed in init substring
  123. ;
  124. ;Exit:
  125. ; returns the index of the first char in string
  126. ; that is in the set of characters specified by control.
  127. ;
  128. ;Uses:
  129. ;
  130. ;Exceptions:
  131. ;
  132. ;*******************************************************************************
  133. ;***
  134. ;char *strpbrk(string, control) - scans string for a character from control
  135. ;
  136. ;Purpose:
  137. ; Finds the first occurence in string of any character from
  138. ; the control string.
  139. ;
  140. ; Algorithm:
  141. ; char *
  142. ; strpbrk (string, control)
  143. ; unsigned char *string, *control;
  144. ; {
  145. ; unsigned char map[32];
  146. ; int count;
  147. ;
  148. ; for (count = 0; count < 32; count++)
  149. ; map[count] = 0;
  150. ; while (*control)
  151. ; {
  152. ; map[*control >> 3] |= (1 << (*control & 7));
  153. ; control++;
  154. ; }
  155. ; while (*string)
  156. ; {
  157. ; if (map[*string >> 3] & (1 << (*string & 7)))
  158. ; return(string);
  159. ; string++;
  160. ; }
  161. ; return(NULL);
  162. ; }
  163. ;
  164. ;Entry:
  165. ; char *string - string to search in
  166. ; char *control - string containing characters to search for
  167. ;
  168. ;Exit:
  169. ; returns a pointer to the first character from control found
  170. ; in string.
  171. ; returns NULL if string and control have no characters in common.
  172. ;
  173. ;Uses:
  174. ;
  175. ;Exceptions:
  176. ;
  177. ;*******************************************************************************
  178. ifdef SSTRCSPN
  179. _STRSPN_ equ <strcspn>
  180. elseifdef SSTRPBRK
  181. _STRSPN_ equ <strpbrk>
  182. else
  183. ; Default is to build strspn()
  184. SSTRSPN equ 1
  185. _STRSPN_ equ <strspn>
  186. endif
  187. % public _STRSPN_
  188. CODESEG
  189. _STRSPN_ proc \
  190. uses esi, \
  191. string:ptr byte, \
  192. control:ptr byte
  193. ; create and zero out char bit map
  194. xor eax,eax
  195. push eax ; 32
  196. push eax
  197. push eax
  198. push eax ; 128
  199. push eax
  200. push eax
  201. push eax
  202. push eax ; 256
  203. map equ [esp]
  204. ; Set control char bits in map
  205. mov edx,control ; si = control string
  206. align @WordSize
  207. lab listnext ; init char bit map
  208. mov al,[edx]
  209. or al,al
  210. jz short listdone
  211. inc edx
  212. bts map,eax
  213. jmp short listnext
  214. lab listdone
  215. ; Loop through comparing source string with control bits
  216. mov esi,string ; si = string
  217. _ifnd SSTRPBRK <or ecx,-1> ; set ecx to -1
  218. align @WordSize
  219. lab dstnext
  220. _ifnd SSTRPBRK <inc ecx>
  221. mov al,[esi]
  222. or al,al
  223. jz short dstdone
  224. inc esi
  225. bt map, eax
  226. ifdef SSTRSPN
  227. jc short dstnext ; strspn: found char, continue
  228. elseifdef SSTRCSPN
  229. jnc short dstnext ; strcspn: did not find char, continue
  230. elseifdef SSTRPBRK
  231. jnc short dstnext ; strpbrk: did not find char, continue
  232. lea eax,[esi - 1] ; found char, return address of it
  233. endif
  234. ; Return code
  235. lab dstdone
  236. _ifnd SSTRPBRK <mov eax,ecx> ; strspn/strcspn: return index
  237. add esp,32
  238. ret ; _cdecl return
  239. _STRSPN_ endp
  240. end