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.

106 lines
2.5 KiB

  1. page ,132
  2. title strrchr - find last occurence of character in string
  3. ;***
  4. ;strrchr.asm - find last occurrence of character in string
  5. ;
  6. ; Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  7. ;
  8. ;Purpose:
  9. ; defines strrchr() - find the last occurrence of a given character
  10. ; in a string.
  11. ;
  12. ;Revision History:
  13. ; 10-27-83 RN initial version
  14. ; 05-18-88 SJM Add model-independent (large model) ifdef
  15. ; 08-04-88 SJM convert to cruntime/ add 32-bit support
  16. ; 08-23-88 JCR 386 cleanup
  17. ; 10-26-88 JCR General cleanup for 386-only code
  18. ; 03-26-90 GJF Changed to _stdcall. Also, fixed the copyright.
  19. ; 05-10-91 GJF Back to _cdecl, sigh...
  20. ; 06-12-01 PML inc->add 1, dec->sub 1 for Pentium 4 perf (vs7#267015)
  21. ;
  22. ;*******************************************************************************
  23. .xlist
  24. include cruntime.inc
  25. .list
  26. page
  27. ;***
  28. ;char *strrchr(string, ch) - find last occurrence of ch in string
  29. ;
  30. ;Purpose:
  31. ; Finds the last occurrence of ch in string. The terminating
  32. ; null character is used as part of the search.
  33. ;
  34. ; Algorithm:
  35. ; char *
  36. ; strrchr (string, ch)
  37. ; char *string, ch;
  38. ; {
  39. ; char *start = string;
  40. ;
  41. ; while (*string++)
  42. ; ;
  43. ; while (--string != start && *string != ch)
  44. ; ;
  45. ; if (*string == ch)
  46. ; return(string);
  47. ; return(NULL);
  48. ; }
  49. ;
  50. ;Entry:
  51. ; char *string - string to search in
  52. ; char ch - character to search for
  53. ;
  54. ;Exit:
  55. ; returns a pointer to the last occurrence of ch in the given
  56. ; string
  57. ; returns NULL if ch does not occurr in the string
  58. ;
  59. ;Uses:
  60. ;
  61. ;Exceptions:
  62. ;
  63. ;*******************************************************************************
  64. CODESEG
  65. public strrchr
  66. strrchr proc \
  67. uses edi, \
  68. string:ptr byte, \
  69. chr:byte
  70. mov edi,[string] ; di = string
  71. xor eax,eax ; al=null byte
  72. or ecx,-1 ; cx = -1
  73. repne scasb ; find the null & count bytes
  74. add ecx,1 ; cx=-byte count (with null)
  75. neg ecx ; cx=+byte count (with null)
  76. sub edi,1 ; di points to terminal null
  77. mov al,chr ; al=search byte
  78. std ; count 'down' on string this time
  79. repne scasb ; find that byte
  80. add edi,1 ; di points to byte which stopped scan
  81. cmp [edi],al ; see if we have a hit
  82. je short returndi ; yes, point to byte
  83. xor eax,eax ; no, return NULL
  84. jmp short toend ; do return sequence
  85. returndi:
  86. mov eax,edi ; ax=pointer to byte
  87. toend:
  88. cld
  89. ifdef _STDCALL_
  90. ret DPSIZE + ISIZE ; _stdcall return
  91. else
  92. ret ; _cdecl return
  93. endif
  94. strrchr endp
  95. end