Print this page
2964 need POSIX 2008 locale object support
Reviewed by: Robert Mustacchi <rm@joyent.com>
Reviewed by: Gordon Ross <gordon.ross@nexenta.com>
Approved by: TBD


   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*

  23  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
  24  */
  25 
  26 /*      Copyright (c) 1988 AT&T     */
  27 /*        All Rights Reserved   */
  28 
  29 #include "lint.h"
  30 #include <string.h>
  31 #include <ctype.h>
  32 #include <sys/types.h>



  33 
  34 /*
  35  * strcasestr() locates the first occurrence in the string s1 of the
  36  * sequence of characters (excluding the terminating null character)
  37  * in the string s2, ignoring case.  strcasestr() returns a pointer
  38  * to the located string, or a null pointer if the string is not found.
  39  * If s2 is empty, the function returns s1.
  40  */
  41 
  42 char *
  43 strcasestr(const char *s1, const char *s2)
  44 {
  45         int *cm = __trans_lower;
  46         const uchar_t *us1 = (const uchar_t *)s1;
  47         const uchar_t *us2 = (const uchar_t *)s2;
  48         const uchar_t *tptr;
  49         int c;
  50 
  51         if (us2 == NULL || *us2 == '\0')
  52                 return ((char *)us1);
  53 
  54         c = cm[*us2];
  55         while (*us1 != '\0') {
  56                 if (c == cm[*us1++]) {
  57                         tptr = us1;
  58                         while (cm[c = *++us2] == cm[*us1++] && c != '\0')
  59                                 continue;
  60                         if (c == '\0')
  61                                 return ((char *)tptr - 1);
  62                         us1 = tptr;
  63                         us2 = (const uchar_t *)s2;
  64                         c = cm[*us2];
  65                 }
  66         }
  67 
  68         return (NULL);






  69 }


   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright 2013 Garrett D'Amore <garrett@damore.org>
  24  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
  25  */
  26 
  27 /*      Copyright (c) 1988 AT&T     */
  28 /*        All Rights Reserved   */
  29 
  30 #include "lint.h"
  31 #include <string.h>
  32 #include <ctype.h>
  33 #include <sys/types.h>
  34 #include <locale.h>
  35 #include "lctype.h"
  36 #include "localeimpl.h"
  37 
  38 /*
  39  * strcasestr() locates the first occurrence in the string s1 of the
  40  * sequence of characters (excluding the terminating null character)
  41  * in the string s2, ignoring case.  strcasestr() returns a pointer
  42  * to the located string, or a null pointer if the string is not found.
  43  * If s2 is empty, the function returns s1.
  44  */
  45 
  46 char *
  47 strcasestr_l(const char *s1, const char *s2, locale_t loc)
  48 {
  49         const int *cm = loc->ctype->lc_trans_lower;
  50         const uchar_t *us1 = (const uchar_t *)s1;
  51         const uchar_t *us2 = (const uchar_t *)s2;
  52         const uchar_t *tptr;
  53         int c;
  54 
  55         if (us2 == NULL || *us2 == '\0')
  56                 return ((char *)us1);
  57 
  58         c = cm[*us2];
  59         while (*us1 != '\0') {
  60                 if (c == cm[*us1++]) {
  61                         tptr = us1;
  62                         while (cm[c = *++us2] == cm[*us1++] && c != '\0')
  63                                 continue;
  64                         if (c == '\0')
  65                                 return ((char *)tptr - 1);
  66                         us1 = tptr;
  67                         us2 = (const uchar_t *)s2;
  68                         c = cm[*us2];
  69                 }
  70         }
  71 
  72         return (NULL);
  73 }
  74 
  75 char *
  76 strcasestr(const char *s1, const char *s2)
  77 {
  78         return (strcasestr_l(s1, s2, uselocale(NULL)));
  79 }