home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 2.ddi / CLIB2.ZIP / STRSTR.CAS < prev    next >
Encoding:
Text File  |  1990-06-07  |  2.6 KB  |  114 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. /*|                                                              |*/
  10. /*|     Turbo C Run Time Library - Version 3.0                   |*/
  11. /*|                                                              |*/
  12. /*|                                                              |*/
  13. /*|     Copyright (c) 1987,1988,1990 by Borland International    |*/
  14. /*|     All Rights Reserved.                                     |*/
  15. /*|                                                              |*/
  16. /*[]------------------------------------------------------------[]*/
  17.  
  18. #pragma  inline
  19. #include <asmrules.h>
  20.  
  21. #if (LDATA)
  22. #define pushES_ asm    push    ES
  23. #define popES_    asm    pop    ES
  24. #else
  25. #define pushES_
  26. #define popES_
  27. #endif
  28.  
  29. #include <string.h>
  30.  
  31. /*-----------------------------------------------------------------------*
  32.  
  33. Name            strstr - scans a string for the occurrence of a given string
  34.  
  35. Usage           char *strstr(const char *str1, const char *str2);
  36.  
  37. Prototype in    string.h
  38.  
  39. Description    strstr scans str1 for the first occurrence of the substring
  40.         str2.
  41.  
  42. Return value    strstr returns a pointer to the element in str1 that contains
  43.         str2 (points to str2 in str1). If str2 does not occur in str1,
  44.         strstr returns NULL.
  45.  
  46. *------------------------------------------------------------------------*/
  47. char *strstr(const char *str1, const char *str2)
  48. {
  49.     if (!*str2)
  50.         return((char *)str1);        /* return str1 if str2 empty */
  51.  
  52.     pushDS_
  53. #if !(LDATA)
  54.     _ES = _DS;
  55. #endif
  56. asm    cld
  57.  
  58. asm    LES_    di, str1
  59.     pushES_
  60. asm    mov    bx, di
  61. asm    xor    ax, ax
  62. asm    mov    cx, -1
  63. asm    repnz    scasb
  64. asm    not    cx
  65. asm    xchg    cx, dx
  66.  
  67. asm    LES_    di, str2
  68.     pushES_
  69. asm    mov    bp, di
  70. asm    xor    ax, ax
  71. asm    mov    cx, -1
  72. asm    repnz    scasb
  73. asm    inc    cx
  74. asm    not    cx
  75.     popDS_
  76.     popES_
  77.  
  78. strLoop:
  79. asm    mov    si, bp
  80. asm    lodsb
  81. asm    xchg    di, bx
  82. asm    xchg    cx, dx
  83. asm    repnz    scasb
  84. asm    mov    bx, di
  85. asm    jnz    NotFound
  86. asm    cmp    cx, dx
  87. asm    jnb    FirstMatch
  88.  
  89. NotFound:
  90. #if (LDATA)
  91. asm    xor    bx, bx
  92. asm    mov    es, bx
  93. #endif
  94. asm    mov    bx, 1
  95. asm    jmp    short End
  96.  
  97. FirstMatch:
  98. asm    xchg    cx, dx
  99. asm    jcxz    End
  100. asm    mov    ax, cx
  101. asm    dec    cx
  102. asm    repz    cmpsb
  103. asm    mov    cx, ax
  104. asm    jnz    strLoop
  105.  
  106. End:
  107.     popDS_
  108. #if (LDATA)
  109.     return (char _es *)(_BX - 1);
  110. #else
  111.     return (char *)(_BX - 1);
  112. #endif
  113. }
  114.