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

  1. /*-----------------------------------------------------------------------*
  2.  * filename - strchr.cas
  3.  *
  4.  * function(s)
  5.  *        strchr - scans a string for the first occurrence of a
  6.  *        given character
  7.  *-----------------------------------------------------------------------*/
  8.  
  9. /*
  10.  *      C/C++ Run Time Library - Version 5.0
  11.  *
  12.  *      Copyright (c) 1987, 1992 by Borland International
  13.  *      All Rights Reserved.
  14.  *
  15.  */
  16.  
  17.  
  18. #pragma  inline
  19. #include <asmrules.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22.  
  23. /*---------------------------------------------------------------------*
  24.  
  25. Name            strchr - scans a string for the first occurrence of a
  26.                          given character
  27.  
  28. Usage           char *strchr(const char *str, int c);
  29.  
  30. Prototype in    string.h
  31.  
  32. Description     strchr scans a string in the forward direction, looking for a
  33.                 specific character. strchr finds the first occurrence of the
  34.                 character ch in the string str.
  35.  
  36.                 The null-terminator is considered
  37.                 to be part of the string, so that, for example
  38.  
  39.                         strchr(strs, 0)
  40.  
  41.                 returns a pointer to the terminating null character of the
  42.                 string "strs".
  43.  
  44. Return value    strchr  returns a pointer to the first occurrence of the
  45.                 character ch in str; if ch does not occur in str, strchr
  46.                 returns NULL.
  47.  
  48. *---------------------------------------------------------------------*/
  49. #undef strchr            /* not an intrinsic */
  50.  
  51. #if defined(__FARFUNCS__)
  52. #include <_farfunc.h>
  53. #endif
  54.  
  55. char _FAR * _CType _FARFUNC strchr(const char _FAR *s, int c)
  56. {
  57. asm     cld
  58. #if  defined(__LARGE__) || defined(__COMPACT__)
  59. asm push    ds
  60. #endif
  61. asm LDS_    si, s
  62. asm mov bl,c
  63. asm test    si, 1           /* SI even? */
  64. asm jz  cmp_loop
  65. asm lodsb
  66. asm cmp al, bl          /* process first character */
  67. asm je  success2
  68. asm and al, al
  69. asm jz  failure
  70. cmp_loop:
  71. asm lodsw               /* get 2 chars at a time */
  72. asm cmp al, bl
  73. asm je  success1        /* if first  char matches */
  74. asm and al, al
  75. asm jz  failure         /* give up if end of string */
  76. asm cmp ah, bl
  77. asm je  success2        /* if second char matches */
  78. asm and ah, ah
  79. asm jnz cmp_loop        /* continue if more characters */
  80. failure:
  81. #if  defined(__LARGE__) || defined(__COMPACT__)
  82. asm pop ds
  83. #endif
  84.         return( NULL );
  85. success2:
  86. asm inc si
  87. success1:
  88. asm lea ax, [si-2]      /* point AX at matching char */
  89. #if LDATA
  90. asm mov dx,ds
  91. #endif
  92. #if  defined(__LARGE__) || defined(__COMPACT__)
  93. asm pop ds
  94. #endif
  95.  
  96. #if LDATA
  97.         return( (char *)(MK_LONG) );
  98. #else
  99.         return( (char *)_AX );
  100. #endif
  101. }
  102.