Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /****************************************************************************
  2. *
  3. *                            Open Watcom Project
  4. *
  5. *    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
  6. *
  7. *  ========================================================================
  8. *
  9. *    This file contains Original Code and/or Modifications of Original
  10. *    Code as defined in and that are subject to the Sybase Open Watcom
  11. *    Public License version 1.0 (the 'License'). You may not use this file
  12. *    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
  13. *    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
  14. *    provided with the Original Code and Modifications, and is also
  15. *    available at www.sybase.com/developer/opensource.
  16. *
  17. *    The Original Code and all software distributed under the License are
  18. *    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  19. *    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
  20. *    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
  21. *    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
  22. *    NON-INFRINGEMENT. Please see the License for the specific language
  23. *    governing rights and limitations under the License.
  24. *
  25. *  ========================================================================
  26. *
  27. * Description:  Implementation of strstr() and wcsstr().
  28. *
  29. ****************************************************************************/
  30.  
  31.  
  32. #include "variety.h"
  33. #include "widechar.h"
  34. #include <stddef.h>
  35. #include <string.h>
  36. #if defined(_M_IX86)
  37.  #include <i86.h>
  38. #endif
  39.  
  40. #if defined(M_I86) && !defined( __WIDECHAR__ )
  41.  
  42. extern  int     i86_memeq( const char *, const char _WCFAR *, int );
  43.  
  44. #define _ZFLAG          (INTR_ZF<<8)
  45.  
  46. #if defined(__SMALL_DATA__)
  47.  
  48. #pragma aux     i86_memeq = \
  49.         0xf3 0xa6       /* rep cmpsb */\
  50.         0x9f            /* lahf */\
  51.         parm caller     [si] [es di] [cx]\
  52.         value           [ax] \
  53.         modify exact    [si di cx ax];
  54.  
  55. #else
  56.  
  57. #pragma aux     i86_memeq = \
  58.         0x1e            /* push ds */ \
  59.         0x8e 0xda       /* mov ds,dx */ \
  60.         0xf3 0xa6       /* rep cmpsb */\
  61.         0x9f            /* lahf */\
  62.         0x1f            /* pop ds */ \
  63.         parm caller     [dx si] [es di] [cx]\
  64.         value           [ax] \
  65.         modify exact    [si di cx ax];
  66.  
  67. #endif
  68.  
  69. #define memeq( p1, p2, len )    ( i86_memeq((p1),(p2),(len)) & _ZFLAG )
  70.  
  71. #else
  72.  
  73. #define memeq( p1, p2, len )    ( memcmp((p1),(p2),(len)*CHARSIZE) == 0 )
  74.  
  75. #endif
  76.  
  77. /* Locate the first occurrence of the string pointed to by s2 in the
  78.    string pointed to by s1.
  79.    The strstr function returns a pointer to the located string, or a
  80.    null pointer if the string is not found.
  81. */
  82.  
  83.  
  84. _WCRTLINK CHAR_TYPE *__F_NAME(strstr,wcsstr) ( const CHAR_TYPE *s1, const CHAR_TYPE *s2 )
  85.     {
  86.         CHAR_TYPE *end_of_s1;
  87.         size_t s1len, s2len;
  88.  
  89.         if( s2[0] == NULLCHAR ) {
  90.             return( (CHAR_TYPE *)s1 );
  91.         } else if( s2[1] == NULLCHAR ) {
  92.             return( __F_NAME(strchr,wcschr)( s1, s2[0] ) );
  93.         }
  94.         #ifdef __WIDECHAR__
  95.             end_of_s1 = (CHAR_TYPE*)s1 + wcslen( s1 );
  96.         #else
  97.             end_of_s1 = memchr( s1, NULLCHAR, ~0u );
  98.         #endif
  99.         s2len = __F_NAME(strlen,wcslen)( s2 );
  100.         for(;;) {
  101.             s1len = end_of_s1 - s1;
  102.             if( s1len < s2len ) break;
  103.             #ifdef __WIDECHAR__
  104.                 s1 = wcschr( s1, *s2 );  /* find start of possible match */
  105.             #else
  106.                 s1 = memchr( s1, *s2, s1len );  /* find start of possible match */
  107.             #endif
  108.             if( s1 == NULL ) break;
  109.             if( memeq( s1, s2, s2len ) ) return( (CHAR_TYPE *)s1 );
  110.             ++s1;
  111.         }
  112.         return( NULL );
  113.     }
  114.