home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 5.ddi / CLIBSRC2.ZIP / STRSTR.CAS < prev    next >
Encoding:
Text File  |  1992-06-10  |  2.5 KB  |  117 lines

  1. /*-----------------------------------------------------------------------*
  2.  * filename - strstr.cas
  3.  *
  4.  * function(s)
  5.  *        strstr - scans a string for the occurrence of a given string
  6.  *-----------------------------------------------------------------------*/
  7.  
  8. /*
  9.  *      C/C++ Run Time Library - Version 5.0
  10.  *
  11.  *      Copyright (c) 1987, 1992 by Borland International
  12.  *      All Rights Reserved.
  13.  *
  14.  */
  15.  
  16.  
  17. #pragma  inline
  18. #include <asmrules.h>
  19.  
  20. #if (LDATA)
  21. #define pushES_ asm     push    ES
  22. #define popES_  asm     pop     ES
  23. #else
  24. #define pushES_
  25. #define popES_
  26. #endif
  27.  
  28. #include <string.h>
  29.  
  30. /*-----------------------------------------------------------------------*
  31.  
  32. Name            strstr - scans a string for the occurrence of a given string
  33.  
  34. Usage           char *strstr(const char *str1, const char *str2);
  35.  
  36. Prototype in    string.h
  37.  
  38. Description     strstr scans str1 for the first occurrence of the substring
  39.                 str2.
  40.  
  41. Return value    strstr returns a pointer to the element in str1 that contains
  42.                 str2 (points to str2 in str1). If str2 does not occur in str1,
  43.                 strstr returns NULL.
  44.  
  45. *------------------------------------------------------------------------*/
  46. #if defined(__FARFUNCS__)
  47. #include <_farfunc.h>
  48. #endif
  49.  
  50. char * _FARFUNC strstr(const char *str1, const char *str2)
  51. {
  52.         if (!*str2)
  53.                 return((char *)str1);           /* return str1 if str2 empty */
  54.  
  55.         pushDS_
  56. #if !(LDATA)
  57.     _ES = _DS;
  58. #endif
  59. asm     cld
  60.  
  61. asm     LES_    di, str1
  62.         pushES_
  63. asm     mov     bx, di
  64. asm     xor     ax, ax
  65. asm     mov     cx, -1
  66. asm     repnz   scasb
  67. asm     not     cx
  68. asm     xchg    cx, dx
  69.  
  70. asm     LES_    di, str2
  71.         pushES_
  72. asm     mov     bp, di
  73. asm     xor     ax, ax
  74. asm     mov     cx, -1
  75. asm     repnz   scasb
  76. asm     inc     cx
  77. asm     not     cx
  78.         popDS_
  79.         popES_
  80.  
  81. strLoop:
  82. asm     mov     si, bp
  83. asm     lodsb
  84. asm     xchg    di, bx
  85. asm     xchg    cx, dx
  86. asm     repnz   scasb
  87. asm     mov     bx, di
  88. asm     jnz     NotFound
  89. asm     cmp     cx, dx
  90. asm     jnb     FirstMatch
  91.  
  92. NotFound:
  93. #if (LDATA)
  94. asm     xor     bx, bx
  95. asm     mov     es, bx
  96. #endif
  97. asm     mov     bx, 1
  98. asm     jmp     short End
  99.  
  100. FirstMatch:
  101. asm     xchg    cx, dx
  102. asm     jcxz    End
  103. asm     mov     ax, cx
  104. asm     dec     cx
  105. asm     repz    cmpsb
  106. asm     mov     cx, ax
  107. asm     jnz     strLoop
  108.  
  109. End:
  110.         popDS_
  111. #if (LDATA)
  112.     return (char _es *)(_BX - 1);
  113. #else
  114.     return (char *)(_BX - 1);
  115. #endif
  116. }
  117.